mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
Think\Db类改进 支持静态化调用驱动类的方法
Think、Debug类的debug方法改进 增加getQueryTimes和getExecuteTimes方法
This commit is contained in:
69
Think/Db.php
69
Think/Db.php
@@ -8,32 +8,34 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
// $Id$
|
||||
namespace Think;
|
||||
|
||||
namespace Think;
|
||||
/**
|
||||
* ThinkPHP 数据库中间层实现类
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Core
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Db {
|
||||
|
||||
static private $instance = [];
|
||||
static private $instance = []; // 数据库连接实例
|
||||
static private $_instance = null; // 当前数据库连接实例
|
||||
|
||||
/**
|
||||
* 取得数据库类实例
|
||||
* @static
|
||||
* @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));
|
||||
if(!isset(self::$instance[$md5])) {
|
||||
// 解析连接参数 支持数组和字符串
|
||||
$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)) {
|
||||
self::$instance[$md5] = new $class($options);
|
||||
self::$instance[$md5] = new $class($options);
|
||||
self::$_instance = self::$instance[$md5];
|
||||
}else{
|
||||
Error::halt('_DB_TYPE_INVALID_:'.$options['dbms']);
|
||||
}
|
||||
@@ -42,21 +44,13 @@ class Db {
|
||||
}
|
||||
|
||||
/**
|
||||
* Lite版本数据库引擎 仅支持原生SQL 包括query和execute方法
|
||||
* 数据库连接参数解析
|
||||
* @static
|
||||
* @access public
|
||||
* @return mixed 返回数据库驱动类
|
||||
* @param mixed $config
|
||||
* @return array
|
||||
*/
|
||||
public static function lite($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=[]){
|
||||
static public function parseConfig($config){
|
||||
if(empty($config)) {
|
||||
$config = Config::get();
|
||||
}
|
||||
@@ -64,17 +58,19 @@ class Db {
|
||||
return self::parseDsn($config);
|
||||
}
|
||||
return [
|
||||
'dbms' => $config['db_type'],
|
||||
'dsn' => $config['db_dsn'],
|
||||
'username' => $config['db_user'],
|
||||
'password' => $config['db_pwd'],
|
||||
'hostname' => $config['db_host'],
|
||||
'hostport' => $config['db_port'],
|
||||
'database' => $config['db_name'],
|
||||
'params' => $config['db_params'],
|
||||
'charset' => $config['db_charset'],
|
||||
'deploy' => $config['db_deploy'],
|
||||
'socket' => $config['db_unix_socket'],
|
||||
'dbms' => $config['db_type'],
|
||||
'dsn' => $config['db_dsn'],
|
||||
'username' => $config['db_user'],
|
||||
'password' => $config['db_pwd'],
|
||||
'hostname' => $config['db_host'],
|
||||
'hostport' => $config['db_port'],
|
||||
'database' => $config['db_name'],
|
||||
'params' => $config['db_params'],
|
||||
'charset' => $config['db_charset'],
|
||||
'deploy' => $config['db_deploy'],
|
||||
'socket' => $config['db_unix_socket'],
|
||||
'debug' => $config['db_debug'],
|
||||
'deploy' => $config['db_deploy'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -109,4 +105,9 @@ class Db {
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
public static function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use Think\Config;
|
||||
use Think\Debug;
|
||||
use Think\Log;
|
||||
use PDO;
|
||||
class Driver {
|
||||
abstract class Driver {
|
||||
// PDO操作实例
|
||||
protected $PDOStatement = null;
|
||||
// 当前操作所属的模型名
|
||||
@@ -26,8 +26,6 @@ class Driver {
|
||||
protected $lastInsID = null;
|
||||
// 返回或者影响记录数
|
||||
protected $numRows = 0;
|
||||
// 返回字段数
|
||||
protected $numCols = 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 $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%';
|
||||
// 查询次数
|
||||
protected $queryTimes = 0;
|
||||
// 执行次数
|
||||
protected $executeTimes = 0;
|
||||
// PDO连接参数
|
||||
protected $options = [
|
||||
@@ -117,13 +117,14 @@ class Driver {
|
||||
//释放前次的查询结果
|
||||
if ( !empty($this->PDOStatement) ) $this->free();
|
||||
$this->queryTimes++;
|
||||
// 记录开始执行时间
|
||||
Debug::remark('queryStartTime','time');
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement)
|
||||
throw_exception($this->error());
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug();
|
||||
// 调试结束
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -146,13 +147,13 @@ class Driver {
|
||||
if ( !empty($this->PDOStatement) ) $this->free();
|
||||
$this->executeTimes++;
|
||||
// 记录开始执行时间
|
||||
Debug::remark('queryStartTime','time');
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement) {
|
||||
throw_exception($this->error());
|
||||
}
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -236,6 +237,25 @@ class Driver {
|
||||
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
|
||||
@@ -858,13 +878,20 @@ class Driver {
|
||||
/**
|
||||
* 数据库调试 记录当前SQL
|
||||
* @access protected
|
||||
* @param boolean $start 调试开始标记 true 开始 false 结束
|
||||
*/
|
||||
protected function debug() {
|
||||
$this->modelSql[$this->model] = $this->queryStr;
|
||||
$this->model = '_think_';
|
||||
// 记录操作结束时间
|
||||
Debug::remark('queryEndTime','time');
|
||||
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
|
||||
protected function debug($start) {
|
||||
if($this->config['debug']) {// 开启数据库调试模式
|
||||
if($start) {
|
||||
Debug::remark('queryStartTime','time');
|
||||
}else{
|
||||
$this->modelSql[$this->model] = $this->queryStr;
|
||||
$this->model = '_think_';
|
||||
// 记录操作结束时间
|
||||
Debug::remark('queryEndTime','time');
|
||||
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,8 +26,6 @@ class Lite {
|
||||
protected $lastInsID = null;
|
||||
// 返回或者影响记录数
|
||||
protected $numRows = 0;
|
||||
// 返回字段数
|
||||
protected $numCols = 0;
|
||||
// 事务指令数
|
||||
protected $transTimes = 0;
|
||||
// 错误信息
|
||||
@@ -116,13 +114,12 @@ class Lite {
|
||||
//释放前次的查询结果
|
||||
if ( !empty($this->PDOStatement) ) $this->free();
|
||||
$this->queryTimes++;
|
||||
// 记录开始执行时间
|
||||
Debug::remark('queryStartTime','time');
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement)
|
||||
throw_exception($this->error());
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug();
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -145,13 +142,13 @@ class Lite {
|
||||
if ( !empty($this->PDOStatement) ) $this->free();
|
||||
$this->executeTimes++;
|
||||
// 记录开始执行时间
|
||||
Debug::remark('queryStartTime','time');
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement) {
|
||||
throw_exception($this->error());
|
||||
}
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -235,6 +232,25 @@ class Lite {
|
||||
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
|
||||
@@ -252,7 +268,7 @@ class Lite {
|
||||
public function error() {
|
||||
if($this->PDOStatement) {
|
||||
$error = $this->PDOStatement->errorInfo();
|
||||
$this->error = $error[2];
|
||||
$this->error = $error[1].':'.$error[2];
|
||||
}else{
|
||||
$this->error = '';
|
||||
}
|
||||
@@ -304,13 +320,20 @@ class Lite {
|
||||
/**
|
||||
* 数据库调试 记录当前SQL
|
||||
* @access protected
|
||||
* @param boolean $start 调试开始标记 true 开始 false 结束
|
||||
*/
|
||||
protected function debug() {
|
||||
$this->modelSql[$this->model] = $this->queryStr;
|
||||
$this->model = '_think_';
|
||||
// 记录操作结束时间
|
||||
Debug::remark('queryEndTime','time');
|
||||
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
|
||||
protected function debug($start) {
|
||||
if($this->config['debug']) {// 开启数据库调试模式
|
||||
if($start) {
|
||||
Debug::remark('queryStartTime','time');
|
||||
}else{
|
||||
$this->modelSql[$this->model] = $this->queryStr;
|
||||
$this->model = '_think_';
|
||||
// 记录操作结束时间
|
||||
Debug::remark('queryEndTime','time');
|
||||
Log::record($this->queryStr.' [ RunTime:'.Debug::getUseTime('queryStartTime','queryEndTime').'s ]','SQL');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user