Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
clover.xml
clover.xml
composer.phar
10 changes: 10 additions & 0 deletions src/PHPSQLParser/PHPSQLCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
use PHPSQLParser\exceptions\UnsupportedFeatureException;
use PHPSQLParser\builders\SelectStatementBuilder;
use PHPSQLParser\builders\DeleteStatementBuilder;
use PHPSQLParser\builders\TruncateStatementBuilder;
use PHPSQLParser\builders\UpdateStatementBuilder;
use PHPSQLParser\builders\InsertStatementBuilder;
use PHPSQLParser\builders\CreateStatementBuilder;
use PHPSQLParser\builders\DropStatementBuilder;
use PHPSQLParser\builders\RenameStatementBuilder;
use PHPSQLParser\builders\ReplaceStatementBuilder;
use PHPSQLParser\builders\ShowStatementBuilder;
use PHPSQLParser\builders\BracketStatementBuilder;
use PHPSQLParser\builders\UnionStatementBuilder;
Expand Down Expand Up @@ -88,10 +90,18 @@ public function create($parsed) {
$builder = new InsertStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'REPLACE':
$builder = new ReplaceStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'DELETE':
$builder = new DeleteStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'TRUNCATE':
$builder = new TruncateStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'UPDATE':
$builder = new UpdateStatementBuilder();
$this->created = $builder->build($parsed);
Expand Down
8 changes: 7 additions & 1 deletion src/PHPSQLParser/builders/FunctionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ protected function buildSelectBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}


protected function buildSubQuery($parsed) {
$builder = new SubQueryBuilder();
return $builder->build($parsed);
}

public function build(array $parsed) {
if (($parsed['expr_type'] !== ExpressionType::AGGREGATE_FUNCTION)
&& ($parsed['expr_type'] !== ExpressionType::SIMPLE_FUNCTION)
Expand All @@ -104,6 +109,7 @@ public function build(array $parsed) {
$len = strlen($sql);
$sql .= $this->build($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildSubQuery($v);
$sql .= $this->buildColRef($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildSelectBracketExpression($v);
Expand Down
10 changes: 9 additions & 1 deletion src/PHPSQLParser/builders/InsertStatementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ protected function buildSELECT($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}


protected function buildSET($parsed) {
$builder = new SetBuilder();
return $builder->build($parsed);
}

public function build(array $parsed) {
// TODO: are there more than one tables possible (like [INSERT][1])
$sql = $this->buildINSERT($parsed['INSERT']);
if (isset($parsed['VALUES'])) {
$sql .= ' ' . $this->buildVALUES($parsed['VALUES']);
}
if (isset($parsed['SET'])) {
$sql .= ' ' . $this->buildSET($parsed['SET']);
}
if (isset($parsed['SELECT'])) {
$sql .= ' ' . $this->buildSELECT($parsed);
}
Expand Down
100 changes: 100 additions & 0 deletions src/PHPSQLParser/builders/ReplaceBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* ReplaceBuilder.php
*
* Builds the [REPLACE] statement part.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <[email protected]>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/

namespace PHPSQLParser\builders;
use PHPSQLParser\exceptions\UnableToCreateSQLException;

/**
* This class implements the builder for the [REPLACE] statement parts.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <[email protected]>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ReplaceBuilder implements Builder {

protected function buildTable($parsed) {
$builder = new TableBuilder();
return $builder->build($parsed, 0);
}

protected function buildSubQuery($parsed) {
$builder = new SubQueryBuilder();
return $builder->build($parsed, 0);
}

protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}

protected function buildBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}

protected function buildColumnList($parsed) {
$builder = new ReplaceColumnListBuilder();
return $builder->build($parsed, 0);
}

public function build(array $parsed) {
$sql = '';
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildTable($v);
$sql .= $this->buildSubQuery($v);
$sql .= $this->buildColumnList($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildBracketExpression($v);

if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('REPLACE', $k, $v, 'expr_type');
}

$sql .= " ";
}
return 'REPLACE ' . substr($sql, 0, -1);
}

}
?>
80 changes: 80 additions & 0 deletions src/PHPSQLParser/builders/ReplaceColumnListBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* ReplaceColumnListBuilder.php
*
* Builds column-list parts of REPLACE statements.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <[email protected]>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/

namespace PHPSQLParser\builders;
use PHPSQLParser\exceptions\UnableToCreateSQLException;
use PHPSQLParser\utils\ExpressionType;

/**
* This class implements the builder for column-list parts of REPLACE statements.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <[email protected]>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ReplaceColumnListBuilder implements Builder {

protected function buildColumn($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}

public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COLUMN_LIST) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColumn($v);

if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('REPLACE column-list subtree', $k, $v, 'expr_type');
}

$sql .= ", ";
}
return "(" . substr($sql, 0, -2) . ")";
}

}
?>
89 changes: 89 additions & 0 deletions src/PHPSQLParser/builders/ReplaceStatementBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* ReplaceStatement.php
*
* Builds the REPLACE statement
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <[email protected]>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/

namespace PHPSQLParser\builders;

/**
* This class implements the builder for the whole Replace statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <[email protected]>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ReplaceStatementBuilder implements Builder {

protected function buildVALUES($parsed) {
$builder = new ValuesBuilder();
return $builder->build($parsed);
}

protected function buildREPLACE($parsed) {
$builder = new ReplaceBuilder();
return $builder->build($parsed);
}

protected function buildSELECT($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}

protected function buildSET($parsed) {
$builder = new SetBuilder();
return $builder->build($parsed);
}

public function build(array $parsed) {
// TODO: are there more than one tables possible (like [REPLACE][1])
$sql = $this->buildREPLACE($parsed['REPLACE']);
if (isset($parsed['VALUES'])) {
$sql .= ' ' . $this->buildVALUES($parsed['VALUES']);
}
if (isset($parsed['SET'])) {
$sql .= ' ' . $this->buildSET($parsed['SET']);
}
if (isset($parsed['SELECT'])) {
$sql .= ' ' . $this->buildSELECT($parsed);
}
return $sql;
}
}
?>
16 changes: 16 additions & 0 deletions src/PHPSQLParser/builders/SelectStatementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ protected function buildLIMIT($parsed) {
$builder = new LimitBuilder();
return $builder->build($parsed);
}

protected function buildUNION($parsed) {
$builder = new UnionStatementBuilder();
return $builder->build($parsed);
}

protected function buildUNIONALL($parsed) {
$builder = new UnionAllStatementBuilder();
return $builder->build($parsed);
}

public function build(array $parsed) {
$sql = "";
Expand All @@ -108,6 +118,12 @@ public function build(array $parsed) {
}
if (isset($parsed['LIMIT'])) {
$sql .= " " . $this->buildLIMIT($parsed['LIMIT']);
}
if (isset($parsed['UNION'])) {
$sql .= " " . $this->buildUNION($parsed);
}
if (isset($parsed['UNION ALL'])) {
$sql .= " " . $this->buildUNIONALL($parsed);
}
return $sql;
}
Expand Down
Loading