Skip to content

Commit ac8dab3

Browse files
Merge pull request #596 from kamil-tekiela/OrderSortKeyword
OrderSortKeyword enum
2 parents 5c4dc47 + ce86c62 commit ac8dab3

21 files changed

+114
-29
lines changed

src/Components/GroupKeyword.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
*/
1414
final class GroupKeyword implements Component
1515
{
16-
/** @var 'ASC'|'DESC'|null */
17-
public string|null $type = null;
16+
public OrderSortKeyword|null $type = null;
1817

1918
/**
2019
* The expression that is used for grouping.

src/Components/OrderKeyword.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ final class OrderKeyword implements Component
1919
/**
2020
* The order type.
2121
*/
22-
public string $type;
22+
public OrderSortKeyword $type;
2323

2424
/**
25-
* @param Expression|null $expr the expression that we are sorting by
26-
* @param string $type the sorting type
25+
* @param Expression|null $expr the expression that we are sorting by
26+
* @param OrderSortKeyword $type the sorting type
2727
*/
28-
public function __construct(Expression|null $expr = null, string $type = 'ASC')
28+
public function __construct(Expression|null $expr = null, OrderSortKeyword $type = OrderSortKeyword::Asc)
2929
{
3030
$this->expr = $expr;
3131
$this->type = $type;
3232
}
3333

3434
public function build(): string
3535
{
36-
return $this->expr . ' ' . $this->type;
36+
return $this->expr . ' ' . $this->type->value;
3737
}
3838

3939
public function __toString(): string

src/Components/OrderSortKeyword.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Components;
6+
7+
enum OrderSortKeyword: string
8+
{
9+
case Asc = 'ASC';
10+
case Desc = 'DESC';
11+
}

src/Parsers/GroupKeywords.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Parsers;
66

77
use PhpMyAdmin\SqlParser\Components\GroupKeyword;
8+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
89
use PhpMyAdmin\SqlParser\Parseable;
910
use PhpMyAdmin\SqlParser\Parser;
1011
use PhpMyAdmin\SqlParser\TokensList;
@@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
6667
($token->type === TokenType::Keyword)
6768
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
6869
) {
69-
$expr->type = $token->keyword;
70+
$expr->type = OrderSortKeyword::from($token->keyword);
7071
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
7172
if (! empty($expr->expr)) {
7273
$ret[] = $expr;

src/Parsers/OrderKeywords.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Parsers;
66

77
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
8+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
89
use PhpMyAdmin\SqlParser\Parseable;
910
use PhpMyAdmin\SqlParser\Parser;
1011
use PhpMyAdmin\SqlParser\TokensList;
@@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
6667
($token->type === TokenType::Keyword)
6768
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
6869
) {
69-
$expr->type = $token->keyword;
70+
$expr->type = OrderSortKeyword::from($token->keyword);
7071
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
7172
if (! empty($expr->expr)) {
7273
$ret[] = $expr;

tests/Components/OrderKeywordTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\SqlParser\Components\Expression;
88
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
9+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
910
use PhpMyAdmin\SqlParser\Parsers\OrderKeywords;
1011
use PhpMyAdmin\SqlParser\Tests\TestCase;
1112

@@ -17,8 +18,8 @@ public function testBuildAll(): void
1718
'a ASC, b DESC',
1819
OrderKeywords::buildAll(
1920
[
20-
new OrderKeyword(new Expression('a'), 'ASC'),
21-
new OrderKeyword(new Expression('b'), 'DESC'),
21+
new OrderKeyword(new Expression('a'), OrderSortKeyword::Asc),
22+
new OrderKeyword(new Expression('b'), OrderSortKeyword::Desc),
2223
],
2324
),
2425
);

tests/data/parser/parseDelete.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,11 @@
547547
"function": null,
548548
"subquery": null
549549
},
550-
"type": "ASC"
550+
"type": {
551+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
552+
"name": "Asc",
553+
"value": "ASC"
554+
}
551555
}
552556
],
553557
"limit": null,

tests/data/parser/parseDelete4.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@
277277
"function": null,
278278
"subquery": null
279279
},
280-
"type": "ASC"
280+
"type": {
281+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
282+
"name": "Asc",
283+
"value": "ASC"
284+
}
281285
}
282286
],
283287
"limit": null,

tests/data/parser/parseDelete5.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@
343343
"function": null,
344344
"subquery": null
345345
},
346-
"type": "ASC"
346+
"type": {
347+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
348+
"name": "Asc",
349+
"value": "ASC"
350+
}
347351
}
348352
],
349353
"limit": {

tests/data/parser/parseDelete6.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@
197197
"function": null,
198198
"subquery": null
199199
},
200-
"type": "ASC"
200+
"type": {
201+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
202+
"name": "Asc",
203+
"value": "ASC"
204+
}
201205
}
202206
],
203207
"limit": null,

tests/data/parser/parseDelete7.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@
267267
"function": null,
268268
"subquery": null
269269
},
270-
"type": "ASC"
270+
"type": {
271+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
272+
"name": "Asc",
273+
"value": "ASC"
274+
}
271275
}
272276
],
273277
"limit": {

tests/data/parser/parseDeleteErr5.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@
365365
"function": null,
366366
"subquery": null
367367
},
368-
"type": "ASC"
368+
"type": {
369+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
370+
"name": "Asc",
371+
"value": "ASC"
372+
}
369373
}
370374
],
371375
"limit": null,

tests/data/parser/parseDeleteErr7.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,11 @@
345345
"function": null,
346346
"subquery": null
347347
},
348-
"type": "ASC"
348+
"type": {
349+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
350+
"name": "Asc",
351+
"value": "ASC"
352+
}
349353
}
350354
],
351355
"limit": null,

tests/data/parser/parseSelect.out

+10-2
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,11 @@
10371037
"function": null,
10381038
"subquery": null
10391039
},
1040-
"type": "DESC"
1040+
"type": {
1041+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1042+
"name": "Desc",
1043+
"value": "DESC"
1044+
}
10411045
},
10421046
{
10431047
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
@@ -1051,7 +1055,11 @@
10511055
"function": null,
10521056
"subquery": null
10531057
},
1054-
"type": "ASC"
1058+
"type": {
1059+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1060+
"name": "Asc",
1061+
"value": "ASC"
1062+
}
10551063
}
10561064
],
10571065
"limit": {

tests/data/parser/parseSelect13.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -11992,7 +11992,11 @@
1199211992
"function": null,
1199311993
"subquery": null
1199411994
},
11995-
"type": "ASC"
11995+
"type": {
11996+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
11997+
"name": "Asc",
11998+
"value": "ASC"
11999+
}
1199612000
}
1199712001
],
1199812002
"limit": null,

tests/data/parser/parseSelect9.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,11 @@
949949
"function": null,
950950
"subquery": null
951951
},
952-
"type": "ASC"
952+
"type": {
953+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
954+
"name": "Asc",
955+
"value": "ASC"
956+
}
953957
}
954958
],
955959
"limit": null,

tests/data/parser/parseSelectErr1.out

+10-2
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,11 @@
983983
"function": null,
984984
"subquery": null
985985
},
986-
"type": "DESC"
986+
"type": {
987+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
988+
"name": "Desc",
989+
"value": "DESC"
990+
}
987991
},
988992
{
989993
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
@@ -997,7 +1001,11 @@
9971001
"function": null,
9981002
"subquery": null
9991003
},
1000-
"type": "ASC"
1004+
"type": {
1005+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1006+
"name": "Asc",
1007+
"value": "ASC"
1008+
}
10011009
}
10021010
],
10031011
"limit": {

tests/data/parser/parseSelectGroupBy.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,11 @@
16361636
"function": null,
16371637
"subquery": null
16381638
},
1639-
"type": "ASC"
1639+
"type": {
1640+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1641+
"name": "Asc",
1642+
"value": "ASC"
1643+
}
16401644
}
16411645
],
16421646
"limit": {

tests/data/parser/parseSelectOrderByComment.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@
300300
"function": null,
301301
"subquery": null
302302
},
303-
"type": "ASC"
303+
"type": {
304+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
305+
"name": "Asc",
306+
"value": "ASC"
307+
}
304308
}
305309
],
306310
"limit": null,

tests/data/parser/parseSelectOrderByIsNull.out

+5-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@
267267
"function": null,
268268
"subquery": null
269269
},
270-
"type": "ASC"
270+
"type": {
271+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
272+
"name": "Asc",
273+
"value": "ASC"
274+
}
271275
}
272276
],
273277
"limit": null,

tests/data/parser/parseSelectUnion2.out

+10-2
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,11 @@
12891289
"function": null,
12901290
"subquery": null
12911291
},
1292-
"type": "ASC"
1292+
"type": {
1293+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1294+
"name": "Asc",
1295+
"value": "ASC"
1296+
}
12931297
},
12941298
{
12951299
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
@@ -1303,7 +1307,11 @@
13031307
"function": null,
13041308
"subquery": null
13051309
},
1306-
"type": "ASC"
1310+
"type": {
1311+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
1312+
"name": "Asc",
1313+
"value": "ASC"
1314+
}
13071315
}
13081316
],
13091317
"limit": null,

0 commit comments

Comments
 (0)