Connection类的debug方法改进,增加第二个参数,改进数据库驱动的获取表信息的sql记录

This commit is contained in:
thinkphp
2016-08-08 11:09:44 +08:00
parent f21e6827e1
commit 4e66bf24e6
5 changed files with 67 additions and 26 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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) {