|
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
|
76 |
|
$state = 0; |
|
52
|
|
|
|
|
53
|
|
|
// By design, the parser will parse first token after the keyword. |
|
54
|
|
|
// In this case, the keyword must be analyzed too, in order to determine |
|
55
|
|
|
// the type of this join. |
|
56
|
76 |
|
if ($list->idx > 0) { |
|
57
|
70 |
|
--$list->idx; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
76 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
|
61
|
|
|
/** |
|
62
|
|
|
* Token parsed at this moment. |
|
63
|
|
|
*/ |
|
64
|
76 |
|
$token = $list->tokens[$list->idx]; |
|
65
|
|
|
|
|
66
|
|
|
// End of statement. |
|
67
|
76 |
|
if ($token->type === TokenType::Delimiter) { |
|
68
|
36 |
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Skipping whitespaces and comments. |
|
72
|
76 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
|
73
|
76 |
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
76 |
|
if ($state === 0) { |
|
77
|
76 |
|
if (($token->type !== TokenType::Keyword) || empty(JoinKeyword::JOINS[$token->keyword])) { |
|
78
|
34 |
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
76 |
|
$expr->type = JoinKeyword::JOINS[$token->keyword]; |
|
82
|
76 |
|
$state = 1; |
|
83
|
76 |
|
} elseif ($state === 1) { |
|
84
|
76 |
|
$expr->expr = Expression::parse($parser, $list, ['field' => 'table']); |
|
85
|
76 |
|
$state = 2; |
|
86
|
58 |
|
} elseif ($state === 2) { |
|
87
|
58 |
|
if ($token->type === TokenType::Keyword) { |
|
88
|
58 |
|
switch ($token->keyword) { |
|
89
|
58 |
|
case 'ON': |
|
90
|
48 |
|
$state = 3; |
|
91
|
48 |
|
break; |
|
92
|
12 |
|
case 'USING': |
|
93
|
2 |
|
$state = 4; |
|
94
|
2 |
|
break; |
|
95
|
|
|
default: |
|
96
|
10 |
|
if (empty(JoinKeyword::JOINS[$token->keyword])) { |
|
97
|
|
|
/* Next clause is starting */ |
|
98
|
8 |
|
break 2; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
8 |
|
$ret[] = $expr; |
|
102
|
8 |
|
$expr = new JoinKeyword(); |
|
103
|
8 |
|
$expr->type = JoinKeyword::JOINS[$token->keyword]; |
|
104
|
8 |
|
$state = 1; |
|
105
|
|
|
|
|
106
|
32 |
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
50 |
|
} elseif ($state === 3) { |
|
110
|
48 |
|
$expr->on = Conditions::parse($parser, $list); |
|
111
|
48 |
|
$ret[] = $expr; |
|
112
|
48 |
|
$expr = new JoinKeyword(); |
|
113
|
48 |
|
$state = 0; |
|
114
|
|
|
} else { |
|
115
|
2 |
|
$expr->using = ArrayObj::parse($parser, $list); |
|
116
|
2 |
|
$ret[] = $expr; |
|
117
|
2 |
|
$expr = new JoinKeyword(); |
|
118
|
2 |
|
$state = 0; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
76 |
|
if (! empty($expr->type)) { |
|
123
|
28 |
|
$ret[] = $expr; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
76 |
|
--$list->idx; |
|
127
|
|
|
|
|
128
|
76 |
|
return $ret; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** @param JoinKeyword[] $component the component to be built */ |
|
132
|
22 |
|
public static function buildAll(array $component): string |
|
133
|
|
|
{ |
|
134
|
22 |
|
return implode(' ', $component); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|