1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser; |
6
|
|
|
|
7
|
|
|
use function class_exists; |
8
|
|
|
use function explode; |
9
|
|
|
use function in_array; |
10
|
|
|
use function intval; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_int; |
13
|
|
|
use function is_numeric; |
14
|
|
|
use function str_replace; |
15
|
|
|
use function str_starts_with; |
16
|
|
|
use function strlen; |
17
|
|
|
use function strtoupper; |
18
|
|
|
use function substr; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Defines a context class that is later extended to define other contexts. |
22
|
|
|
* |
23
|
|
|
* A context is a collection of keywords, operators and functions used for parsing. |
24
|
|
|
* |
25
|
|
|
* Holds the configuration of the context that is currently used. |
26
|
|
|
*/ |
27
|
|
|
abstract class Context |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The maximum length of a keyword. |
31
|
|
|
*/ |
32
|
|
|
public const KEYWORD_MAX_LENGTH = 30; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The maximum length of a label. |
36
|
|
|
* |
37
|
|
|
* Ref: https://dev.mysql.com/doc/refman/5.7/en/statement-labels.html |
38
|
|
|
*/ |
39
|
|
|
public const LABEL_MAX_LENGTH = 16; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The maximum length of an operator. |
43
|
|
|
*/ |
44
|
|
|
public const OPERATOR_MAX_LENGTH = 4; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The name of the default content. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public static $defaultContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The name of the loaded context. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public static $loadedContext = '\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The prefix concatenated to the context name when an incomplete class name |
62
|
|
|
* is specified. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public static $contextPrefix = '\\PhpMyAdmin\\SqlParser\\Contexts\\Context'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* List of keywords. |
70
|
|
|
* |
71
|
|
|
* Because, PHP's associative arrays are basically hash tables, it is more |
72
|
|
|
* efficient to store keywords as keys instead of values. |
73
|
|
|
* |
74
|
|
|
* The value associated to each keyword represents its flags. |
75
|
|
|
* |
76
|
|
|
* @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED |
77
|
|
|
* Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY |
78
|
|
|
* Token::FLAG_KEYWORD_FUNCTION |
79
|
|
|
* |
80
|
|
|
* Elements are sorted by flags, length and keyword. |
81
|
|
|
* |
82
|
|
|
* @var array<string,int> |
83
|
|
|
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int> |
84
|
|
|
*/ |
85
|
|
|
public static $keywords = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* List of operators and their flags. |
89
|
|
|
* |
90
|
|
|
* @var array<string, int> |
91
|
|
|
*/ |
92
|
|
|
public static $operators = [ |
93
|
|
|
// Some operators (*, =) may have ambiguous flags, because they depend on |
94
|
|
|
// the context they are being used in. |
95
|
|
|
// For example: 1. SELECT * FROM table; # SQL specific (wildcard) |
96
|
|
|
// SELECT 2 * 3; # arithmetic |
97
|
|
|
// 2. SELECT * FROM table WHERE foo = 'bar'; |
98
|
|
|
// SET @i = 0; |
99
|
|
|
|
100
|
|
|
// @see Token::FLAG_OPERATOR_ARITHMETIC |
101
|
|
|
'%' => 1, |
102
|
|
|
'*' => 1, |
103
|
|
|
'+' => 1, |
104
|
|
|
'-' => 1, |
105
|
|
|
'/' => 1, |
106
|
|
|
|
107
|
|
|
// @see Token::FLAG_OPERATOR_LOGICAL |
108
|
|
|
'!' => 2, |
109
|
|
|
'!=' => 2, |
110
|
|
|
'&&' => 2, |
111
|
|
|
'<' => 2, |
112
|
|
|
'<=' => 2, |
113
|
|
|
'<=>' => 2, |
114
|
|
|
'<>' => 2, |
115
|
|
|
'=' => 2, |
116
|
|
|
'>' => 2, |
117
|
|
|
'>=' => 2, |
118
|
|
|
'||' => 2, |
119
|
|
|
|
120
|
|
|
// @see Token::FLAG_OPERATOR_BITWISE |
121
|
|
|
'&' => 4, |
122
|
|
|
'<<' => 4, |
123
|
|
|
'>>' => 4, |
124
|
|
|
'^' => 4, |
125
|
|
|
'|' => 4, |
126
|
|
|
'~' => 4, |
127
|
|
|
|
128
|
|
|
// @see Token::FLAG_OPERATOR_ASSIGNMENT |
129
|
|
|
':=' => 8, |
130
|
|
|
|
131
|
|
|
// @see Token::FLAG_OPERATOR_SQL |
132
|
|
|
'(' => 16, |
133
|
|
|
')' => 16, |
134
|
|
|
'.' => 16, |
135
|
|
|
',' => 16, |
136
|
|
|
';' => 16, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* The mode of the MySQL server that will be used in lexing, parsing and building the statements. |
141
|
|
|
* |
142
|
|
|
* @internal use the {@see Context::getMode()} method instead. |
143
|
|
|
* |
144
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html |
145
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/ |
146
|
|
|
* |
147
|
|
|
* @var int |
148
|
|
|
*/ |
149
|
|
|
public static $mode = self::SQL_MODE_NONE; |
150
|
|
|
|
151
|
|
|
public const SQL_MODE_NONE = 0; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_allow_invalid_dates |
155
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#allow_invalid_dates |
156
|
|
|
*/ |
157
|
|
|
public const SQL_MODE_ALLOW_INVALID_DATES = 1; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_ansi_quotes |
161
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#ansi_quotes |
162
|
|
|
*/ |
163
|
|
|
public const SQL_MODE_ANSI_QUOTES = 2; |
164
|
|
|
|
165
|
|
|
/** Compatibility mode for Microsoft's SQL server. This is the equivalent of {@see SQL_MODE_ANSI_QUOTES}. */ |
166
|
|
|
public const SQL_MODE_COMPAT_MYSQL = 2; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_error_for_division_by_zero |
170
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#error_for_division_by_zero |
171
|
|
|
*/ |
172
|
|
|
public const SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO = 4; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_high_not_precedence |
176
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#high_not_precedence |
177
|
|
|
*/ |
178
|
|
|
public const SQL_MODE_HIGH_NOT_PRECEDENCE = 8; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_ignore_space |
182
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#ignore_space |
183
|
|
|
*/ |
184
|
|
|
public const SQL_MODE_IGNORE_SPACE = 16; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_auto_create_user |
188
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_auto_create_user |
189
|
|
|
*/ |
190
|
|
|
public const SQL_MODE_NO_AUTO_CREATE_USER = 32; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_auto_value_on_zero |
194
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_auto_value_on_zero |
195
|
|
|
*/ |
196
|
|
|
public const SQL_MODE_NO_AUTO_VALUE_ON_ZERO = 64; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_backslash_escapes |
200
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_backslash_escapes |
201
|
|
|
*/ |
202
|
|
|
public const SQL_MODE_NO_BACKSLASH_ESCAPES = 128; |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_dir_in_create |
206
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_dir_in_create |
207
|
|
|
*/ |
208
|
|
|
public const SQL_MODE_NO_DIR_IN_CREATE = 256; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_engine_substitution |
212
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_engine_substitution |
213
|
|
|
*/ |
214
|
|
|
public const SQL_MODE_NO_ENGINE_SUBSTITUTION = 512; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_field_options |
218
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_field_options |
219
|
|
|
*/ |
220
|
|
|
public const SQL_MODE_NO_FIELD_OPTIONS = 1024; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_key_options |
224
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_key_options |
225
|
|
|
*/ |
226
|
|
|
public const SQL_MODE_NO_KEY_OPTIONS = 2048; |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_table_options |
230
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_table_options |
231
|
|
|
*/ |
232
|
|
|
public const SQL_MODE_NO_TABLE_OPTIONS = 4096; |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_unsigned_subtraction |
236
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_unsigned_subtraction |
237
|
|
|
*/ |
238
|
|
|
public const SQL_MODE_NO_UNSIGNED_SUBTRACTION = 8192; |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_zero_date |
242
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_zero_date |
243
|
|
|
*/ |
244
|
|
|
public const SQL_MODE_NO_ZERO_DATE = 16384; |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_zero_in_date |
248
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#no_zero_in_date |
249
|
|
|
*/ |
250
|
|
|
public const SQL_MODE_NO_ZERO_IN_DATE = 32768; |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_only_full_group_by |
254
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#only_full_group_by |
255
|
|
|
*/ |
256
|
|
|
public const SQL_MODE_ONLY_FULL_GROUP_BY = 65536; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_pipes_as_concat |
260
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#pipes_as_concat |
261
|
|
|
*/ |
262
|
|
|
public const SQL_MODE_PIPES_AS_CONCAT = 131072; |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_real_as_float |
266
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#real_as_float |
267
|
|
|
*/ |
268
|
|
|
public const SQL_MODE_REAL_AS_FLOAT = 262144; |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_strict_all_tables |
272
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#strict_all_tables |
273
|
|
|
*/ |
274
|
|
|
public const SQL_MODE_STRICT_ALL_TABLES = 524288; |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_strict_trans_tables |
278
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#strict_trans_tables |
279
|
|
|
*/ |
280
|
|
|
public const SQL_MODE_STRICT_TRANS_TABLES = 1048576; |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Custom mode. |
284
|
|
|
* The table and column names and any other field that must be escaped will not be. |
285
|
|
|
* Reserved keywords are being escaped regardless this mode is used or not. |
286
|
|
|
*/ |
287
|
|
|
public const SQL_MODE_NO_ENCLOSING_QUOTES = 1073741824; |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Equivalent to {@see SQL_MODE_REAL_AS_FLOAT}, {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, |
291
|
|
|
* {@see SQL_MODE_IGNORE_SPACE}. |
292
|
|
|
* |
293
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi |
294
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#ansi |
295
|
|
|
*/ |
296
|
|
|
public const SQL_MODE_ANSI = 393234; |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE}, |
300
|
|
|
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}. |
301
|
|
|
* |
302
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_db2 |
303
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#db2 |
304
|
|
|
*/ |
305
|
|
|
public const SQL_MODE_DB2 = 138258; |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE}, |
309
|
|
|
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}, |
310
|
|
|
* {@see SQL_MODE_NO_AUTO_CREATE_USER}. |
311
|
|
|
* |
312
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_maxdb |
313
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#maxdb |
314
|
|
|
*/ |
315
|
|
|
public const SQL_MODE_MAXDB = 138290; |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE}, |
319
|
|
|
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}. |
320
|
|
|
* |
321
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_mssql |
322
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#mssql |
323
|
|
|
*/ |
324
|
|
|
public const SQL_MODE_MSSQL = 138258; |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE}, |
328
|
|
|
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}, |
329
|
|
|
* {@see SQL_MODE_NO_AUTO_CREATE_USER}. |
330
|
|
|
* |
331
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_oracle |
332
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#oracle |
333
|
|
|
*/ |
334
|
|
|
public const SQL_MODE_ORACLE = 138290; |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE}, |
338
|
|
|
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}. |
339
|
|
|
* |
340
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_postgresql |
341
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#postgresql |
342
|
|
|
*/ |
343
|
|
|
public const SQL_MODE_POSTGRESQL = 138258; |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Equivalent to {@see SQL_MODE_STRICT_TRANS_TABLES}, {@see SQL_MODE_STRICT_ALL_TABLES}, |
347
|
|
|
* {@see SQL_MODE_NO_ZERO_IN_DATE}, {@see SQL_MODE_NO_ZERO_DATE}, {@see SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO}, |
348
|
|
|
* {@see SQL_MODE_NO_AUTO_CREATE_USER}. |
349
|
|
|
* |
350
|
|
|
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_traditional |
351
|
|
|
* @link https://mariadb.com/kb/en/sql-mode/#traditional |
352
|
|
|
*/ |
353
|
|
|
public const SQL_MODE_TRADITIONAL = 1622052; |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Checks if the given string is a keyword. |
357
|
|
|
* |
358
|
|
|
* @param bool $isReserved checks if the keyword is reserved |
359
|
|
|
*/ |
360
|
1380 |
|
public static function isKeyword(string $string, bool $isReserved = false): int|null |
361
|
|
|
{ |
362
|
1380 |
|
$upperString = strtoupper($string); |
363
|
|
|
|
364
|
|
|
if ( |
365
|
1380 |
|
! isset(static::$keywords[$upperString]) |
366
|
1380 |
|
|| ($isReserved && ! (static::$keywords[$upperString] & Token::FLAG_KEYWORD_RESERVED)) |
367
|
|
|
) { |
368
|
1380 |
|
return null; |
369
|
|
|
} |
370
|
|
|
|
371
|
1352 |
|
return static::$keywords[$upperString]; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Checks if the given string is an operator and returns the appropriate flag for the operator. |
376
|
|
|
*/ |
377
|
1396 |
|
public static function isOperator(string $string): int|null |
378
|
|
|
{ |
379
|
1396 |
|
return static::$operators[$string] ?? null; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Checks if the given character is a whitespace. |
384
|
|
|
*/ |
385
|
1404 |
|
public static function isWhitespace(string $string): bool |
386
|
|
|
{ |
387
|
1404 |
|
return $string === ' ' || $string === "\r" || $string === "\n" || $string === "\t"; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Checks if the given string is the beginning of a whitespace. |
392
|
|
|
* |
393
|
|
|
* @return int|null the appropriate flag for the comment type |
394
|
|
|
*/ |
395
|
1396 |
|
public static function isComment(string $string, bool $end = false): int|null |
396
|
|
|
{ |
397
|
1396 |
|
if ($string === '') { |
398
|
2 |
|
return null; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
// If comment is Bash style (#): |
402
|
1396 |
|
if (str_starts_with($string, '#')) { |
403
|
8 |
|
return Token::FLAG_COMMENT_BASH; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
// If comment is a MySQL command |
407
|
1396 |
|
if (str_starts_with($string, '/*!')) { |
408
|
|
|
return Token::FLAG_COMMENT_MYSQL_CMD; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
// If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict |
412
|
|
|
// with wildcard and a real opening C style. |
413
|
|
|
// It would look like the following valid SQL statement: "SELECT */* comment */ FROM...". |
414
|
1396 |
|
if (str_starts_with($string, '/*') || str_starts_with($string, '*/')) { |
415
|
102 |
|
return Token::FLAG_COMMENT_C; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
// If comment is SQL style (--\s?): |
419
|
|
|
if ( |
420
|
1396 |
|
str_starts_with($string, '-- ') |
421
|
1396 |
|
|| str_starts_with($string, "--\r") |
422
|
1396 |
|
|| str_starts_with($string, "--\n") |
423
|
1396 |
|
|| str_starts_with($string, "--\t") |
424
|
1396 |
|
|| ($string === '--' && $end) |
425
|
|
|
) { |
426
|
66 |
|
return Token::FLAG_COMMENT_SQL; |
427
|
|
|
} |
428
|
|
|
|
429
|
1396 |
|
return null; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Checks if the given string is a boolean value. |
434
|
|
|
* This actually check only for `TRUE` and `FALSE` because `1` or `0` are |
435
|
|
|
* actually numbers and are parsed by specific methods. |
436
|
|
|
*/ |
437
|
1380 |
|
public static function isBool(string $string): bool |
438
|
|
|
{ |
439
|
1380 |
|
$upperString = strtoupper($string); |
440
|
|
|
|
441
|
1380 |
|
return $upperString === 'TRUE' || $upperString === 'FALSE'; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Checks if the given character can be a part of a number. |
446
|
|
|
*/ |
447
|
2 |
|
public static function isNumber(string $string): bool |
448
|
|
|
{ |
449
|
2 |
|
return in_array($string, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', '+', 'e', 'E'], true); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Checks if the given character is the beginning of a symbol. A symbol |
454
|
|
|
* can be either a variable or a field name. |
455
|
|
|
* |
456
|
|
|
* @return int|null the appropriate flag for the symbol type |
457
|
|
|
*/ |
458
|
1380 |
|
public static function isSymbol(string $string): int|null |
459
|
|
|
{ |
460
|
1380 |
|
if ($string === '') { |
461
|
2 |
|
return null; |
462
|
|
|
} |
463
|
|
|
|
464
|
1380 |
|
if (str_starts_with($string, '@')) { |
465
|
122 |
|
return Token::FLAG_SYMBOL_VARIABLE; |
466
|
|
|
} |
467
|
|
|
|
468
|
1378 |
|
if (str_starts_with($string, '`')) { |
469
|
350 |
|
return Token::FLAG_SYMBOL_BACKTICK; |
470
|
|
|
} |
471
|
|
|
|
472
|
1378 |
|
if (str_starts_with($string, ':') || str_starts_with($string, '?')) { |
473
|
6 |
|
return Token::FLAG_SYMBOL_PARAMETER; |
474
|
|
|
} |
475
|
|
|
|
476
|
1378 |
|
return null; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Checks if the given character is the beginning of a string. |
481
|
|
|
* |
482
|
|
|
* @param string $string string to be checked |
483
|
|
|
* |
484
|
|
|
* @return int|null the appropriate flag for the string type |
485
|
|
|
*/ |
486
|
1380 |
|
public static function isString(string $string): int|null |
487
|
|
|
{ |
488
|
1380 |
|
if ($string === '') { |
489
|
2 |
|
return null; |
490
|
|
|
} |
491
|
|
|
|
492
|
1380 |
|
if (str_starts_with($string, '\'')) { |
493
|
324 |
|
return Token::FLAG_STRING_SINGLE_QUOTES; |
494
|
|
|
} |
495
|
|
|
|
496
|
1380 |
|
if (str_starts_with($string, '"')) { |
497
|
152 |
|
return Token::FLAG_STRING_DOUBLE_QUOTES; |
498
|
|
|
} |
499
|
|
|
|
500
|
1380 |
|
return null; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Checks if the given character can be a separator for two lexeme. |
505
|
|
|
* |
506
|
|
|
* @param string $string string to be checked |
507
|
|
|
*/ |
508
|
1380 |
|
public static function isSeparator(string $string): bool |
509
|
|
|
{ |
510
|
|
|
// NOTES: Only non-alphanumeric ASCII characters may be separators. |
511
|
|
|
// `~` is the last printable ASCII character. |
512
|
1380 |
|
return $string <= '~' |
513
|
1380 |
|
&& $string !== '_' |
514
|
1380 |
|
&& $string !== '$' |
515
|
1380 |
|
&& ($string < '0' || $string > '9') |
516
|
1380 |
|
&& ($string < 'a' || $string > 'z') |
517
|
1380 |
|
&& ($string < 'A' || $string > 'Z'); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Loads the specified context. |
522
|
|
|
* |
523
|
|
|
* Contexts may be used by accessing the context directly. |
524
|
|
|
* |
525
|
|
|
* @param string $context name of the context or full class name that defines the context |
526
|
|
|
* |
527
|
|
|
* @return bool true if the context was loaded, false otherwise |
528
|
|
|
*/ |
529
|
1744 |
|
public static function load(string $context = ''): bool |
530
|
|
|
{ |
531
|
1744 |
|
if (empty($context)) { |
532
|
1744 |
|
$context = self::$defaultContext; |
533
|
|
|
} |
534
|
|
|
|
535
|
1744 |
|
if ($context[0] !== '\\') { |
536
|
|
|
// Short context name (must be formatted into class name). |
537
|
46 |
|
$context = self::$contextPrefix . $context; |
538
|
|
|
} |
539
|
|
|
|
540
|
1744 |
|
if (! class_exists($context)) { |
541
|
12 |
|
return false; |
542
|
|
|
} |
543
|
|
|
|
544
|
1744 |
|
self::$loadedContext = $context; |
545
|
1744 |
|
self::$keywords = $context::$keywords; |
|
|
|
|
546
|
|
|
|
547
|
1744 |
|
return true; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Loads the context with the closest version to the one specified. |
552
|
|
|
* |
553
|
|
|
* The closest context is found by replacing last digits with zero until one |
554
|
|
|
* is loaded successfully. |
555
|
|
|
* |
556
|
|
|
* @see Context::load() |
557
|
|
|
* |
558
|
|
|
* @param string $context name of the context or full class name that defines the context |
559
|
|
|
* |
560
|
|
|
* @return string|null The loaded context. `null` if no context was loaded. |
561
|
|
|
*/ |
562
|
14 |
|
public static function loadClosest(string $context = ''): string|null |
563
|
|
|
{ |
564
|
14 |
|
$length = strlen($context); |
565
|
14 |
|
for ($i = $length; $i > 0;) { |
566
|
|
|
/* Trying to load the new context */ |
567
|
14 |
|
if (static::load($context)) { |
568
|
12 |
|
return $context; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/* Replace last two non zero digits by zeroes */ |
572
|
|
|
do { |
573
|
10 |
|
$i -= 2; |
574
|
10 |
|
$part = substr($context, $i, 2); |
575
|
|
|
/* No more numeric parts to strip */ |
576
|
10 |
|
if (! is_numeric($part)) { |
577
|
6 |
|
break 2; |
578
|
|
|
} |
579
|
8 |
|
} while (intval($part) === 0 && $i > 0); |
580
|
|
|
|
581
|
8 |
|
$context = substr($context, 0, $i) . '00' . substr($context, $i + 2); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/* Fallback to loading at least matching engine */ |
585
|
6 |
|
if (str_starts_with($context, 'MariaDb')) { |
586
|
2 |
|
return static::loadClosest('MariaDb100300'); |
587
|
|
|
} |
588
|
|
|
|
589
|
4 |
|
if (str_starts_with($context, 'MySql')) { |
590
|
2 |
|
return static::loadClosest('MySql50700'); |
591
|
|
|
} |
592
|
|
|
|
593
|
2 |
|
return null; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Gets the SQL mode. |
598
|
|
|
*/ |
599
|
72 |
|
public static function getMode(): int |
600
|
|
|
{ |
601
|
72 |
|
return static::$mode; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Sets the SQL mode. |
606
|
|
|
* |
607
|
|
|
* @param int|string $mode |
608
|
|
|
* |
609
|
|
|
* @return void |
610
|
|
|
*/ |
611
|
898 |
|
public static function setMode($mode = self::SQL_MODE_NONE) |
612
|
|
|
{ |
613
|
898 |
|
if (is_int($mode)) { |
614
|
832 |
|
static::$mode = $mode; |
615
|
|
|
|
616
|
832 |
|
return; |
617
|
|
|
} |
618
|
|
|
|
619
|
66 |
|
static::$mode = self::SQL_MODE_NONE; |
620
|
66 |
|
if ($mode === '') { |
621
|
4 |
|
return; |
622
|
|
|
} |
623
|
|
|
|
624
|
64 |
|
$modes = explode(',', $mode); |
625
|
64 |
|
foreach ($modes as $sqlMode) { |
626
|
64 |
|
static::$mode |= self::getModeFromString($sqlMode); |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* @psalm-suppress MixedReturnStatement, MixedInferredReturnType Is caused by the LSB of the constants |
632
|
|
|
*/ |
633
|
64 |
|
private static function getModeFromString(string $mode): int |
634
|
|
|
{ |
635
|
64 |
|
return match ($mode) { |
636
|
64 |
|
'ALLOW_INVALID_DATES' => self::SQL_MODE_ALLOW_INVALID_DATES, |
637
|
64 |
|
'ANSI_QUOTES' => self::SQL_MODE_ANSI_QUOTES, |
638
|
64 |
|
'COMPAT_MYSQL' => self::SQL_MODE_COMPAT_MYSQL, |
639
|
64 |
|
'ERROR_FOR_DIVISION_BY_ZERO' => self::SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO, |
640
|
64 |
|
'HIGH_NOT_PRECEDENCE' => self::SQL_MODE_HIGH_NOT_PRECEDENCE, |
641
|
64 |
|
'IGNORE_SPACE' => self::SQL_MODE_IGNORE_SPACE, |
642
|
64 |
|
'NO_AUTO_CREATE_USER' => self::SQL_MODE_NO_AUTO_CREATE_USER, |
643
|
64 |
|
'NO_AUTO_VALUE_ON_ZERO' => self::SQL_MODE_NO_AUTO_VALUE_ON_ZERO, |
644
|
64 |
|
'NO_BACKSLASH_ESCAPES' => self::SQL_MODE_NO_BACKSLASH_ESCAPES, |
645
|
64 |
|
'NO_DIR_IN_CREATE' => self::SQL_MODE_NO_DIR_IN_CREATE, |
646
|
64 |
|
'NO_ENGINE_SUBSTITUTION' => self::SQL_MODE_NO_ENGINE_SUBSTITUTION, |
647
|
64 |
|
'NO_FIELD_OPTIONS' => self::SQL_MODE_NO_FIELD_OPTIONS, |
648
|
64 |
|
'NO_KEY_OPTIONS' => self::SQL_MODE_NO_KEY_OPTIONS, |
649
|
64 |
|
'NO_TABLE_OPTIONS' => self::SQL_MODE_NO_TABLE_OPTIONS, |
650
|
64 |
|
'NO_UNSIGNED_SUBTRACTION' => self::SQL_MODE_NO_UNSIGNED_SUBTRACTION, |
651
|
64 |
|
'NO_ZERO_DATE' => self::SQL_MODE_NO_ZERO_DATE, |
652
|
64 |
|
'NO_ZERO_IN_DATE' => self::SQL_MODE_NO_ZERO_IN_DATE, |
653
|
64 |
|
'ONLY_FULL_GROUP_BY' => self::SQL_MODE_ONLY_FULL_GROUP_BY, |
654
|
64 |
|
'PIPES_AS_CONCAT' => self::SQL_MODE_PIPES_AS_CONCAT, |
655
|
64 |
|
'REAL_AS_FLOAT' => self::SQL_MODE_REAL_AS_FLOAT, |
656
|
64 |
|
'STRICT_ALL_TABLES' => self::SQL_MODE_STRICT_ALL_TABLES, |
657
|
64 |
|
'STRICT_TRANS_TABLES' => self::SQL_MODE_STRICT_TRANS_TABLES, |
658
|
64 |
|
'NO_ENCLOSING_QUOTES' => self::SQL_MODE_NO_ENCLOSING_QUOTES, |
659
|
64 |
|
'ANSI' => self::SQL_MODE_ANSI, |
660
|
64 |
|
'DB2' => self::SQL_MODE_DB2, |
661
|
64 |
|
'MAXDB' => self::SQL_MODE_MAXDB, |
662
|
64 |
|
'MSSQL' => self::SQL_MODE_MSSQL, |
663
|
64 |
|
'ORACLE' => self::SQL_MODE_ORACLE, |
664
|
64 |
|
'POSTGRESQL' => self::SQL_MODE_POSTGRESQL, |
665
|
64 |
|
'TRADITIONAL' => self::SQL_MODE_TRADITIONAL, |
666
|
64 |
|
default => self::SQL_MODE_NONE, |
667
|
64 |
|
}; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Escapes the symbol by adding surrounding backticks. |
672
|
|
|
* |
673
|
|
|
* @param string[]|string $str the string to be escaped |
674
|
|
|
* @param string $quote quote to be used when escaping |
675
|
|
|
* |
676
|
|
|
* @return string|string[] |
677
|
|
|
*/ |
678
|
140 |
|
public static function escape($str, string $quote = '`') |
679
|
|
|
{ |
680
|
140 |
|
if (is_array($str)) { |
681
|
30 |
|
foreach ($str as $key => $value) { |
682
|
30 |
|
$str[$key] = static::escape($value); |
683
|
|
|
} |
684
|
|
|
|
685
|
30 |
|
return $str; |
686
|
|
|
} |
687
|
|
|
|
688
|
140 |
|
if ((static::$mode & self::SQL_MODE_NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) { |
|
|
|
|
689
|
2 |
|
return $str; |
690
|
|
|
} |
691
|
|
|
|
692
|
140 |
|
if (static::$mode & self::SQL_MODE_ANSI_QUOTES) { |
693
|
2 |
|
$quote = '"'; |
694
|
|
|
} |
695
|
|
|
|
696
|
140 |
|
return $quote . str_replace($quote, $quote . $quote, $str) . $quote; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* Returns char used to quote identifiers based on currently set SQL Mode (ie. standard or ANSI_QUOTES) |
701
|
|
|
* |
702
|
|
|
* @return string either " (double quote, ansi_quotes mode) or ` (backtick, standard mode) |
703
|
|
|
*/ |
704
|
|
|
public static function getIdentifierQuote(): string |
705
|
|
|
{ |
706
|
|
|
return self::hasMode(self::SQL_MODE_ANSI_QUOTES) ? '"' : '`'; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Function verifies that given SQL Mode constant is currently set |
711
|
|
|
* |
712
|
|
|
* @param int $flag for example {@see Context::SQL_MODE_ANSI_QUOTES} |
713
|
|
|
* |
714
|
|
|
* @return bool false on empty param, true/false on given constant/int value |
715
|
|
|
*/ |
716
|
4 |
|
public static function hasMode(int|null $flag = null): bool |
717
|
|
|
{ |
718
|
4 |
|
if (empty($flag)) { |
719
|
|
|
return false; |
720
|
|
|
} |
721
|
|
|
|
722
|
4 |
|
return (self::$mode & $flag) === $flag; |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|