minor change

This commit is contained in:
alejandro.campos@artica.es 2023-06-28 20:53:29 +02:00
parent f061c16388
commit 029425256d
1 changed files with 23 additions and 22 deletions

View File

@ -7568,7 +7568,7 @@ function reporting_sql($report, $content, $pdf=false)
if (is_metaconsole() === true && $content['server_name'] === 'all') { if (is_metaconsole() === true && $content['server_name'] === 'all') {
$sync = new Synchronizer(); $sync = new Synchronizer();
$results = $sync->apply( $results = $sync->apply(
function ($node) use ($report, $content) { function ($node) use ($report, $content, $pdf) {
try { try {
$node->connect(); $node->connect();
$rs = reporting_sql_auxiliary($report, $content, $pdf); $rs = reporting_sql_auxiliary($report, $content, $pdf);
@ -7663,38 +7663,38 @@ function reporting_sql_auxiliary($report, $content, $pdf=false)
} }
if ($pdf === true && isset($config['limit_sql_pdf']) === true && $config['limit_sql_pdf'] > 0) { if ($pdf === true && isset($config['limit_sql_pdf']) === true && $config['limit_sql_pdf'] > 0) {
$pattern = '/\sLIMIT\s+(\d+)(\s*,\s*(\d+))?\s*(;+|(\\G)+)?$/i'; $pattern_limit_offset = '/LIMIT\s+(\d+)(?:\s*,\s*(\d+))?/i';
if (preg_match($pattern, $sql, $matches)) {
// Item query contains a LIMIT clause.
$limit_size = $limit1 = (int) $matches[1];
if (isset($matches[3]) === true && $matches[3] !== '') { if (preg_match($pattern_limit_offset, $sql, $matches_limit_offset)) {
// The LIMIT clause is a range LIMIT. // Item query contains a LIMIT clause.
$limit2 = (int) $matches[3]; $limit1 = (int) $matches_limit_offset[1];
$range_size = abs($limit2 - $limit1) + 1;
if ($range_size > $config['limit_sql_pdf']) { if (isset($matches_limit_offset[2]) === true && $matches_limit_offset[2] !== '') {
// Set new LIMIT only if it is more restrictive than the LIMIT size specified in the item query. // The LIMIT clause has a second limit value in the form of LIMIT X, Y.
$new_limit2 = ($limit1 + $config['limit_sql_pdf']); $limit2 = (int) $matches_limit_offset[2];
$new_limit = "$limit1, $new_limit2";
// Replace item query limit by new calculated limit. if ($config['limit_sql_pdf'] < $limit2) {
$sql = preg_replace($pattern, " LIMIT $new_limit", $sql); // Overwrite the second limit value only if $config['limit_sql_pdf'] is less than the original limit.
$new_limit2 = $config['limit_sql_pdf'];
$sql = preg_replace($pattern_limit_offset, " LIMIT $limit1, $new_limit2", $sql);
} }
} else { } else {
// The LIMIT clause is a simple LIMIT. // The LIMIT clause is a simple LIMIT in the form of LIMIT X.
if ($limit_size > $config['limit_sql_pdf']) { if ($config['limit_sql_pdf'] < $limit1) {
// Set new LIMIT only if it is more restrictive than the LIMIT specified in the item query. // Overwrite the limit value only if $config['limit_sql_pdf'] is less than the original limit.
$new_limit = $config['limit_sql_pdf']; $new_limit1 = $config['limit_sql_pdf'];
$sql = preg_replace($pattern, " LIMIT $new_limit", $sql); $sql = preg_replace($pattern_limit_offset, " LIMIT $new_limit1", $sql);
} }
} }
} else { } else {
$limit_str = ' LIMIT '.$config['limit_sql_pdf']; $limit_str = ' LIMIT '.$config['limit_sql_pdf'];
// Check if SQL ends with semicolon or "\G". // Check if SQL ends with semicolon or "\G".
if (substr(trim($sql), -1) === ';') { if (substr(trim($sql), -1) === ';') {
$sql = str_replace(';', '', trim($sql)); $sql = rtrim($sql, ';');
$sql .= $limit_str.';'; $sql .= $limit_str.';';
} else if (substr(trim($sql), -2) === '\\G') { } else if (substr(trim($sql), -2) === '\\G') {
$sql = str_replace('\G', '', trim($sql)); $sql = rtrim($sql, '\G');
$sql .= $limit_str.'\G'; $sql .= $limit_str.'\G';
} else { } else {
$sql .= $limit_str; $sql .= $limit_str;
@ -7732,6 +7732,7 @@ function reporting_sql_auxiliary($report, $content, $pdf=false)
} }
$result = db_get_all_rows_sql($sql, $historical_db); $result = db_get_all_rows_sql($sql, $historical_db);
if ($result !== false) { if ($result !== false) {
foreach ($result as $row) { foreach ($result as $row) {
$data_row = []; $data_row = [];