Skip to content

Commit 30501a9

Browse files
committed
added tests for index columns.
change index.col.direction to upper case to match everything else
1 parent 8cde164 commit 30501a9

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/SQLParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,10 @@ function parse_index_columns(&$tokens, &$index){
824824
}
825825

826826
if (StrToUpper($tokens[0]) == 'ASC'){
827-
$col['direction'] = 'asc';
827+
$col['direction'] = 'ASC';
828828
array_shift($tokens);
829829
}elseif (StrToUpper($tokens[0]) == 'DESC'){
830-
$col['direction'] = 'desc';
830+
$col['direction'] = 'DESC';
831831
array_shift($tokens);
832832
}
833833

tests/IndexTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,46 @@ function testIndexModes(){
9090

9191
function testIndexCols(){
9292

93-
# TODO
93+
# single column
94+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar))");
95+
$this->assertEquals(count($tbl['indexes'][0]['cols']), 1);
96+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
97+
98+
# multi column
99+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar, baz))");
100+
$this->assertEquals(count($tbl['indexes'][0]['cols']), 2);
101+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
102+
$this->assertEquals($tbl['indexes'][0]['cols'][1]['name'], 'baz');
103+
104+
# length (or not)
105+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar))");
106+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
107+
$this->assertEquals(array_key_exists('length', $tbl['indexes'][0]['cols'][0]), false);
108+
109+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar (100)))");
110+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
111+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['length'], '100');
112+
113+
# direction (or not)
114+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar))");
115+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
116+
$this->assertEquals(array_key_exists('direction', $tbl['indexes'][0]['cols'][0]), false);
117+
118+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar ASC))");
119+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
120+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['direction'], "ASC");
121+
122+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar desc))");
123+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
124+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['direction'], "DESC");
125+
126+
# everything
127+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz INT, PRIMARY KEY (bar (10) ASC, baz DESC))");
128+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], 'bar');
129+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['length'], '10');
130+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['direction'], "ASC");
131+
$this->assertEquals($tbl['indexes'][0]['cols'][1]['name'], 'baz');
132+
$this->assertEquals($tbl['indexes'][0]['cols'][1]['direction'], "DESC");
94133
}
95134

96135
function testIndexOptions(){

0 commit comments

Comments
 (0)