FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sql-parser/src/Context.php at master · phpmyadmin/sql-parser · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
phpmyadmin
/
sql-parser
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
119
Star
484
Code
Issues
58
Pull requests
14
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
sql-parser
/
src
/
Context.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
677 lines (594 loc) · 22.7 KB
Breadcrumbs
sql-parser
/
src
/
Context.php
Copy path
File metadata and controls
677 lines (594 loc) · 22.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
<?php
declare
(strict_types=
1
);
namespace
PhpMyAdmin
\
SqlParser
;
use
PhpMyAdmin
\
SqlParser
\
Contexts
\
ContextMySql50700
;
use
function
array_map
;
use
function
class_exists
;
use
function
explode
;
use
function
in_array
;
use
function
intval
;
use
function
is_int
;
use
function
is_numeric
;
use
function
preg_match
;
use
function
str_replace
;
use
function
str_starts_with
;
use
function
strlen
;
use
function
strtoupper
;
use
function
substr
;
/**
* Defines a context class that is later extended to define other contexts.
*
* A context is a collection of keywords, operators and functions used for parsing.
*
* Holds the configuration of the context that is currently used.
*/
final
class
Context
{
/**
* The maximum length of a keyword.
*/
public
const
KEYWORD_MAX_LENGTH
=
30
;
/**
* The maximum length of a label.
*
* Ref: https://dev.mysql.com/doc/refman/5.7/en/statement-labels.html
*/
public
const
LABEL_MAX_LENGTH
=
16
;
/**
* The maximum length of an operator.
*/
public
const
OPERATOR_MAX_LENGTH
=
4
;
/**
* The name of the loaded context.
*/
public
static
string
$
loadedContext
= ContextMySql50700::class;
/**
* The prefix concatenated to the context name when an incomplete class name
* is specified.
*/
private
const
CONTEXT_PREFIX
=
'
PhpMyAdmin
\\
SqlParser
\\
Contexts
\\
Context
'
;
/**
* List of keywords.
*
* Because, PHP's associative arrays are basically hash tables, it is more
* efficient to store keywords as keys instead of values.
*
* The value associated to each keyword represents its flags.
*
* @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED
* Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY
* Token::FLAG_KEYWORD_FUNCTION
*
* Elements are sorted by flags, length and keyword.
*
* @var array<string,int>
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
*/
public
static
array
$
keywords
= [];
/**
* List of operators and their flags.
*/
private
const
OPERATORS
= [
// Some operators (*, =) may have ambiguous flags, because they depend on
// the context they are being used in.
// For example: 1. SELECT * FROM table; # SQL specific (wildcard)
// SELECT 2 * 3; # arithmetic
// 2. SELECT * FROM table WHERE foo = 'bar';
// SET @i = 0;
// @see Token::FLAG_OPERATOR_ARITHMETIC
'
%
'
=>
1
,
'
*
'
=>
1
,
'
+
'
=>
1
,
'
-
'
=>
1
,
'
/
'
=>
1
,
// @see Token::FLAG_OPERATOR_LOGICAL
'
!
'
=>
2
,
'
!=
'
=>
2
,
'
&&
'
=>
2
,
'
<
'
=>
2
,
'
<=
'
=>
2
,
'
<=>
'
=>
2
,
'
<>
'
=>
2
,
'
=
'
=>
2
,
'
>
'
=>
2
,
'
>=
'
=>
2
,
'
||
'
=>
2
,
// @see Token::FLAG_OPERATOR_BITWISE
'
&
'
=>
4
,
'
<<
'
=>
4
,
'
>>
'
=>
4
,
'
^
'
=>
4
,
'
|
'
=>
4
,
'
~
'
=>
4
,
// @see Token::FLAG_OPERATOR_ASSIGNMENT
'
:=
'
=>
8
,
// @see Token::FLAG_OPERATOR_SQL
'
(
'
=>
16
,
'
)
'
=>
16
,
'
.
'
=>
16
,
'
,
'
=>
16
,
'
->
'
=>
16
,
'
->>
'
=>
16
,
'
;
'
=>
16
,
];
/**
* The mode of the MySQL server that will be used in lexing, parsing and building the statements.
*
* @internal use the {@see Context::getMode()} method instead.
*
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html
* @link https://mariadb.com/kb/en/sql-mode/
*/
public
static
int
$
mode
=
self
::
SQL_MODE_NONE
;
public
const
SQL_MODE_NONE
=
0
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_allow_invalid_dates
* @link https://mariadb.com/kb/en/sql-mode/#allow_invalid_dates
*/
public
const
SQL_MODE_ALLOW_INVALID_DATES
=
1
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_ansi_quotes
* @link https://mariadb.com/kb/en/sql-mode/#ansi_quotes
*/
public
const
SQL_MODE_ANSI_QUOTES
=
2
;
/** Compatibility mode for Microsoft's SQL server. This is the equivalent of {@see SQL_MODE_ANSI_QUOTES}. */
public
const
SQL_MODE_COMPAT_MYSQL
=
2
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_error_for_division_by_zero
* @link https://mariadb.com/kb/en/sql-mode/#error_for_division_by_zero
*/
public
const
SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO
=
4
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_high_not_precedence
* @link https://mariadb.com/kb/en/sql-mode/#high_not_precedence
*/
public
const
SQL_MODE_HIGH_NOT_PRECEDENCE
=
8
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_ignore_space
* @link https://mariadb.com/kb/en/sql-mode/#ignore_space
*/
public
const
SQL_MODE_IGNORE_SPACE
=
16
;
/**
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_auto_create_user
* @link https://mariadb.com/kb/en/sql-mode/#no_auto_create_user
*/
public
const
SQL_MODE_NO_AUTO_CREATE_USER
=
32
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_auto_value_on_zero
* @link https://mariadb.com/kb/en/sql-mode/#no_auto_value_on_zero
*/
public
const
SQL_MODE_NO_AUTO_VALUE_ON_ZERO
=
64
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_backslash_escapes
* @link https://mariadb.com/kb/en/sql-mode/#no_backslash_escapes
*/
public
const
SQL_MODE_NO_BACKSLASH_ESCAPES
=
128
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_dir_in_create
* @link https://mariadb.com/kb/en/sql-mode/#no_dir_in_create
*/
public
const
SQL_MODE_NO_DIR_IN_CREATE
=
256
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_engine_substitution
* @link https://mariadb.com/kb/en/sql-mode/#no_engine_substitution
*/
public
const
SQL_MODE_NO_ENGINE_SUBSTITUTION
=
512
;
/**
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_field_options
* @link https://mariadb.com/kb/en/sql-mode/#no_field_options
*/
public
const
SQL_MODE_NO_FIELD_OPTIONS
=
1024
;
/**
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_key_options
* @link https://mariadb.com/kb/en/sql-mode/#no_key_options
*/
public
const
SQL_MODE_NO_KEY_OPTIONS
=
2048
;
/**
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_table_options
* @link https://mariadb.com/kb/en/sql-mode/#no_table_options
*/
public
const
SQL_MODE_NO_TABLE_OPTIONS
=
4096
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_unsigned_subtraction
* @link https://mariadb.com/kb/en/sql-mode/#no_unsigned_subtraction
*/
public
const
SQL_MODE_NO_UNSIGNED_SUBTRACTION
=
8192
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_zero_date
* @link https://mariadb.com/kb/en/sql-mode/#no_zero_date
*/
public
const
SQL_MODE_NO_ZERO_DATE
=
16384
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_no_zero_in_date
* @link https://mariadb.com/kb/en/sql-mode/#no_zero_in_date
*/
public
const
SQL_MODE_NO_ZERO_IN_DATE
=
32768
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_only_full_group_by
* @link https://mariadb.com/kb/en/sql-mode/#only_full_group_by
*/
public
const
SQL_MODE_ONLY_FULL_GROUP_BY
=
65536
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_pipes_as_concat
* @link https://mariadb.com/kb/en/sql-mode/#pipes_as_concat
*/
public
const
SQL_MODE_PIPES_AS_CONCAT
=
131072
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_real_as_float
* @link https://mariadb.com/kb/en/sql-mode/#real_as_float
*/
public
const
SQL_MODE_REAL_AS_FLOAT
=
262144
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_strict_all_tables
* @link https://mariadb.com/kb/en/sql-mode/#strict_all_tables
*/
public
const
SQL_MODE_STRICT_ALL_TABLES
=
524288
;
/**
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_strict_trans_tables
* @link https://mariadb.com/kb/en/sql-mode/#strict_trans_tables
*/
public
const
SQL_MODE_STRICT_TRANS_TABLES
=
1048576
;
/**
* Custom mode.
* The table and column names and any other field that must be escaped will not be.
* Reserved keywords are being escaped regardless this mode is used or not.
*/
public
const
SQL_MODE_NO_ENCLOSING_QUOTES
=
1073741824
;
/**
* Equivalent to {@see SQL_MODE_REAL_AS_FLOAT}, {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES},
* {@see SQL_MODE_IGNORE_SPACE}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi
* @link https://mariadb.com/kb/en/sql-mode/#ansi
*/
public
const
SQL_MODE_ANSI
=
393234
;
/**
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE},
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_db2
* @link https://mariadb.com/kb/en/sql-mode/#db2
*/
public
const
SQL_MODE_DB2
=
138258
;
/**
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE},
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS},
* {@see SQL_MODE_NO_AUTO_CREATE_USER}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_maxdb
* @link https://mariadb.com/kb/en/sql-mode/#maxdb
*/
public
const
SQL_MODE_MAXDB
=
138290
;
/**
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE},
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_mssql
* @link https://mariadb.com/kb/en/sql-mode/#mssql
*/
public
const
SQL_MODE_MSSQL
=
138258
;
/**
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE},
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS},
* {@see SQL_MODE_NO_AUTO_CREATE_USER}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_oracle
* @link https://mariadb.com/kb/en/sql-mode/#oracle
*/
public
const
SQL_MODE_ORACLE
=
138290
;
/**
* Equivalent to {@see SQL_MODE_PIPES_AS_CONCAT}, {@see SQL_MODE_ANSI_QUOTES}, {@see SQL_MODE_IGNORE_SPACE},
* {@see SQL_MODE_NO_KEY_OPTIONS}, {@see SQL_MODE_NO_TABLE_OPTIONS}, {@see SQL_MODE_NO_FIELD_OPTIONS}.
*
* @link https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_postgresql
* @link https://mariadb.com/kb/en/sql-mode/#postgresql
*/
public
const
SQL_MODE_POSTGRESQL
=
138258
;
/**
* Equivalent to {@see SQL_MODE_STRICT_TRANS_TABLES}, {@see SQL_MODE_STRICT_ALL_TABLES},
* {@see SQL_MODE_NO_ZERO_IN_DATE}, {@see SQL_MODE_NO_ZERO_DATE}, {@see SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO},
* {@see SQL_MODE_NO_AUTO_CREATE_USER}.
*
* @link https://dev.mysql.com/doc/refman/en/sql-mode.html#sqlmode_traditional
* @link https://mariadb.com/kb/en/sql-mode/#traditional
*/
public
const
SQL_MODE_TRADITIONAL
=
1622052
;
/**
* Checks if the given string is a keyword.
*
* @param bool $isReserved checks if the keyword is reserved
*/
public
static
function
isKeyword
(
string
$
string
,
bool
$
isReserved
=
false
):
int
|
null
{
$
upperString
=
strtoupper
(
$
string
);
if
(
!
isset
(
static
::
$
keywords
[
$
upperString
])
|| (
$
isReserved
&& ! (
static
::
$
keywords
[
$
upperString
] & Token::
FLAG_KEYWORD_RESERVED
))
) {
return
null
;
}
return
static
::
$
keywords
[
$
upperString
];
}
/**
* Checks if the given string is an operator and returns the appropriate flag for the operator.
*/
public
static
function
isOperator
(
string
$
string
):
int
|
null
{
return
self
::
OPERATORS
[
$
string
] ??
null
;
}
/**
* Checks if the given character is a whitespace.
*/
public
static
function
isWhitespace
(
string
$
character
):
bool
{
return
match
(
$
character
) {
'
'
,
"\r"
,
"\n"
,
"\t"
=>
true
,
default
=>
false
,
};
}
/**
* Checks if the given string is the beginning of a whitespace.
*
* @return int|null the appropriate flag for the comment type
*/
public
static
function
isComment
(
string
$
string
,
bool
$
end
=
false
):
int
|
null
{
return
match
(
true
) {
str_starts_with
(
$
string
,
'
#
'
) => Token::
FLAG_COMMENT_BASH
,
str_starts_with
(
$
string
,
'
/*!
'
) => Token::
FLAG_COMMENT_MYSQL_CMD
,
// If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
// with wildcard and a real opening C style.
// It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
str_starts_with
(
$
string
,
'
/*
'
) ||
str_starts_with
(
$
string
,
'
*/
'
) => Token::
FLAG_COMMENT_C
,
str_starts_with
(
$
string
,
'
--
'
)
||
str_starts_with
(
$
string
,
"
--
\r"
)
||
str_starts_with
(
$
string
,
"
--
\n"
)
||
str_starts_with
(
$
string
,
"
--
\t"
)
|| (
$
string
===
'
--
'
&&
$
end
) => Token::
FLAG_COMMENT_SQL
,
default
=>
null
,
};
}
/**
* Checks if the given string is a boolean value.
* This actually check only for `TRUE` and `FALSE` because `1` or `0` are
* actually numbers and are parsed by specific methods.
*/
public
static
function
isBool
(
string
$
string
):
bool
{
$
upperString
=
strtoupper
(
$
string
);
return
$
upperString
===
'
TRUE
'
||
$
upperString
===
'
FALSE
'
;
}
/**
* Checks if the given character can be a part of a number.
*/
public
static
function
isNumber
(
string
$
string
):
bool
{
return
in_array
(
$
string
, [
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
'
8
'
,
'
9
'
,
'
.
'
,
'
-
'
,
'
+
'
,
'
e
'
,
'
E
'
],
true
);
}
/**
* Checks if the given character is the beginning of a symbol. A symbol
* can be either a variable or a field name.
*
* @return int|null the appropriate flag for the symbol type
*/
public
static
function
isSymbol
(
string
$
character
):
int
|
null
{
return
match
(
$
character
) {
'
@
'
=> Token::
FLAG_SYMBOL_VARIABLE
,
'
`
'
=> Token::
FLAG_SYMBOL_BACKTICK
,
'
:
'
,
'
?
'
=> Token::
FLAG_SYMBOL_PARAMETER
,
default
=>
null
,
};
}
/**
* Checks if the given character is the beginning of a string.
*
* @param string $character a character to be checked
*
* @return int|null the appropriate flag for the string type
*/
public
static
function
isString
(
string
$
character
):
int
|
null
{
return
match
(
$
character
) {
'\''
=> Token::
FLAG_STRING_SINGLE_QUOTES
,
'
"
'
=> Token::
FLAG_STRING_DOUBLE_QUOTES
,
default
=>
null
,
};
}
/**
* Checks if the given character can be a separator for two lexeme.
*
* @param string $string string to be checked
*/
public
static
function
isSeparator
(
string
$
string
):
bool
{
// NOTES: Only non-alphanumeric ASCII characters may be separators.
// `~` is the last printable ASCII character.
return
$
string
<=
'
~
'
&&
$
string
!==
'
_
'
&&
$
string
!==
'
$
'
&& (
$
string
<
'
0
'
||
$
string
>
'
9
'
)
&& (
$
string
<
'
a
'
||
$
string
>
'
z
'
)
&& (
$
string
<
'
A
'
||
$
string
>
'
Z
'
);
}
/**
* Loads the specified context.
*
* Contexts may be used by accessing the context directly.
*
* @param string $context name of the context or full class name that defines the context
*
* @return bool true if the context was loaded, false otherwise
*/
public
static
function
load
(
string
$
context
=
''
):
bool
{
if
(
$
context
===
''
) {
$
context
= ContextMySql50700::class;
}
$
contextClass
=
$
context
;
if
(!
class_exists
(
$
contextClass
)) {
$
contextClass
=
self
::
CONTEXT_PREFIX
.
$
context
;
if
(!
class_exists
(
$
contextClass
)) {
$
contextClass
=
'\\'
.
$
context
;
if
(!
class_exists
(
$
contextClass
)) {
return
false
;
}
}
}
self
::
$
loadedContext
=
$
contextClass
;
self
::
$
keywords
=
$
contextClass
::
KEYWORDS
;
return
true
;
}
/**
* Loads the context with the closest version to the one specified.
*
* The closest context is found by replacing last digits with zero until one
* is loaded successfully.
*
* @see Context::load()
*
* @param string $context name of the context or full class name that defines the context
*
* @return string|null The loaded context. `null` if no context was loaded.
*/
public
static
function
loadClosest
(
string
$
context
=
''
):
string
|
null
{
$
length
=
strlen
(
$
context
);
for
(
$
i
=
$
length
;
$
i
>
0
;) {
/* Trying to load the new context */
if
(
static
::
load
(
$
context
)) {
return
$
context
;
}
/* Replace last two non zero digits by zeroes */
do
{
$
i
-=
2
;
$
part
=
substr
(
$
context
,
$
i
,
2
);
/* No more numeric parts to strip */
if
(!
is_numeric
(
$
part
)) {
break
2
;
}
}
while
(
intval
(
$
part
) ===
0
&&
$
i
>
0
);
$
context
=
substr
(
$
context
,
0
,
$
i
) .
'
00
'
.
substr
(
$
context
,
$
i
+
2
);
}
/* Fallback to loading at least matching engine */
if
(
str_starts_with
(
$
context
,
'
MariaDb
'
)) {
return
static
::
loadClosest
(
'
MariaDb100300
'
);
}
if
(
str_starts_with
(
$
context
,
'
MySql
'
)) {
return
static
::
loadClosest
(
'
MySql50700
'
);
}
return
null
;
}
/**
* Gets the SQL mode.
*/
public
static
function
getMode
():
int
{
return
static
::
$
mode
;
}
/**
* Sets the SQL mode.
*/
public
static
function
setMode
(
int
|
string
$
mode
=
self
::
SQL_MODE_NONE
):
void
{
if
(
is_int
(
$
mode
)) {
static
::
$
mode
=
$
mode
;
return
;
}
static
::
$
mode
=
self
::
SQL_MODE_NONE
;
if
(
$
mode
===
''
) {
return
;
}
$
modes
=
explode
(
'
,
'
,
$
mode
);
foreach
(
$
modes
as
$
sqlMode
) {
static
::
$
mode
|=
self
::
getModeFromString
(
$
sqlMode
);
}
}
/** @psalm-suppress MixedReturnStatement, MixedInferredReturnType Is caused by the LSB of the constants */
private
static
function
getModeFromString
(
string
$
mode
):
int
{
return
match
(
$
mode
) {
'
ALLOW_INVALID_DATES
'
=>
self
::
SQL_MODE_ALLOW_INVALID_DATES
,
'
ANSI_QUOTES
'
=>
self
::
SQL_MODE_ANSI_QUOTES
,
'
COMPAT_MYSQL
'
=>
self
::
SQL_MODE_COMPAT_MYSQL
,
'
ERROR_FOR_DIVISION_BY_ZERO
'
=>
self
::
SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO
,
'
HIGH_NOT_PRECEDENCE
'
=>
self
::
SQL_MODE_HIGH_NOT_PRECEDENCE
,
'
IGNORE_SPACE
'
=>
self
::
SQL_MODE_IGNORE_SPACE
,
'
NO_AUTO_CREATE_USER
'
=>
self
::
SQL_MODE_NO_AUTO_CREATE_USER
,
'
NO_AUTO_VALUE_ON_ZERO
'
=>
self
::
SQL_MODE_NO_AUTO_VALUE_ON_ZERO
,
'
NO_BACKSLASH_ESCAPES
'
=>
self
::
SQL_MODE_NO_BACKSLASH_ESCAPES
,
'
NO_DIR_IN_CREATE
'
=>
self
::
SQL_MODE_NO_DIR_IN_CREATE
,
'
NO_ENGINE_SUBSTITUTION
'
=>
self
::
SQL_MODE_NO_ENGINE_SUBSTITUTION
,
'
NO_FIELD_OPTIONS
'
=>
self
::
SQL_MODE_NO_FIELD_OPTIONS
,
'
NO_KEY_OPTIONS
'
=>
self
::
SQL_MODE_NO_KEY_OPTIONS
,
'
NO_TABLE_OPTIONS
'
=>
self
::
SQL_MODE_NO_TABLE_OPTIONS
,
'
NO_UNSIGNED_SUBTRACTION
'
=>
self
::
SQL_MODE_NO_UNSIGNED_SUBTRACTION
,
'
NO_ZERO_DATE
'
=>
self
::
SQL_MODE_NO_ZERO_DATE
,
'
NO_ZERO_IN_DATE
'
=>
self
::
SQL_MODE_NO_ZERO_IN_DATE
,
'
ONLY_FULL_GROUP_BY
'
=>
self
::
SQL_MODE_ONLY_FULL_GROUP_BY
,
'
PIPES_AS_CONCAT
'
=>
self
::
SQL_MODE_PIPES_AS_CONCAT
,
'
REAL_AS_FLOAT
'
=>
self
::
SQL_MODE_REAL_AS_FLOAT
,
'
STRICT_ALL_TABLES
'
=>
self
::
SQL_MODE_STRICT_ALL_TABLES
,
'
STRICT_TRANS_TABLES
'
=>
self
::
SQL_MODE_STRICT_TRANS_TABLES
,
'
NO_ENCLOSING_QUOTES
'
=>
self
::
SQL_MODE_NO_ENCLOSING_QUOTES
,
'
ANSI
'
=>
self
::
SQL_MODE_ANSI
,
'
DB2
'
=>
self
::
SQL_MODE_DB2
,
'
MAXDB
'
=>
self
::
SQL_MODE_MAXDB
,
'
MSSQL
'
=>
self
::
SQL_MODE_MSSQL
,
'
ORACLE
'
=>
self
::
SQL_MODE_ORACLE
,
'
POSTGRESQL
'
=>
self
::
SQL_MODE_POSTGRESQL
,
'
TRADITIONAL
'
=>
self
::
SQL_MODE_TRADITIONAL
,
default
=>
self
::
SQL_MODE_NONE
,
};
}
/**
* Escapes the symbol by adding surrounding backticks.
*
* @param string $str the string to be escaped
* @param string $quote quote to be used when escaping
*/
public
static
function
escape
(
string
$
str
,
string
$
quote
=
'
`
'
):
string
{
if
(
(
static
::
$
mode
&
self
::
SQL_MODE_NO_ENCLOSING_QUOTES
) && ! (
static
::
isKeyword
(
$
str
,
true
) ||
self
::
doesIdentifierRequireQuoting
(
$
str
)
)
) {
return
$
str
;
}
if
(
static
::
$
mode
&
self
::
SQL_MODE_ANSI_QUOTES
) {
$
quote
=
'
"
'
;
}
return
$
quote
.
str_replace
(
$
quote
,
$
quote
.
$
quote
,
$
str
) .
$
quote
;
}
/**
* Escapes the symbol by adding surrounding backticks.
*
* @param string[] $strings the string to be escaped
*
* @return string[]
*/
public
static
function
escapeAll
(
array
$
strings
):
array
{
return
array_map
(
static
::
escape
(...),
$
strings
);
}
/**
* Function verifies that given SQL Mode constant is currently set
*
* @param int $flag for example {@see Context::SQL_MODE_ANSI_QUOTES}
*
* @return bool false on empty param, true/false on given constant/int value
*/
public
static
function
hasMode
(
int
|
null
$
flag
=
null
):
bool
{
if
(
empty
(
$
flag
)) {
return
false
;
}
return
(
self
::
$
mode
&
$
flag
) ===
$
flag
;
}
private
static
function
doesIdentifierRequireQuoting
(
string
$
identifier
):
bool
{
return
preg_match
(
'
/^[$]|^\d+$|[^0-9a-zA-Z$_\x80-\xffff]/
'
,
$
identifier
) ===
1
;
}
}
Back
|
FazBrowse Home
|
New Git URL