Skip to content

Commit 10cfc9c

Browse files
committed
Handle all types of CREATE TABLE key that can have multiple columns.
1 parent 5b1036a commit 10cfc9c

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

src/PHPSQLParser/builders/ColumnListBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function buildColumnReference($parsed) {
6363
return $builder->build($parsed);
6464
}
6565

66-
public function build(array $parsed, $delim = ' ') {
66+
public function build(array $parsed, $delim = ', ') {
6767
if ($parsed['expr_type'] !== ExpressionType::COLUMN_LIST) {
6868
return '';
6969
}

src/PHPSQLParser/builders/CreateIndexTableBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CreateIndexTableBuilder implements Builder {
5454

5555
protected function buildColumnList($parsed) {
5656
$builder = new ColumnListBuilder();
57-
return $builder->build($parsed, ', ');
57+
return $builder->build($parsed);
5858
}
5959

6060
public function build(array $parsed) {

src/PHPSQLParser/builders/IndexKeyBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function buildIndexType($parsed) {
7070

7171
protected function buildColumnList($parsed) {
7272
$builder = new ColumnListBuilder();
73-
return $builder->build($parsed, ', ');
73+
return $builder->build($parsed);
7474
}
7575

7676
public function build(array $parsed) {

src/PHPSQLParser/builders/RefClauseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function buildBracketExpression($parsed) {
8484

8585
protected function buildColumnList($parsed) {
8686
$builder = new ColumnListBuilder();
87-
return $builder->build($parsed, ',');
87+
return $builder->build($parsed);
8888
}
8989

9090
public function build(array $parsed) {

tests/cases/creator/issue22Test.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,42 @@
4343
use PHPSQLParser\PHPSQLParser;
4444
use PHPSQLParser\PHPSQLCreator;
4545

46+
/**
47+
* https://github.com/greenlion/PHP-SQL-Parser/issues/22
48+
*/
4649
class Issue22Test extends \PHPUnit_Framework_TestCase {
4750

48-
/**
49-
* https://github.com/greenlion/PHP-SQL-Parser/issues/22
50-
*/
51-
public function testIssue22() {
52-
$sql = "CREATE TABLE IF NOT EXISTS wp_md_3_term_taxonomy (term_taxonomy_id bigint (20) NOT NULL auto_increment, term_id bigint (20) NOT NULL default 0, taxonomy varchar (32) NOT NULL default '', description longtext NOT NULL, parent bigint (20) NOT NULL default 0, count bigint (20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), KEY term_id_taxonomy (term_id, taxonomy), KEY taxonomy (taxonomy)) DEFAULT CHARACTER SET utf8mb4";
51+
protected function _test( $sql, $message ) {
5352
$parser = new PHPSQLParser();
5453
$parser->parse( $sql );
5554
$creator = new PHPSQLCreator();
5655
$created = $creator->create( $parser->parsed );
57-
$this->assertSame( $sql, $created, 'Creating a CREATE statement with multi column index' );
56+
$this->assertSame( $sql, $created, $message );
57+
}
58+
59+
public function testIssue22_key() {
60+
$sql = "CREATE TABLE wp_md_3_term_taxonomy (term_taxonomy_id bigint (20) NOT NULL auto_increment, term_id bigint (20) NOT NULL default 0, taxonomy varchar (32) NOT NULL default '', description longtext NOT NULL, parent bigint (20) NOT NULL default 0, count bigint (20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), KEY term_id_taxonomy (term_id, taxonomy), KEY taxonomy (taxonomy)) DEFAULT CHARACTER SET utf8mb4";
61+
$this->_test( $sql, 'Creating a CREATE statement with multi column KEY index' );
62+
}
63+
64+
public function testIssue22_primaryKey() {
65+
$sql = "CREATE TABLE wp_md_3_term_relationships (object_id bigint (20) NOT NULL default 0, term_taxonomy_id bigint (20) NOT NULL default 0, term_order int (11) NOT NULL default 0, PRIMARY KEY (object_id, term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id)) DEFAULT CHARACTER SET utf8mb4";
66+
$this->_test( $sql, 'Creating a CREATE statement with multi column PRIMARY KEY index' );
67+
}
68+
69+
public function testIssue22_index() {
70+
$sql = "CREATE TABLE wp_md_3_term_relationships (object_id bigint (20) NOT NULL default 0, term_taxonomy_id bigint (20) NOT NULL default 0, term_order int (11) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), INDEX term_id_taxonomy (term_id, taxonomy)) DEFAULT CHARACTER SET utf8mb4";
71+
$this->_test( $sql, 'Creating a CREATE statement with multi column INDEX' );
72+
}
73+
74+
public function testIssue22_foreignKey() {
75+
$sql = "CREATE TABLE child (col_a INT NOT NULL, col_b INT NOT NULL, FOREIGN KEY 'a_b' (col_a, col_b)) DEFAULT CHARACTER SET utf8mb4";
76+
$this->_test( $sql, 'Creating a CREATE statement with multi column FOREIGN KEY' );
77+
}
78+
79+
public function testIssue22_foreignKeyReferences() {
80+
$sql = "CREATE TABLE child (col_a INT NOT NULL, col_b INT NOT NULL, FOREIGN KEY 'a_b' (col_a, col_b) REFERENCES parent (parent_a, parent_b)) DEFAULT CHARACTER SET utf8mb4";
81+
$this->_test( $sql, 'Creating a CREATE statement with multi column FOREIGN KEY with multi column REFERENCES' );
5882
}
5983
}
6084

0 commit comments

Comments
 (0)