From 2f01c540a8137103a32941ae1963b52dce86e68a Mon Sep 17 00:00:00 2001 From: Ahmed El Moden Date: Fri, 28 May 2021 14:56:58 +0200 Subject: [PATCH] Fix: Handle TRUNCATE() function and TRUNCATE keyword. Issue #338. --- src/PHPSQLParser/processors/SQLProcessor.php | 12 +++++++- tests/cases/parser/issue338Test.php | 30 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/cases/parser/issue338Test.php diff --git a/src/PHPSQLParser/processors/SQLProcessor.php b/src/PHPSQLParser/processors/SQLProcessor.php index 4c8ccfc3..f366f34a 100644 --- a/src/PHPSQLParser/processors/SQLProcessor.php +++ b/src/PHPSQLParser/processors/SQLProcessor.php @@ -242,7 +242,6 @@ public function process($tokens) { case 'DELETE': case 'ALTER': case 'INSERT': - case 'TRUNCATE': case 'OPTIMIZE': case 'GRANT': case 'REVOKE': @@ -264,6 +263,17 @@ public function process($tokens) { $out[$upper][0] = $trim; continue 2; + case 'TRUNCATE': + if ($prev_category === '') { + // set the category in case these get subclauses in a future version of MySQL + $token_category = $upper; + $out[$upper][0] = $trim; + continue 2; + } + // part of the CREATE TABLE statement or a function + $out[$prev_category][] = $trim; + continue 2; + case 'REPLACE': if ($prev_category === '') { // set the category in case these get subclauses in a future version of MySQL diff --git a/tests/cases/parser/issue338Test.php b/tests/cases/parser/issue338Test.php new file mode 100644 index 00000000..c28b799f --- /dev/null +++ b/tests/cases/parser/issue338Test.php @@ -0,0 +1,30 @@ +parse($sql, true); + $parsed = $parser->parsed; + + $this->assertNotFalse($parsed); + $this->assertTrue(is_array($parsed)); + $this->assertTrue(!array_key_exists('TRUNCATE', $parsed)); + + $sql = "TRUNCATE TABLE truncate_table"; + $parser->parse($sql, true); + $parsed = $parser->parsed; + + $this->assertNotFalse($parsed); + $this->assertTrue(is_array($parsed)); + $this->assertTrue(array_key_exists('TRUNCATE', $parsed)); + } + +}