Skip to content

Commit

Permalink
Move _dbgQuery to Query\Utilities as debugLogQueryIntoSession
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 31, 2020
1 parent eecab19 commit 8e164bd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
41 changes: 7 additions & 34 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@
use function basename;
use function closelog;
use function count;
use function debug_backtrace;
use function defined;
use function explode;
use function htmlspecialchars;
use function implode;
use function intval;
use function is_array;
use function is_int;
use function is_string;
use function mb_strtolower;
use function md5;
use function microtime;
use function openlog;
use function reset;
Expand Down Expand Up @@ -187,36 +184,6 @@ public function getCache(): Cache
return $this->cache;
}

/**
* Stores query data into session data for debugging purposes
*
* @param string $query Query text
* @param mixed $link link type
* @param object|bool $result Query result
* @param int|float $time Time to execute query
*/
private function _dbgQuery(string $query, $link, $result, $time): void
{
$dbgInfo = [];
$error_message = $this->getError($link);
if ($result == false && is_string($error_message)) {
$dbgInfo['error']
= '<span class="color_red">'
. htmlspecialchars($error_message) . '</span>';
}
$dbgInfo['query'] = htmlspecialchars($query);
$dbgInfo['time'] = $time;
// Get and slightly format backtrace, this is used
// in the javascript console.
// Strip call to _dbgQuery
$dbgInfo['trace'] = Error::processBacktrace(
array_slice(debug_backtrace(), 1)
);
$dbgInfo['hash'] = md5($query);

$_SESSION['debug']['queries'][] = $dbgInfo;
}

/**
* runs a query and returns the result
*
Expand Down Expand Up @@ -251,7 +218,13 @@ public function tryQuery(

if ($debug) {
$time = microtime(true) - $time;
$this->_dbgQuery($query, $link, $result, $time);
$errorMessage = $this->getError($link);
Utilities::debugLogQueryIntoSession(
$query,
is_string($errorMessage) ? $errorMessage : null,
$result,
$time
);
if ($GLOBALS['cfg']['DBG']['sqllog']) {
$warningsCount = '';
if (($options & self::QUERY_STORE) == self::QUERY_STORE) {
Expand Down
37 changes: 37 additions & 0 deletions libraries/classes/Query/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

namespace PhpMyAdmin\Query;

use PhpMyAdmin\Error;
use PhpMyAdmin\Url;
use function array_slice;
use function debug_backtrace;
use function explode;
use function htmlspecialchars;
use function intval;
use function md5;
use function sprintf;
use function strcasecmp;
use function strnatcasecmp;
use function strpos;
Expand Down Expand Up @@ -163,4 +170,34 @@ public static function versionToInt(string $version): int

return (int) sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2]));
}

/**
* Stores query data into session data for debugging purposes
*
* @param string $query Query text
* @param string|null $errorMessage Error message from getError()
* @param object|bool $result Query result
* @param int|float $time Time to execute query
*/
public static function debugLogQueryIntoSession(string $query, ?string $errorMessage, $result, $time): void
{
$dbgInfo = [];

if ($result === false && $errorMessage !== null) {
$dbgInfo['error']
= '<span class="color_red">'
. htmlspecialchars($errorMessage) . '</span>';
}
$dbgInfo['query'] = htmlspecialchars($query);
$dbgInfo['time'] = $time;
// Get and slightly format backtrace, this is used
// in the javascript console.
// Strip call to debugLogQueryIntoSession
$dbgInfo['trace'] = Error::processBacktrace(
array_slice(debug_backtrace(), 1)
);
$dbgInfo['hash'] = md5($query);

$_SESSION['debug']['queries'][] = $dbgInfo;
}
}

0 comments on commit 8e164bd

Please sign in to comment.