Skip to content

Commit d78dacf

Browse files
authored
Add Additional Builders for CREATE statements (#363)
* Add support for collation in the ColumnTypeBuilder * Add Comment definition to ColumnTypeBuilder * Add FulltextIndexBuilder * Add UniqueIndexBuilder * Add unique and full text indexs to TableBracketExpressionBuilder Co-authored-by: Alex Tiernan-Berry <a.tiernan-berry@leads.io>
1 parent c365130 commit d78dacf

4 files changed

Lines changed: 224 additions & 0 deletions

File tree

src/PHPSQLParser/builders/ColumnTypeBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ protected function buildCharacterSet($parsed) {
8080
return $parsed['base_expr'];
8181
}
8282

83+
protected function buildCollation($parsed) {
84+
if ($parsed['expr_type'] !== ExpressionType::COLLATE) {
85+
return "";
86+
}
87+
return $parsed['base_expr'];
88+
}
89+
90+
protected function buildComment($parsed) {
91+
if ($parsed['expr_type'] !== ExpressionType::COMMENT) {
92+
return "";
93+
}
94+
return $parsed['base_expr'];
95+
}
96+
8397
public function build(array $parsed) {
8498
if ($parsed['expr_type'] !== ExpressionType::COLUMN_TYPE) {
8599
return "";
@@ -92,6 +106,8 @@ public function build(array $parsed) {
92106
$sql .= $this->buildReserved($v);
93107
$sql .= $this->buildDefaultValue($v);
94108
$sql .= $this->buildCharacterSet($v);
109+
$sql .= $this->buildCollation($v);
110+
$sql .= $this->buildComment($v);
95111

96112
if ($len == strlen($sql)) {
97113
throw new UnableToCreateSQLException('CREATE TABLE column-type subtree', $k, $v, 'expr_type');
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* IndexKeyBuilder.php
4+
*
5+
* Builds index key part of a CREATE TABLE statement.
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 <andre.rothe@phosco.info>
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+
42+
namespace PHPSQLParser\builders;
43+
use PHPSQLParser\exceptions\UnableToCreateSQLException;
44+
use PHPSQLParser\utils\ExpressionType;
45+
46+
/**
47+
* This class implements the builder for the index key part of a CREATE TABLE statement.
48+
* You can overwrite all functions to achieve another handling.
49+
*
50+
* @author André Rothe <andre.rothe@phosco.info>
51+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52+
*
53+
*/
54+
class FulltextIndexBuilder implements Builder {
55+
56+
protected function buildReserved($parsed) {
57+
$builder = new ReservedBuilder();
58+
return $builder->build($parsed);
59+
}
60+
61+
protected function buildConstant($parsed) {
62+
$builder = new ConstantBuilder();
63+
return $builder->build($parsed);
64+
}
65+
66+
protected function buildIndexKey($parsed) {
67+
if ($parsed['expr_type'] !== ExpressionType::INDEX) {
68+
return "";
69+
}
70+
return $parsed['base_expr'];
71+
}
72+
73+
protected function buildColumnList($parsed) {
74+
$builder = new ColumnListBuilder();
75+
return $builder->build($parsed);
76+
}
77+
78+
public function build(array $parsed) {
79+
if ($parsed['expr_type'] !== ExpressionType::FULLTEXT_IDX) {
80+
return "";
81+
}
82+
$sql = "";
83+
foreach ($parsed['sub_tree'] as $k => $v) {
84+
$len = strlen($sql);
85+
$sql .= $this->buildReserved($v);
86+
$sql .= $this->buildColumnList($v);
87+
$sql .= $this->buildConstant($v);
88+
$sql .= $this->buildIndexKey($v);
89+
90+
if ($len == strlen($sql)) {
91+
throw new UnableToCreateSQLException('CREATE TABLE fulltext-index key subtree', $k, $v, 'expr_type');
92+
}
93+
94+
$sql .= " ";
95+
}
96+
return substr($sql, 0, -1);
97+
}
98+
}
99+
?>

src/PHPSQLParser/builders/TableBracketExpressionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ protected function buildIndexKey($parsed) {
8484
return $builder->build($parsed);
8585
}
8686

87+
protected function buildUniqueIndex($parsed) {
88+
$builder = new UniqueIndexBuilder();
89+
return $builder->build($parsed);
90+
}
91+
92+
protected function buildFulltextIndex($parsed) {
93+
$builder = new FulltextIndexBuilder();
94+
return $builder->build($parsed);
95+
}
96+
8797
public function build(array $parsed) {
8898
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
8999
return "";
@@ -97,6 +107,8 @@ public function build(array $parsed) {
97107
$sql .= $this->buildLikeExpression($v);
98108
$sql .= $this->buildForeignKey($v);
99109
$sql .= $this->buildIndexKey($v);
110+
$sql .= $this->buildUniqueIndex($v);
111+
$sql .= $this->buildFulltextIndex($v);
100112

101113
if ($len == strlen($sql)) {
102114
throw new UnableToCreateSQLException('CREATE TABLE create-def expression subtree', $k, $v, 'expr_type');
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* IndexKeyBuilder.php
4+
*
5+
* Builds index key part of a CREATE TABLE statement.
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 <andre.rothe@phosco.info>
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+
42+
namespace PHPSQLParser\builders;
43+
use PHPSQLParser\exceptions\UnableToCreateSQLException;
44+
use PHPSQLParser\utils\ExpressionType;
45+
46+
/**
47+
* This class implements the builder for the index key part of a CREATE TABLE statement.
48+
* You can overwrite all functions to achieve another handling.
49+
*
50+
* @author André Rothe <andre.rothe@phosco.info>
51+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52+
*
53+
*/
54+
class UniqueIndexBuilder implements Builder {
55+
56+
protected function buildReserved($parsed) {
57+
$builder = new ReservedBuilder();
58+
return $builder->build($parsed);
59+
}
60+
61+
protected function buildConstant($parsed) {
62+
$builder = new ConstantBuilder();
63+
return $builder->build($parsed);
64+
}
65+
66+
protected function buildIndexType($parsed) {
67+
$builder = new IndexTypeBuilder();
68+
return $builder->build($parsed);
69+
}
70+
71+
protected function buildColumnList($parsed) {
72+
$builder = new ColumnListBuilder();
73+
return $builder->build($parsed);
74+
}
75+
76+
public function build(array $parsed) {
77+
if ($parsed['expr_type'] !== ExpressionType::UNIQUE_IDX) {
78+
return "";
79+
}
80+
$sql = "";
81+
foreach ($parsed['sub_tree'] as $k => $v) {
82+
$len = strlen($sql);
83+
$sql .= $this->buildReserved($v);
84+
$sql .= $this->buildColumnList($v);
85+
$sql .= $this->buildConstant($v);
86+
$sql .= $this->buildIndexType($v);
87+
88+
if ($len == strlen($sql)) {
89+
throw new UnableToCreateSQLException('CREATE TABLE unique-index key subtree', $k, $v, 'expr_type');
90+
}
91+
92+
$sql .= " ";
93+
}
94+
return substr($sql, 0, -1);
95+
}
96+
}
97+
?>

0 commit comments

Comments
 (0)