mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 23:02:48 +08:00
Connection类的debug方法改进,增加第二个参数,改进数据库驱动的获取表信息的sql记录
This commit is contained in:
@@ -738,9 +738,10 @@ abstract class Connection
|
|||||||
* 数据库调试 记录当前SQL及分析性能
|
* 数据库调试 记录当前SQL及分析性能
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param boolean $start 调试开始标记 true 开始 false 结束
|
* @param boolean $start 调试开始标记 true 开始 false 结束
|
||||||
|
* @param string $sql 执行的SQL语句 留空自动获取
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function debug($start)
|
protected function debug($start, $sql = '')
|
||||||
{
|
{
|
||||||
if (!empty($this->config['debug'])) {
|
if (!empty($this->config['debug'])) {
|
||||||
// 开启数据库调试模式
|
// 开启数据库调试模式
|
||||||
@@ -750,14 +751,15 @@ abstract class Connection
|
|||||||
// 记录操作结束时间
|
// 记录操作结束时间
|
||||||
Debug::remark('queryEndTime', 'time');
|
Debug::remark('queryEndTime', 'time');
|
||||||
$runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime');
|
$runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime');
|
||||||
$log = $this->queryStr . ' [ RunTime:' . $runtime . 's ]';
|
$sql = $sql ?: $this->queryStr;
|
||||||
|
$log = $sql . ' [ RunTime:' . $runtime . 's ]';
|
||||||
$result = [];
|
$result = [];
|
||||||
// SQL性能分析
|
// SQL性能分析
|
||||||
if ($this->config['sql_explain'] && 0 === stripos(trim($this->queryStr), 'select')) {
|
if ($this->config['sql_explain'] && 0 === stripos(trim($sql), 'select')) {
|
||||||
$result = $this->getExplain($this->queryStr);
|
$result = $this->getExplain($sql);
|
||||||
}
|
}
|
||||||
// SQL监听
|
// SQL监听
|
||||||
$this->trigger($this->queryStr, $runtime, $result);
|
$this->trigger($sql, $runtime, $result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,8 +54,12 @@ class Mysql extends Connection
|
|||||||
if (strpos($tableName, '.')) {
|
if (strpos($tableName, '.')) {
|
||||||
$tableName = str_replace('.', '`.`', $tableName);
|
$tableName = str_replace('.', '`.`', $tableName);
|
||||||
}
|
}
|
||||||
$sql = 'SHOW COLUMNS FROM `' . $tableName . '`';
|
$sql = 'SHOW COLUMNS FROM `' . $tableName . '`';
|
||||||
$pdo = $this->linkID->query($sql);
|
// 调试开始
|
||||||
|
$this->debug(true);
|
||||||
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
if ($result) {
|
if ($result) {
|
||||||
@@ -82,8 +86,12 @@ class Mysql extends Connection
|
|||||||
*/
|
*/
|
||||||
public function getTables($dbName = '')
|
public function getTables($dbName = '')
|
||||||
{
|
{
|
||||||
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
||||||
$pdo = $this->linkID->query($sql);
|
// 调试开始
|
||||||
|
$this->debug(true);
|
||||||
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
@@ -111,7 +119,8 @@ class Mysql extends Connection
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function supportSavepoint(){
|
protected function supportSavepoint()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,9 +46,13 @@ class Pgsql extends Connection
|
|||||||
$this->initConnect(true);
|
$this->initConnect(true);
|
||||||
list($tableName) = explode(' ', $tableName);
|
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 . '\');';
|
$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);
|
$this->debug(true);
|
||||||
$info = [];
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
$info = [];
|
||||||
if ($result) {
|
if ($result) {
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
$val = array_change_key_case($val);
|
$val = array_change_key_case($val);
|
||||||
@@ -73,8 +77,12 @@ class Pgsql extends Connection
|
|||||||
*/
|
*/
|
||||||
public function getTables($dbName = '')
|
public function getTables($dbName = '')
|
||||||
{
|
{
|
||||||
$sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
|
$sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
|
||||||
$pdo = $this->linkID->query($sql);
|
// 调试开始
|
||||||
|
$this->debug(true);
|
||||||
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
@@ -94,7 +102,8 @@ class Pgsql extends Connection
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function supportSavepoint(){
|
protected function supportSavepoint()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,9 +43,13 @@ class Sqlite extends Connection
|
|||||||
$this->initConnect(true);
|
$this->initConnect(true);
|
||||||
list($tableName) = explode(' ', $tableName);
|
list($tableName) = explode(' ', $tableName);
|
||||||
$sql = 'PRAGMA table_info( ' . $tableName . ' )';
|
$sql = 'PRAGMA table_info( ' . $tableName . ' )';
|
||||||
$pdo = $this->linkID->query($sql);
|
// 调试开始
|
||||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
$this->debug(true);
|
||||||
$info = [];
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
$info = [];
|
||||||
if ($result) {
|
if ($result) {
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
$val = array_change_key_case($val);
|
$val = array_change_key_case($val);
|
||||||
@@ -73,7 +77,11 @@ class Sqlite extends Connection
|
|||||||
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
|
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
|
||||||
. "UNION ALL SELECT name FROM sqlite_temp_master "
|
. "UNION ALL SELECT name FROM sqlite_temp_master "
|
||||||
. "WHERE type='table' ORDER BY name";
|
. "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);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
@@ -93,7 +101,8 @@ class Sqlite extends Connection
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function supportSavepoint(){
|
protected function supportSavepoint()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,11 @@ class Sqlsrv extends Connection
|
|||||||
AND t.table_schema = c.table_schema
|
AND t.table_schema = c.table_schema
|
||||||
AND t.table_name = c.table_name
|
AND t.table_name = c.table_name
|
||||||
WHERE t.table_name = '$tableName'";
|
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);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
if ($result) {
|
if ($result) {
|
||||||
@@ -74,8 +78,12 @@ class Sqlsrv extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
|
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
|
||||||
$pdo = $this->linkID->query($sql);
|
// 调试开始
|
||||||
|
$this->debug(true);
|
||||||
|
$pdo = $this->linkID->query($sql);
|
||||||
|
// 调试结束
|
||||||
|
$this->debug(false, $sql);
|
||||||
$result = $pdo->fetch(PDO::FETCH_ASSOC);
|
$result = $pdo->fetch(PDO::FETCH_ASSOC);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$info[$result['column_name']]['primary'] = true;
|
$info[$result['column_name']]['primary'] = true;
|
||||||
@@ -95,7 +103,11 @@ class Sqlsrv extends Connection
|
|||||||
FROM INFORMATION_SCHEMA.TABLES
|
FROM INFORMATION_SCHEMA.TABLES
|
||||||
WHERE TABLE_TYPE = 'BASE TABLE'
|
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);
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($result as $key => $val) {
|
foreach ($result as $key => $val) {
|
||||||
|
|||||||
Reference in New Issue
Block a user