Skip to content

Commit dd35448

Browse files
committed
New Insert functionality, Replace and Truncate
1 parent 9fe9056 commit dd35448

10 files changed

Lines changed: 444 additions & 2 deletions

src/PHPSQLParser/PHPSQLCreator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@
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;
5355
use PHPSQLParser\builders\UnionStatementBuilder;
@@ -88,10 +90,18 @@ public function create($parsed) {
8890
$builder = new InsertStatementBuilder();
8991
$this->created = $builder->build($parsed);
9092
break;
93+
case 'REPLACE':
94+
$builder = new ReplaceStatementBuilder();
95+
$this->created = $builder->build($parsed);
96+
break;
9197
case 'DELETE':
9298
$builder = new DeleteStatementBuilder();
9399
$this->created = $builder->build($parsed);
94100
break;
101+
case 'TRUNCATE':
102+
$builder = new TruncateStatementBuilder();
103+
$this->created = $builder->build($parsed);
104+
break;
95105
case 'UPDATE':
96106
$builder = new UpdateStatementBuilder();
97107
$this->created = $builder->build($parsed);

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+
?>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* ReplaceStatement.php
4+
*
5+
* Builds the REPLACE 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 <[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+
44+
/**
45+
* This class implements the builder for the whole Replace statement. You can overwrite
46+
* all functions to achieve another handling.
47+
*
48+
* @author André Rothe <[email protected]>
49+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
50+
*
51+
*/
52+
class ReplaceStatementBuilder implements Builder {
53+
54+
protected function buildVALUES($parsed) {
55+
$builder = new ValuesBuilder();
56+
return $builder->build($parsed);
57+
}
58+
59+
protected function buildREPLACE($parsed) {
60+
$builder = new ReplaceBuilder();
61+
return $builder->build($parsed);
62+
}
63+
64+
protected function buildSELECT($parsed) {
65+
$builder = new SelectStatementBuilder();
66+
return $builder->build($parsed);
67+
}
68+
69+
protected function buildSET($parsed) {
70+
$builder = new SetBuilder();
71+
return $builder->build($parsed);
72+
}
73+
74+
public function build(array $parsed) {
75+
// TODO: are there more than one tables possible (like [REPLACE][1])
76+
$sql = $this->buildREPLACE($parsed['REPLACE']);
77+
if (isset($parsed['VALUES'])) {
78+
$sql .= ' ' . $this->buildVALUES($parsed['VALUES']);
79+
}
80+
if (isset($parsed['SET'])) {
81+
$sql .= ' ' . $this->buildSET($parsed['SET']);
82+
}
83+
if (isset($parsed['SELECT'])) {
84+
$sql .= ' ' . $this->buildSELECT($parsed);
85+
}
86+
return $sql;
87+
}
88+
}
89+
?>

0 commit comments

Comments
 (0)