From 9042a98ef618b740233e2d3c2f87870055c3ccd6 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 22 Mar 2024 14:45:16 +0000 Subject: [PATCH] Log Service : When sql syntax error occurs, log sql in error level. (#2443) * Factory : nameFilter protect against multiple `-` in search term. * LogService : When sql syntax error occurs, log sql in error level. --- lib/Factory/BaseFactory.php | 13 ++++++++++--- lib/Service/LogService.php | 11 +++++++---- lib/Service/LogServiceInterface.php | 10 +++++----- lib/Storage/PdoStorageService.php | 11 ++++++++--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/Factory/BaseFactory.php b/lib/Factory/BaseFactory.php index 082939a82a..42ba87340f 100644 --- a/lib/Factory/BaseFactory.php +++ b/lib/Factory/BaseFactory.php @@ -362,8 +362,15 @@ protected function parseComparisonOperator($variable) * @param array $params Array of parameters passed by reference * @param bool $useRegex flag to match against a regex pattern */ - public function nameFilter($tableName, $tableColumn, $terms, &$body, &$params, $useRegex = false, $logicalOperator = 'OR') - { + public function nameFilter( + $tableName, + $tableColumn, + $terms, + &$body, + &$params, + $useRegex = false, + $logicalOperator = 'OR' + ) { $i = 0; $tableAndColumn = $tableName . '.' . $tableColumn; @@ -377,7 +384,7 @@ public function nameFilter($tableName, $tableColumn, $terms, &$body, &$params, $ $searchName = trim($searchName); // Discard any incompatible - if ($searchName === '-' || empty($searchName)) { + if (empty(ltrim($searchName, '-')) || empty($searchName)) { continue; } diff --git a/lib/Service/LogService.php b/lib/Service/LogService.php index fa35672a8d..eda228990b 100644 --- a/lib/Service/LogService.php +++ b/lib/Service/LogService.php @@ -1,6 +1,6 @@ mode) == 'test') { + if (strtolower($this->mode) == 'test' || $logAsError) { $paramSql = ''; foreach ($params as $key => $param) { $paramSql .= 'SET @' . $key . '=\'' . $param . '\';' . PHP_EOL; } - $this->log->debug($paramSql . str_replace(':', '@', $sql)); + + ($logAsError) + ? $this->log->error($paramSql . str_replace(':', '@', $sql)) + : $this->log->debug($paramSql . str_replace(':', '@', $sql)); } } diff --git a/lib/Service/LogServiceInterface.php b/lib/Service/LogServiceInterface.php index 6ffa11ced4..57674625bf 100644 --- a/lib/Service/LogServiceInterface.php +++ b/lib/Service/LogServiceInterface.php @@ -1,8 +1,8 @@ errorInfo[1] ?? $PDOException->getCode(); + + // syntax error, log the sql and params in error level. + if ($errorCode == 1064 && $this->log != null) { + $this->log->sql($sql, $params, true); + } + // Throw if we're not expected to reconnect. if (!$reconnect) { throw $PDOException; } - $errorCode = $PDOException->errorInfo[1] ?? $PDOException->getCode(); - if ($errorCode != 2006) { throw $PDOException; } else {