mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 14:52:47 +08:00
数据库中间层改为PDO
增加Think\Db\Lite 用于操作原生SQL需求
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Db\Driver;
|
||||
use Think\Db;
|
||||
use Think\Db\Driver;
|
||||
/**
|
||||
* Sqlsrv数据库驱动
|
||||
* @category Extend
|
||||
@@ -18,180 +18,8 @@ use Think\Db;
|
||||
* @subpackage Driver.Db
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Sqlsrv extends Db{
|
||||
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
|
||||
* @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指令
|
||||
* @return mixed
|
||||
*/
|
||||
public function query($str) {
|
||||
$this->initConnect(false);
|
||||
if ( !$this->_linkID ) return false;
|
||||
$this->queryStr = $str;
|
||||
//释放前次的查询结果
|
||||
if ( $this->queryID ) $this->free();
|
||||
N('db_query',1);
|
||||
// 记录开始执行时间
|
||||
G('queryStartTime');
|
||||
$this->queryID = sqlsrv_query($this->_linkID,$str,array(), 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指令
|
||||
* @return integer
|
||||
*/
|
||||
public function execute($str) {
|
||||
$this->initConnect(true);
|
||||
if ( !$this->_linkID ) return false;
|
||||
$this->queryStr = $str;
|
||||
//释放前次的查询结果
|
||||
if ( $this->queryID ) $this->free();
|
||||
N('db_write',1);
|
||||
// 记录开始执行时间
|
||||
G('queryStartTime');
|
||||
$this->queryID= sqlsrv_query($this->_linkID,$str);
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
@@ -299,33 +127,4 @@ class Sqlsrv extends Db{
|
||||
return $this->execute($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭数据库
|
||||
* @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['message'];
|
||||
}
|
||||
if('' != $this->queryStr){
|
||||
$this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
|
||||
}
|
||||
$result? ThinkLog::record($error['message'],'ERR'):throw_exception($this->error);
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user