diff --git a/src/Components/GroupKeyword.php b/src/Components/GroupKeyword.php index 94059e4a..e19a4c1f 100644 --- a/src/Components/GroupKeyword.php +++ b/src/Components/GroupKeyword.php @@ -13,8 +13,7 @@ */ final class GroupKeyword implements Component { - /** @var 'ASC'|'DESC'|null */ - public string|null $type = null; + public OrderSortKeyword|null $type = null; /** * The expression that is used for grouping. diff --git a/src/Components/OrderKeyword.php b/src/Components/OrderKeyword.php index 9b4a013e..59bf0458 100644 --- a/src/Components/OrderKeyword.php +++ b/src/Components/OrderKeyword.php @@ -19,13 +19,13 @@ final class OrderKeyword implements Component /** * The order type. */ - public string $type; + public OrderSortKeyword $type; /** - * @param Expression|null $expr the expression that we are sorting by - * @param string $type the sorting type + * @param Expression|null $expr the expression that we are sorting by + * @param OrderSortKeyword $type the sorting type */ - public function __construct(Expression|null $expr = null, string $type = 'ASC') + public function __construct(Expression|null $expr = null, OrderSortKeyword $type = OrderSortKeyword::Asc) { $this->expr = $expr; $this->type = $type; @@ -33,7 +33,7 @@ public function __construct(Expression|null $expr = null, string $type = 'ASC') public function build(): string { - return $this->expr . ' ' . $this->type; + return $this->expr . ' ' . $this->type->value; } public function __toString(): string diff --git a/src/Components/OrderSortKeyword.php b/src/Components/OrderSortKeyword.php new file mode 100644 index 00000000..109b593d --- /dev/null +++ b/src/Components/OrderSortKeyword.php @@ -0,0 +1,11 @@ +type === TokenType::Keyword) && (($token->keyword === 'ASC') || ($token->keyword === 'DESC')) ) { - $expr->type = $token->keyword; + $expr->type = OrderSortKeyword::from($token->keyword); } elseif (($token->type === TokenType::Operator) && ($token->value === ',')) { if (! empty($expr->expr)) { $ret[] = $expr; diff --git a/src/Parsers/OrderKeywords.php b/src/Parsers/OrderKeywords.php index 2b1e53ea..b4205570 100644 --- a/src/Parsers/OrderKeywords.php +++ b/src/Parsers/OrderKeywords.php @@ -5,6 +5,7 @@ namespace PhpMyAdmin\SqlParser\Parsers; use PhpMyAdmin\SqlParser\Components\OrderKeyword; +use PhpMyAdmin\SqlParser\Components\OrderSortKeyword; use PhpMyAdmin\SqlParser\Parseable; use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\TokensList; @@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options = ($token->type === TokenType::Keyword) && (($token->keyword === 'ASC') || ($token->keyword === 'DESC')) ) { - $expr->type = $token->keyword; + $expr->type = OrderSortKeyword::from($token->keyword); } elseif (($token->type === TokenType::Operator) && ($token->value === ',')) { if (! empty($expr->expr)) { $ret[] = $expr; diff --git a/tests/Components/OrderKeywordTest.php b/tests/Components/OrderKeywordTest.php index ce58930e..1150d86f 100644 --- a/tests/Components/OrderKeywordTest.php +++ b/tests/Components/OrderKeywordTest.php @@ -6,6 +6,7 @@ use PhpMyAdmin\SqlParser\Components\Expression; use PhpMyAdmin\SqlParser\Components\OrderKeyword; +use PhpMyAdmin\SqlParser\Components\OrderSortKeyword; use PhpMyAdmin\SqlParser\Parsers\OrderKeywords; use PhpMyAdmin\SqlParser\Tests\TestCase; @@ -17,8 +18,8 @@ public function testBuildAll(): void 'a ASC, b DESC', OrderKeywords::buildAll( [ - new OrderKeyword(new Expression('a'), 'ASC'), - new OrderKeyword(new Expression('b'), 'DESC'), + new OrderKeyword(new Expression('a'), OrderSortKeyword::Asc), + new OrderKeyword(new Expression('b'), OrderSortKeyword::Desc), ], ), ); diff --git a/tests/data/parser/parseDelete.out b/tests/data/parser/parseDelete.out index c2eef551..db8c66b6 100644 --- a/tests/data/parser/parseDelete.out +++ b/tests/data/parser/parseDelete.out @@ -547,7 +547,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseDelete4.out b/tests/data/parser/parseDelete4.out index 130a27b7..ed0aee28 100644 --- a/tests/data/parser/parseDelete4.out +++ b/tests/data/parser/parseDelete4.out @@ -277,7 +277,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseDelete5.out b/tests/data/parser/parseDelete5.out index c39329d2..63ab09e2 100644 --- a/tests/data/parser/parseDelete5.out +++ b/tests/data/parser/parseDelete5.out @@ -343,7 +343,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": { diff --git a/tests/data/parser/parseDelete6.out b/tests/data/parser/parseDelete6.out index 8b72a6c4..f9e4dc05 100644 --- a/tests/data/parser/parseDelete6.out +++ b/tests/data/parser/parseDelete6.out @@ -197,7 +197,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseDelete7.out b/tests/data/parser/parseDelete7.out index 47639d82..4a082790 100644 --- a/tests/data/parser/parseDelete7.out +++ b/tests/data/parser/parseDelete7.out @@ -267,7 +267,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": { diff --git a/tests/data/parser/parseDeleteErr5.out b/tests/data/parser/parseDeleteErr5.out index 394ddc06..5bcdaf70 100644 --- a/tests/data/parser/parseDeleteErr5.out +++ b/tests/data/parser/parseDeleteErr5.out @@ -365,7 +365,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseDeleteErr7.out b/tests/data/parser/parseDeleteErr7.out index bbb17420..0877086c 100644 --- a/tests/data/parser/parseDeleteErr7.out +++ b/tests/data/parser/parseDeleteErr7.out @@ -345,7 +345,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseSelect.out b/tests/data/parser/parseSelect.out index 91e0a155..9e5e14b0 100644 --- a/tests/data/parser/parseSelect.out +++ b/tests/data/parser/parseSelect.out @@ -1037,7 +1037,11 @@ "function": null, "subquery": null }, - "type": "DESC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Desc", + "value": "DESC" + } }, { "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword", @@ -1051,7 +1055,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": { diff --git a/tests/data/parser/parseSelect13.out b/tests/data/parser/parseSelect13.out index d1f25e65..20188b9b 100644 --- a/tests/data/parser/parseSelect13.out +++ b/tests/data/parser/parseSelect13.out @@ -11992,7 +11992,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseSelect9.out b/tests/data/parser/parseSelect9.out index 2e26e99f..3014931a 100644 --- a/tests/data/parser/parseSelect9.out +++ b/tests/data/parser/parseSelect9.out @@ -949,7 +949,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseSelectErr1.out b/tests/data/parser/parseSelectErr1.out index 89fe2a3a..5dccca8f 100644 --- a/tests/data/parser/parseSelectErr1.out +++ b/tests/data/parser/parseSelectErr1.out @@ -983,7 +983,11 @@ "function": null, "subquery": null }, - "type": "DESC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Desc", + "value": "DESC" + } }, { "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword", @@ -997,7 +1001,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": { diff --git a/tests/data/parser/parseSelectGroupBy.out b/tests/data/parser/parseSelectGroupBy.out index 96be1a9e..0281b468 100644 --- a/tests/data/parser/parseSelectGroupBy.out +++ b/tests/data/parser/parseSelectGroupBy.out @@ -1636,7 +1636,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": { diff --git a/tests/data/parser/parseSelectOrderByComment.out b/tests/data/parser/parseSelectOrderByComment.out index 7400b44e..f4096199 100644 --- a/tests/data/parser/parseSelectOrderByComment.out +++ b/tests/data/parser/parseSelectOrderByComment.out @@ -300,7 +300,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseSelectOrderByIsNull.out b/tests/data/parser/parseSelectOrderByIsNull.out index 8e465ece..a1caf1ef 100644 --- a/tests/data/parser/parseSelectOrderByIsNull.out +++ b/tests/data/parser/parseSelectOrderByIsNull.out @@ -267,7 +267,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null, diff --git a/tests/data/parser/parseSelectUnion2.out b/tests/data/parser/parseSelectUnion2.out index 6305bb53..99792891 100644 --- a/tests/data/parser/parseSelectUnion2.out +++ b/tests/data/parser/parseSelectUnion2.out @@ -1289,7 +1289,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } }, { "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword", @@ -1303,7 +1307,11 @@ "function": null, "subquery": null }, - "type": "ASC" + "type": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword", + "name": "Asc", + "value": "ASC" + } } ], "limit": null,