Skip to content

Commit ef1377d

Browse files
authored
Merge pull request #186 from gluneaumt/master
New functionality, Insert Set, Replace, Union and Union all and more Thank you for your contribution.
2 parents 4f7fb69 + dd35448 commit ef1377d

22 files changed

Lines changed: 669 additions & 13 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
composer.lock
3-
clover.xml
3+
clover.xml
4+
composer.phar

src/PHPSQLParser/PHPSQLCreator.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@
4343
use PHPSQLParser\exceptions\UnsupportedFeatureException;
4444
use PHPSQLParser\builders\SelectStatementBuilder;
4545
use PHPSQLParser\builders\DeleteStatementBuilder;
46+
use PHPSQLParser\builders\TruncateStatementBuilder;
4647
use PHPSQLParser\builders\UpdateStatementBuilder;
4748
use PHPSQLParser\builders\InsertStatementBuilder;
4849
use PHPSQLParser\builders\CreateStatementBuilder;
4950
use PHPSQLParser\builders\DropStatementBuilder;
5051
use PHPSQLParser\builders\RenameStatementBuilder;
52+
use PHPSQLParser\builders\ReplaceStatementBuilder;
5153
use PHPSQLParser\builders\ShowStatementBuilder;
5254
use PHPSQLParser\builders\BracketStatementBuilder;
55+
use PHPSQLParser\builders\UnionStatementBuilder;
56+
use PHPSQLParser\builders\UnionAllStatementBuilder;
5357

5458
/**
5559
* This class generates SQL from the output of the PHPSQLParser.
@@ -71,8 +75,12 @@ public function create($parsed) {
7175
switch ($k) {
7276

7377
case 'UNION':
78+
$builder = new UnionStatementBuilder();
79+
$this->created = $builder->build($parsed);
80+
break;
7481
case 'UNION ALL':
75-
throw new UnsupportedFeatureException($k);
82+
$builder = new UnionAllStatementBuilder();
83+
$this->created = $builder->build($parsed);
7684
break;
7785
case 'SELECT':
7886
$builder = new SelectStatementBuilder();
@@ -82,10 +90,18 @@ public function create($parsed) {
8290
$builder = new InsertStatementBuilder();
8391
$this->created = $builder->build($parsed);
8492
break;
93+
case 'REPLACE':
94+
$builder = new ReplaceStatementBuilder();
95+
$this->created = $builder->build($parsed);
96+
break;
8597
case 'DELETE':
8698
$builder = new DeleteStatementBuilder();
8799
$this->created = $builder->build($parsed);
88100
break;
101+
case 'TRUNCATE':
102+
$builder = new TruncateStatementBuilder();
103+
$this->created = $builder->build($parsed);
104+
break;
89105
case 'UPDATE':
90106
$builder = new UpdateStatementBuilder();
91107
$this->created = $builder->build($parsed);

src/PHPSQLParser/builders/FromBuilder.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,39 @@ protected function buildSubQuery($parsed, $key) {
6969

7070
public function build(array $parsed) {
7171
$sql = "";
72-
foreach ($parsed as $k => $v) {
73-
$len = strlen($sql);
74-
$sql .= $this->buildTable($v, $k);
75-
$sql .= $this->buildTableExpression($v, $k);
76-
$sql .= $this->buildSubquery($v, $k);
72+
if (array_key_exists("UNION ALL", $parsed) || array_key_exists("UNION", $parsed)) {
73+
foreach ($parsed as $union_type => $outer_v) {
74+
$first = true;
7775

78-
if ($len == strlen($sql)) {
79-
throw new UnableToCreateSQLException('FROM', $k, $v, 'expr_type');
76+
foreach ($outer_v as $item) {
77+
if (!$first) {
78+
$sql .= " $union_type ";
79+
}
80+
else {
81+
$first = false;
82+
}
83+
84+
$select_builder = new SelectStatementBuilder();
85+
86+
$len = strlen($sql);
87+
$sql .= $select_builder->build($item);
88+
89+
if ($len === strlen($sql)) {
90+
throw new UnableToCreateSQLException('FROM', $union_type, $outer_v, 'expr_type');
91+
}
92+
}
93+
}
94+
}
95+
else {
96+
foreach ($parsed as $k => $v) {
97+
$len = strlen($sql);
98+
$sql .= $this->buildTable($v, $k);
99+
$sql .= $this->buildTableExpression($v, $k);
100+
$sql .= $this->buildSubquery($v, $k);
101+
102+
if ($len == strlen($sql)) {
103+
throw new UnableToCreateSQLException('FROM', $k, $v, 'expr_type');
104+
}
80105
}
81106
}
82107
return "FROM " . $sql;

src/PHPSQLParser/builders/FunctionBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ protected function buildSelectBracketExpression($parsed) {
8787
$builder = new SelectBracketExpressionBuilder();
8888
return $builder->build($parsed);
8989
}
90-
90+
91+
protected function buildSubQuery($parsed) {
92+
$builder = new SubQueryBuilder();
93+
return $builder->build($parsed);
94+
}
95+
9196
public function build(array $parsed) {
9297
if (($parsed['expr_type'] !== ExpressionType::AGGREGATE_FUNCTION)
9398
&& ($parsed['expr_type'] !== ExpressionType::SIMPLE_FUNCTION)
@@ -104,6 +109,7 @@ public function build(array $parsed) {
104109
$len = strlen($sql);
105110
$sql .= $this->build($v);
106111
$sql .= $this->buildConstant($v);
112+
$sql .= $this->buildSubQuery($v);
107113
$sql .= $this->buildColRef($v);
108114
$sql .= $this->buildReserved($v);
109115
$sql .= $this->buildSelectBracketExpression($v);

src/PHPSQLParser/builders/InsertStatementBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,21 @@ protected function buildSELECT($parsed) {
6565
$builder = new SelectStatementBuilder();
6666
return $builder->build($parsed);
6767
}
68-
68+
69+
protected function buildSET($parsed) {
70+
$builder = new SetBuilder();
71+
return $builder->build($parsed);
72+
}
73+
6974
public function build(array $parsed) {
7075
// TODO: are there more than one tables possible (like [INSERT][1])
7176
$sql = $this->buildINSERT($parsed['INSERT']);
7277
if (isset($parsed['VALUES'])) {
7378
$sql .= ' ' . $this->buildVALUES($parsed['VALUES']);
7479
}
80+
if (isset($parsed['SET'])) {
81+
$sql .= ' ' . $this->buildSET($parsed['SET']);
82+
}
7583
if (isset($parsed['SELECT'])) {
7684
$sql .= ' ' . $this->buildSELECT($parsed);
7785
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* ReplaceBuilder.php
4+
*
5+
* Builds the [REPLACE] statement part.
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+
42+
namespace PHPSQLParser\builders;
43+
use PHPSQLParser\exceptions\UnableToCreateSQLException;
44+
45+
/**
46+
* This class implements the builder for the [REPLACE] statement parts.
47+
* You can overwrite all functions to achieve another handling.
48+
*
49+
* @author André Rothe <[email protected]>
50+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
51+
*
52+
*/
53+
class ReplaceBuilder implements Builder {
54+
55+
protected function buildTable($parsed) {
56+
$builder = new TableBuilder();
57+
return $builder->build($parsed, 0);
58+
}
59+
60+
protected function buildSubQuery($parsed) {
61+
$builder = new SubQueryBuilder();
62+
return $builder->build($parsed, 0);
63+
}
64+
65+
protected function buildReserved($parsed) {
66+
$builder = new ReservedBuilder();
67+
return $builder->build($parsed);
68+
}
69+
70+
protected function buildBracketExpression($parsed) {
71+
$builder = new SelectBracketExpressionBuilder();
72+
return $builder->build($parsed);
73+
}
74+
75+
protected function buildColumnList($parsed) {
76+
$builder = new ReplaceColumnListBuilder();
77+
return $builder->build($parsed, 0);
78+
}
79+
80+
public function build(array $parsed) {
81+
$sql = '';
82+
foreach ($parsed as $k => $v) {
83+
$len = strlen($sql);
84+
$sql .= $this->buildTable($v);
85+
$sql .= $this->buildSubQuery($v);
86+
$sql .= $this->buildColumnList($v);
87+
$sql .= $this->buildReserved($v);
88+
$sql .= $this->buildBracketExpression($v);
89+
90+
if ($len == strlen($sql)) {
91+
throw new UnableToCreateSQLException('REPLACE', $k, $v, 'expr_type');
92+
}
93+
94+
$sql .= " ";
95+
}
96+
return 'REPLACE ' . substr($sql, 0, -1);
97+
}
98+
99+
}
100+
?>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* ReplaceColumnListBuilder.php
4+
*
5+
* Builds column-list parts of REPLACE statements.
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+
42+
namespace PHPSQLParser\builders;
43+
use PHPSQLParser\exceptions\UnableToCreateSQLException;
44+
use PHPSQLParser\utils\ExpressionType;
45+
46+
/**
47+
* This class implements the builder for column-list parts of REPLACE statements.
48+
* You can overwrite all functions to achieve another handling.
49+
*
50+
* @author André Rothe <[email protected]>
51+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
52+
*
53+
*/
54+
class ReplaceColumnListBuilder implements Builder {
55+
56+
protected function buildColumn($parsed) {
57+
$builder = new ColumnReferenceBuilder();
58+
return $builder->build($parsed);
59+
}
60+
61+
public function build(array $parsed) {
62+
if ($parsed['expr_type'] !== ExpressionType::COLUMN_LIST) {
63+
return "";
64+
}
65+
$sql = "";
66+
foreach ($parsed['sub_tree'] as $k => $v) {
67+
$len = strlen($sql);
68+
$sql .= $this->buildColumn($v);
69+
70+
if ($len == strlen($sql)) {
71+
throw new UnableToCreateSQLException('REPLACE column-list subtree', $k, $v, 'expr_type');
72+
}
73+
74+
$sql .= ", ";
75+
}
76+
return "(" . substr($sql, 0, -2) . ")";
77+
}
78+
79+
}
80+
?>

0 commit comments

Comments
 (0)