123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- defined('THINK_PATH') or exit();
- /**
- * Sqlsrv数据库驱动
- * @category Extend
- * @package Extend
- * @subpackage Driver.Db
- * @author liu21st <liu21st@gmail.com>
- */
- class DbSqlsrv extends Db{
- 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
- * @param array $config 数据库配置数组
- */
- public function __construct($config='') {
- if ( !function_exists('sqlsrv_connect') ) {
- throw_exception(L('_NOT_SUPPERT_').':sqlsrv');
- }
- if(!empty($config)) {
- $this->config = $config;
- }
- }
- /**
- * 连接数据库方法
- * @access public
- */
- public function connect($config='',$linkNum=0) {
- if ( !isset($this->linkID[$linkNum]) ) {
- if(empty($config)) $config = $this->config;
- $host = $config['hostname'].($config['hostport']?",{$config['hostport']}":'');
- $connectInfo = array('Database'=>$config['database'],'UID'=>$config['username'],'PWD'=>$config['password'],'CharacterSet' => C('DEFAULT_CHARSET'));
- $this->linkID[$linkNum] = sqlsrv_connect( $host, $connectInfo);
- if ( !$this->linkID[$linkNum] ) $this->error(false);
- // 标记连接成功
- $this->connected = true;
- //注销数据库安全信息
- if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
- }
- return $this->linkID[$linkNum];
- }
- /**
- * 释放查询结果
- * @access public
- */
- public function free() {
- sqlsrv_free_stmt($this->queryID);
- $this->queryID = null;
- }
- /**
- * 执行查询 返回数据集
- * @access public
- * @param string $str sql指令
- * @param array $bind 参数绑定
- * @return mixed
- */
- public function query($str,$bind=array()) {
- $this->initConnect(false);
- if ( !$this->_linkID ) return false;
- //释放前次的查询结果
- if ( $this->queryID ) $this->free();
- N('db_query',1);
- // 记录开始执行时间
- G('queryStartTime');
- $str = str_replace(array_keys($bind),'?',$str);
- $bind = array_values($bind);
- $this->queryStr = $str;
- $this->queryID = sqlsrv_query($this->_linkID,$str,$bind, array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
- $this->debug();
- if ( false === $this->queryID ) {
- $this->error();
- return false;
- } else {
- $this->numRows = sqlsrv_num_rows($this->queryID);
- return $this->getAll();
- }
- }
- /**
- * 执行语句
- * @access public
- * @param string $str sql指令
- * @param array $bind 参数绑定
- * @return integer
- */
- public function execute($str,$bind=array()) {
- $this->initConnect(true);
- if ( !$this->_linkID ) return false;
- //释放前次的查询结果
- if ( $this->queryID ) $this->free();
- N('db_write',1);
- // 记录开始执行时间
- G('queryStartTime');
- $str = str_replace(array_keys($bind),'?',$str);
- $bind = array_values($bind);
- $this->queryStr = $str;
- $this->queryID= sqlsrv_query($this->_linkID,$str,$bind);
- $this->debug();
- if ( false === $this->queryID ) {
- $this->error();
- return false;
- } else {
- $this->numRows = sqlsrv_rows_affected($this->queryID);
- $this->lastInsID = $this->mssql_insert_id();
- return $this->numRows;
- }
- }
- /**
- * 用于获取最后插入的ID
- * @access public
- * @return integer
- */
- public function mssql_insert_id() {
- $query = "SELECT @@IDENTITY as last_insert_id";
- $result = sqlsrv_query($this->_linkID,$query);
- list($last_insert_id) = sqlsrv_fetch_array($result);
- sqlsrv_free_stmt($result);
- return $last_insert_id;
- }
- /**
- * 启动事务
- * @access public
- * @return void
- */
- public function startTrans() {
- $this->initConnect(true);
- if ( !$this->_linkID ) return false;
- //数据rollback 支持
- if ($this->transTimes == 0) {
- sqlsrv_begin_transaction($this->_linkID);
- }
- $this->transTimes++;
- return ;
- }
- /**
- * 用于非自动提交状态下面的查询提交
- * @access public
- * @return boolen
- */
- public function commit() {
- if ($this->transTimes > 0) {
- $result = sqlsrv_commit($this->_linkID);
- $this->transTimes = 0;
- if(!$result){
- $this->error();
- return false;
- }
- }
- return true;
- }
- /**
- * 事务回滚
- * @access public
- * @return boolen
- */
- public function rollback() {
- if ($this->transTimes > 0) {
- $result = sqlsrv_rollback($this->_linkID);
- $this->transTimes = 0;
- if(!$result){
- $this->error();
- return false;
- }
- }
- return true;
- }
- /**
- * 获得所有的查询数据
- * @access private
- * @return array
- */
- private function getAll() {
- //返回数据集
- $result = array();
- if($this->numRows >0) {
- while($row = sqlsrv_fetch_array($this->queryID,SQLSRV_FETCH_ASSOC))
- $result[] = $row;
- }
- return $result;
- }
- /**
- * 取得数据表的字段信息
- * @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}'");
- $pk = $this->query("SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='{$tableName}'");
- $info = array();
- if($result) {
- foreach ($result as $key => $val) {
- $info[$val['column_name']] = array(
- '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' => $val['column_name'] == $pk[0]['COLUMN_NAME'],
- '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 = array();
- 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()';
- }
- /**
- * 字段名分析
- * @access protected
- * @param string $key
- * @return string
- */
- protected function parseKey(&$key) {
- $key = trim($key);
- if(!preg_match('/[,\'\"\*\(\)\[.\s]/',$key)) {
- $key = '['.$key.']';
- }
- return $key;
- }
-
- /**
- * 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,$this->parseBind(!empty($options['bind'])?$options['bind']:array()));
- }
- /**
- * 删除记录
- * @access public
- * @param array $options 表达式
- * @return false | integer
- */
- public function delete($options=array()) {
- $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,$this->parseBind(!empty($options['bind'])?$options['bind']:array()));
- }
- /**
- * 关闭数据库
- * @access public
- */
- public function close() {
- if ($this->_linkID){
- sqlsrv_close($this->_linkID);
- }
- $this->_linkID = null;
- }
- /**
- * 数据库错误信息
- * 并显示当前的SQL语句
- * @access public
- * @return string
- */
- public function error($result = true) {
- $errors = sqlsrv_errors();
- $this->error = '';
- foreach( $errors as $error ) {
- $this->error .= $error['code'].':'.$error['message'];
- }
- if('' != $this->queryStr){
- $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
- }
- $result? trace($this->error,'','ERR'):throw_exception($this->error);
- return $this->error;
- }
- }
|