forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntoKeywords.php
More file actions
119 lines (102 loc) · 3.75 KB
/
Copy pathIntoKeywords.php
File metadata and controls
119 lines (102 loc) · 3.75 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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Parsers;
use PhpMyAdmin\SqlParser\Components\IntoKeyword;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
/**
* `INTO` keyword parser.
*/
final class IntoKeywords 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
*/
public static function parse(Parser $parser, TokensList $list, array $options = []): IntoKeyword
{
$ret = new IntoKeyword();
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 -----------------------[ name ]----------------------> 1
* 0 ---------------------[ OUTFILE ]---------------------> 2
*
* 1 ------------------------[ ( ]------------------------> (END)
*
* 2 ---------------------[ filename ]--------------------> 1
*/
$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 (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
if (($state === 0) && ($token->keyword === 'OUTFILE')) {
$ret->type = 'OUTFILE';
$state = 2;
continue;
}
// No other keyword is expected except for $state = 4, which expects `LINES`
if ($state !== 4) {
break;
}
}
if ($state === 0) {
if (
(isset($options['fromInsert'])
&& $options['fromInsert'])
|| (isset($options['fromReplace'])
&& $options['fromReplace'])
) {
$ret->dest = Expressions::parse(
$parser,
$list,
[
'parseField' => 'table',
'breakOnAlias' => true,
],
);
} else {
$ret->values = ExpressionArray::parse($parser, $list);
}
$state = 1;
} elseif ($state === 1) {
if (($token->type === TokenType::Operator) && ($token->value === '(')) {
$ret->columns = ArrayObjs::parse($parser, $list)->values;
++$list->idx;
}
break;
} elseif ($state === 2) {
$ret->dest = $token->value;
$state = 3;
} elseif ($state === 3) {
$ret->parseFileOptions($parser, $list, $token->keyword);
$state = 4;
} elseif ($state === 4) {
if ($token->type === TokenType::Keyword && $token->keyword !== 'LINES') {
break;
}
$ret->parseFileOptions($parser, $list, $token->keyword);
$state = 5;
}
}
--$list->idx;
return $ret;
}
}