From 9ac9d43a464baf60f3e15668cee19584e65b52d4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 10 Apr 2016 14:17:54 +0800 Subject: [PATCH] =?UTF-8?q?Db=E7=B1=BB=E5=A2=9E=E5=8A=A0listen=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=E7=94=A8=E4=BA=8E=E7=9B=91=E5=90=ACSQL=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=93=8D=E4=BD=9C=20=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 10 ++++----- library/think/db/Driver.php | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 3aad0536..2730fab8 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -547,22 +547,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function event($event, $callback, $override = false) { if ($override) { - self::$event[$event] = []; + static::$event[$event] = []; } - self::$event[$event][] = $callback; + static::$event[$event][] = $callback; } /** * 触发事件 - * @access public + * @access protected * @param string $event 事件名 * @param mixed $params 传入参数(引用) * @return bool */ protected function trigger($event, &$params) { - if (isset(self::$event[$event])) { - foreach (self::$event[$event] as $callback) { + if (isset(static::$event[$event])) { + foreach (static::$event[$event] as $callback) { if (is_callable($callback)) { $result = call_user_func_array($callback, [ & $params]); if (false === $result) { diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 97c57f8e..2e079072 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -46,6 +46,8 @@ abstract class Driver protected $linkID = null; // 查询参数 protected $options = []; + // 监听回调 + protected static $event = []; // 数据库连接参数配置 protected $config = [ @@ -2176,17 +2178,53 @@ abstract class Driver } else { // 记录操作结束时间 Debug::remark('queryEndTime', 'time'); - $log = $this->queryStr . ' [ RunTime:' . Debug::getRangeTime('queryStartTime', 'queryEndTime') . 's ]'; + $runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime'); + $log = $this->queryStr . ' [ RunTime:' . $runtime . 's ]'; + $result = []; // SQL性能分析 if (0 === stripos(trim($this->queryStr), 'select')) { $result = $this->getExplain($this->queryStr); - Log::record('[ EXPLAIN : ' . var_export($result, true) . ' ]', 'sql'); } - Log::record('[ SQL ] ' . $log, 'sql'); + // SQL监听 + $this->trigger($this->queryStr, $runtime, $result); } } } + /** + * 监听SQL执行 + * @access public + * @param callable $callback 回调方法 + * @return void + */ + public function listen($callback) + { + self::$event[] = $callback; + } + + /** + * 触发SQL事件 + * @access protected + * @param string $sql SQL语句 + * @param float $runtime SQL运行时间 + * @param mixed $explain SQL分析 + * @return bool + */ + protected function trigger($sql, $runtime, $explain = []) + { + if (!empty(self::$event)) { + foreach (self::$event as $callback) { + if (is_callable($callback)) { + call_user_func_array($callback, [$sql, $runtime, $explain]); + } + } + } else { + // 未注册监听则记录到日志中 + Log::record('[ SQL ] ' . $this->queryStr . ' [ RunTime:' . $runtime . 's ]', 'sql'); + Log::record('[ EXPLAIN : ' . var_export($result, true) . ' ]', 'sql'); + } + } + /** * 初始化数据库连接 * @access protected