-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathIntoKeyword.php
More file actions
173 lines (148 loc) · 4.54 KB
/
IntoKeyword.php
File metadata and controls
173 lines (148 loc) · 4.54 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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Parsers\Expressions;
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function trim;
/**
* `INTO` keyword parser.
*/
final class IntoKeyword implements Component
{
/**
* FIELDS/COLUMNS Options for `SELECT...INTO` statements.
*/
private const STATEMENT_FIELDS_OPTIONS = [
'TERMINATED BY' => [
1,
'expr',
],
'OPTIONALLY' => 2,
'ENCLOSED BY' => [
3,
'expr',
],
'ESCAPED BY' => [
4,
'expr',
],
];
/**
* LINES Options for `SELECT...INTO` statements.
*/
private const STATEMENT_LINES_OPTIONS = [
'STARTING BY' => [
1,
'expr',
],
'TERMINATED BY' => [
2,
'expr',
],
];
/**
* Type of target (OUTFILE or SYMBOL).
*/
public string|null $type = null;
/**
* The destination, which can be a table or a file.
*/
public string|Expression|null $dest = null;
/**
* The name of the columns.
*
* @var string[]|null
*/
public array|null $columns = null;
/**
* The values to be selected into (SELECT .. INTO @var1).
*
* @var Expression[]|null
*/
public array|null $values = null;
/**
* Options for FIELDS/COLUMNS keyword.
*
* @see IntoKeyword::STATEMENT_FIELDS_OPTIONS
*/
public OptionsArray|null $fieldsOptions = null;
/**
* Whether to use `FIELDS` or `COLUMNS` while building.
*/
public bool|null $fieldsKeyword = null;
/**
* Options for OPTIONS keyword.
*
* @see IntoKeyword::STATEMENT_LINES_OPTIONS
*/
public OptionsArray|null $linesOptions = null;
/**
* @param string|null $type type of destination (may be OUTFILE)
* @param string|Expression|null $dest actual destination
* @param string[]|null $columns column list of destination
* @param Expression[]|null $values selected fields
* @param OptionsArray|null $fieldsOptions options for FIELDS/COLUMNS keyword
* @param bool|null $fieldsKeyword options for OPTIONS keyword
*/
public function __construct(
string|null $type = null,
string|Expression|null $dest = null,
array|null $columns = null,
array|null $values = null,
OptionsArray|null $fieldsOptions = null,
bool|null $fieldsKeyword = null,
) {
$this->type = $type;
$this->dest = $dest;
$this->columns = $columns;
$this->values = $values;
$this->fieldsOptions = $fieldsOptions;
$this->fieldsKeyword = $fieldsKeyword;
}
/**
* @param Parser $parser The parser
* @param TokensList $list A token list
* @param string $keyword The keyword
*/
public function parseFileOptions(Parser $parser, TokensList $list, string $keyword = 'FIELDS'): void
{
++$list->idx;
if ($keyword === 'FIELDS' || $keyword === 'COLUMNS') {
// parse field options
$this->fieldsOptions = OptionsArrays::parse($parser, $list, self::STATEMENT_FIELDS_OPTIONS);
$this->fieldsKeyword = ($keyword === 'FIELDS');
} else {
// parse line options
$this->linesOptions = OptionsArrays::parse($parser, $list, self::STATEMENT_LINES_OPTIONS);
}
}
public function build(): string
{
if ($this->dest instanceof Expression) {
$columns = ! empty($this->columns) ? '(`' . implode('`, `', $this->columns) . '`)' : '';
return $this->dest . $columns;
}
if (isset($this->values)) {
return Expressions::buildAll($this->values);
}
$ret = 'OUTFILE "' . $this->dest . '"';
$fieldsOptionsString = $this->fieldsOptions?->build() ?? '';
if (trim($fieldsOptionsString) !== '') {
$ret .= $this->fieldsKeyword ? ' FIELDS' : ' COLUMNS';
$ret .= ' ' . $fieldsOptionsString;
}
$linesOptionsString = $this->linesOptions?->build() ?? '';
if (trim($linesOptionsString) !== '') {
$ret .= ' LINES ' . $linesOptionsString;
}
return $ret;
}
public function __toString(): string
{
return $this->build();
}
}