// +---------------------------------------------------------------------- namespace think\db\builder; use think\db\Builder; /** * Sqlsrv数据库驱动 */ class Sqlsrv extends Builder { protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%'; /** * order分析 * @access protected * @param mixed $order * @return string */ protected function parseOrder($order) { return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()'; } /** * 随机排序 * @access protected * @return string */ protected function parseRand() { return 'rand()'; } /** * 字段名分析 * @access protected * @param string $key * @return string */ protected function parseKey($key) { $key = trim($key); if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) { $key = '[' . $key . ']'; } return $key; } /** * limit * @access protected * @param mixed $limit * @return string */ protected function parseLimit($limit) { if (empty($limit)) { return ''; } $limit = explode(',', $limit); if (count($limit) > 1) { $limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')'; } else { $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")"; } return 'WHERE ' . $limitStr; } /** * 生成update SQL * @access public * @param array $fields 数据 * @param array $options 表达式 * @return string */ public function update($data, $options) { $table = $this->parseTable($options['table']); $data = $this->parseData($data, $options); if (empty($data)) { return ''; } foreach ($data as $key => $val) { $set[] = $key . '=' . $val; } $sql = str_replace( ['%TABLE%', '%SET%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'], [ $this->parseTable($options['table']), implode(',', $set), $this->parseJoin($options['join']), $this->parseWhere($options['where'], $options['table']), '', $this->parseLimit($options['limit']), $this->parseLimit($options['lock']), $this->parseComment($options['comment']), ], $this->updateSql); return $sql; } }