1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components\Parsers; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\ArrayObj; |
8
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
9
|
|
|
use PhpMyAdmin\SqlParser\Components\JoinKeyword; |
10
|
|
|
use PhpMyAdmin\SqlParser\Parseable; |
11
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
12
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
13
|
|
|
use PhpMyAdmin\SqlParser\TokenType; |
14
|
|
|
|
15
|
|
|
use function implode; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `JOIN` keyword parser. |
19
|
|
|
*/ |
20
|
|
|
final class JoinKeywords implements Parseable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Parser $parser the parser that serves as context |
24
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
25
|
|
|
* @param array<string, mixed> $options parameters for parsing |
26
|
|
|
* |
27
|
|
|
* @return JoinKeyword[] |
28
|
|
|
*/ |
29
|
76 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
30
|
|
|
{ |
31
|
76 |
|
$ret = []; |
32
|
|
|
|
33
|
76 |
|
$expr = new JoinKeyword(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The state of the parser. |
37
|
|
|
* |
38
|
|
|
* Below are the states of the parser. |
39
|
|
|
* |
40
|
|
|
* 0 -----------------------[ JOIN ]----------------------> 1 |
41
|
|
|
* |
42
|
|
|
* 1 -----------------------[ expr ]----------------------> 2 |
43
|
|
|
* |
44
|
|
|
* 2 ------------------------[ ON ]-----------------------> 3 |
45
|
|
|
* 2 -----------------------[ USING ]---------------------> 4 |
46
|
|
|
* |
47
|
|
|
* 3 --------------------[ conditions ]-------------------> 0 |
48
|
|
|
* |
49
|
|
|
* 4 ----------------------[ columns ]--------------------> 0 |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
76 |
|
$state = 0; |
54
|
|
|
|
55
|
|
|
// By design, the parser will parse first token after the keyword. |
56
|
|
|
// In this case, the keyword must be analyzed too, in order to determine |
57
|
|
|
// the type of this join. |
58
|
76 |
|
if ($list->idx > 0) { |
59
|
70 |
|
--$list->idx; |
60
|
|
|
} |
61
|
|
|
|
62
|
76 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
63
|
|
|
/** |
64
|
|
|
* Token parsed at this moment. |
65
|
|
|
*/ |
66
|
76 |
|
$token = $list->tokens[$list->idx]; |
67
|
|
|
|
68
|
|
|
// End of statement. |
69
|
76 |
|
if ($token->type === TokenType::Delimiter) { |
70
|
36 |
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Skipping whitespaces and comments. |
74
|
76 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
75
|
76 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
76 |
|
if ($state === 0) { |
79
|
76 |
|
if (($token->type !== TokenType::Keyword) || empty(JoinKeyword::JOINS[$token->keyword])) { |
80
|
34 |
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
76 |
|
$expr->type = JoinKeyword::JOINS[$token->keyword]; |
84
|
76 |
|
$state = 1; |
85
|
76 |
|
} elseif ($state === 1) { |
86
|
76 |
|
$expr->expr = Expression::parse($parser, $list, ['field' => 'table']); |
87
|
76 |
|
$state = 2; |
88
|
58 |
|
} elseif ($state === 2) { |
89
|
58 |
|
if ($token->type === TokenType::Keyword) { |
90
|
58 |
|
switch ($token->keyword) { |
91
|
58 |
|
case 'ON': |
92
|
48 |
|
$state = 3; |
93
|
48 |
|
break; |
94
|
12 |
|
case 'USING': |
95
|
2 |
|
$state = 4; |
96
|
2 |
|
break; |
97
|
|
|
default: |
98
|
10 |
|
if (empty(JoinKeyword::JOINS[$token->keyword])) { |
99
|
|
|
/* Next clause is starting */ |
100
|
8 |
|
break 2; |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
$ret[] = $expr; |
104
|
8 |
|
$expr = new JoinKeyword(); |
105
|
8 |
|
$expr->type = JoinKeyword::JOINS[$token->keyword]; |
106
|
8 |
|
$state = 1; |
107
|
|
|
|
108
|
32 |
|
break; |
109
|
|
|
} |
110
|
|
|
} |
111
|
50 |
|
} elseif ($state === 3) { |
112
|
48 |
|
$expr->on = Conditions::parse($parser, $list); |
113
|
48 |
|
$ret[] = $expr; |
114
|
48 |
|
$expr = new JoinKeyword(); |
115
|
48 |
|
$state = 0; |
116
|
2 |
|
} elseif ($state === 4) { |
117
|
2 |
|
$expr->using = ArrayObj::parse($parser, $list); |
118
|
2 |
|
$ret[] = $expr; |
119
|
2 |
|
$expr = new JoinKeyword(); |
120
|
2 |
|
$state = 0; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
76 |
|
if (! empty($expr->type)) { |
125
|
28 |
|
$ret[] = $expr; |
126
|
|
|
} |
127
|
|
|
|
128
|
76 |
|
--$list->idx; |
129
|
|
|
|
130
|
76 |
|
return $ret; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @param JoinKeyword[] $component the component to be built */ |
134
|
22 |
|
public static function buildAll(array $component): string |
135
|
|
|
{ |
136
|
22 |
|
return implode(' ', $component); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|