<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* sqlsrv数据库驱动类
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Db
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class DbSqlsrv extends Db{
protected $selectSql = 'SELECT T1.* FROM (SELECT ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER, thinkphp.* FROM (SELECT %DISTINCT% %FIELDS% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 WHERE %LIMIT%';
/**
+----------------------------------------------------------
* 架构函数 读取数据库配置信息
+----------------------------------------------------------
* @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
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
// 处理不带端口号的socket连接情况
$options = array();
$options['UID'] = $config['username'];
$options['PWD'] = $config['password'];
$options['Database'] = $config['database'];
$options['CharacterSet'] = C('DB_CHARSET');
if(!($this->linkID[$linkNum] = sqlsrv_connect($config['hostname'],$options))) {
throw sqlsrv_errors();
}
// 标记连接成功
$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 = 0;
}
/**
+----------------------------------------------------------
* 执行查询 返回数据集
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $str sql指令
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function query($str) {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//释放前次的查询结果
if ( $this->queryID ) $this->free();
$this->Q(1);
$this->queryID = sqlsrv_query($this->_linkID , $str ,null,array('Scrollable'=>SQLSRV_CURSOR_KEYSET)
);
$this->debug();
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
if(($this->numRows = sqlsrv_num_rows($this->queryID)) === false) {
dump(sqlsrv_errors());
}
return $this->getAll();
}
}
/**
+----------------------------------------------------------
* 执行语句
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $str sql指令
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function execute($str) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//释放前次的查询结果
if ( $this->queryID ) $this->free();
$this->W(1);
$result = sqlsrv_query($this->_linkID, $str );
$this->debug();
if ( false === $result ) {
$this->error();
return false;
} else {
$this->numRows = sqlsrv_rows_affected($this->_linkID);
$this->lastInsID = $this->sqlsrv_insert_id();
return $this->numRows;
}
}
/**
+----------------------------------------------------------
* 用于获取最后插入的ID
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
public function sqlsrv_insert_id()
{
$query = "SELECT @@IDENTITY as last_insert_id";
$result = sqlsrv_query($query, $this->_linkID );
list($last_insert_id) = sqlsrv_fetch($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_query($this->_linkID, 'BEGIN TRAN' );
}
$this->transTimes++;
return ;
}
/**
+----------------------------------------------------------
* 用于非自动提交状态下面的查询提交
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function commit()
{
if ($this->transTimes > 0) {
$result = sqlsrv_query($this->_linkID,'COMMIT TRAN' );
$this->transTimes = 0;
if(!$result){
throw_exception($this->error());
}
}
- 1
- 2
前往页