improve sql debug

This commit is contained in:
xiaomlove
2025-09-21 17:00:32 +07:00
parent 3dca0e5774
commit 1b2ba8dc48
3 changed files with 23 additions and 24 deletions
+2 -2
View File
@@ -3,6 +3,8 @@ ini_set('error_reporting', E_ALL);
ini_set('display_errors', 0); ini_set('display_errors', 0);
require_once __DIR__ . '/constants.php'; require_once __DIR__ . '/constants.php';
require_once $rootpath . 'vendor/autoload.php'; require_once $rootpath . 'vendor/autoload.php';
$USERUPDATESET = array();
$query_name=array();
\Nexus\Nexus::boot(); \Nexus\Nexus::boot();
if (!file_exists($rootpath . '.env')) { if (!file_exists($rootpath . '.env')) {
$installScriptRelativePath = 'install/install.php'; $installScriptRelativePath = 'install/install.php';
@@ -27,8 +29,6 @@ if (!isRunningInConsole() && !in_array($script, ['announce', 'scrape', 'torrentr
} }
define('TIMENOW', time()); define('TIMENOW', time());
$USERUPDATESET = array();
$query_name=array();
define ("UC_PEASANT", 0); define ("UC_PEASANT", 0);
define ("UC_USER", 1); define ("UC_USER", 1);
+15 -5
View File
@@ -2008,8 +2008,8 @@ function userlogin() {
$nip = ip2long($ip); $nip = ip2long($ip);
if ($nip) //$nip would be false for IPv6 address if ($nip) //$nip would be false for IPv6 address
{ {
$res = sql_query("SELECT * FROM bans WHERE $nip >= first AND $nip <= last") or sqlerr(__FILE__, __LINE__); $res = sql_query("SELECT * FROM bans WHERE first <= $nip AND last >= $nip") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0) if (mysql_num_rows($res) > 0)
{ {
header("HTTP/1.1 403 Forbidden"); header("HTTP/1.1 403 Forbidden");
print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>".$lang_functions['text_unauthorized_ip']."</body></html>\n"); print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>".$lang_functions['text_unauthorized_ip']."</body></html>\n");
@@ -2042,7 +2042,6 @@ function userlogin() {
// error_reporting(E_ALL & ~E_NOTICE); // error_reporting(E_ALL & ~E_NOTICE);
// error_reporting(-1); // error_reporting(-1);
// } // }
return $loginResult = true; return $loginResult = true;
} }
@@ -2932,13 +2931,24 @@ function stdfoot() {
$yearfounded = ($year ? $year : 2007); $yearfounded = ($year ? $year : 2007);
print(" (c) "." <a href=\"" . get_protocol_prefix() . $BASEURL."\" target=\"_self\">".$SITENAME."</a> ".($icplicense_main ? " ".$icplicense_main." " : "").(date("Y") != $yearfounded ? $yearfounded."-" : "").date("Y")." ".VERSION."<br /><br />"); print(" (c) "." <a href=\"" . get_protocol_prefix() . $BASEURL."\" target=\"_self\">".$SITENAME."</a> ".($icplicense_main ? " ".$icplicense_main." " : "").(date("Y") != $yearfounded ? $yearfounded."-" : "").date("Y")." ".VERSION."<br /><br />");
printf ("[page created in <b> %s </b> sec", sprintf("%.3f", $totaltime)); printf ("[page created in <b> %s </b> sec", sprintf("%.3f", $totaltime));
print (" with <b>".count($query_name)."</b> db queries, <b>".$Cache->getCacheReadTimes()."</b> reads and <b>".$Cache->getCacheWriteTimes()."</b> writes of Redis and <b>".mksize(memory_get_usage())."</b> ram]"); $debugQuery = $enablesqldebug_tweak == 'yes' && get_user_class() >= $sqldebug_tweak;
if ($debugQuery) {
$query_name_laravel = last_query(true);
$dbQueryCount = count($query_name) + count($query_name_laravel);
} else {
$query_name_laravel = [];
$dbQueryCount = count($query_name) + last_query('COUNT');
}
print (" with <b>".$dbQueryCount."</b> db queries, <b>".$Cache->getCacheReadTimes()."</b> reads and <b>".$Cache->getCacheWriteTimes()."</b> writes of Redis and <b>".mksize(memory_get_usage())."</b> ram]");
print ("</div>\n"); print ("</div>\n");
if ($enablesqldebug_tweak == 'yes' && get_user_class() >= $sqldebug_tweak) { if ($debugQuery) {
print("<div id=\"sql_debug\" style='text-align: left;'>SQL query list: <ul>"); print("<div id=\"sql_debug\" style='text-align: left;'>SQL query list: <ul>");
foreach($query_name as $query) { foreach($query_name as $query) {
print(sprintf('<li>%s [%s]</li>', htmlspecialchars($query['query']), $query['time'])); print(sprintf('<li>%s [%s]</li>', htmlspecialchars($query['query']), $query['time']));
} }
foreach($query_name_laravel as $query) {
print(sprintf('<li>%s [%s ms]</li>', htmlspecialchars($query['raw_query']), $query['time']));
}
print("</ul>"); print("</ul>");
print("Redis key read: <ul>"); print("Redis key read: <ul>");
foreach($Cache->getKeyHits('read') as $keyName => $hits) { foreach($Cache->getKeyHits('read') as $keyName => $hits) {
+6 -17
View File
@@ -82,7 +82,7 @@ function sql_query($query)
$end = microtime(true); $end = microtime(true);
$query_name[] = [ $query_name[] = [
'query' => $query, 'query' => $query,
'time' => sprintf('%.3f ms', ($end - $begin) * 1000), 'time' => sprintf('%.2f ms', ($end - $begin) * 1000),
]; ];
return $result; return $result;
} }
@@ -626,26 +626,15 @@ function last_query($all = false)
} else { } else {
$connection = \Illuminate\Support\Facades\DB::connection(config('database.default')); $connection = \Illuminate\Support\Facades\DB::connection(config('database.default'));
} }
$pdo = $connection->getPdo();
} }
$queries = $connection->getQueryLog(); if ($all === 'COUNT') {
if (!$all) { return count($connection->getQueryLog());
$queries = [last($queries)];
}
$queryFormatted = [];
foreach ($queries as $query) {
$sqlWithPlaceholders = str_replace(['%', '?'], ['%%', '%s'], $query['query']);
$bindings = $query['bindings'];
$realSql = $sqlWithPlaceholders;
if (count($bindings) > 0) {
$realSql = vsprintf($sqlWithPlaceholders, array_map([$pdo, 'quote'], $bindings));
}
$queryFormatted[] = $realSql;
} }
$queries = $connection->getRawQueryLog();
if ($all) { if ($all) {
return nexus_json_encode($queryFormatted); return $queries;
} }
return $queryFormatted[0]; return isset($queries[0]) ? last($queries) : '';
} }
function format_datetime($datetime, $format = 'Y-m-d H:i') function format_datetime($datetime, $format = 'Y-m-d H:i')