From 4e66bf24e677234e2eec5ddf36f30de8fe1fa0f4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 8 Aug 2016 11:09:44 +0800 Subject: [PATCH] =?UTF-8?q?Connection=E7=B1=BB=E7=9A=84debug=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=B9=E8=BF=9B=EF=BC=8C=E5=A2=9E=E5=8A=A0=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E4=B8=AA=E5=8F=82=E6=95=B0=EF=BC=8C=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=A9=B1=E5=8A=A8=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=A1=A8=E4=BF=A1=E6=81=AF=E7=9A=84sql=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 12 +++++++----- library/think/db/connector/Mysql.php | 21 +++++++++++++++------ library/think/db/connector/Pgsql.php | 21 +++++++++++++++------ library/think/db/connector/Sqlite.php | 19 ++++++++++++++----- library/think/db/connector/Sqlsrv.php | 20 ++++++++++++++++---- 5 files changed, 67 insertions(+), 26 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 6712e0d9..f5182be4 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -738,9 +738,10 @@ abstract class Connection * 数据库调试 记录当前SQL及分析性能 * @access protected * @param boolean $start 调试开始标记 true 开始 false 结束 + * @param string $sql 执行的SQL语句 留空自动获取 * @return void */ - protected function debug($start) + protected function debug($start, $sql = '') { if (!empty($this->config['debug'])) { // 开启数据库调试模式 @@ -750,14 +751,15 @@ abstract class Connection // 记录操作结束时间 Debug::remark('queryEndTime', 'time'); $runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime'); - $log = $this->queryStr . ' [ RunTime:' . $runtime . 's ]'; + $sql = $sql ?: $this->queryStr; + $log = $sql . ' [ RunTime:' . $runtime . 's ]'; $result = []; // SQL性能分析 - if ($this->config['sql_explain'] && 0 === stripos(trim($this->queryStr), 'select')) { - $result = $this->getExplain($this->queryStr); + if ($this->config['sql_explain'] && 0 === stripos(trim($sql), 'select')) { + $result = $this->getExplain($sql); } // SQL监听 - $this->trigger($this->queryStr, $runtime, $result); + $this->trigger($sql, $runtime, $result); } } } diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 5f83f496..42a6376b 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -54,8 +54,12 @@ class Mysql extends Connection if (strpos($tableName, '.')) { $tableName = str_replace('.', '`.`', $tableName); } - $sql = 'SHOW COLUMNS FROM `' . $tableName . '`'; - $pdo = $this->linkID->query($sql); + $sql = 'SHOW COLUMNS FROM `' . $tableName . '`'; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; if ($result) { @@ -82,8 +86,12 @@ class Mysql extends Connection */ public function getTables($dbName = '') { - $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; - $pdo = $this->linkID->query($sql); + $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { @@ -110,8 +118,9 @@ class Mysql extends Connection } return $result; } - - protected function supportSavepoint(){ + + protected function supportSavepoint() + { return true; } } diff --git a/library/think/db/connector/Pgsql.php b/library/think/db/connector/Pgsql.php index 161165ac..06e08f54 100644 --- a/library/think/db/connector/Pgsql.php +++ b/library/think/db/connector/Pgsql.php @@ -46,9 +46,13 @@ class Pgsql extends Connection $this->initConnect(true); list($tableName) = explode(' ', $tableName); $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');'; - $pdo = $this->linkID->query($sql); - $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); + $result = $pdo->fetchAll(PDO::FETCH_ASSOC); + $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); @@ -73,8 +77,12 @@ class Pgsql extends Connection */ public function getTables($dbName = '') { - $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; - $pdo = $this->linkID->query($sql); + $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { @@ -94,7 +102,8 @@ class Pgsql extends Connection return []; } - protected function supportSavepoint(){ + protected function supportSavepoint() + { return true; } } diff --git a/library/think/db/connector/Sqlite.php b/library/think/db/connector/Sqlite.php index d8f63f81..1a52905e 100644 --- a/library/think/db/connector/Sqlite.php +++ b/library/think/db/connector/Sqlite.php @@ -43,9 +43,13 @@ class Sqlite extends Connection $this->initConnect(true); list($tableName) = explode(' ', $tableName); $sql = 'PRAGMA table_info( ' . $tableName . ' )'; - $pdo = $this->linkID->query($sql); - $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); + $result = $pdo->fetchAll(PDO::FETCH_ASSOC); + $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); @@ -73,7 +77,11 @@ class Sqlite extends Connection $sql = "SELECT name FROM sqlite_master WHERE type='table' " . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type='table' ORDER BY name"; - $pdo = $this->linkID->query($sql); + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { @@ -93,7 +101,8 @@ class Sqlite extends Connection return []; } - protected function supportSavepoint(){ + protected function supportSavepoint() + { return true; } } diff --git a/library/think/db/connector/Sqlsrv.php b/library/think/db/connector/Sqlsrv.php index cce0c6c8..1100f5dd 100644 --- a/library/think/db/connector/Sqlsrv.php +++ b/library/think/db/connector/Sqlsrv.php @@ -58,7 +58,11 @@ class Sqlsrv extends Connection AND t.table_schema = c.table_schema AND t.table_name = c.table_name WHERE t.table_name = '$tableName'"; - $pdo = $this->linkID->query($sql); + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; if ($result) { @@ -74,8 +78,12 @@ class Sqlsrv extends Connection ]; } } - $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'"; - $pdo = $this->linkID->query($sql); + $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'"; + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetch(PDO::FETCH_ASSOC); if ($result) { $info[$result['column_name']]['primary'] = true; @@ -95,7 +103,11 @@ class Sqlsrv extends Connection FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' "; - $pdo = $this->linkID->query($sql); + // 调试开始 + $this->debug(true); + $pdo = $this->linkID->query($sql); + // 调试结束 + $this->debug(false, $sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) {