1
|
|
|
<?php |
2
|
|
|
namespace Graze\Morphism\Parse; |
3
|
|
|
|
4
|
|
|
use RuntimeException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Represents a lexical token parsed from an SQL input stream. |
8
|
|
|
*/ |
9
|
|
|
class Token |
10
|
|
|
{ |
11
|
|
|
const SYMBOL = 'symbol'; |
12
|
|
|
const IDENTIFIER = 'identifier'; |
13
|
|
|
const STRING = 'string'; |
14
|
|
|
const NUMBER = 'number'; |
15
|
|
|
const BIN = 'bin'; |
16
|
|
|
const HEX = 'hex'; |
17
|
|
|
const WHITESPACE = 'whitespace'; |
18
|
|
|
const COMMENT = 'comment'; |
19
|
|
|
const CONDITIONAL_START = 'conditional-start'; |
20
|
|
|
const CONDITIONAL_END = 'conditional-end'; |
21
|
|
|
const EOF = 'EOF'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string 'symbol' | 'identifier' | 'string' | 'number' | 'bin' | |
25
|
|
|
* 'hex' | 'whitespace' | 'comment' | 'conditional-start' | |
26
|
|
|
* 'conditional-end' | 'EOF' |
27
|
|
|
*/ |
28
|
|
|
public $type; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string the textual content of the token |
32
|
|
|
*/ |
33
|
|
|
public $text; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
private static $quoteNames = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param string $type |
42
|
|
|
* @param string $text |
43
|
|
|
*/ |
44
|
646 |
|
public function __construct($type, $text = '') |
45
|
|
|
{ |
46
|
646 |
|
$this->type = $type; |
47
|
646 |
|
$this->text = (string) $text; |
48
|
646 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Controls the behaviour of escapeIdentifier(). |
52
|
|
|
* |
53
|
|
|
* If $quoteNames is true, future calls to escapeIdentifier will use |
54
|
|
|
* backquotes to delimit all identifiers. If false, no quoting will take |
55
|
|
|
* place. |
56
|
|
|
* |
57
|
|
|
* @param bool $quoteNames whether to quote identifiers |
58
|
|
|
*/ |
59
|
732 |
|
public static function setQuoteNames($quoteNames) |
60
|
|
|
{ |
61
|
732 |
|
self::$quoteNames = !!$quoteNames; |
62
|
732 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the current state of behaviour for escapeIdentifier(). |
66
|
|
|
* True indicate that identifiers will be delimited with backquotes; |
67
|
|
|
* false indicates that no quoting will take place. |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
732 |
|
public static function getQuoteNames() |
72
|
|
|
{ |
73
|
732 |
|
return self::$quoteNames; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns a string representation suitable for debug output. |
78
|
|
|
* e.g. 'identifier[pr_name]' |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
99 |
|
public function toDebugString() |
83
|
|
|
{ |
84
|
99 |
|
return "{$this->type}[{$this->text}]"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns true if this token represents end-of-file. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
25 |
|
public function isEof() |
93
|
|
|
{ |
94
|
25 |
|
return $this->type === self::EOF; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns true if the token has the specified type and text content |
99
|
|
|
* (case insensitive). |
100
|
|
|
* |
101
|
|
|
* @param string $type e.g. 'symbol' | 'identifier' | ... |
102
|
|
|
* @param string $text content of token |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
509 |
|
public function eq($type, $text) |
106
|
|
|
{ |
107
|
509 |
|
return ($this->type === $type) |
108
|
509 |
|
&& strcasecmp($this->text, $text) === 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates a string token by parsing the given string. |
113
|
|
|
* The supplied string should exclude the quote delimiters. |
114
|
|
|
* |
115
|
|
|
* @param string $string string to parse |
116
|
|
|
* @param string $quoteChar character that was used as the quote delimiter |
117
|
|
|
* @return Token |
118
|
|
|
*/ |
119
|
59 |
|
public static function fromString($string, $quoteChar) |
120
|
|
|
{ |
121
|
59 |
|
$text = ''; |
122
|
59 |
|
$n = strlen($string); |
123
|
59 |
|
for ($i = 0; $i < $n; ++$i) { |
124
|
54 |
|
$ch = $string[$i]; |
125
|
54 |
|
if ($ch === $quoteChar) { |
126
|
3 |
|
++$i; |
127
|
|
|
} |
128
|
54 |
|
if ($ch === '\\') { |
129
|
6 |
|
++$i; |
130
|
6 |
|
$ch = $string[$i]; |
131
|
|
|
switch ($ch) { |
132
|
6 |
|
case '0': |
133
|
1 |
|
$ch = chr(0); |
134
|
1 |
|
break; |
135
|
6 |
|
case 'b': |
136
|
1 |
|
$ch = chr(8); |
137
|
1 |
|
break; |
138
|
6 |
|
case 'n': |
139
|
1 |
|
$ch = chr(10); |
140
|
1 |
|
break; |
141
|
6 |
|
case 'r': |
142
|
1 |
|
$ch = chr(13); |
143
|
1 |
|
break; |
144
|
6 |
|
case 't': |
145
|
1 |
|
$ch = chr(9); |
146
|
1 |
|
break; |
147
|
6 |
|
case 'z': |
148
|
1 |
|
$ch = chr(26); |
149
|
1 |
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
54 |
|
$text .= $ch; |
153
|
|
|
} |
154
|
59 |
|
return new self(self::STRING, $text); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Creates an identifier token by parsing the given string. |
159
|
|
|
* The supplied string should exclude any backquote delimiters. |
160
|
|
|
* |
161
|
|
|
* @param string $string string to parse |
162
|
|
|
* @return Token |
163
|
|
|
*/ |
164
|
15 |
|
public static function fromIdentifier($string) |
165
|
|
|
{ |
166
|
15 |
|
return new self('identifier', str_replace('``', '`', $string)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns a quote delimited, escaped string suitable for use in SQL. |
171
|
|
|
* If $string is null, returns `NULL`. |
172
|
|
|
* |
173
|
|
|
* @param string|null $value - value to convert |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
125 |
|
public static function escapeString($value) |
177
|
|
|
{ |
178
|
125 |
|
if (is_null($value)) { |
179
|
1 |
|
return 'NULL'; |
180
|
|
|
} |
181
|
124 |
|
$value = strtr( |
182
|
124 |
|
$value, |
183
|
|
|
[ |
184
|
124 |
|
"'" => "''", |
185
|
124 |
|
chr(0) => "\\0", |
186
|
124 |
|
chr(10) => "\\n", |
187
|
124 |
|
chr(13) => "\\r", |
188
|
124 |
|
"\\" => "\\\\", |
189
|
|
|
] |
190
|
|
|
); |
191
|
124 |
|
return "'$value'"; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns an identifier suitable for use as in SQL. |
196
|
|
|
* |
197
|
|
|
* If setQuoteNames(true) has previously been called, the identifier |
198
|
|
|
* will be delimited with backquotes, otherwise no delimiters will be |
199
|
|
|
* added. |
200
|
|
|
* |
201
|
|
|
* @param string $string text to use as the identifier name |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
303 |
|
public static function escapeIdentifier($string) |
205
|
|
|
{ |
206
|
303 |
|
if (!self::$quoteNames) { |
207
|
4 |
|
return $string; |
208
|
|
|
} |
209
|
299 |
|
$string = strtr($string, [ "`" => "``" ]); |
210
|
299 |
|
return "`" . $string . "`"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the string represented by the token. |
215
|
|
|
* |
216
|
|
|
* Tokens of type 'string' or 'number' will simply return the parsed text, |
217
|
|
|
* whereas 'hex' or 'bin' tokens will be reinterpreted as strings. E.g. |
218
|
|
|
* a 'hex' token generated from the sequence x'41424344' in the token |
219
|
|
|
* stream will be returned as 'ABCD'. |
220
|
|
|
* |
221
|
|
|
* An exception will be thrown for any other token type. |
222
|
|
|
* |
223
|
|
|
* @throws RuntimeException |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
20 |
|
public function asString() |
227
|
|
|
{ |
228
|
20 |
|
switch ($this->type) { |
229
|
20 |
|
case self::STRING: |
230
|
4 |
|
return $this->text; |
231
|
|
|
|
232
|
16 |
|
case self::NUMBER: |
233
|
9 |
|
preg_match('/^([+-]?)0*(.*)$/', $this->text, $pregMatch); |
234
|
9 |
|
list(, $sign, $value) = $pregMatch; |
235
|
9 |
|
if ($value == '') { |
236
|
1 |
|
$value = '0'; |
237
|
8 |
|
} elseif ($value[0] == '.') { |
238
|
1 |
|
$value = '0' . $value; |
239
|
|
|
} |
240
|
9 |
|
if ($sign == '-') { |
241
|
2 |
|
$value = $sign . $value; |
242
|
|
|
} |
243
|
9 |
|
return $value; |
244
|
|
|
|
245
|
7 |
|
case self::HEX: |
246
|
4 |
|
return pack('H*', $this->text); |
247
|
|
|
|
248
|
3 |
|
case self::BIN: |
249
|
2 |
|
$bytes = ''; |
250
|
2 |
|
for ($text = $this->text; $text !== ''; $text = substr($text, 0, -8)) { |
251
|
2 |
|
$bytes = chr(/** @scrutinizer ignore-type */ bindec(substr($text, -8))) . $bytes; |
252
|
|
|
} |
253
|
2 |
|
return $bytes; |
254
|
|
|
|
255
|
|
|
default: |
256
|
1 |
|
throw new RuntimeException("Expected string"); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return the token as a number. |
262
|
|
|
* |
263
|
|
|
* @return int|number |
264
|
|
|
*/ |
265
|
25 |
|
public function asNumber() |
266
|
|
|
{ |
267
|
25 |
|
switch ($this->type) { |
268
|
25 |
|
case self::NUMBER: |
269
|
17 |
|
return 1 * $this->text; |
270
|
|
|
|
271
|
8 |
|
case self::STRING: |
272
|
3 |
|
if (is_numeric($this->text)) { |
273
|
2 |
|
return 1 * $this->text; |
274
|
|
|
} else { |
275
|
1 |
|
throw new RuntimeException("Not a valid number: {$this->text}"); |
276
|
|
|
} |
277
|
|
|
|
278
|
5 |
|
case self::HEX: |
279
|
2 |
|
return hexdec($this->text); |
280
|
|
|
|
281
|
3 |
|
case self::BIN: |
282
|
2 |
|
return bindec($this->text); |
283
|
|
|
|
284
|
|
|
default: |
285
|
1 |
|
throw new RuntimeException("Expected a number"); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Return the token value as a date string. |
291
|
|
|
* |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
10 |
|
public function asDate() |
295
|
|
|
{ |
296
|
10 |
|
$text = $this->text; |
297
|
10 |
|
if ($text === '0') { |
298
|
2 |
|
return '0000-00-00'; |
299
|
|
|
} |
300
|
|
|
// MySQL actually recognises a bunch of other date formats, |
301
|
|
|
// but YYYY-MM-DD is the only one we're prepared to accept. |
302
|
8 |
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $text, $pregMatch)) { |
303
|
4 |
|
return $text; |
304
|
|
|
} else { |
305
|
4 |
|
throw new RuntimeException("Expected a date"); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Return the token as a timestamp string. |
311
|
|
|
* |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
9 |
|
public function asTime() |
315
|
|
|
{ |
316
|
9 |
|
$text = $this->text; |
317
|
9 |
|
if ($text === '0') { |
318
|
2 |
|
return '00:00:00'; |
319
|
|
|
} |
320
|
|
|
// MySQL actually recognises a bunch of other time formats, |
321
|
|
|
// but HH:MM:SS is the only one we're prepared to accept. |
322
|
7 |
|
if (preg_match('/^\d{2}:\d{2}:\d{2}$/', $text)) { |
323
|
3 |
|
return $text; |
324
|
|
|
} else { |
325
|
4 |
|
throw new RuntimeException("Expected a time"); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Return the token as a date and time string. |
331
|
|
|
* |
332
|
|
|
* @return string |
333
|
|
|
*/ |
334
|
17 |
|
public function asDateTime() |
335
|
|
|
{ |
336
|
17 |
|
$text = $this->text; |
337
|
17 |
|
if ($text === '0') { |
338
|
3 |
|
return '0000-00-00 00:00:00'; |
339
|
|
|
} |
340
|
|
|
// MySQL actually recognises a bunch of other datetime formats, |
341
|
|
|
// but YYYY-MM-DD and YYYY-MM-DD HH:MM:SS are the only ones we're |
342
|
|
|
// prepared to accept. |
343
|
14 |
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $text)) { |
344
|
3 |
|
return "$text 00:00:00"; |
345
|
|
|
} |
346
|
11 |
|
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $text)) { |
347
|
6 |
|
return $text; |
348
|
|
|
} |
349
|
5 |
|
throw new RuntimeException("Bad datetime"); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|