From 98630a78c996905aa961ae4bd7a3ffffd88a0838 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 6 Feb 2024 17:25:12 +0700 Subject: [PATCH 01/10] Update test according to main PR --- tests/CommandTest.php | 5 +++-- tests/QueryBuilderTest.php | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d924a5503..c59a5b305 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -282,9 +282,10 @@ public function testUpdate( array $columns, array|string $conditions, array $params, - string $expected + array $expectedValues, + int $expectedCount, ): void { - parent::testUpdate($table, $columns, $conditions, $params, $expected); + parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount); } /** diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index fa87cdb02..1cfbaaf41 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -645,10 +645,11 @@ public function testUpdate( string $table, array $columns, array|string $condition, - string $expectedSQL, - array $expectedParams + array $params, + string $expectedSql, + array $expectedParams, ): void { - parent::testUpdate($table, $columns, $condition, $expectedSQL, $expectedParams); + parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams); } /** From 1607c22055933b8ffda2f091c64cfddd193eadf1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 6 Feb 2024 11:01:01 +0000 Subject: [PATCH 02/10] Apply Rector changes (CI) --- tests/CommandTest.php | 2 +- tests/QueryBuilderTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index c59a5b305..acfff86aa 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -283,7 +283,7 @@ public function testUpdate( array|string $conditions, array $params, array $expectedValues, - int $expectedCount, + int $expectedCount = null, ): void { parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount); } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 1cfbaaf41..441cd1c8a 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -647,7 +647,7 @@ public function testUpdate( array|string $condition, array $params, string $expectedSql, - array $expectedParams, + array $expectedParams = null, ): void { parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams); } From 00b11ebeb7cbd9bb483e24781f804de887779bda Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 30 Mar 2024 08:59:52 +0700 Subject: [PATCH 03/10] Adapt SqlParser to the driver --- src/Builder/ExpressionBuilder.php | 16 +++++++ src/DQLQueryBuilder.php | 3 ++ src/SqlParser.php | 64 ++++++++++++++++++++++++++++ tests/Provider/SqlParserProvider.php | 40 +++++++++++++++++ tests/SqlParserTest.php | 25 +++++++++++ 5 files changed, 148 insertions(+) create mode 100644 src/Builder/ExpressionBuilder.php create mode 100644 src/SqlParser.php create mode 100644 tests/Provider/SqlParserProvider.php create mode 100644 tests/SqlParserTest.php diff --git a/src/Builder/ExpressionBuilder.php b/src/Builder/ExpressionBuilder.php new file mode 100644 index 000000000..8f1f24644 --- /dev/null +++ b/src/Builder/ExpressionBuilder.php @@ -0,0 +1,16 @@ + ArrayExpressionBuilder::class, JsonExpression::class => JsonExpressionBuilder::class, CompositeExpression::class => CompositeExpressionBuilder::class, + Expression::class => ExpressionBuilder::class, ]); } } diff --git a/src/SqlParser.php b/src/SqlParser.php new file mode 100644 index 000000000..d9c73582c --- /dev/null +++ b/src/SqlParser.php @@ -0,0 +1,64 @@ +length - 1; + + while ($this->position < $length) { + $pos = $this->position++; + + match ($this->sql[$pos]) { + ':' => ($word = $this->parseWord()) === '' + ? $this->skipChars(':') + : $result = ':' . $word, + '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), + 'e', 'E' => $this->sql[$this->position] === "'" + ? ++$this->position && $this->skipQuotedWithEscape("'") + : null, + '$' => $this->skipQuotedWithDollar(), + '-' => $this->sql[$this->position] === '-' + ? ++$this->position && $this->skipToAfterChar("\n") + : null, + '/' => $this->sql[$this->position] === '*' + ? ++$this->position && $this->skipToAfterString('*/') + : null, + default => null, + }; + + if ($result !== null) { + $position = $pos; + + return $result; + } + } + + return null; + } + + /** + * Skips dollar-quoted string. + */ + private function skipQuotedWithDollar(): void + { + $pos = $this->position; + $identifier = $this->parseIdentifier(); + + if ($this->sql[$this->position] !== '$') { + $this->position = $pos; + return; + } + + ++$this->position; + + $this->skipToAfterString('$' . $identifier . '$'); + } +} diff --git a/tests/Provider/SqlParserProvider.php b/tests/Provider/SqlParserProvider.php new file mode 100644 index 000000000..1a1bcff8b --- /dev/null +++ b/tests/Provider/SqlParserProvider.php @@ -0,0 +1,40 @@ + Date: Sat, 30 Mar 2024 09:26:29 +0700 Subject: [PATCH 04/10] Revert "Apply Rector changes (CI)" This reverts commit 1607c22055933b8ffda2f091c64cfddd193eadf1. --- tests/CommandTest.php | 2 +- tests/QueryBuilderTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index acfff86aa..c59a5b305 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -283,7 +283,7 @@ public function testUpdate( array|string $conditions, array $params, array $expectedValues, - int $expectedCount = null, + int $expectedCount, ): void { parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount); } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 441cd1c8a..1cfbaaf41 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -647,7 +647,7 @@ public function testUpdate( array|string $condition, array $params, string $expectedSql, - array $expectedParams = null, + array $expectedParams, ): void { parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams); } From dba7667215ba08136e8fb47a74e25d5bfb13bff8 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 30 Mar 2024 09:31:46 +0700 Subject: [PATCH 05/10] Disable Rector for ./tests directory --- rector.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rector.php b/rector.php index f55daaed4..dc433c139 100644 --- a/rector.php +++ b/rector.php @@ -10,7 +10,10 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/src', - __DIR__ . '/tests', + /** + * Disabled ./tests directory due to different branches with main package when testing + */ + // __DIR__ . '/tests', ]); // register a single rule From b05a97636c34d40204efba42c2affbaa66ab7edb Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 30 Mar 2024 16:28:04 +0700 Subject: [PATCH 06/10] Add test and fix it --- src/SqlParser.php | 78 +++++++++++++++++++++++++++- tests/Provider/SqlParserProvider.php | 10 ++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/SqlParser.php b/src/SqlParser.php index d9c73582c..f4b22ba74 100644 --- a/src/SqlParser.php +++ b/src/SqlParser.php @@ -23,7 +23,7 @@ public function getNextPlaceholder(int|null &$position = null): string|null '"', "'" => $this->skipQuotedWithoutEscape($this->sql[$pos]), 'e', 'E' => $this->sql[$this->position] === "'" ? ++$this->position && $this->skipQuotedWithEscape("'") - : null, + : $this->skipIdentifier(), '$' => $this->skipQuotedWithDollar(), '-' => $this->sql[$this->position] === '-' ? ++$this->position && $this->skipToAfterChar("\n") @@ -31,6 +31,10 @@ public function getNextPlaceholder(int|null &$position = null): string|null '/' => $this->sql[$this->position] === '*' ? ++$this->position && $this->skipToAfterString('*/') : null, + // Identifiers can contain dollar sign which can be used for quoting. Skip them. + '_','a', 'b', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', + 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' => $this->skipIdentifier(), default => null, }; @@ -44,13 +48,64 @@ public function getNextPlaceholder(int|null &$position = null): string|null return null; } + /** + * Parses and returns identifier. Equals to `[_a-zA-Z][$\w]+` in regular expressions. + * + * @return string Parsed identifier. + */ + protected function parseIdentifier(): string + { + return match ($this->sql[$this->position]) { + '_', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', + 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', + 'V', 'W', 'X', 'Y', 'Z' => $this->sql[$this->position++] . $this->parseWordWithDollar(), + default => '', + }; + } + + /** + * Parses and returns identifier without dollar sign. Equals to `[_a-zA-Z]\w+` in regular expressions. + * + * @return string Parsed identifier. + */ + private function parseIdentifierWithoutDollar(): string + { + return parent::parseIdentifier(); + } + + /** + * Parses and returns word symbols include dollar sign. Equals to `[$\w]+` in regular expressions. + * + * @return string Parsed word symbols. + */ + private function parseWordWithDollar(): string + { + $word = ''; + $continue = true; + + while ($continue && $this->position < $this->length) { + match ($this->sql[$this->position]) { + '$', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', + 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', + 'V', 'W', 'X', 'Y', 'Z' => $word .= $this->sql[$this->position++], + default => $continue = false, + }; + } + + return $word; + } + /** * Skips dollar-quoted string. */ private function skipQuotedWithDollar(): void { $pos = $this->position; - $identifier = $this->parseIdentifier(); + $identifier = $this->parseIdentifierWithoutDollar(); if ($this->sql[$this->position] !== '$') { $this->position = $pos; @@ -61,4 +116,23 @@ private function skipQuotedWithDollar(): void $this->skipToAfterString('$' . $identifier . '$'); } + + /** + * Skips an identifier. Equals to `[$\w]+` in regular expressions. + */ + private function skipIdentifier(): void + { + $continue = true; + + while ($continue && $this->position < $this->length) { + match ($this->sql[$this->position]) { + '$', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', + 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', + 'V', 'W', 'X', 'Y', 'Z' => ++$this->position, + default => $continue = false, + }; + } + } } diff --git a/tests/Provider/SqlParserProvider.php b/tests/Provider/SqlParserProvider.php index 1a1bcff8b..79b62f693 100644 --- a/tests/Provider/SqlParserProvider.php +++ b/tests/Provider/SqlParserProvider.php @@ -35,6 +35,16 @@ public static function getNextPlaceholder(): array ':age', 29, ], + [ + 'name = $name AND age = :age', + ':age', + 24, + ], + [ + 'name = name$1$ AND age = :age', + ':age', + 25, + ], ]; } } From eb4283180ce44f6da09177c5e5e1789fb520f614 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 30 Mar 2024 16:47:07 +0700 Subject: [PATCH 07/10] Fix test --- tests/Provider/SqlParserProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Provider/SqlParserProvider.php b/tests/Provider/SqlParserProvider.php index 79b62f693..d100362c6 100644 --- a/tests/Provider/SqlParserProvider.php +++ b/tests/Provider/SqlParserProvider.php @@ -38,7 +38,7 @@ public static function getNextPlaceholder(): array [ 'name = $name AND age = :age', ':age', - 24, + 23, ], [ 'name = name$1$ AND age = :age', From 128dca40d936f4c0346d8b4dab8c3b58bbd0f177 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 30 Mar 2024 17:36:50 +0700 Subject: [PATCH 08/10] Remove unused methods --- src/SqlParser.php | 53 +---------------------------------------------- 1 file changed, 1 insertion(+), 52 deletions(-) diff --git a/src/SqlParser.php b/src/SqlParser.php index f4b22ba74..6bf60aa51 100644 --- a/src/SqlParser.php +++ b/src/SqlParser.php @@ -48,64 +48,13 @@ public function getNextPlaceholder(int|null &$position = null): string|null return null; } - /** - * Parses and returns identifier. Equals to `[_a-zA-Z][$\w]+` in regular expressions. - * - * @return string Parsed identifier. - */ - protected function parseIdentifier(): string - { - return match ($this->sql[$this->position]) { - '_', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', - 'v', 'w', 'x', 'y', 'z', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', - 'V', 'W', 'X', 'Y', 'Z' => $this->sql[$this->position++] . $this->parseWordWithDollar(), - default => '', - }; - } - - /** - * Parses and returns identifier without dollar sign. Equals to `[_a-zA-Z]\w+` in regular expressions. - * - * @return string Parsed identifier. - */ - private function parseIdentifierWithoutDollar(): string - { - return parent::parseIdentifier(); - } - - /** - * Parses and returns word symbols include dollar sign. Equals to `[$\w]+` in regular expressions. - * - * @return string Parsed word symbols. - */ - private function parseWordWithDollar(): string - { - $word = ''; - $continue = true; - - while ($continue && $this->position < $this->length) { - match ($this->sql[$this->position]) { - '$', '_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', - 'v', 'w', 'x', 'y', 'z', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', - 'V', 'W', 'X', 'Y', 'Z' => $word .= $this->sql[$this->position++], - default => $continue = false, - }; - } - - return $word; - } - /** * Skips dollar-quoted string. */ private function skipQuotedWithDollar(): void { $pos = $this->position; - $identifier = $this->parseIdentifierWithoutDollar(); + $identifier = $this->parseIdentifier(); if ($this->sql[$this->position] !== '$') { $this->position = $pos; From fd06ade08a0a38ed8ae75016f3e849aa6c594c3f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 1 Apr 2024 19:21:27 +0700 Subject: [PATCH 09/10] Update according main PR --- src/Builder/ExpressionBuilder.php | 4 ++-- src/SqlParser.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Builder/ExpressionBuilder.php b/src/Builder/ExpressionBuilder.php index 8f1f24644..adddac1b5 100644 --- a/src/Builder/ExpressionBuilder.php +++ b/src/Builder/ExpressionBuilder.php @@ -4,10 +4,10 @@ namespace Yiisoft\Db\Pgsql\Builder; -use Yiisoft\Db\Expression\ExpressionBuilder as BaseExpressionBuilder; +use Yiisoft\Db\Expression\AbstractExpressionBuilder; use Yiisoft\Db\Pgsql\SqlParser; -final class ExpressionBuilder extends BaseExpressionBuilder +final class ExpressionBuilder extends AbstractExpressionBuilder { protected function createSqlParser(string $sql): SqlParser { diff --git a/src/SqlParser.php b/src/SqlParser.php index 6bf60aa51..d56877d7f 100644 --- a/src/SqlParser.php +++ b/src/SqlParser.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Pgsql; -use Yiisoft\Db\Syntax\SqlParser as BaseSqlParser; +use Yiisoft\Db\Syntax\AbstractSqlParser; -final class SqlParser extends BaseSqlParser +final class SqlParser extends AbstractSqlParser { public function getNextPlaceholder(int|null &$position = null): string|null { From 689cccd17f58b5853aed9271b19dde8603e2fa73 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 10 Apr 2024 19:11:22 +0700 Subject: [PATCH 10/10] Add line to CHANGELOG.md [skip ci] --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ea1c5e64..dfa9f2dc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # PostgreSQL driver for Yii Database Change Log -## 1.3.1 under development +## 2.0.0 under development -- no changes in this release. +- Enh #336: Implement `SqlParser` and `ExpressionBuilder` driver classes (@Tigrov) ## 1.3.0 March 21, 2024