Skip to content

Commit 204f7cd

Browse files
committed
Create the StatementType enum
It replaces the string value of the querytype field of the statement flags. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 1f0c925 commit 204f7cd

4 files changed

Lines changed: 82 additions & 54 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $query = 'OPTIMIZE TABLE tbl';
7272
$parser = new Parser($query);
7373
$flags = Query::getFlags($parser->statements[0]);
7474

75-
echo $flags['querytype'];
75+
echo $flags['querytype']?->value;
7676
```
7777

7878
### Parsing and building SQL query

src/Utils/Query.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* limit?: bool,
6565
* offset?: bool,
6666
* order?: bool,
67-
* querytype: ('ALTER'|'ANALYZE'|'CALL'|'CHECK'|'CHECKSUM'|'CREATE'|'DELETE'|'DROP'|'EXPLAIN'|'INSERT'|'LOAD'|'OPTIMIZE'|'REPAIR'|'REPLACE'|'SELECT'|'SET'|'SHOW'|'UPDATE'|false),
67+
* querytype: StatementType|null,
6868
* reload?: bool,
6969
* select_from?: bool,
7070
* union?: bool
@@ -114,7 +114,7 @@ class Query
114114
* limit: false,
115115
* offset: false,
116116
* order: false,
117-
* querytype: false,
117+
* querytype: null,
118118
* reload: false,
119119
* select_from: false,
120120
* union: false
@@ -258,7 +258,7 @@ class Query
258258
* The type of the query (which is usually the first keyword of
259259
* the statement).
260260
*/
261-
'querytype' => false,
261+
'querytype' => null,
262262

263263
/*
264264
* Whether a page reload is required.
@@ -288,7 +288,7 @@ class Query
288288
*/
289289
private static function getFlagsSelect(SelectStatement $statement, array $flags): array
290290
{
291-
$flags['querytype'] = 'SELECT';
291+
$flags['querytype'] = StatementType::Select;
292292
$flags['is_select'] = true;
293293

294294
if ($statement->from !== []) {
@@ -364,72 +364,72 @@ private static function getFlagsSelect(SelectStatement $statement, array $flags)
364364
*/
365365
public static function getFlags(Statement|null $statement, bool $all = false): array
366366
{
367-
$flags = ['querytype' => false];
367+
$flags = ['querytype' => null];
368368
if ($all) {
369369
$flags = self::$allFlags;
370370
}
371371

372372
if ($statement instanceof AlterStatement) {
373-
$flags['querytype'] = 'ALTER';
373+
$flags['querytype'] = StatementType::Alter;
374374
$flags['reload'] = true;
375375
} elseif ($statement instanceof CreateStatement) {
376-
$flags['querytype'] = 'CREATE';
376+
$flags['querytype'] = StatementType::Create;
377377
$flags['reload'] = true;
378378
} elseif ($statement instanceof AnalyzeStatement) {
379-
$flags['querytype'] = 'ANALYZE';
379+
$flags['querytype'] = StatementType::Analyze;
380380
$flags['is_maint'] = true;
381381
} elseif ($statement instanceof CheckStatement) {
382-
$flags['querytype'] = 'CHECK';
382+
$flags['querytype'] = StatementType::Check;
383383
$flags['is_maint'] = true;
384384
} elseif ($statement instanceof ChecksumStatement) {
385-
$flags['querytype'] = 'CHECKSUM';
385+
$flags['querytype'] = StatementType::Checksum;
386386
$flags['is_maint'] = true;
387387
} elseif ($statement instanceof OptimizeStatement) {
388-
$flags['querytype'] = 'OPTIMIZE';
388+
$flags['querytype'] = StatementType::Optimize;
389389
$flags['is_maint'] = true;
390390
} elseif ($statement instanceof RepairStatement) {
391-
$flags['querytype'] = 'REPAIR';
391+
$flags['querytype'] = StatementType::Repair;
392392
$flags['is_maint'] = true;
393393
} elseif ($statement instanceof CallStatement) {
394-
$flags['querytype'] = 'CALL';
394+
$flags['querytype'] = StatementType::Call;
395395
$flags['is_procedure'] = true;
396396
} elseif ($statement instanceof DeleteStatement) {
397-
$flags['querytype'] = 'DELETE';
397+
$flags['querytype'] = StatementType::Delete;
398398
$flags['is_delete'] = true;
399399
$flags['is_affected'] = true;
400400
} elseif ($statement instanceof DropStatement) {
401-
$flags['querytype'] = 'DROP';
401+
$flags['querytype'] = StatementType::Drop;
402402
$flags['reload'] = true;
403403

404404
if ($statement->options->has('DATABASE') || $statement->options->has('SCHEMA')) {
405405
$flags['drop_database'] = true;
406406
}
407407
} elseif ($statement instanceof ExplainStatement) {
408-
$flags['querytype'] = 'EXPLAIN';
408+
$flags['querytype'] = StatementType::Explain;
409409
$flags['is_explain'] = true;
410410
} elseif ($statement instanceof InsertStatement) {
411-
$flags['querytype'] = 'INSERT';
411+
$flags['querytype'] = StatementType::Insert;
412412
$flags['is_affected'] = true;
413413
$flags['is_insert'] = true;
414414
} elseif ($statement instanceof LoadStatement) {
415-
$flags['querytype'] = 'LOAD';
415+
$flags['querytype'] = StatementType::Load;
416416
$flags['is_affected'] = true;
417417
$flags['is_insert'] = true;
418418
} elseif ($statement instanceof ReplaceStatement) {
419-
$flags['querytype'] = 'REPLACE';
419+
$flags['querytype'] = StatementType::Replace;
420420
$flags['is_affected'] = true;
421421
$flags['is_replace'] = true;
422422
$flags['is_insert'] = true;
423423
} elseif ($statement instanceof SelectStatement) {
424424
$flags = self::getFlagsSelect($statement, $flags);
425425
} elseif ($statement instanceof ShowStatement) {
426-
$flags['querytype'] = 'SHOW';
426+
$flags['querytype'] = StatementType::Show;
427427
$flags['is_show'] = true;
428428
} elseif ($statement instanceof UpdateStatement) {
429-
$flags['querytype'] = 'UPDATE';
429+
$flags['querytype'] = StatementType::Update;
430430
$flags['is_affected'] = true;
431431
} elseif ($statement instanceof SetStatement) {
432-
$flags['querytype'] = 'SET';
432+
$flags['querytype'] = StatementType::Set;
433433
}
434434

435435
if (

src/Utils/StatementType.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Utils;
6+
7+
enum StatementType: string
8+
{
9+
case Alter = 'ALTER';
10+
case Analyze = 'ANALYZE';
11+
case Call = 'CALL';
12+
case Check = 'CHECK';
13+
case Checksum = 'CHECKSUM';
14+
case Create = 'CREATE';
15+
case Delete = 'DELETE';
16+
case Drop = 'DROP';
17+
case Explain = 'EXPLAIN';
18+
case Insert = 'INSERT';
19+
case Load = 'LOAD';
20+
case Optimize = 'OPTIMIZE';
21+
case Repair = 'REPAIR';
22+
case Replace = 'REPLACE';
23+
case Select = 'SELECT';
24+
case Set = 'SET';
25+
case Show = 'SHOW';
26+
case Update = 'UPDATE';
27+
}

0 commit comments

Comments
 (0)