Think\Db类改进 支持静态化调用驱动类的方法

Think、Debug类的debug方法改进
增加getQueryTimes和getExecuteTimes方法
This commit is contained in:
thinkphp
2013-03-30 13:25:21 +08:00
parent d58c1aa106
commit 97f9395a96
3 changed files with 113 additions and 62 deletions

View File

@@ -8,32 +8,34 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// $Id$
namespace Think;
namespace Think;
/** /**
* ThinkPHP 数据库中间层实现类 * ThinkPHP 数据库中间层实现类
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
*/ */
class Db { class Db {
static private $instance = []; static private $instance = []; // 数据库连接实例
static private $_instance = null; // 当前数据库连接实例
/** /**
* 取得数据库类实例 * 取得数据库类实例
* @static * @static
* @access public * @access public
* @return mixed 返回数据库驱动类 * @param mixed $config 连接配置
* @param boolean $lite 是否lite方式
* @return Object 返回数据库驱动类
*/ */
public static function getInstance($config=[]) { public static function instance($config=[],$lite=false) {
$md5 = md5(serialize($config)); $md5 = md5(serialize($config));
if(!isset(self::$instance[$md5])) { if(!isset(self::$instance[$md5])) {
// 解析连接参数 支持数组和字符串
$options = self::parseConfig($config); $options = self::parseConfig($config);
$class = 'Think\\Db\\Driver\\'.ucwords($options['dbms']); // 如果采用lite方式 仅支持原生SQL 包括query和execute方法
$class = $lite? 'Think\Db\Lite' : 'Think\\Db\\Driver\\'.ucwords($options['dbms']);
if(class_exists($class)) { if(class_exists($class)) {
self::$instance[$md5] = new $class($options); self::$instance[$md5] = new $class($options);
self::$_instance = self::$instance[$md5];
}else{ }else{
Error::halt('_DB_TYPE_INVALID_:'.$options['dbms']); Error::halt('_DB_TYPE_INVALID_:'.$options['dbms']);
} }
@@ -42,21 +44,13 @@ class Db {
} }
/** /**
* Lite版本数据库引擎 仅支持原生SQL 包括query和execute方法 * 数据库连接参数解析
* @static * @static
* @access public * @access public
* @return mixed 返回数据库驱动类 * @param mixed $config
* @return array
*/ */
public static function lite($config=[]) { static public function parseConfig($config){
static $_instance = [];
$md5 = md5(serialize($config));
if(!isset($_instance[$md5])) {
$_instance[$md5] = new Think\Db\Lite(self::parseConfig($config));
}
return $_instance[$md5];
}
static public function parseConfig($config=[]){
if(empty($config)) { if(empty($config)) {
$config = Config::get(); $config = Config::get();
} }
@@ -75,6 +69,8 @@ class Db {
'charset' => $config['db_charset'], 'charset' => $config['db_charset'],
'deploy' => $config['db_deploy'], 'deploy' => $config['db_deploy'],
'socket' => $config['db_unix_socket'], 'socket' => $config['db_unix_socket'],
'debug' => $config['db_debug'],
'deploy' => $config['db_deploy'],
]; ];
} }
@@ -109,4 +105,9 @@ class Db {
} }
return $dsn; return $dsn;
} }
// 调用驱动类的方法
public static function __callStatic($method, $params){
return call_user_func_array(array(self::$_instance, $method), $params);
}
} }

View File

@@ -14,7 +14,7 @@ use Think\Config;
use Think\Debug; use Think\Debug;
use Think\Log; use Think\Log;
use PDO; use PDO;
class Driver { abstract class Driver {
// PDO操作实例 // PDO操作实例
protected $PDOStatement = null; protected $PDOStatement = null;
// 当前操作所属的模型名 // 当前操作所属的模型名
@@ -26,8 +26,6 @@ class Driver {
protected $lastInsID = null; protected $lastInsID = null;
// 返回或者影响记录数 // 返回或者影响记录数
protected $numRows = 0; protected $numRows = 0;
// 返回字段数
protected $numCols = 0;
// 事务指令数 // 事务指令数
protected $transTimes = 0; protected $transTimes = 0;
// 错误信息 // 错误信息
@@ -42,7 +40,9 @@ class Driver {
protected $comparison = ['eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN']; protected $comparison = ['eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN'];
// 查询表达式 // 查询表达式
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%'; protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%';
// 查询次数
protected $queryTimes = 0; protected $queryTimes = 0;
// 执行次数
protected $executeTimes = 0; protected $executeTimes = 0;
// PDO连接参数 // PDO连接参数
protected $options = [ protected $options = [
@@ -117,13 +117,14 @@ class Driver {
//释放前次的查询结果 //释放前次的查询结果
if ( !empty($this->PDOStatement) ) $this->free(); if ( !empty($this->PDOStatement) ) $this->free();
$this->queryTimes++; $this->queryTimes++;
// 记录开始执行时间 // 调试开始
Debug::remark('queryStartTime','time'); $this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str); $this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) if(false === $this->PDOStatement)
throw_exception($this->error()); throw_exception($this->error());
$result = $this->PDOStatement->execute($bind); $result = $this->PDOStatement->execute($bind);
$this->debug(); // 调试结束
$this->debug(false);
if ( false === $result ) { if ( false === $result ) {
$this->error(); $this->error();
return false; return false;
@@ -146,13 +147,13 @@ class Driver {
if ( !empty($this->PDOStatement) ) $this->free(); if ( !empty($this->PDOStatement) ) $this->free();
$this->executeTimes++; $this->executeTimes++;
// 记录开始执行时间 // 记录开始执行时间
Debug::remark('queryStartTime','time'); $this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str); $this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) { if(false === $this->PDOStatement) {
throw_exception($this->error()); throw_exception($this->error());
} }
$result = $this->PDOStatement->execute($bind); $result = $this->PDOStatement->execute($bind);
$this->debug(); $this->debug(false);
if ( false === $result) { if ( false === $result) {
$this->error(); $this->error();
return false; return false;
@@ -236,6 +237,25 @@ class Driver {
return $result; return $result;
} }
/**
* 获得查询次数
* @access public
* @param boolean $execute 是否包含所有查询
* @return integer
*/
public function getQueryTimes($execute=false){
return $execute?$this->queryTimes+$this->executeTimes:$this->queryTimes;
}
/**
* 获得执行次数
* @access public
* @return integer
*/
public function getExecuteTimes(){
return $this->executeTimes;
}
/** /**
* 关闭数据库 * 关闭数据库
* @access public * @access public
@@ -858,14 +878,21 @@ class Driver {
/** /**
* 数据库调试 记录当前SQL * 数据库调试 记录当前SQL
* @access protected * @access protected
* @param boolean $start 调试开始标记 true 开始 false 结束
*/ */
protected function debug() { protected function debug($start) {
if($this->config['debug']) {// 开启数据库调试模式
if($start) {
Debug::remark('queryStartTime','time');
}else{
$this->modelSql[$this->model] = $this->queryStr; $this->modelSql[$this->model] = $this->queryStr;
$this->model = '_think_'; $this->model = '_think_';
// 记录操作结束时间 // 记录操作结束时间
Debug::remark('queryEndTime','time'); Debug::remark('queryEndTime','time');
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL'); Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
} }
}
}
/** /**
* 初始化数据库连接 * 初始化数据库连接

View File

@@ -26,8 +26,6 @@ class Lite {
protected $lastInsID = null; protected $lastInsID = null;
// 返回或者影响记录数 // 返回或者影响记录数
protected $numRows = 0; protected $numRows = 0;
// 返回字段数
protected $numCols = 0;
// 事务指令数 // 事务指令数
protected $transTimes = 0; protected $transTimes = 0;
// 错误信息 // 错误信息
@@ -116,13 +114,12 @@ class Lite {
//释放前次的查询结果 //释放前次的查询结果
if ( !empty($this->PDOStatement) ) $this->free(); if ( !empty($this->PDOStatement) ) $this->free();
$this->queryTimes++; $this->queryTimes++;
// 记录开始执行时间 $this->debug(true);
Debug::remark('queryStartTime','time');
$this->PDOStatement = $this->_linkID->prepare($str); $this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) if(false === $this->PDOStatement)
throw_exception($this->error()); throw_exception($this->error());
$result = $this->PDOStatement->execute($bind); $result = $this->PDOStatement->execute($bind);
$this->debug(); $this->debug(false);
if ( false === $result ) { if ( false === $result ) {
$this->error(); $this->error();
return false; return false;
@@ -145,13 +142,13 @@ class Lite {
if ( !empty($this->PDOStatement) ) $this->free(); if ( !empty($this->PDOStatement) ) $this->free();
$this->executeTimes++; $this->executeTimes++;
// 记录开始执行时间 // 记录开始执行时间
Debug::remark('queryStartTime','time'); $this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str); $this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) { if(false === $this->PDOStatement) {
throw_exception($this->error()); throw_exception($this->error());
} }
$result = $this->PDOStatement->execute($bind); $result = $this->PDOStatement->execute($bind);
$this->debug(); $this->debug(false);
if ( false === $result) { if ( false === $result) {
$this->error(); $this->error();
return false; return false;
@@ -235,6 +232,25 @@ class Lite {
return $result; return $result;
} }
/**
* 获得查询次数
* @access public
* @param boolean $execute 是否包含所有查询
* @return integer
*/
public function getQueryTimes($execute=false){
return $execute?$this->queryTimes+$this->executeTimes:$this->queryTimes;
}
/**
* 获得执行次数
* @access public
* @return integer
*/
public function getExecuteTimes(){
return $this->executeTimes;
}
/** /**
* 关闭数据库 * 关闭数据库
* @access public * @access public
@@ -252,7 +268,7 @@ class Lite {
public function error() { public function error() {
if($this->PDOStatement) { if($this->PDOStatement) {
$error = $this->PDOStatement->errorInfo(); $error = $this->PDOStatement->errorInfo();
$this->error = $error[2]; $this->error = $error[1].':'.$error[2];
}else{ }else{
$this->error = ''; $this->error = '';
} }
@@ -304,14 +320,21 @@ class Lite {
/** /**
* 数据库调试 记录当前SQL * 数据库调试 记录当前SQL
* @access protected * @access protected
* @param boolean $start 调试开始标记 true 开始 false 结束
*/ */
protected function debug() { protected function debug($start) {
if($this->config['debug']) {// 开启数据库调试模式
if($start) {
Debug::remark('queryStartTime','time');
}else{
$this->modelSql[$this->model] = $this->queryStr; $this->modelSql[$this->model] = $this->queryStr;
$this->model = '_think_'; $this->model = '_think_';
// 记录操作结束时间 // 记录操作结束时间
Debug::remark('queryEndTime','time'); Debug::remark('queryEndTime','time');
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL'); Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
} }
}
}
/** /**
* 初始化数据库连接 * 初始化数据库连接