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 array_search; |
13
|
|
|
use function implode; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* `JOIN` keyword parser. |
17
|
|
|
*/ |
18
|
|
|
final class JoinKeyword implements Component |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Types of join. |
22
|
|
|
* |
23
|
|
|
* @var array<string, string> |
24
|
|
|
*/ |
25
|
|
|
public static $joins = [ |
26
|
|
|
'CROSS JOIN' => 'CROSS', |
27
|
|
|
'FULL JOIN' => 'FULL', |
28
|
|
|
'FULL OUTER JOIN' => 'FULL', |
29
|
|
|
'INNER JOIN' => 'INNER', |
30
|
|
|
'JOIN' => 'JOIN', |
31
|
|
|
'LEFT JOIN' => 'LEFT', |
32
|
|
|
'LEFT OUTER JOIN' => 'LEFT', |
33
|
|
|
'RIGHT JOIN' => 'RIGHT', |
34
|
|
|
'RIGHT OUTER JOIN' => 'RIGHT', |
35
|
|
|
'NATURAL JOIN' => 'NATURAL', |
36
|
|
|
'NATURAL LEFT JOIN' => 'NATURAL LEFT', |
37
|
|
|
'NATURAL RIGHT JOIN' => 'NATURAL RIGHT', |
38
|
|
|
'NATURAL LEFT OUTER JOIN' => 'NATURAL LEFT OUTER', |
39
|
|
|
'NATURAL RIGHT OUTER JOIN' => 'NATURAL RIGHT OUTER', |
40
|
|
|
'STRAIGHT_JOIN' => 'STRAIGHT', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Type of this join. |
45
|
|
|
* |
46
|
|
|
* @see JoinKeyword::$joins |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $type; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Join expression. |
54
|
|
|
* |
55
|
|
|
* @var Expression |
56
|
|
|
*/ |
57
|
|
|
public $expr; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Join conditions. |
61
|
|
|
* |
62
|
|
|
* @var Condition[] |
63
|
|
|
*/ |
64
|
|
|
public $on; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Columns in Using clause. |
68
|
|
|
* |
69
|
|
|
* @var ArrayObj |
70
|
|
|
*/ |
71
|
|
|
public $using; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see JoinKeyword::$joins |
75
|
|
|
* |
76
|
|
|
* @param string $type Join type |
77
|
|
|
* @param Expression $expr join expression |
78
|
|
|
* @param Condition[] $on join conditions |
79
|
|
|
* @param ArrayObj $using columns joined |
80
|
|
|
*/ |
81
|
66 |
|
public function __construct($type = null, $expr = null, $on = null, $using = null) |
82
|
|
|
{ |
83
|
66 |
|
$this->type = $type; |
84
|
66 |
|
$this->expr = $expr; |
85
|
66 |
|
$this->on = $on; |
86
|
66 |
|
$this->using = $using; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Parser $parser the parser that serves as context |
91
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
92
|
|
|
* @param array<string, mixed> $options parameters for parsing |
93
|
|
|
* |
94
|
|
|
* @return JoinKeyword[] |
95
|
|
|
*/ |
96
|
66 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []) |
97
|
|
|
{ |
98
|
66 |
|
$ret = []; |
99
|
|
|
|
100
|
66 |
|
$expr = new static(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The state of the parser. |
104
|
|
|
* |
105
|
|
|
* Below are the states of the parser. |
106
|
|
|
* |
107
|
|
|
* 0 -----------------------[ JOIN ]----------------------> 1 |
108
|
|
|
* |
109
|
|
|
* 1 -----------------------[ expr ]----------------------> 2 |
110
|
|
|
* |
111
|
|
|
* 2 ------------------------[ ON ]-----------------------> 3 |
112
|
|
|
* 2 -----------------------[ USING ]---------------------> 4 |
113
|
|
|
* |
114
|
|
|
* 3 --------------------[ conditions ]-------------------> 0 |
115
|
|
|
* |
116
|
|
|
* 4 ----------------------[ columns ]--------------------> 0 |
117
|
|
|
* |
118
|
|
|
* @var int |
119
|
|
|
*/ |
120
|
66 |
|
$state = 0; |
121
|
|
|
|
122
|
|
|
// By design, the parser will parse first token after the keyword. |
123
|
|
|
// In this case, the keyword must be analyzed too, in order to determine |
124
|
|
|
// the type of this join. |
125
|
66 |
|
if ($list->idx > 0) { |
126
|
60 |
|
--$list->idx; |
127
|
|
|
} |
128
|
|
|
|
129
|
66 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
130
|
|
|
/** |
131
|
|
|
* Token parsed at this moment. |
132
|
|
|
*/ |
133
|
66 |
|
$token = $list->tokens[$list->idx]; |
134
|
|
|
|
135
|
|
|
// End of statement. |
136
|
66 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
137
|
36 |
|
break; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Skipping whitespaces and comments. |
141
|
66 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
142
|
66 |
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
66 |
|
if ($state === 0) { |
146
|
66 |
|
if (($token->type !== Token::TYPE_KEYWORD) || empty(static::$joins[$token->keyword])) { |
147
|
24 |
|
break; |
148
|
|
|
} |
149
|
|
|
|
150
|
66 |
|
$expr->type = static::$joins[$token->keyword]; |
151
|
66 |
|
$state = 1; |
152
|
66 |
|
} elseif ($state === 1) { |
153
|
66 |
|
$expr->expr = Expression::parse($parser, $list, ['field' => 'table']); |
154
|
66 |
|
$state = 2; |
155
|
48 |
|
} elseif ($state === 2) { |
156
|
48 |
|
if ($token->type === Token::TYPE_KEYWORD) { |
157
|
48 |
|
switch ($token->keyword) { |
158
|
48 |
|
case 'ON': |
159
|
38 |
|
$state = 3; |
160
|
38 |
|
break; |
161
|
12 |
|
case 'USING': |
162
|
2 |
|
$state = 4; |
163
|
2 |
|
break; |
164
|
|
|
default: |
165
|
10 |
|
if (empty(static::$joins[$token->keyword])) { |
166
|
|
|
/* Next clause is starting */ |
167
|
8 |
|
break 2; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
$ret[] = $expr; |
171
|
8 |
|
$expr = new static(); |
172
|
8 |
|
$expr->type = static::$joins[$token->keyword]; |
173
|
8 |
|
$state = 1; |
174
|
|
|
|
175
|
46 |
|
break; |
176
|
|
|
} |
177
|
|
|
} |
178
|
40 |
|
} elseif ($state === 3) { |
179
|
38 |
|
$expr->on = Condition::parse($parser, $list); |
180
|
38 |
|
$ret[] = $expr; |
181
|
38 |
|
$expr = new static(); |
182
|
38 |
|
$state = 0; |
183
|
2 |
|
} elseif ($state === 4) { |
184
|
2 |
|
$expr->using = ArrayObj::parse($parser, $list); |
185
|
2 |
|
$ret[] = $expr; |
186
|
2 |
|
$expr = new static(); |
187
|
2 |
|
$state = 0; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
66 |
|
if (! empty($expr->type)) { |
192
|
28 |
|
$ret[] = $expr; |
193
|
|
|
} |
194
|
|
|
|
195
|
66 |
|
--$list->idx; |
196
|
|
|
|
197
|
66 |
|
return $ret; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param JoinKeyword[] $component the component to be built |
202
|
|
|
* @param array<string, mixed> $options parameters for building |
203
|
|
|
*/ |
204
|
20 |
|
public static function build($component, array $options = []): string |
205
|
|
|
{ |
206
|
20 |
|
$ret = []; |
207
|
20 |
|
foreach ($component as $c) { |
208
|
20 |
|
$ret[] = array_search($c->type, static::$joins) . ' ' . $c->expr |
209
|
20 |
|
. (! empty($c->on) |
210
|
20 |
|
? ' ON ' . Condition::build($c->on) : '') |
211
|
20 |
|
. (! empty($c->using) |
212
|
20 |
|
? ' USING ' . ArrayObj::build($c->using) : ''); |
213
|
|
|
} |
214
|
|
|
|
215
|
20 |
|
return implode(' ', $ret); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function __toString(): string |
219
|
|
|
{ |
220
|
|
|
return static::build($this); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|