Skip to content

Commit db57e3a

Browse files
authored
Merge pull request #238 from deliciousbrains/issues-22-multi-column-index-delim
Use a comma for the multi column index delimiter
2 parents e28d54d + 10cfc9c commit db57e3a

4 files changed

Lines changed: 87 additions & 3 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/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) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* issue22Test.php
4+
*
5+
* Test case for PHPSQLCreator.
6+
*
7+
* PHP version 5
8+
*
9+
* LICENSE:
10+
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
11+
* All rights reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
* 3. The name of the author may not be used to endorse or promote products
22+
* derived from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author André Rothe <[email protected]>
36+
* @copyright 2010-2014 Justin Swanhart and André Rothe
37+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
38+
* @version SVN: $Id$
39+
*
40+
*/
41+
namespace PHPSQLParser\Test\Creator;
42+
43+
use PHPSQLParser\PHPSQLParser;
44+
use PHPSQLParser\PHPSQLCreator;
45+
46+
/**
47+
* https://github.com/greenlion/PHP-SQL-Parser/issues/22
48+
*/
49+
class Issue22Test extends \PHPUnit_Framework_TestCase {
50+
51+
protected function _test( $sql, $message ) {
52+
$parser = new PHPSQLParser();
53+
$parser->parse( $sql );
54+
$creator = new PHPSQLCreator();
55+
$created = $creator->create( $parser->parsed );
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' );
82+
}
83+
}
84+

0 commit comments

Comments
 (0)