// +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Sqlsrv数据库驱动 * @category Extend * @package Extend * @subpackage Driver.Db * @author liu21st */ class Sqlsrv extends Driver{ 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%'; /** * 取得数据表的字段信息 * @access public * @return array */ public function getFields($tableName) { $result = $this->query("SELECT column_name, data_type, column_default, is_nullable FROM information_schema.tables AS t JOIN information_schema.columns AS c ON t.table_catalog = c.table_catalog AND t.table_schema = c.table_schema AND t.table_name = c.table_name WHERE t.table_name = '$tableName'"); $info = []; if($result) { foreach ($result as $key => $val) { $info[$val['column_name']] = [ 'name' => $val['column_name'], 'type' => $val['data_type'], 'notnull' => (bool) ($val['is_nullable'] === ''), // not null is empty, null is yes 'default' => $val['column_default'], 'primary' => false, 'autoinc' => false, ]; } } return $info; } /** * 取得数据表的字段信息 * @access public * @return array */ public function getTables($dbName='') { $result = $this->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' "); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * order分析 * @access protected * @param mixed $order * @return string */ protected function parseOrder($order) { return !empty($order)? ' ORDER BY '.$order:' ORDER BY rand()'; } /** * limit * @access public * @param mixed $limit * @return string */ public 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; } /** * 更新记录 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return false | integer */ public function update($data,$options) { $this->model = $options['model']; $sql = 'UPDATE ' .$this->parseTable($options['table']) .$this->parseSet($data) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql); } /** * 删除记录 * @access public * @param array $options 表达式 * @return false | integer */ public function delete($options=[]) { $this->model = $options['model']; $sql = 'DELETE FROM ' .$this->parseTable($options['table']) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql); } }