1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components\Parsers; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
8
|
|
|
use PhpMyAdmin\SqlParser\Components\GroupKeyword; |
9
|
|
|
use PhpMyAdmin\SqlParser\Parseable; |
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
11
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
12
|
|
|
use PhpMyAdmin\SqlParser\TokenType; |
13
|
|
|
|
14
|
|
|
use function implode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* `GROUP BY` keyword parser. |
18
|
|
|
*/ |
19
|
|
|
final class GroupKeywords implements Parseable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param Parser $parser the parser that serves as context |
23
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
24
|
|
|
* @param array<string, mixed> $options parameters for parsing |
25
|
|
|
* |
26
|
|
|
* @return GroupKeyword[] |
27
|
|
|
*/ |
28
|
38 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
29
|
|
|
{ |
30
|
38 |
|
$ret = []; |
31
|
|
|
|
32
|
38 |
|
$expr = new GroupKeyword(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The state of the parser. |
36
|
|
|
* |
37
|
|
|
* Below are the states of the parser. |
38
|
|
|
* |
39
|
|
|
* 0 --------------------[ expression ]-------------------> 1 |
40
|
|
|
* |
41
|
|
|
* 1 ------------------------[ , ]------------------------> 0 |
42
|
|
|
* 1 -------------------[ ASC / DESC ]--------------------> 1 |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
38 |
|
$state = 0; |
47
|
|
|
|
48
|
38 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
49
|
|
|
/** |
50
|
|
|
* Token parsed at this moment. |
51
|
|
|
*/ |
52
|
38 |
|
$token = $list->tokens[$list->idx]; |
53
|
|
|
|
54
|
|
|
// End of statement. |
55
|
38 |
|
if ($token->type === TokenType::Delimiter) { |
56
|
16 |
|
break; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Skipping whitespaces and comments. |
60
|
38 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
61
|
38 |
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
38 |
|
if ($state === 0) { |
65
|
38 |
|
$expr->expr = Expression::parse($parser, $list); |
66
|
38 |
|
$state = 1; |
67
|
30 |
|
} elseif ($state === 1) { |
68
|
|
|
if ( |
69
|
30 |
|
($token->type === TokenType::Keyword) |
70
|
30 |
|
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC')) |
71
|
|
|
) { |
72
|
2 |
|
$expr->type = $token->keyword; |
73
|
30 |
|
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) { |
74
|
14 |
|
if (! empty($expr->expr)) { |
75
|
14 |
|
$ret[] = $expr; |
76
|
|
|
} |
77
|
|
|
|
78
|
14 |
|
$expr = new GroupKeyword(); |
79
|
14 |
|
$state = 0; |
80
|
|
|
} else { |
81
|
24 |
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Last iteration was not processed. |
87
|
38 |
|
if (! empty($expr->expr)) { |
88
|
38 |
|
$ret[] = $expr; |
89
|
|
|
} |
90
|
|
|
|
91
|
38 |
|
--$list->idx; |
92
|
|
|
|
93
|
38 |
|
return $ret; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @param GroupKeyword[] $component the component to be built */ |
97
|
32 |
|
public static function buildAll(array $component): string |
98
|
|
|
{ |
99
|
32 |
|
return implode(', ', $component); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|