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 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 diff --git a/src/Builder/ExpressionBuilder.php b/src/Builder/ExpressionBuilder.php new file mode 100644 index 000000000..adddac1b5 --- /dev/null +++ b/src/Builder/ExpressionBuilder.php @@ -0,0 +1,16 @@ + ArrayExpressionBuilder::class, JsonExpression::class => JsonExpressionBuilder::class, StructuredExpression::class => StructuredExpressionBuilder::class, + Expression::class => ExpressionBuilder::class, ]); } } diff --git a/src/SqlParser.php b/src/SqlParser.php new file mode 100644 index 000000000..d56877d7f --- /dev/null +++ b/src/SqlParser.php @@ -0,0 +1,87 @@ +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("'") + : $this->skipIdentifier(), + '$' => $this->skipQuotedWithDollar(), + '-' => $this->sql[$this->position] === '-' + ? ++$this->position && $this->skipToAfterChar("\n") + : 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, + }; + + 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 . '$'); + } + + /** + * 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/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/Provider/SqlParserProvider.php b/tests/Provider/SqlParserProvider.php new file mode 100644 index 000000000..d100362c6 --- /dev/null +++ b/tests/Provider/SqlParserProvider.php @@ -0,0 +1,50 @@ +