forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferences.php
More file actions
98 lines (86 loc) · 2.59 KB
/
Copy pathReferences.php
File metadata and controls
98 lines (86 loc) · 2.59 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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Parsers;
use PhpMyAdmin\SqlParser\Components\Reference;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
/**
* `REFERENCES` keyword parser.
*/
final class References implements Parseable
{
/**
* All references options.
*/
private const REFERENCES_OPTIONS = [
'MATCH' => [
1,
'var',
],
'ON DELETE' => [
2,
'var',
],
'ON UPDATE' => [
3,
'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 = []): Reference
{
$ret = new Reference();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ----------------------[ table ]---------------------> 1
*
* 1 ---------------------[ columns ]--------------------> 2
*
* 2 ---------------------[ options ]--------------------> (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->table = Expressions::parse(
$parser,
$list,
[
'parseField' => 'table',
'breakOnAlias' => true,
],
);
$state = 1;
} elseif ($state === 1) {
$ret->columns = ArrayObjs::parse($parser, $list)->values;
$state = 2;
} else {
$ret->options = OptionsArrays::parse($parser, $list, self::REFERENCES_OPTIONS);
++$list->idx;
break;
}
}
--$list->idx;
return $ret;
}
}