diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 566a5038..c6e196f6 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -255,7 +255,7 @@ protected function processExpression( $parameterContainer, ) : $platform->quoteValue((string) $argument->getValue()), - $argument instanceof Identifier => $platform->quoteIdentifierInFragment($argument->getValue()), + $argument instanceof Identifier => IdentifierQuoter::quote($argument->getValue(), $platform), $argument instanceof Literal => $argument->getValue(), $argument instanceof Identifiers => $this->processIdentifiersArgument($argument, $platform), $argument instanceof SelectArgument => $this->processExpressionOrSelect( @@ -338,7 +338,7 @@ protected function processIdentifiersArgument( $processedIdentifiers = []; foreach ($identifiers as $identifier) { - $processedIdentifiers[] = $platform->quoteIdentifierInFragment($identifier); + $processedIdentifiers[] = IdentifierQuoter::quote($identifier, $platform); } return implode(', ', $processedIdentifiers); diff --git a/test/unit/Sql/PredicateIdentifierNameTest.php b/test/unit/Sql/PredicateIdentifierNameTest.php new file mode 100644 index 00000000..4516bf7c --- /dev/null +++ b/test/unit/Sql/PredicateIdentifierNameTest.php @@ -0,0 +1,355 @@ + + */ + public static function brokenNameProvider(): array + { + return [ + 'hyphen' => ['x-y', '"x-y"'], + 'space' => ['my col', '"my col"'], + 'quote character' => ['a"b', '"a\'b"'], + 'non-ascii' => ['nâme', '"nâme"'], + 'tab' => ["a\tb", "\"a\tb\""], + 'dotted hyphen' => ['t-1.x-y', '"t-1"."x-y"'], + 'dotted space' => ['t.my col', '"t"."my col"'], + 'hyphen qualified star' => ['t-1.*', '"t-1".*'], + ]; + } + + /** + * @return iterable + */ + public static function brokenPredicateProvider(): iterable + { + foreach (self::predicateMethodProvider() as $method => [$apply, $template]) { + foreach (self::brokenNameProvider() as $case => [$name, $quoted]) { + yield "{$method} {$case}" => [$apply, $template, $name, $quoted]; + } + } + } + + /** + * @return array + */ + public static function hyphenWordPlatformProvider(): array + { + return [ + 'hyphen' => ['x-y', '`x-y`'], + 'hyphen arithmetic' => ['a-b * c', '`a-b` * `c`'], + 'dotted hyphen' => ['t-1.x-y', '`t-1`.`x-y`'], + 'space' => ['my col', '`my col`'], + 'quote character' => ['a`b', '`a``b`'], + ]; + } + + /** + * @return array + */ + public static function predicateMethodProvider(): array + { + return [ + 'equalTo' => [static fn(Predicate $p, string $n): Predicate => $p->equalTo($n, 1), "%s = '1'"], + 'notEqualTo' => [static fn(Predicate $p, string $n): Predicate => $p->notEqualTo($n, 1), "%s != '1'"], + 'lessThan' => [static fn(Predicate $p, string $n): Predicate => $p->lessThan($n, 1), "%s < '1'"], + 'greaterThan' => [static fn(Predicate $p, string $n): Predicate => $p->greaterThan($n, 1), "%s > '1'"], + 'like' => [static fn(Predicate $p, string $n): Predicate => $p->like($n, 'a%'), "%s LIKE 'a%%'"], + 'in' => [static fn(Predicate $p, string $n): Predicate => $p->in($n, [1, 2]), "%s IN ('1', '2')"], + 'between' => [ + static fn(Predicate $p, string $n): Predicate => $p->between($n, 1, 2), + "%s BETWEEN '1' AND '2'", + ], + 'isNull' => [static fn(Predicate $p, string $n): Predicate => $p->isNull($n), '%s IS NULL'], + ]; + } + + /** + * @return array + */ + public static function rejectedNameProvider(): array + { + return [ + 'function call' => [ + new TrustingSql92Platform(), + 'COUNT(id)', + InvalidArgumentException::forUnquotableIdentifier('COUNT(id)'), + ], + 'spaced minus' => [ + new TrustingSql92Platform(), + 'a - b', + InvalidArgumentException::forUnquotableIdentifier('a - b'), + ], + 'arithmetic with hyphen' => [ + new TrustingSql92Platform(), + 'x-y * 2', + InvalidArgumentException::forUnquotableIdentifier('x-y * 2'), + ], + 'plus on hyphen platform' => [ + new TrustingHyphenWordPlatform(), + 'a+b', + InvalidArgumentException::forUnquotableIdentifier('a+b'), + ], + 'empty' => [ + new TrustingSql92Platform(), + '', + InvalidArgumentException::forEmptyIdentifierPart(''), + ], + 'empty qualifier part' => [ + new TrustingSql92Platform(), + 'a..b', + InvalidArgumentException::forEmptyIdentifierPart('a..b'), + ], + ]; + } + + /** + * Names whose fragment quoting renders valid SQL, and that rendering + * + * @return array + */ + public static function unchangedNameProvider(): array + { + return [ + 'plain' => ['col', '"col"'], + 'dotted' => ['t.col', '"t"."col"'], + 'qualified star' => ['t.*', '"t".*'], + 'arithmetic' => ['price*qty', '"price"*"qty"'], + 'spaced arithmetic' => ['a * b', '"a" * "b"'], + 'dotted arithmetic' => ['t.a * t.b', '"t"."a" * "t"."b"'], + 'inline alias' => ['x as y', '"x" as "y"'], + 'inline alias upper' => ['x AS y', '"x" AS "y"'], + 'comma' => ['a,b', '"a,b"'], + ]; + } + + /** + * @return iterable + */ + public static function unchangedPredicateProvider(): iterable + { + foreach (self::predicateMethodProvider() as $method => [$apply, $template]) { + foreach (self::unchangedNameProvider() as $case => [$name, $quoted]) { + yield "{$method} {$case}" => [$apply, $template, $name, $quoted]; + } + } + } + + #[Test] + #[DataProvider('hyphenWordPlatformProvider')] + public function followsThePlatformFragmentPatternForWordCharacters(string $name, string $quoted): void + { + $select = new Select('foo'); + $select->where->equalTo($name, 1); + + self::assertSame( + "SELECT `foo`.* FROM `foo` WHERE {$quoted} = '1'", + $select->getSqlString(new TrustingHyphenWordPlatform()), + ); + } + + /** + * @param Closure(Predicate, string): Predicate $apply + */ + #[Test] + #[DataProvider('unchangedPredicateProvider')] + public function keepsValidWhereNameRenderingUnchanged( + Closure $apply, + string $template, + string $name, + string $quoted, + ): void { + $select = new Select('foo'); + $apply($select->where, $name); + + self::assertSame( + sprintf("SELECT \"foo\".* FROM \"foo\" WHERE {$template}", $quoted), + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + public function leavesExpressionWhereUnchanged(): void + { + $select = new Select('foo'); + $select->where(new Expression('x-y = ?', [1])); + + self::assertSame( + "SELECT \"foo\".* FROM \"foo\" WHERE x-y = '1'", + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + public function leavesLiteralWhereUnchanged(): void + { + $select = new Select('foo'); + $select->where(new Literal('x-y = 1')); + + self::assertSame( + 'SELECT "foo".* FROM "foo" WHERE x-y = 1', + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + public function leavesStringWhereUnchanged(): void + { + $select = new Select('foo'); + $select->where('x-y = 1'); + + self::assertSame( + 'SELECT "foo".* FROM "foo" WHERE x-y = 1', + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('brokenNameProvider')] + #[DataProvider('unchangedNameProvider')] + public function quotesDeleteWhereName(string $name, string $quoted): void + { + $delete = new Delete('foo'); + $delete->where->equalTo($name, 1); + + self::assertSame( + "DELETE FROM \"foo\" WHERE {$quoted} = '1'", + $delete->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + public function quotesEachNameOfAnIdentifiersArgument(): void + { + $select = new Select('foo'); + $select->where(new Expression('(?) IN (SELECT 1)', [Argument::identifiers(['x-y', 't.col', 'a * b'])])); + + self::assertSame( + 'SELECT "foo".* FROM "foo" WHERE ("x-y", "t"."col", "a" * "b") IN (SELECT 1)', + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('brokenNameProvider')] + #[DataProvider('unchangedNameProvider')] + public function quotesHavingName(string $name, string $quoted): void + { + $select = new Select('foo'); + $select->having->greaterThan($name, 1); + + self::assertSame( + "SELECT \"foo\".* FROM \"foo\" HAVING {$quoted} > '1'", + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + /** + * @param Closure(Predicate, string): Predicate $apply + */ + #[Test] + #[DataProvider('brokenPredicateProvider')] + public function quotesPreviouslyBrokenWhereNameAsOneIdentifier( + Closure $apply, + string $template, + string $name, + string $quoted, + ): void { + $select = new Select('foo'); + $apply($select->where, $name); + + self::assertSame( + sprintf("SELECT \"foo\".* FROM \"foo\" WHERE {$template}", $quoted), + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('brokenNameProvider')] + #[DataProvider('unchangedNameProvider')] + public function quotesUpdateWhereName(string $name, string $quoted): void + { + $update = new Update('foo'); + $update->set(['bar' => 2]); + $update->where->equalTo($name, 1); + + self::assertSame( + "UPDATE \"foo\" SET \"bar\" = '2' WHERE {$quoted} = '1'", + $update->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('brokenNameProvider')] + #[DataProvider('unchangedNameProvider')] + public function quotesWhereArrayKeyAsPredicateName(string $name, string $quoted): void + { + $select = new Select('foo'); + $select->where([$name => 1]); + + self::assertSame( + "SELECT \"foo\".* FROM \"foo\" WHERE {$quoted} = '1'", + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('brokenNameProvider')] + #[DataProvider('unchangedNameProvider')] + public function quotesWhereArrayKeyWithNullAsIsNullName(string $name, string $quoted): void + { + $select = new Select('foo'); + $select->where([$name => null]); + + self::assertSame( + "SELECT \"foo\".* FROM \"foo\" WHERE {$quoted} IS NULL", + $select->getSqlString(new TrustingSql92Platform()), + ); + } + + #[Test] + #[DataProvider('rejectedNameProvider')] + public function rejectsWhereNamesThatCannotBeQuotedAsWritten( + PlatformInterface $platform, + string $name, + InvalidArgumentException $expected, + ): void { + $select = new Select('foo'); + $select->where->equalTo($name, 1); + + $this->expectExceptionObject($expected); + + $select->getSqlString($platform); + } +}