|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components\Parsers; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
|
8
|
|
|
use PhpMyAdmin\SqlParser\Components\SetOperation; |
|
9
|
|
|
use PhpMyAdmin\SqlParser\Parseable; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\Token; |
|
12
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
|
13
|
|
|
use PhpMyAdmin\SqlParser\TokenType; |
|
14
|
|
|
|
|
15
|
|
|
use function implode; |
|
16
|
|
|
use function in_array; |
|
17
|
|
|
use function trim; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* `SET` keyword parser. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class SetOperations implements Parseable |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param Parser $parser the parser that serves as context |
|
26
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
|
27
|
|
|
* @param array<string, mixed> $options parameters for parsing |
|
28
|
|
|
* |
|
29
|
|
|
* @return SetOperation[] |
|
30
|
|
|
*/ |
|
31
|
120 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
|
32
|
|
|
{ |
|
33
|
120 |
|
$ret = []; |
|
34
|
|
|
|
|
35
|
120 |
|
$expr = new SetOperation(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The state of the parser. |
|
39
|
|
|
* |
|
40
|
|
|
* Below are the states of the parser. |
|
41
|
|
|
* |
|
42
|
|
|
* 0 ---------------------[ col_name ]--------------------> 0 |
|
43
|
|
|
* 0 ---------------------[ = or := ]---------------------> 1 |
|
44
|
|
|
* 1 -----------------------[ value ]---------------------> 1 |
|
45
|
|
|
* 1 ------------------------[ , ]------------------------> 0 |
|
46
|
|
|
*/ |
|
47
|
120 |
|
$state = 0; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Token when the parser has seen the latest comma |
|
51
|
|
|
*/ |
|
52
|
120 |
|
$commaLastSeenAt = null; |
|
53
|
|
|
|
|
54
|
120 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
|
55
|
|
|
/** |
|
56
|
|
|
* Token parsed at this moment. |
|
57
|
|
|
*/ |
|
58
|
120 |
|
$token = $list->tokens[$list->idx]; |
|
59
|
|
|
|
|
60
|
|
|
// End of statement. |
|
61
|
120 |
|
if ($token->type === TokenType::Delimiter) { |
|
62
|
90 |
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Skipping whitespaces and comments. |
|
66
|
100 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
|
67
|
90 |
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// No keyword is expected. |
|
71
|
|
|
if ( |
|
72
|
100 |
|
($token->type === TokenType::Keyword) |
|
73
|
100 |
|
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED) |
|
74
|
100 |
|
&& ($state === 0) |
|
75
|
|
|
) { |
|
76
|
32 |
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
100 |
|
if ($state === 0) { |
|
80
|
100 |
|
if (in_array($token->token, ['=', ':='], true)) { |
|
81
|
100 |
|
$state = 1; |
|
82
|
98 |
|
} elseif ($token->value !== ',') { |
|
83
|
98 |
|
$expr->column .= $token->token; |
|
84
|
24 |
|
} elseif ($token->value === ',') { |
|
85
|
62 |
|
$commaLastSeenAt = $token; |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
100 |
|
$tmp = Expression::parse( |
|
89
|
100 |
|
$parser, |
|
90
|
100 |
|
$list, |
|
91
|
100 |
|
['breakOnAlias' => true], |
|
92
|
100 |
|
); |
|
93
|
100 |
|
if ($tmp === null) { |
|
94
|
2 |
|
$parser->error('Missing expression.', $token); |
|
95
|
2 |
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
98 |
|
$expr->column = trim($expr->column); |
|
99
|
98 |
|
$expr->value = $tmp->expr; |
|
100
|
98 |
|
$ret[] = $expr; |
|
101
|
98 |
|
$expr = new SetOperation(); |
|
102
|
98 |
|
$state = 0; |
|
103
|
98 |
|
$commaLastSeenAt = null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
120 |
|
--$list->idx; |
|
108
|
|
|
|
|
109
|
|
|
// We saw a comma, but didn't see a column-value pair after it |
|
110
|
120 |
|
if ($commaLastSeenAt !== null) { |
|
|
|
|
|
|
111
|
2 |
|
$parser->error('Unexpected token.', $commaLastSeenAt); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
120 |
|
return $ret; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** @param SetOperation[] $component the component to be built */ |
|
118
|
38 |
|
public static function buildAll(array $component): string |
|
119
|
|
|
{ |
|
120
|
38 |
|
return implode(', ', $component); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|