Skip to content

Commit b510bc3

Browse files
committed
actually include constraint information in the index (duh).
added tests for constraints
1 parent 103ef2b commit b510bc3

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/SQLParser.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){
337337
array_shift($tokens);
338338
}else{
339339
array_shift($tokens);
340-
$constraint = array_shift($tokens);
340+
$constraint = $this->decode_identifier(array_shift($tokens));
341341
}
342342
}
343343

@@ -364,6 +364,11 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){
364364
'type' => 'INDEX',
365365
);
366366

367+
if ($has_constraint){
368+
$index['constraint'] = true;
369+
if (!is_null($constraint)) $index['constraint_name'] = $constraint;
370+
}
371+
367372
if ($tokens[0] == 'UNIQUE' ) $index['type'] = 'UNIQUE';
368373
if ($tokens[0] == 'UNIQUE INDEX') $index['type'] = 'UNIQUE';
369374
if ($tokens[0] == 'UNIQUE KEY' ) $index['type'] = 'UNIQUE';
@@ -394,6 +399,11 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){
394399
'type' => 'PRIMARY',
395400
);
396401

402+
if ($has_constraint){
403+
$index['constraint'] = true;
404+
if (!is_null($constraint)) $index['constraint_name'] = $constraint;
405+
}
406+
397407
array_shift($tokens);
398408

399409
$this->parse_index_mode($tokens, $index);
@@ -442,10 +452,20 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){
442452
return;
443453

444454

445-
# older stuff
455+
# unsupported
456+
457+
case 'FOREIGN KEY':
458+
459+
# TODO
460+
$fields[] = array(
461+
'_' => 'FOREIGN KEY',
462+
'tokens' => $tokens,
463+
);
464+
return;
446465

447466
case 'CHECK':
448467

468+
# TODO
449469
$fields[] = array(
450470
'_' => 'CHECK',
451471
'tokens' => $tokens,

tests/IndexTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ function testIndexTypes(){
5353

5454
function testConstraints(){
5555

56-
# TODO
56+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, CONSTRAINT UNIQUE INDEX(bar))");
57+
$this->assertEquals($tbl['indexes'][0]['constraint'], true);
58+
$this->assertEquals(array_key_exists('constraint_name', $tbl['indexes'][0]), false);
59+
60+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, CONSTRAINT `qux` UNIQUE INDEX(bar))");
61+
$this->assertEquals($tbl['indexes'][0]['constraint'], true);
62+
$this->assertEquals($tbl['indexes'][0]['constraint_name'], 'qux');
63+
64+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, CONSTRAINT `qux` PRIMARY KEY(bar))");
65+
$this->assertEquals($tbl['indexes'][0]['constraint'], true);
66+
$this->assertEquals($tbl['indexes'][0]['constraint_name'], 'qux');
5767
}
5868

5969
function testIndexNames(){

0 commit comments

Comments
 (0)