forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionDefinitions.php
More file actions
184 lines (164 loc) · 5.47 KB
/
Copy pathPartitionDefinitions.php
File metadata and controls
184 lines (164 loc) · 5.47 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
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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Parsers;
use PhpMyAdmin\SqlParser\Components\PartitionDefinition;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use function implode;
/**
* Parses the create definition of a partition.
*
* Used for parsing `CREATE TABLE` statement.
*/
final class PartitionDefinitions implements Parseable
{
/**
* All field options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static array $partitionOptions = [
'STORAGE ENGINE' => [
1,
'var',
],
'ENGINE' => [
1,
'var',
],
'COMMENT' => [
2,
'var',
],
'DATA DIRECTORY' => [
3,
'var',
],
'INDEX DIRECTORY' => [
4,
'var',
],
'MAX_ROWS' => [
5,
'var',
],
'MIN_ROWS' => [
6,
'var',
],
'TABLESPACE' => [
7,
'var',
],
'NODEGROUP' => [
8,
'var',
],
];
/**
* @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
*/
public static function parse(Parser $parser, TokensList $list, array $options = []): PartitionDefinition
{
$ret = new PartitionDefinition();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 -------------[ PARTITION | SUBPARTITION ]------------> 1
*
* 1 -----------------------[ name ]----------------------> 2
*
* 2 ----------------------[ VALUES ]---------------------> 3
*
* 3 ---------------------[ LESS THAN ]-------------------> 4
* 3 ------------------------[ IN ]-----------------------> 4
*
* 4 -----------------------[ expr ]----------------------> 5
*
* 5 ----------------------[ options ]--------------------> 6
*
* 6 ------------------[ subpartitions ]------------------> (END)
*/
$state = 0;
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) {
$ret->isSubpartition = ($token->type === TokenType::Keyword) && ($token->keyword === 'SUBPARTITION');
$state = 1;
} elseif ($state === 1) {
$ret->name = (string) $token->value;
// Looking ahead for a 'VALUES' keyword.
// Loop until the end of the partition name (delimited by a whitespace)
while ($nextToken = $list->tokens[++$list->idx]) {
if ($nextToken->type !== TokenType::None) {
break;
}
$ret->name .= $nextToken->value;
}
$idx = $list->idx--;
// Get the first token after the white space.
$nextToken = $list->tokens[++$idx];
$state = ($nextToken->type === TokenType::Keyword)
&& ($nextToken->value === 'VALUES')
? 2 : 5;
} elseif ($state === 2) {
$state = 3;
} elseif ($state === 3) {
$ret->type = $token->value;
$state = 4;
} elseif ($state === 4) {
if ($token->value === 'MAXVALUE') {
$ret->expr = $token->value;
} else {
$ret->expr = Expressions::parse(
$parser,
$list,
[
'parenthesesDelimited' => true,
'breakOnAlias' => true,
],
);
}
$state = 5;
} elseif ($state === 5) {
$ret->options = OptionsArrays::parse($parser, $list, static::$partitionOptions);
$state = 6;
} elseif ($state === 6) {
if (($token->type === TokenType::Operator) && ($token->value === '(')) {
$ret->subpartitions = ArrayObjs::parse(
$parser,
$list,
['type' => self::class],
);
++$list->idx;
}
break;
}
}
--$list->idx;
return $ret;
}
/** @param PartitionDefinition[] $component the component to be built */
public static function buildAll(array $component): string
{
return "(\n" . implode(",\n", $component) . "\n)";
}
}