mirror of
https://github.com/e107inc/e107.git
synced 2025-08-12 09:34:54 +02:00
Fixes #5490 debug SQL queries
This commit is contained in:
@@ -80,6 +80,7 @@ class e107_db_debug
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -213,8 +214,11 @@ class e107_db_debug
|
|||||||
*/
|
*/
|
||||||
function Mark_Query($query, $rli, $origQryRes, $aTrace, $mytime, $curtable)
|
function Mark_Query($query, $rli, $origQryRes, $aTrace, $mytime, $curtable)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
if(!$this->active)
|
if(!$this->active)
|
||||||
{
|
{
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// global $sql;
|
// global $sql;
|
||||||
@@ -245,13 +249,16 @@ class e107_db_debug
|
|||||||
else
|
else
|
||||||
{ // Don't run 'EXPLAIN' on other queries
|
{ // Don't run 'EXPLAIN' on other queries
|
||||||
$sQryRes = $origQryRes; // Return from original query could be TRUE or a link resource if success
|
$sQryRes = $origQryRes; // Return from original query could be TRUE or a link resource if success
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record Basic query info
|
// Record Basic query info
|
||||||
$sCallingFile = varset($aTrace[2]['file']);
|
$sCallingFile = varset($aTrace[2]['file']);
|
||||||
$sCallingLine = varset($aTrace[2]['line']);
|
$sCallingLine = varset($aTrace[2]['line']);
|
||||||
|
|
||||||
$t = &$this->aSQLdetails[$sql->db_QueryCount()];
|
$dbQryCount = $sql->db_QueryCount();
|
||||||
|
|
||||||
|
$t = &$this->aSQLdetails[$dbQryCount];
|
||||||
$t['marker'] = $this->curTimeMark;
|
$t['marker'] = $this->curTimeMark;
|
||||||
$t['caller'] = "$sCallingFile($sCallingLine)";
|
$t['caller'] = "$sCallingFile($sCallingLine)";
|
||||||
$t['query'] = $query;
|
$t['query'] = $query;
|
||||||
@@ -260,6 +267,7 @@ class e107_db_debug
|
|||||||
$t['nFields'] = $nFields;
|
$t['nFields'] = $nFields;
|
||||||
$t['time'] = $mytime;
|
$t['time'] = $mytime;
|
||||||
|
|
||||||
|
|
||||||
if($bExplained)
|
if($bExplained)
|
||||||
{
|
{
|
||||||
$bRowHeaders = false;
|
$bRowHeaders = false;
|
||||||
@@ -286,6 +294,8 @@ class e107_db_debug
|
|||||||
{
|
{
|
||||||
$this->aDBbyTable[$curtable]['DB Time'] += $mytime;
|
$this->aDBbyTable[$curtable]['DB Time'] += $mytime;
|
||||||
$this->aDBbyTable[$curtable]['DB Count']++;
|
$this->aDBbyTable[$curtable]['DB Count']++;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -294,6 +304,7 @@ class e107_db_debug
|
|||||||
$this->aDBbyTable[$curtable]['%DB Count'] = 0; // placeholder
|
$this->aDBbyTable[$curtable]['%DB Count'] = 0; // placeholder
|
||||||
$this->aDBbyTable[$curtable]['DB Time'] = $mytime;
|
$this->aDBbyTable[$curtable]['DB Time'] = $mytime;
|
||||||
$this->aDBbyTable[$curtable]['DB Count'] = 1;
|
$this->aDBbyTable[$curtable]['DB Count'] = 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -307,7 +318,7 @@ class e107_db_debug
|
|||||||
function Show_SQL_Details($force = false)
|
function Show_SQL_Details($force = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
global $sql;
|
$sql = e107::getDb();
|
||||||
//
|
//
|
||||||
// Show stats from aSQLdetails array
|
// Show stats from aSQLdetails array
|
||||||
//
|
//
|
||||||
@@ -331,7 +342,9 @@ class e107_db_debug
|
|||||||
$badCount = 0;
|
$badCount = 0;
|
||||||
$okCount = 0;
|
$okCount = 0;
|
||||||
|
|
||||||
foreach($this->aSQLdetails as $cQuery)
|
$SQLdetails = $this->getSQLDetails();
|
||||||
|
|
||||||
|
foreach($SQLdetails as $cQuery)
|
||||||
{
|
{
|
||||||
if($cQuery['ok'] == 1)
|
if($cQuery['ok'] == 1)
|
||||||
{
|
{
|
||||||
@@ -341,6 +354,7 @@ class e107_db_debug
|
|||||||
{
|
{
|
||||||
$badCount++;
|
$badCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($badCount)
|
if($badCount)
|
||||||
@@ -349,7 +363,7 @@ class e107_db_debug
|
|||||||
$text .= "<tr><th colspan='2'><b>$badCount Query Errors!</b></td></tr>\n";
|
$text .= "<tr><th colspan='2'><b>$badCount Query Errors!</b></td></tr>\n";
|
||||||
$text .= "<tr><th><b>Index</b></td><th><b>Query / Error</b></td></tr>\n";
|
$text .= "<tr><th><b>Index</b></td><th><b>Query / Error</b></td></tr>\n";
|
||||||
|
|
||||||
foreach($this->aSQLdetails as $idx => $cQuery)
|
foreach($SQLdetails as $idx => $cQuery)
|
||||||
{
|
{
|
||||||
if(!$cQuery['ok'])
|
if(!$cQuery['ok'])
|
||||||
{
|
{
|
||||||
@@ -364,7 +378,7 @@ class e107_db_debug
|
|||||||
// Optionally list good queries
|
// Optionally list good queries
|
||||||
//
|
//
|
||||||
|
|
||||||
if($okCount && E107_DBG_SQLDETAILS)
|
if($okCount && (E107_DBG_SQLDETAILS || $force))
|
||||||
{
|
{
|
||||||
$text .= "\n<table class='table table-striped table-bordered'>\n";
|
$text .= "\n<table class='table table-striped table-bordered'>\n";
|
||||||
$text .= "<tr><th colspan='3'><b>" . $this->countLabel($okCount) . " Good Queries</b></td></tr>\n";
|
$text .= "<tr><th colspan='3'><b>" . $this->countLabel($okCount) . " Good Queries</b></td></tr>\n";
|
||||||
@@ -373,7 +387,7 @@ class e107_db_debug
|
|||||||
";
|
";
|
||||||
|
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach($this->aSQLdetails as $idx => $cQuery)
|
foreach($SQLdetails as $idx => $cQuery)
|
||||||
{
|
{
|
||||||
if($count > 500)
|
if($count > 500)
|
||||||
{
|
{
|
||||||
@@ -396,14 +410,13 @@ class e107_db_debug
|
|||||||
$text .= "\n</table><br />\n";
|
$text .= "\n</table><br />\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Optionally list query details
|
// Optionally list query details
|
||||||
//
|
//
|
||||||
if(E107_DBG_SQLDETAILS)
|
if(E107_DBG_SQLDETAILS || $force)
|
||||||
{
|
{
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach($this->aSQLdetails as $idx => $cQuery)
|
foreach($SQLdetails as $idx => $cQuery)
|
||||||
{
|
{
|
||||||
$text .= "\n<table class='table table-striped table-bordered' style='width: 100%;'>\n";
|
$text .= "\n<table class='table table-striped table-bordered' style='width: 100%;'>\n";
|
||||||
$text .= "<tr><td colspan='" . $cQuery['nFields'] . "'><b>" . $idx . ") Query:</b> [" . $cQuery['marker'] . " - " . $cQuery['caller'] . "]<br />" . $cQuery['query'] . "</td></tr>\n";
|
$text .= "<tr><td colspan='" . $cQuery['nFields'] . "'><b>" . $idx . ") Query:</b> [" . $cQuery['marker'] . " - " . $cQuery['caller'] . "]<br />" . $cQuery['query'] . "</td></tr>\n";
|
||||||
@@ -434,12 +447,24 @@ class e107_db_debug
|
|||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getSQLDetails()
|
||||||
|
{
|
||||||
|
return $this->aSQLdetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSQLDetails($aSQLdetails)
|
||||||
|
{
|
||||||
|
$this->aSQLdetails = $aSQLdetails ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $amount
|
* @param $amount
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function countLabel($amount)
|
function countLabel($amount)
|
||||||
{
|
{
|
||||||
|
|
||||||
$inc = '';
|
$inc = '';
|
||||||
|
|
||||||
if($amount < 30)
|
if($amount < 30)
|
||||||
@@ -1111,7 +1136,7 @@ class e107_db_debug
|
|||||||
* $db_debug->log("message");
|
* $db_debug->log("message");
|
||||||
* @param string|array $message
|
* @param string|array $message
|
||||||
* @param int $TraceLev
|
* @param int $TraceLev
|
||||||
* @return bool true on success , false on error
|
* @return bool|null true on success , false on error
|
||||||
*/
|
*/
|
||||||
public function log($message, $TraceLev = 1)
|
public function log($message, $TraceLev = 1)
|
||||||
{
|
{
|
||||||
|
@@ -70,7 +70,7 @@ class e_db_pdo implements e_db
|
|||||||
|
|
||||||
private $debugMode = false;
|
private $debugMode = false;
|
||||||
|
|
||||||
private $queryCount = 0;
|
protected static $querycount = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -329,7 +329,7 @@ class e_db_pdo implements e_db
|
|||||||
public function db_Query($query, $rli = NULL, $qry_from = '', $debug = false, $log_type = '', $log_remark = '')
|
public function db_Query($query, $rli = NULL, $qry_from = '', $debug = false, $log_type = '', $log_remark = '')
|
||||||
{
|
{
|
||||||
global $db_time, $queryinfo;
|
global $db_time, $queryinfo;
|
||||||
$this->queryCount++;
|
self::$querycount++;
|
||||||
|
|
||||||
$this->_getMySQLaccess();
|
$this->_getMySQLaccess();
|
||||||
$this->mySQLlastQuery = $query;
|
$this->mySQLlastQuery = $query;
|
||||||
@@ -601,6 +601,7 @@ class e_db_pdo implements e_db
|
|||||||
case 'single': // single field value returned.
|
case 'single': // single field value returned.
|
||||||
if($select && !$this->select($table, $fields, $where, $noWhere, $debug))
|
if($select && !$this->select($table, $fields, $where, $noWhere, $debug))
|
||||||
{
|
{
|
||||||
|
$this->mySQLcurTable = $table;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
elseif(!$select && !$this->gen($table, $debug))
|
elseif(!$select && !$this->gen($table, $debug))
|
||||||
@@ -1840,7 +1841,7 @@ class e_db_pdo implements e_db
|
|||||||
*/
|
*/
|
||||||
public function queryCount()
|
public function queryCount()
|
||||||
{
|
{
|
||||||
return $this->queryCount;
|
return self::$querycount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -23,10 +23,10 @@
|
|||||||
}
|
}
|
||||||
catch(Exception $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
$this->assertTrue(false, "Couldn't load e107_db_debug object");
|
$this::fail("Couldn't load e107_db_debug object");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->dbg->active(true);
|
$this->dbg->active(false);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
public function testShowIf()
|
public function testShowIf()
|
||||||
@@ -37,13 +37,13 @@
|
|||||||
public function testShow_Log()
|
public function testShow_Log()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_Log();
|
$result = $this->dbg->Show_Log();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShow_Includes()
|
public function testShow_Includes()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_Includes();
|
$result = $this->dbg->Show_Includes();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSave()
|
public function testSave()
|
||||||
@@ -59,6 +59,7 @@
|
|||||||
|
|
||||||
public function testLog()
|
public function testLog()
|
||||||
{
|
{
|
||||||
|
$this->dbg->active(true);
|
||||||
$res = $this->dbg->log('hello world');
|
$res = $this->dbg->log('hello world');
|
||||||
$this->assertTrue($res, 'db_debug->log() method returned false.');
|
$this->assertTrue($res, 'db_debug->log() method returned false.');
|
||||||
|
|
||||||
@@ -71,26 +72,62 @@
|
|||||||
public function testShow_Performance()
|
public function testShow_Performance()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_Performance();
|
$result = $this->dbg->Show_Performance();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShow_PATH()
|
public function testShow_PATH()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_PATH();
|
$result = $this->dbg->Show_PATH();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShow_SQL_Details()
|
public function testShow_SQL_Details()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_SQL_Details();
|
$result = $this->dbg->Show_SQL_Details();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
|
|
||||||
|
$this->dbg->active(true);
|
||||||
|
$this->dbg->setSQLDetails(null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* public function testGetSqlDetails()
|
||||||
|
{
|
||||||
|
$this->dbg->setSQLDetails(null);
|
||||||
|
$result = $this->dbg->getSQLDetails();
|
||||||
|
$this::assertEmpty($result);
|
||||||
|
|
||||||
|
$this->dbg->active(true);
|
||||||
|
$this->dbg->setSQLDetails(null);
|
||||||
|
|
||||||
|
e107::getDb()->retrieve('SELECT * FROM #user');
|
||||||
|
e107::getDb()->retrieve("SELECT DISTINCT dblog_eventcode,dblog_title FROM #admin_log",true);
|
||||||
|
|
||||||
|
$result = $this->dbg->getSQLDetails();
|
||||||
|
$this::assertNotEmpty($result);
|
||||||
|
$result = array_values($result);
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
0 => 'SELECT * FROM e107_user ',
|
||||||
|
1 => 'SELECT DISTINCT dblog_eventcode,dblog_title FROM e107_admin_log ',
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
foreach($expected as $i => $expected_query)
|
||||||
|
{
|
||||||
|
|
||||||
|
$this::assertSame($expected_query, $result[$i]['query']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
public function testShow_SC_BB()
|
public function testShow_SC_BB()
|
||||||
{
|
{
|
||||||
$result = $this->dbg->Show_SC_BB();
|
$result = $this->dbg->Show_SC_BB();
|
||||||
$this->assertEmpty($result);
|
$this::assertEmpty($result);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
public function testLogCode()
|
public function testLogCode()
|
||||||
|
Reference in New Issue
Block a user