From 37f43534dbffd77281f8f876aef6e69a784c3f54 Mon Sep 17 00:00:00 2001 From: zzy Date: Fri, 13 Dec 2024 17:32:32 +0800 Subject: [PATCH 1/3] Fix the bug with default negative values, and add the ability to handle field comments. --- src/SQLParser.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SQLParser.php b/src/SQLParser.php index 5105744..6eeace8 100644 --- a/src/SQLParser.php +++ b/src/SQLParser.php @@ -108,7 +108,7 @@ private function _lex($sql){ # [ [ ] ] # # ::= ... - if (preg_match('!(\d+\.?\d*|\.\d+)!A', $sql, $m, 0, $pos)){ + if (preg_match('![-+]?(\d+\.?\d*|\.\d+)!A', $sql, $m, 0, $pos)){ $source_map[] = array($pos, strlen($m[0])); $pos += strlen($m[0]); continue; @@ -760,6 +760,11 @@ function parse_field($tokens){ # [UNIQUE [KEY] | [PRIMARY] KEY] # [COMMENT 'string'] + if (count($tokens) >= 1 && StrToUpper($tokens[0]) == 'COMMENT'){ + $f['comment'] = $this->decode_value($tokens[1]); + array_shift($tokens); + array_shift($tokens); + } # [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] # [STORAGE {DISK|MEMORY|DEFAULT}] # [reference_definition] From 7308b62557ffd8662f36904f8e059238d38ef1f3 Mon Sep 17 00:00:00 2001 From: Cal Henderson Date: Fri, 13 Dec 2024 18:44:47 +0000 Subject: [PATCH 2/3] added test for negative default values (generally tests negative numbers too) --- tests/FieldTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/FieldTest.php b/tests/FieldTest.php index c8b19a1..eff1159 100644 --- a/tests/FieldTest.php +++ b/tests/FieldTest.php @@ -335,6 +335,16 @@ function testFieldOptions(){ 'null' => true, ) ), $fields); + + $fields = $this->get_fields("bar INT NOT NULL DEFAULT -1"); + $this->assertEquals(array( + array( + 'name' => "bar", + 'type' => "INT", + 'default' => '-1', + 'null' => false, + ) + ), $fields); } function testVirtualOptions(){ From a670a900cae83cd9f019711aa7e8d296235fcb5a Mon Sep 17 00:00:00 2001 From: Cal Henderson Date: Fri, 13 Dec 2024 18:46:33 +0000 Subject: [PATCH 3/3] added tests for field comments --- tests/FieldTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/FieldTest.php b/tests/FieldTest.php index eff1159..14709c6 100644 --- a/tests/FieldTest.php +++ b/tests/FieldTest.php @@ -345,6 +345,9 @@ function testFieldOptions(){ 'null' => false, ) ), $fields); + + $fields = $this->get_fields("ucount int NOT NULL comment 'user count'"); + $this->assertEquals('user count', $fields[0]['comment']); } function testVirtualOptions(){