From 346cdaad72aa9a71ad96b444139220c2508ce1f3 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 11 Dec 2024 11:23:19 +0900 Subject: [PATCH] Fix for params regex comment update --- src/DB/IO.php | 1 - src/DB/Support/ConvertPlaceholder.php | 17 +++++++++----- test/phpunit/DB/CoreLibsDBIOTest.php | 32 +++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/DB/IO.php b/src/DB/IO.php index d579fd1..4daad5b 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -1332,7 +1332,6 @@ class IO */ private function __dbCheckQueryParams(string $query, array $params): bool { - // $this->log->debug('DB QUERY PARAMS REGEX', ConvertPlaceholder::REGEX_LOOKUP_PLACEHOLDERS); $placeholder_count = $this->__dbCountQueryParams($query); $params_count = count($params); if ($params_count != $placeholder_count) { diff --git a/src/DB/Support/ConvertPlaceholder.php b/src/DB/Support/ConvertPlaceholder.php index fd8eea6..dff0378 100644 --- a/src/DB/Support/ConvertPlaceholder.php +++ b/src/DB/Support/ConvertPlaceholder.php @@ -18,17 +18,20 @@ class ConvertPlaceholder // NOTE some combinations are allowed, but the query will fail before this /** @var string split regex, entries before $ group */ private const PATTERN_QUERY_SPLIT = - ',|' // for ',' mostly in INSERT - . '[(<>=]|' // general set for (, <, >, = in any query with any combination - . '(?:[\(,]\s*\-\-[\s\w]*)\r?\n|' // a comment that starts after a ( or , + '\?\?|' // UNKNOWN: double ??, is this to avoid something? + . '[\(,]|' // for ',' and '(' mostly in INSERT or ANY() + . '[<>=]|' // general set for <, >, = in any query with any combination . '\^@|' // text search for start from text with ^@ . '\|\||' // concats two elements . '&&|' // array overlap - . '\-\|\-|' // range overlap + . '\-\|\-|' // range overlap for array . '[^-]-{1}|' // single -, used in JSON too . '->|->>|#>|#>>|@>|<@|@@|@\?|\?{1}|\?\||\?&|#-'; //JSON searches, Array searchs, etc /** @var string the main regex including the pattern query split */ - private const PATTERN_ELEMENT = '(?:\'.*?\')?\s*(?:\?\?|' . self::PATTERN_QUERY_SPLIT . ')\s*'; + private const PATTERN_ELEMENT = '(?:\'.*?\')?\s*(?:' . self::PATTERN_QUERY_SPLIT . ')\s*'; + /** @var string comment regex + * anything that starts with -- and ends with a line break but any character that is not line break inbetween */ + private const PATTERN_COMMENT = '(?:\-\-[^\r\n]*?\r?\n)*\s*'; /** @var string parts to ignore in the SQL */ private const PATTERN_IGNORE = // digit -> ignore @@ -45,6 +48,7 @@ class ConvertPlaceholder /** @var string replace regex for named (:...) entries */ public const REGEX_REPLACE_NAMED = '/' . '(' . self::PATTERN_ELEMENT . ')' + . self::PATTERN_COMMENT . '(' . self::PATTERN_IGNORE . self::PATTERN_NAMED @@ -53,6 +57,7 @@ class ConvertPlaceholder /** @var string replace regex for question mark (?) entries */ public const REGEX_REPLACE_QUESTION_MARK = '/' . '(' . self::PATTERN_ELEMENT . ')' + . self::PATTERN_COMMENT . '(' . self::PATTERN_IGNORE . self::PATTERN_QUESTION_MARK @@ -61,6 +66,7 @@ class ConvertPlaceholder /** @var string replace regex for numbered ($n) entries */ public const REGEX_REPLACE_NUMBERED = '/' . '(' . self::PATTERN_ELEMENT . ')' + . self::PATTERN_COMMENT . '(' . self::PATTERN_IGNORE . self::PATTERN_NUMBERED @@ -71,6 +77,7 @@ class ConvertPlaceholder // prefix string part, must match towards // seperator for ( = , ? - [and json/jsonb in pg doc section 9.15] . self::PATTERN_ELEMENT + . self::PATTERN_COMMENT // match for replace part . '(?:' // ignore parts diff --git a/test/phpunit/DB/CoreLibsDBIOTest.php b/test/phpunit/DB/CoreLibsDBIOTest.php index faadf6e..1476729 100644 --- a/test/phpunit/DB/CoreLibsDBIOTest.php +++ b/test/phpunit/DB/CoreLibsDBIOTest.php @@ -5141,9 +5141,9 @@ final class CoreLibsDBIOTest extends TestCase INSERT INTO table_with_primary_key ( row_int, row_numeric, row_varchar, row_varchar_literal ) VALUES ( - -- comment 1 + -- comment 1 かな $1, $2, - -- comment 2 + -- comment 2 - $3 -- comment 3 , $4 @@ -5152,6 +5152,23 @@ final class CoreLibsDBIOTest extends TestCase 'count' => 4, 'convert' => false ], + 'comment in update' => [ + 'query' => << 4, + 'convert' => false, + ], // Note some are not set 'a complete set of possible' => [ 'query' => << 12, 'convert' => false, + ], + // all the same + 'all the same numbered' => [ + 'query' => << 1, + 'convert' => false, ] ]; }