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