diff --git a/src/Sql/Select.php b/src/Sql/Select.php index ec480998..724458ed 100644 --- a/src/Sql/Select.php +++ b/src/Sql/Select.php @@ -608,7 +608,7 @@ protected function processOrder( } $orders[] = [ - $platform->quoteIdentifierInFragment($k), + IdentifierQuoter::quote($k, $platform), strcasecmp(trim($v), self::ORDER_DESCENDING) === 0 ? self::ORDER_DESCENDING : self::ORDER_ASCENDING, diff --git a/test/unit/Sql/SelectOrderNameTest.php b/test/unit/Sql/SelectOrderNameTest.php new file mode 100644 index 00000000..1a9200ea --- /dev/null +++ b/test/unit/Sql/SelectOrderNameTest.php @@ -0,0 +1,169 @@ +|string, string}> + */ + public static function brokenOrderProvider(): array + { + return [ + 'hyphen' => ['x-y', '"x-y" ASC'], + 'hyphen descending' => ['x-y DESC', '"x-y" DESC'], + 'dotted hyphen' => ['t-1.x-y DESC', '"t-1"."x-y" DESC'], + 'quote character' => ['a"b DESC', '"a\'b" DESC'], + 'non-ascii' => ['café DESC', '"café" DESC'], + 'hyphen in comma list' => ['x-y DESC, b', '"x-y" DESC, "b" ASC'], + 'hyphen array key' => [['x-y' => 'DESC'], '"x-y" DESC'], + 'space array key' => [['my col' => 'DESC'], '"my col" DESC'], + 'dotted space array key' => [['t.my col' => 'ASC'], '"t"."my col" ASC'], + 'quote character array key' => [['a"b' => 'DESC'], '"a\'b" DESC'], + 'non-ascii array key' => [['café' => 'DESC'], '"café" DESC'], + 'hyphen array list' => [['x-y DESC', 'b'], '"x-y" DESC, "b" ASC'], + 'hyphen qualified star' => ['x-y.* DESC', '"x-y".* DESC'], + 'as after qualifier' => [['t.as' => 'DESC'], '"t"."as" DESC'], + ]; + } + + /** + * @return array + */ + public static function platformOrderProvider(): array + { + return [ + 'hyphen word character' => [new TrustingHyphenWordPlatform(), 'x-y DESC', '`x-y` DESC'], + 'hyphen word character arithmetic' => [ + new TrustingHyphenWordPlatform(), + 'price*x-y DESC', + '`price`*`x-y` DESC', + ], + 'identifier quoting off' => [ + new TestSql92Platform(quoteIdentifiers: false), + 'x-y DESC', + 'x-y DESC', + ], + 'identifier quoting off arithmetic' => [ + new TestSql92Platform(quoteIdentifiers: false), + 'price*qty DESC', + 'price*qty DESC', + ], + ]; + } + + /** + * @return array|string, string}> + */ + public static function unquotableOrderProvider(): array + { + return [ + 'arithmetic with hyphen' => [new TrustingSql92Platform(), 'price*x-y DESC', 'price*x-y'], + 'function call' => [new TrustingSql92Platform(), ['COUNT(id)' => 'DESC'], 'COUNT(id)'], + 'plus on hyphen platform' => [new TrustingHyphenWordPlatform(), 'x+y DESC', 'x+y'], + ]; + } + + /** + * @return array|string, string}> + */ + public static function validOrderProvider(): array + { + return [ + 'plain' => ['a', '"a" ASC'], + 'plain descending' => ['a DESC', '"a" DESC'], + 'lower case descending' => ['a desc', '"a" DESC'], + 'dotted' => ['t.col DESC', '"t"."col" DESC'], + 'comma list' => ['a DESC, b', '"a" DESC, "b" ASC'], + 'arithmetic' => ['price*qty DESC', '"price"*"qty" DESC'], + 'string with space' => ['my col DESC', '"my" ASC'], + 'array key' => [['a' => 'DESC'], '"a" DESC'], + 'array key unknown direction' => [['a' => 'sideways'], '"a" ASC'], + 'spaced arithmetic key' => [['price * qty' => 'DESC'], '"price" * "qty" DESC'], + 'dotted arithmetic key' => [['t.price*t.qty' => 'DESC'], '"t"."price"*"t"."qty" DESC'], + 'trailing space key' => [['a ' => 'DESC'], '"a" DESC'], + 'array list' => [['a DESC', 'b'], '"a" DESC, "b" ASC'], + 'expression' => [new Expression('RAND()'), 'RAND()'], + 'expression in array' => [['a', new Expression('RAND()')], '"a" ASC, RAND()'], + ]; + } + + #[Test] + #[DataProvider('platformOrderProvider')] + public function followsThePlatformFragmentRulesForOrderNames( + PlatformInterface $platform, + string $order, + string $expected, + ): void { + $select = (new Select('foo'))->order($order); + + self::assertStringEndsWith(" ORDER BY {$expected}", $select->getSqlString($platform)); + } + + /** + * @param ExpressionInterface|array|string $order + */ + #[Test] + #[DataProvider('brokenOrderProvider')] + public function quotesOrderNameAsIdentifierChainWhenFragmentQuotingWouldBreakIt( + ExpressionInterface|array|string $order, + string $expected, + ): void { + self::assertSame("SELECT \"foo\".* FROM \"foo\" ORDER BY {$expected}", $this->render($order)); + } + + /** + * @param array|string $order + */ + #[Test] + #[DataProvider('unquotableOrderProvider')] + public function rejectsOrderNamesHoldingExpressionSyntax( + PlatformInterface $platform, + array|string $order, + string $name, + ): void { + $select = (new Select('foo'))->order($order); + + $this->expectExceptionObject(InvalidArgumentException::forUnquotableIdentifier($name)); + + $select->getSqlString($platform); + } + + /** + * @param ExpressionInterface|array|string $order + */ + #[Test] + #[DataProvider('validOrderProvider')] + public function rendersValidOrderUnchanged(ExpressionInterface|array|string $order, string $expected): void + { + self::assertSame("SELECT \"foo\".* FROM \"foo\" ORDER BY {$expected}", $this->render($order)); + } + + /** + * @param ExpressionInterface|array|string $order + */ + private function render(ExpressionInterface|array|string $order): string + { + return (new Select('foo'))->order($order) + ->getSqlString(new TrustingSql92Platform()); + } +}