phpmyadmin / sql-parser

A validating SQL lexer and parser with a focus on MySQL dialect.
https://packagist.org/packages/phpmyadmin/sql-parser
GNU General Public License v2.0
442 stars 102 forks source link

Allow parsing KILL statement #557

Closed MoonE closed 6 months ago

MoonE commented 7 months ago

Fixes #556

MoonE commented 7 months ago

Could also use $CLAUSES to build it. Though it fails to build an invalid statement like KILL ; which may not matter.

diff --git a/src/Statements/KillStatement.php b/src/Statements/KillStatement.php
index 7040a0f3..4ef3f532 100644
--- a/src/Statements/KillStatement.php
+++ b/src/Statements/KillStatement.php
@@ -28,16 +28,22 @@ class KillStatement extends Statement
         'QUERY' => 1,
     ];

+    public static $CLAUSES = [
+        'KILL' => [
+            'KILL',
+            2,
+        ],
+        // Used for options.
+        '_OPTIONS' => [
+            '_OPTIONS',
+            1,
+        ],
+        'EXPRESSION' => [
+            'KILL',
+            1,
+        ],
+    ];
+
     /** @var Expression|null */
     public $processListId = null;
-
-    public function build(): string
-    {
-        $option = $this->options === null || $this->options->isEmpty()
-            ? ''
-            : ' ' . OptionsArray::build($this->options);
-        $expression = $this->processListId === null ? '' : ' ' . Expression::build($this->processListId);
-
-        return trim('KILL' . $option . $expression);
-    }
 }
diff --git a/tests/Parser/KillStatementTest.php b/tests/Parser/KillStatementTest.php
index 99b81f75..e3a1e4c8 100644
--- a/tests/Parser/KillStatementTest.php
+++ b/tests/Parser/KillStatementTest.php
@@ -53,7 +53,6 @@ class KillStatementTest extends TestCase
             ['KILL (SELECT 3 + 4)'],
             ['KILL QUERY 3'],
             ['KILL CONNECTION 3'],
-            ['KILL'],
         ];
     }
 }
niconoe- commented 6 months ago

LGTM but I don’t know the KILL command 😅