-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathSetOperations.php
More file actions
121 lines (103 loc) · 3.52 KB
/
SetOperations.php
File metadata and controls
121 lines (103 loc) · 3.52 KB
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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Parsers;
use PhpMyAdmin\SqlParser\Components\SetOperation;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use function implode;
use function in_array;
use function trim;
/**
* `SET` keyword parser.
*/
final class SetOperations implements Parseable
{
/**
* @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 SetOperation[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = []): array
{
$ret = [];
$expr = new SetOperation();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ---------------------[ col_name ]--------------------> 0
* 0 ---------------------[ = or := ]---------------------> 1
* 1 -----------------------[ value ]---------------------> 1
* 1 ------------------------[ , ]------------------------> 0
*/
$state = 0;
/**
* Token when the parser has seen the latest comma
*/
$commaLastSeenAt = null;
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;
}
// No keyword is expected.
if (
($token->type === TokenType::Keyword)
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
&& ($state === 0)
) {
break;
}
if ($state === 0) {
if (in_array($token->token, ['=', ':='], true)) {
$state = 1;
} elseif ($token->value !== ',') {
$expr->column .= $token->token;
} elseif ($token->value === ',') {
$commaLastSeenAt = $token;
}
} else {
$tmp = Expressions::parse(
$parser,
$list,
['breakOnAlias' => true],
);
if ($tmp === null) {
$parser->error('Missing expression.', $token);
break;
}
$expr->column = trim($expr->column);
$expr->value = $tmp->expr;
$ret[] = $expr;
$expr = new SetOperation();
$state = 0;
$commaLastSeenAt = null;
}
}
--$list->idx;
// We saw a comma, but didn't see a column-value pair after it
if ($commaLastSeenAt !== null) {
$parser->error('Unexpected token.', $commaLastSeenAt);
}
return $ret;
}
/** @param SetOperation[] $component the component to be built */
public static function buildAll(array $component): string
{
return implode(', ', $component);
}
}