1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
8
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
9
|
|
|
use PhpMyAdmin\SqlParser\Token; |
10
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
11
|
|
|
|
12
|
|
|
use function implode; |
13
|
|
|
use function trim; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* `GROUP BY` keyword parser. |
17
|
|
|
*/ |
18
|
|
|
final class GroupKeyword implements Component |
19
|
|
|
{ |
20
|
|
|
/** @var mixed */ |
21
|
|
|
public $type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The expression that is used for grouping. |
25
|
|
|
* |
26
|
|
|
* @var Expression |
27
|
|
|
*/ |
28
|
|
|
public $expr; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Expression $expr the expression that we are sorting by |
32
|
|
|
*/ |
33
|
38 |
|
public function __construct($expr = null) |
34
|
|
|
{ |
35
|
38 |
|
$this->expr = $expr; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Parser $parser the parser that serves as context |
40
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
41
|
|
|
* @param array<string, mixed> $options parameters for parsing |
42
|
|
|
* |
43
|
|
|
* @return GroupKeyword[] |
44
|
|
|
*/ |
45
|
38 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
46
|
|
|
{ |
47
|
38 |
|
$ret = []; |
48
|
|
|
|
49
|
38 |
|
$expr = new static(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The state of the parser. |
53
|
|
|
* |
54
|
|
|
* Below are the states of the parser. |
55
|
|
|
* |
56
|
|
|
* 0 --------------------[ expression ]-------------------> 1 |
57
|
|
|
* |
58
|
|
|
* 1 ------------------------[ , ]------------------------> 0 |
59
|
|
|
* 1 -------------------[ ASC / DESC ]--------------------> 1 |
60
|
|
|
* |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
38 |
|
$state = 0; |
64
|
|
|
|
65
|
38 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
66
|
|
|
/** |
67
|
|
|
* Token parsed at this moment. |
68
|
|
|
*/ |
69
|
38 |
|
$token = $list->tokens[$list->idx]; |
70
|
|
|
|
71
|
|
|
// End of statement. |
72
|
38 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
73
|
16 |
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Skipping whitespaces and comments. |
77
|
38 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
78
|
38 |
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
38 |
|
if ($state === 0) { |
82
|
38 |
|
$expr->expr = Expression::parse($parser, $list); |
83
|
38 |
|
$state = 1; |
84
|
30 |
|
} elseif ($state === 1) { |
85
|
|
|
if ( |
86
|
30 |
|
($token->type === Token::TYPE_KEYWORD) |
87
|
30 |
|
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC')) |
88
|
|
|
) { |
89
|
2 |
|
$expr->type = $token->keyword; |
90
|
30 |
|
} elseif (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) { |
91
|
14 |
|
if (! empty($expr->expr)) { |
92
|
14 |
|
$ret[] = $expr; |
93
|
|
|
} |
94
|
|
|
|
95
|
14 |
|
$expr = new static(); |
96
|
14 |
|
$state = 0; |
97
|
|
|
} else { |
98
|
24 |
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Last iteration was not processed. |
104
|
38 |
|
if (! empty($expr->expr)) { |
105
|
38 |
|
$ret[] = $expr; |
106
|
|
|
} |
107
|
|
|
|
108
|
38 |
|
--$list->idx; |
109
|
|
|
|
110
|
38 |
|
return $ret; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param GroupKeyword $component the component to be built |
115
|
|
|
*/ |
116
|
36 |
|
public static function build($component): string |
117
|
|
|
{ |
118
|
36 |
|
return trim((string) $component->expr); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param GroupKeyword[] $component the component to be built |
123
|
|
|
*/ |
124
|
32 |
|
public static function buildAll(array $component): string |
125
|
|
|
{ |
126
|
32 |
|
return implode(', ', $component); |
127
|
|
|
} |
128
|
|
|
|
129
|
30 |
|
public function __toString(): string |
130
|
|
|
{ |
131
|
30 |
|
return static::build($this); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|