Skip to content

Commit df86dc7

Browse files
committed
fixed index mode parser to not overflow for btree mode as last token of index.
added support for index comments (mysql 5.7) added tests for all idnex options
1 parent 30501a9 commit df86dc7

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/SQLParser.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,8 @@ function _extract_tokens($sql, &$source_map){
798798

799799
function parse_index_mode(&$tokens, &$index){
800800
if (count($tokens) >= 1){
801-
if ($tokens[0] == 'USING BTREE'){ $index['mode'] = 'BTREE'; array_shift($tokens); }
802-
if ($tokens[0] == 'USING HASH' ){ $index['mode'] = 'HASH'; array_shift($tokens); }
801+
if ($tokens[0] == 'USING BTREE'){ $index['mode'] = 'BTREE'; array_shift($tokens); return; }
802+
if ($tokens[0] == 'USING HASH' ){ $index['mode'] = 'HASH'; array_shift($tokens); return; }
803803
}
804804
}
805805

@@ -853,6 +853,7 @@ function parse_index_options(&$tokens, &$index){
853853
# KEY_BLOCK_SIZE [=] value
854854
# | index_type
855855
# | WITH PARSER parser_name
856+
# | COMMENT 'string'
856857

857858
if (count($tokens) >= 1){
858859
if ($tokens[0] == 'KEY_BLOCK_SIZE'){
@@ -872,6 +873,14 @@ function parse_index_options(&$tokens, &$index){
872873
array_shift($tokens);
873874
}
874875
}
876+
877+
if (count($tokens) >= 1){
878+
if ($tokens[0] == 'COMMENT'){
879+
$index['comment'] = $this->decode_value($tokens[1]);
880+
array_shift($tokens);
881+
array_shift($tokens);
882+
}
883+
}
875884
}
876885

877886

tests/IndexTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,27 @@ function testIndexCols(){
134134

135135
function testIndexOptions(){
136136

137-
# TODO
137+
# key block size
138+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) KEY_BLOCK_SIZE 4)");
139+
$this->assertEquals($tbl['indexes'][0]['key_block_size'], "4");
140+
141+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) KEY_BLOCK_SIZE=4)");
142+
$this->assertEquals($tbl['indexes'][0]['key_block_size'], "4");
143+
144+
# key mode
145+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) USING BTREE)");
146+
$this->assertEquals($tbl['indexes'][0]['mode'], "BTREE");
147+
148+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) USING HASH)");
149+
$this->assertEquals($tbl['indexes'][0]['mode'], "HASH");
150+
151+
# parser
152+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) WITH PARSER foo)");
153+
$this->assertEquals($tbl['indexes'][0]['parser'], "foo");
154+
155+
# comment
156+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar) COMMENT \"hello world\")");
157+
$this->assertEquals($tbl['indexes'][0]['comment'], "hello world");
138158
}
139159

140160
function get_first_table($str){

0 commit comments

Comments
 (0)