Initial commit

This commit is contained in:
2020-10-07 10:37:15 +02:00
commit ce5f440392
28157 changed files with 4429172 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?php
/**
* @author mfris
*
*/
namespace PHPSQLParser;
/**
*
* @author mfris
* @package PHPSQLParser
*/
final class Options
{
/**
* @var array
*/
private $options;
/**
* @const string
*/
const CONSISTENT_SUB_TREES = 'consistent_sub_trees';
/**
* @const string
*/
const ANSI_QUOTES = 'ansi_quotes';
/**
* Options constructor.
*
* @param array $options
*/
public function __construct(array $options)
{
$this->options = $options;
}
/**
* @return bool
*/
public function getConsistentSubtrees()
{
return (isset($this->options[self::CONSISTENT_SUB_TREES]) && $this->options[self::CONSISTENT_SUB_TREES]);
}
/**
* @return bool
*/
public function getANSIQuotes()
{
return (isset($this->options[self::ANSI_QUOTES]) && $this->options[self::ANSI_QUOTES]);
}
}

View File

@@ -0,0 +1,142 @@
<?php
/**
* PHPSQLCreator.php
*
* A creator, which generates SQL from the output of PHPSQLParser.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 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 <andre.rothe@phosco.info>
* @copyright 2010-2014 André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser;
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;
use PHPSQLParser\builders\UnionAllStatementBuilder;
use PHPSQLParser\builders\AlterStatementBuilder;
/**
* This class generates SQL from the output of the PHPSQLParser.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class PHPSQLCreator {
public function __construct($parsed = false) {
if ($parsed) {
$this->create($parsed);
}
}
public function create($parsed) {
$k = key($parsed);
switch ($k) {
case 'UNION':
$builder = new UnionStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'UNION ALL':
$builder = new UnionAllStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'SELECT':
$builder = new SelectStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'INSERT':
$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);
break;
case 'RENAME':
$builder = new RenameStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'SHOW':
$builder = new ShowStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'CREATE':
$builder = new CreateStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'BRACKET':
$builder = new BracketStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'DROP':
$builder = new DropStatementBuilder();
$this->created = $builder->build($parsed);
break;
case 'ALTER':
$builder = new AlterStatementBuilder();
$this->created = $builder->build($parsed);
break;
default:
throw new UnsupportedFeatureException($k);
break;
}
return $this->created;
}
}
?>

View File

@@ -0,0 +1,139 @@
<?php
/**
* PHPSQLParser.php
*
* A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL
*
* 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 <andre.rothe@phosco.info>
* @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;
use PHPSQLParser\positions\PositionCalculator;
use PHPSQLParser\processors\DefaultProcessor;
use PHPSQLParser\utils\PHPSQLParserConstants;
/**
* This class implements the parser functionality.
*
* @author Justin Swanhart <greenlion@gmail.com>
* @author André Rothe <arothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*/
class PHPSQLParser {
public $parsed;
/**
* @var Options
*/
private $options;
/**
* Constructor. It simply calls the parse() function.
* Use the public variable $parsed to get the output.
*
* @param String|bool $sql The SQL statement.
* @param bool $calcPositions True, if the output should contain [position], false otherwise.
* @param array $options
*/
public function __construct($sql = false, $calcPositions = false, array $options = array()) {
$this->options = new Options($options);
if ($sql) {
$this->parse($sql, $calcPositions);
}
}
/**
* It parses the given SQL statement and generates a detailled
* output array for every part of the statement. The method can
* also generate [position] fields within the output, which hold
* the character position for every statement part. The calculation
* of the positions needs some time, if you don't need positions in
* your application, set the parameter to false.
*
* @param String $sql The SQL statement.
* @param boolean $calcPositions True, if the output should contain [position], false otherwise.
*
* @return array An associative array with all meta information about the SQL statement.
*/
public function parse($sql, $calcPositions = false) {
$processor = new DefaultProcessor($this->options);
$queries = $processor->process($sql);
// calc the positions of some important tokens
if ($calcPositions) {
$calculator = new PositionCalculator();
$queries = $calculator->setPositionsWithinSQL($sql, $queries);
}
// store the parsed queries
$this->parsed = $queries;
return $this->parsed;
}
/**
* Add a custom function to the parser. no return value
*
* @param String $token The name of the function to add
*
* @return null
*/
public function addCustomFunction($token) {
PHPSQLParserConstants::getInstance()->addCustomFunction($token);
}
/**
* Remove a custom function from the parser. no return value
*
* @param String $token The name of the function to remove
*
* @return null
*/
public function removeCustomFunction($token) {
PHPSQLParserConstants::getInstance()->removeCustomFunction($token);
}
/**
* Returns the list of custom functions
*
* @return array Returns an array of all custom functions
*/
public function getCustomFunctions() {
return PHPSQLParserConstants::getInstance()->getCustomFunctions();
}
}
?>

View File

@@ -0,0 +1,70 @@
<?php
/**
* AliasBuilder.php
*
* Builds aliases.
*
* 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 <andre.rothe@phosco.info>
* @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 aliases.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class AliasBuilder implements Builder {
public function hasAlias($parsed) {
return isset($parsed['alias']);
}
public function build(array $parsed) {
if (!isset($parsed['alias']) || $parsed['alias'] === false) {
return "";
}
$sql = "";
if ($parsed['alias']['as']) {
$sql .= " AS";
}
$sql .= " " . $parsed['alias']['name'];
return $sql;
}
}
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* AliasReferenceBuilder.php
*
* Builds Alias references.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for alias references.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class AliasReferenceBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::ALIAS) {
return "";
}
$sql = $parsed['base_expr'];
return $sql;
}
}
?>

View File

@@ -0,0 +1,35 @@
<?php
namespace PHPSQLParser\builders;
/**
* This class implements the builder for the [DELETE] part. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class AlterBuilder implements Builder
{
public function build(array $parsed)
{
$sql = '';
foreach ($parsed as $term) {
if ($term === ' ') {
continue;
}
if (substr($term, 0, 1) === '(' ||
strpos($term, "\n") !== false) {
$sql = rtrim($sql);
}
$sql .= $term . ' ';
}
$sql = rtrim($sql);
return $sql;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace PHPSQLParser\builders;
class AlterStatementBuilder implements Builder
{
protected function buildSubTree($parsed) {
$builder = new SubTreeBuilder();
return $builder->build($parsed);
}
private function buildAlter($parsed)
{
$builder = new AlterBuilder();
return $builder->build($parsed);
}
public function build(array $parsed)
{
$alter = $parsed['ALTER'];
$sql = $this->buildAlter($alter);
return $sql;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* BracketStatementBuilder.php
*
* Builds the parentheses around a 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 <andre.rothe@phosco.info>
* @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 parentheses around a statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class BracketStatementBuilder implements Builder {
protected function buildSelectBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed, " ");
}
protected function buildSelectStatement($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = "";
foreach ($parsed['BRACKET'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildSelectBracketExpression($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('BRACKET', $k, $v, 'expr_type');
}
}
return trim($sql . " " . trim($this->buildSelectStatement($parsed)));
}
}
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* Builder.php
*
* Interface declaration for all builder classes.
*
* 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 <andre.rothe@phosco.info>
* @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;
/**
* A builder can create a part of an SQL statement. The necessary information
* are provided by the function parameter as array. This array is a subtree
* of the PHPSQLParser output.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*/
interface Builder {
/**
* Builds a part of an SQL statement.
*
* @param array $parsed a subtree of the PHPSQLParser output array
*
* @return A string, which contains a part of an SQL statement.
*/
public function build(array $parsed);
}
?>

View File

@@ -0,0 +1,91 @@
<?php
/**
* CharacterSetBuilder.php
*
* Builds the CHARACTER SET part of a CREATE TABLE 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 <andre.rothe@phosco.info>
* @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 the CHARACTER SET statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CharacterSetBuilder implements Builder {
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::CHARSET) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildOperator($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE options CHARACTER SET subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,85 @@
<?php
/**
* CheckBuilder.php
*
* Builds the CHECK statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the CHECK statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CheckBuilder implements Builder {
protected function buildSelectBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::CHECK) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildSelectBracketExpression($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE check subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,91 @@
<?php
/**
* CollationBuilder.php
*
* Builds the collation expression part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the collation statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CollationBuilder implements Builder {
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COLLATE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildConstant($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE options collation subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,85 @@
<?php
/**
* ColumnDefinitionBuilder.php
*
* Builds the column definition statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the columndefinition statement part
* of CREATE TABLE. You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ColumnDefinitionBuilder implements Builder {
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildColumnType($parsed) {
$builder = new ColumnTypeBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COLDEF) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildColumnType($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE primary key subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* ColumnListBuilder.php
*
* Builds column-list parts of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ColumnListBuilder implements Builder {
protected function buildIndexColumn($parsed) {
$builder = new IndexColumnBuilder();
return $builder->build($parsed);
}
protected function buildColumnReference($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
public function build(array $parsed, $delim = ', ') {
if ($parsed['expr_type'] !== ExpressionType::COLUMN_LIST) {
return '';
}
$sql = '';
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildIndexColumn($v);
$sql .= $this->buildColumnReference($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE column-list subtree', $k, $v, 'expr_type');
}
$sql .= $delim;
}
return '(' . substr($sql, 0, -strlen($delim)) . ')';
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* ColumnReferenceBuilder.php
*
* Builds Column references.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for column references.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ColumnReferenceBuilder implements Builder {
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COLREF) {
return "";
}
$sql = $parsed['base_expr'];
$sql .= $this->buildAlias($parsed);
return $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* ColumnTypeExpressionBuilder.php
*
* Builds the bracket expressions within a column type.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for bracket expressions within a column type.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ColumnTypeBracketExpressionBuilder implements Builder {
protected function buildSubTree($parsed, $delim) {
$builder = new SubTreeBuilder();
return $builder->build($parsed, $delim);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
return "";
}
$sql = $this->buildSubTree($parsed, ",");
$sql = "(" . $sql . ")";
return $sql;
}
}
?>

View File

@@ -0,0 +1,107 @@
<?php
/**
* ColumnTypeBuilder.php
*
* Builds the column type statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the column type statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ColumnTypeBuilder implements Builder {
protected function buildColumnTypeBracketExpression($parsed) {
$builder = new ColumnTypeBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildDataType($parsed) {
$builder = new DataTypeBuilder();
return $builder->build($parsed);
}
protected function buildDefaultValue($parsed) {
$builder = new DefaultValueBuilder();
return $builder->build($parsed);
}
protected function buildCharacterSet($parsed) {
if ($parsed['expr_type'] !== ExpressionType::CHARSET) {
return "";
}
return $parsed['base_expr'];
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COLUMN_TYPE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildDataType($v);
$sql .= $this->buildColumnTypeBracketExpression($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildDefaultValue($v);
$sql .= $this->buildCharacterSet($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE column-type subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* ConstantBuilder.php
*
* Builds constant (String, Integer, etc.) parts.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for constants.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ConstantBuilder implements Builder {
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::CONSTANT) {
return "";
}
$sql = $parsed['base_expr'];
$sql .= $this->buildAlias($parsed);
return $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* ConstraintBuilder.php
*
* Builds the constraint statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the constraint statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ConstraintBuilder implements Builder {
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::CONSTRAINT) {
return '';
}
$sql = $parsed['sub_tree'] === false ? '' : $this->buildConstant($parsed['sub_tree']);
return "CONSTRAINT" . (empty($sql) ? '' : (' ' . $sql));
}
}
?>

View File

@@ -0,0 +1,87 @@
<?php
/**
* CreateBuilder.php
*
* Builds the CREATE 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the [CREATE] part. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateBuilder implements Builder {
protected function buildCreateTable($parsed) {
$builder = new CreateTableBuilder();
return $builder->build($parsed);
}
protected function buildCreateIndex($parsed) {
$builder = new CreateIndexBuilder();
return $builder->build($parsed);
}
protected function buildSubTree($parsed) {
$builder = new SubTreeBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$create = $parsed['CREATE'];
$sql = $this->buildSubTree($create);
if (($create['expr_type'] === ExpressionType::TABLE)
|| ($create['expr_type'] === ExpressionType::TEMPORARY_TABLE)) {
$sql .= ' ' . $this->buildCreateTable($parsed['TABLE']);
}
if ($create['expr_type'] === ExpressionType::INDEX) {
$sql .= ' ' . $this->buildCreateIndex($parsed['INDEX']);
}
// TODO: add more expr_types here (like VIEW), if available in parser output
return "CREATE " . $sql;
}
}
?>

View File

@@ -0,0 +1,80 @@
<?php
/**
* CreateIndex.php
*
* Builds the CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 CREATE INDEX statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateIndexBuilder implements Builder {
protected function buildIndexType($parsed) {
$builder = new CreateIndexTypeBuilder();
return $builder->build($parsed);
}
protected function buildIndexTable($parsed) {
$builder = new CreateIndexTableBuilder();
return $builder->build($parsed);
}
protected function buildIndexOptions($parsed) {
$builder = new CreateIndexOptionsBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $parsed['name'];
$sql .= ' ' . $this->buildIndexType($parsed);
$sql = trim($sql);
$sql .= ' ' . $this->buildIndexTable($parsed);
$sql = trim($sql);
$sql .= $this->buildIndexOptions($parsed);
return trim($sql);
}
}
?>

View File

@@ -0,0 +1,109 @@
<?php
/**
* CreateIndexOptionsBuilder.php
*
* Builds index options part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 index options of a CREATE INDEX
* statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateIndexOptionsBuilder implements Builder {
protected function buildIndexParser($parsed) {
$builder = new IndexParserBuilder();
return $builder->build($parsed);
}
protected function buildIndexSize($parsed) {
$builder = new IndexSizeBuilder();
return $builder->build($parsed);
}
protected function buildIndexType($parsed) {
$builder = new IndexTypeBuilder();
return $builder->build($parsed);
}
protected function buildIndexComment($parsed) {
$builder = new IndexCommentBuilder();
return $builder->build($parsed);
}
protected function buildIndexAlgorithm($parsed) {
$builder = new IndexAlgorithmBuilder();
return $builder->build($parsed);
}
protected function buildIndexLock($parsed) {
$builder = new IndexLockBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['options'] === false) {
return '';
}
$sql = '';
foreach ($parsed['options'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildIndexAlgorithm($v);
$sql .= $this->buildIndexLock($v);
$sql .= $this->buildIndexComment($v);
$sql .= $this->buildIndexParser($v);
$sql .= $this->buildIndexSize($v);
$sql .= $this->buildIndexType($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE INDEX options', $k, $v, 'expr_type');
}
$sql .= ' ';
}
return ' ' . substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,72 @@
<?php
/**
* CreateIndexTable.php
*
* Builds the table part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the table part of a CREATE INDEX statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateIndexTableBuilder implements Builder {
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if (!isset($parsed['on']) || $parsed['on'] === false) {
return '';
}
$table = $parsed['on'];
if ($table['expr_type'] !== ExpressionType::TABLE) {
return '';
}
return 'ON ' . $table['name'] . ' ' . $this->buildColumnList($table['sub_tree']);
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* CreateIndexTypeBuilder.php
*
* Builds index type part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 index type of a CREATE INDEX
* statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateIndexTypeBuilder extends IndexTypeBuilder {
public function build(array $parsed) {
if (!isset($parsed['index-type']) || $parsed['index-type'] === false) {
return '';
}
return parent::build($parsed['index-type']);
}
}
?>

View File

@@ -0,0 +1,80 @@
<?php
/**
* CreateStatement.php
*
* Builds the CREATE 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 <andre.rothe@phosco.info>
* @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 Create statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateStatementBuilder implements Builder {
protected function buildLIKE($parsed) {
$builder = new LikeBuilder();
return $builder->build($parsed);
}
protected function buildSelectStatement($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}
protected function buildCREATE($parsed) {
$builder = new CreateBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $this->buildCREATE($parsed);
if (isset($parsed['LIKE'])) {
$sql .= " " . $this->buildLIKE($parsed['LIKE']);
}
if (isset($parsed['SELECT'])) {
$sql .= " " . $this->buildSelectStatement($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,78 @@
<?php
/**
* CreateTable.php
*
* Builds the CREATE TABLE 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 <andre.rothe@phosco.info>
* @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 CREATE TABLE statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateTableBuilder implements Builder {
protected function buildCreateTableDefinition($parsed) {
$builder = new CreateTableDefinitionBuilder();
return $builder->build($parsed);
}
protected function buildCreateTableOptions($parsed) {
$builder = new CreateTableOptionsBuilder();
return $builder->build($parsed);
}
protected function buildCreateTableSelectOption($parsed) {
$builder = new CreateTableSelectOptionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $parsed['name'];
$sql .= $this->buildCreateTableDefinition($parsed);
$sql .= $this->buildCreateTableOptions($parsed);
$sql .= $this->buildCreateTableSelectOption($parsed);
return $sql;
}
}
?>

View File

@@ -0,0 +1,66 @@
<?php
/**
* CreateTableDefinitionBuilder.php
*
* Builds the create definitions of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 create definitions of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateTableDefinitionBuilder implements Builder {
protected function buildTableBracketExpression($parsed) {
$builder = new TableBracketExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if (!isset($parsed) || $parsed['create-def'] === false) {
return "";
}
return $this->buildTableBracketExpression($parsed['create-def']);
}
}
?>

View File

@@ -0,0 +1,102 @@
<?php
/**
* CreateTableOptionsBuilder.php
*
* Builds the table-options statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 table-options statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateTableOptionsBuilder implements Builder {
protected function buildExpression($parsed) {
$builder = new SelectExpressionBuilder();
return $builder->build($parsed);
}
protected function buildCharacterSet($parsed) {
$builder = new CharacterSetBuilder();
return $builder->build($parsed);
}
protected function buildCollation($parsed) {
$builder = new CollationBuilder();
return $builder->build($parsed);
}
/**
* Returns a well-formatted delimiter string. If you don't need nice SQL,
* you could simply return $parsed['delim'].
*
* @param array $parsed The part of the output array, which contains the current expression.
* @return a string, which is added right after the expression
*/
protected function getDelimiter($parsed) {
return ($parsed['delim'] === false ? '' : (trim($parsed['delim']) . ' '));
}
public function build(array $parsed) {
if (!isset($parsed['options']) || $parsed['options'] === false) {
return "";
}
$options = $parsed['options'];
$sql = "";
foreach ($options as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildExpression($v);
$sql .= $this->buildCharacterSet($v);
$sql .= $this->buildCollation($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE options', $k, $v, 'expr_type');
}
$sql .= $this->getDelimiter($v);
}
return " " . substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,65 @@
<?php
/**
* CreateTableSelectOptionBuilder.php
*
* Builds the select-options statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 select-options statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateTableSelectOptionBuilder implements Builder {
public function build(array $parsed) {
if (!isset($parsed['select-option']) || $parsed['select-option'] === false) {
return "";
}
$option = $parsed['select-option'];
$sql = ($option['duplicates'] === false ? '' : (' ' . $option['duplicates']));
$sql .= ($option['as'] === false ? '' : ' AS');
return $sql;
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* DataTypeBuilder.php
*
* Builds the data-type statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the data-type statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DataTypeBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::DATA_TYPE) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* DatabaseBuilder.php
*
* Builds the database within the SHOW 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for a database within SHOW statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DatabaseBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::DATABASE) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* DefaultValueBuilder.php
*
* Builds the default value statement part of a column of a CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the default value statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DefaultValueBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::DEF_VALUE) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,74 @@
<?php
/**
* DeleteBuilder.php
*
* Builds the DELETE 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 <andre.rothe@phosco.info>
* @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 [DELETE] part. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DeleteBuilder implements Builder {
public function build(array $parsed) {
$sql = "DELETE ";
$right = -1;
if ($parsed['options'] !== false) {
foreach ($parsed['options'] as $k => $v) {
$sql .= $v . " ";
}
}
if ($parsed['tables'] !== false) {
foreach ($parsed['tables'] as $k => $v) {
$sql .= $v . ", ";
$right = -2;
}
}
return substr($sql, 0, $right);
}
}
?>

View File

@@ -0,0 +1,78 @@
<?php
/**
* DeleteStatementBuilder.php
*
* Builds the DELETE 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 <andre.rothe@phosco.info>
* @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 Delete statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DeleteStatementBuilder implements Builder {
protected function buildWHERE($parsed) {
$builder = new WhereBuilder();
return $builder->build($parsed);
}
protected function buildFROM($parsed) {
$builder = new FromBuilder();
return $builder->build($parsed);
}
protected function buildDELETE($parsed) {
$builder = new DeleteBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $this->buildDELETE($parsed['DELETE']) . " " . $this->buildFROM($parsed['FROM']);
if (isset($parsed['WHERE'])) {
$sql .= " " . $this->buildWHERE($parsed['WHERE']);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,61 @@
<?php
/**
* DirectionBuilder.php
*
* Builds direction (e.g. of the order-by clause).
*
* 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 <andre.rothe@phosco.info>
* @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 directions (e.g. of the order-by clause).
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DirectionBuilder implements Builder {
public function build(array $parsed) {
if (!isset($parsed['direction']) || $parsed['direction'] === false) {
return "";
}
return (" " . $parsed['direction']);
}
}
?>

View File

@@ -0,0 +1,100 @@
<?php
/**
* DropBuilder.php
*
* Builds the CREATE 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 <andre.rothe@phosco.info>
* @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 the [DROP] part. You can overwrite
* all functions to achieve another handling.
*/
class DropBuilder implements Builder {
protected function buildDropIndex( $parsed ) {
$builder = new DropIndexBuilder();
return $builder->build( $parsed );
}
protected function buildReserved( $parsed ) {
$builder = new ReservedBuilder();
return $builder->build( $parsed );
}
protected function buildExpression( $parsed ) {
$builder = new DropExpressionBuilder();
return $builder->build( $parsed );
}
protected function buildSubTree( $parsed ) {
$sql = '';
foreach ( $parsed['sub_tree'] as $k => $v ) {
$len = strlen( $sql );
$sql .= $this->buildReserved( $v );
$sql .= $this->buildExpression( $v );
if ( $len == strlen( $sql ) ) {
throw new UnableToCreateSQLException( 'DROP subtree', $k, $v, 'expr_type' );
}
$sql .= ' ';
}
return $sql;
}
public function build( array $parsed ) {
$drop = $parsed['DROP'];
$sql = $this->buildSubTree( $drop );
if ( $drop['expr_type'] === ExpressionType::INDEX ) {
$sql .= '' . $this->buildDropIndex( $parsed['INDEX'] ) . ' ';
}
return 'DROP ' . substr( $sql, 0, -1 );
}
}
?>

View File

@@ -0,0 +1,103 @@
<?php
/**
* DropExpressionBuilder.php
*
* Builds the object list of a DROP 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 <andre.rothe@phosco.info>
* @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 the object list of a DROP statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DropExpressionBuilder implements Builder {
protected function buildTable($parsed, $index) {
$builder = new TableBuilder();
return $builder->build($parsed, $index);
}
protected function buildDatabase($parsed) {
$builder = new DatabaseBuilder();
return $builder->build($parsed);
}
protected function buildSchema($parsed) {
$builder = new SchemaBuilder();
return $builder->build($parsed);
}
protected function buildTemporaryTable($parsed) {
$builder = new TempTableBuilder();
return $builder->build($parsed);
}
protected function buildView($parsed) {
$builder = new ViewBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
return "";
}
$sql = '';
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildTable($v, 0);
$sql .= $this->buildView($v);
$sql .= $this->buildSchema($v);
$sql .= $this->buildDatabase($v);
$sql .= $this->buildTemporaryTable($v, 0);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('DROP object-list subtree', $k, $v, 'expr_type');
}
$sql .= ', ';
}
return substr($sql, 0, -2);
}
}
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* DropIndexBuilder.php
*
* Builds the CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 DROP INDEX statement. You can overwrite
* all functions to achieve another handling.
*/
class DropIndexBuilder implements Builder {
protected function buildIndexTable($parsed) {
$builder = new DropIndexTableBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $parsed['name'];
$sql = trim($sql);
$sql .= ' ' . $this->buildIndexTable($parsed);
return trim($sql);
}
}
?>

View File

@@ -0,0 +1,67 @@
<?php
/**
* DropIndexTable.php
*
* Builds the table part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for the table part of a DROP INDEX statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DropIndexTableBuilder implements Builder {
public function build(array $parsed) {
if (!isset($parsed['on']) || $parsed['on'] === false) {
return '';
}
$table = $parsed['on'];
if ($table['expr_type'] !== ExpressionType::TABLE) {
return '';
}
return 'ON ' . $table['name'];
}
}
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* DropStatement.php
*
* Builds the DROP 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 <andre.rothe@phosco.info>
* @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 DROP TABLE statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DropStatementBuilder implements Builder {
protected function buildDROP( $parsed ) {
$builder = new DropBuilder();
return $builder->build( $parsed );
}
public function build( array $parsed ) {
return $this->buildDROP( $parsed );
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* DatabaseBuilder.php
*
* Builds the database within the SHOW 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for a database within SHOW statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class EngineBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::ENGINE) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,97 @@
<?php
/**
* ForeignKeyBuilder.php
*
* Builds the FOREIGN KEY statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the FOREIGN KEY statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ForeignKeyBuilder implements Builder {
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildForeignRef($parsed) {
$builder = new ForeignRefBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::FOREIGN_KEY) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildConstant($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildColumnList($v);
$sql .= $this->buildForeignRef($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE foreign key subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,92 @@
<?php
/**
* ForeignRefBuilder.php
*
* Builds the FOREIGN KEY REFERENCES statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the FOREIGN KEY REFERENCES statement
* part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ForeignRefBuilder implements Builder {
protected function buildTable($parsed) {
$builder = new TableBuilder();
return $builder->build($parsed, 0);
}
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::REFERENCE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildTable($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildColumnList($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE foreign ref subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,110 @@
<?php
/**
* FromBuilder.php
*
* Builds the FROM 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 <andre.rothe@phosco.info>
* @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 [FROM] part. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class FromBuilder implements Builder {
protected function buildTable($parsed, $key) {
$builder = new TableBuilder();
return $builder->build($parsed, $key);
}
protected function buildTableExpression($parsed, $key) {
$builder = new TableExpressionBuilder();
return $builder->build($parsed, $key);
}
protected function buildSubQuery($parsed, $key) {
$builder = new SubQueryBuilder();
return $builder->build($parsed, $key);
}
public function build(array $parsed) {
$sql = "";
if (array_key_exists("UNION ALL", $parsed) || array_key_exists("UNION", $parsed)) {
foreach ($parsed as $union_type => $outer_v) {
$first = true;
foreach ($outer_v as $item) {
if (!$first) {
$sql .= " $union_type ";
}
else {
$first = false;
}
$select_builder = new SelectStatementBuilder();
$len = strlen($sql);
$sql .= $select_builder->build($item);
if ($len === strlen($sql)) {
throw new UnableToCreateSQLException('FROM', $union_type, $outer_v, 'expr_type');
}
}
}
}
else {
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildTable($v, $k);
$sql .= $this->buildTableExpression($v, $k);
$sql .= $this->buildSubquery($v, $k);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('FROM', $k, $v, 'expr_type');
}
}
}
return "FROM " . $sql;
}
}
?>

View File

@@ -0,0 +1,128 @@
<?php
/**
* FunctionBuilder.php
*
* Builds function statements.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2015 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 <andre.rothe@phosco.info>
* @copyright 2010-2015 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 function calls.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class FunctionBuilder implements Builder {
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function isReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->isReserved($parsed);
}
protected function buildSelectExpression($parsed) {
$builder = new SelectExpressionBuilder();
return $builder->build($parsed);
}
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)
&& ($parsed['expr_type'] !== ExpressionType::CUSTOM_FUNCTION)) {
return "";
}
if ($parsed['sub_tree'] === false) {
return $parsed['base_expr'] . "()" . $this->buildAlias($parsed);
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$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);
$sql .= $this->buildSelectExpression($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('function subtree', $k, $v, 'expr_type');
}
$sql .= ($this->isReserved($v) ? " " : ",");
}
return $parsed['base_expr'] . "(" . substr($sql, 0, -1) . ")" . $this->buildAlias($parsed);
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* GroupByAliasBuilder.php
*
* Builds an alias within a GROUP-BY clause.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for an alias within the GROUP-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class GroupByAliasBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::ALIAS) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,101 @@
<?php
/**
* GroupByBuilder.php
*
* Builds the GROUP-BY clause.
*
* 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 <andre.rothe@phosco.info>
* @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 GROUP-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class GroupByBuilder implements Builder {
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildPosition($parsed) {
$builder = new PositionBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildGroupByAlias($parsed) {
$builder = new GroupByAliasBuilder();
return $builder->build($parsed);
}
protected function buildGroupByExpression($parsed) {
$builder = new GroupByExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = "";
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildPosition($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildGroupByExpression($v);
$sql .= $this->buildGroupByAlias($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('GROUP', $k, $v, 'expr_type');
}
$sql .= ", ";
}
$sql = substr($sql, 0, -2);
return "GROUP BY " . $sql;
}
}
?>

View File

@@ -0,0 +1,89 @@
<?php
/**
* GroupByExpressionBuilder.php
*
* Builds an expression within a GROUP-BY clause.
*
* 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 oohook <oohook@163.com>
* @copyright 2010-2016 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
* @example group by id desc
*
*/
namespace PHPSQLParser\builders;
use PHPSQLParser\exceptions\UnableToCreateSQLException;
use PHPSQLParser\utils\ExpressionType;
/**
* This class implements the builder for an alias within the GROUP-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author oohook <oohook@163.com>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class GroupByExpressionBuilder implements Builder {
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildReserved($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('GROUP expression subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
$sql = substr($sql, 0, -1);
return $sql;
}
}
?>

View File

@@ -0,0 +1,90 @@
<?php
/**
* HavingBracketExpressionBuilder.php
*
* Builds bracket expressions within the HAVING 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 <andre.rothe@phosco.info>
* @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 bracket expressions within the HAVING part.
* You can overwrite all functions to achieve another handling.
*
* @author Ian Barker <ian@theorganicagency.com>
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class HavingBracketExpressionBuilder extends WhereBracketExpressionBuilder {
protected function buildHavingExpression($parsed) {
$builder = new HavingExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildInList($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildHavingExpression($v);
$sql .= $this->build($v);
$sql .= $this->buildUserVariable($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('HAVING expression subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
$sql = "(" . substr($sql, 0, -1) . ")";
return $sql;
}
}
?>

View File

@@ -0,0 +1,97 @@
<?php
/**
* HavingBuilder.php
*
* Builds the HAVING 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 <andre.rothe@phosco.info>
* @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 HAVING part.
* You can overwrite all functions to achieve another handling.
*
* @author Ian Barker <ian@theorganicagency.com>
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class HavingBuilder extends WhereBuilder {
protected function buildAliasReference($parsed) {
$builder = new AliasReferenceBuilder();
return $builder->build($parsed);
}
protected function buildHavingExpression($parsed) {
$builder = new HavingExpressionBuilder();
return $builder->build($parsed);
}
protected function buildHavingBracketExpression($parsed) {
$builder = new HavingBracketExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = "HAVING ";
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildAliasReference($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildColRef($v);
$sql .= $this->buildSubQuery($v);
$sql .= $this->buildInList($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildHavingExpression($v);
$sql .= $this->buildHavingBracketExpression($v);
$sql .= $this->buildUserVariable($v);
if (strlen($sql) == $len) {
throw new UnableToCreateSQLException('HAVING', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,94 @@
<?php
/**
* HavingExpressionBuilder.php
*
* Builds expressions within the HAVING 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 <andre.rothe@phosco.info>
* @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 expressions within the HAVING part.
* You can overwrite all functions to achieve another handling.
*
* @author Ian Barker <ian@theorganicagency.com>
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class HavingExpressionBuilder extends WhereExpressionBuilder {
protected function buildHavingExpression($parsed) {
return $this->build($parsed);
}
protected function buildHavingBracketExpression($parsed) {
$builder = new HavingBracketExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildInList($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildHavingExpression($v);
$sql .= $this->buildHavingBracketExpression($v);
$sql .= $this->buildUserVariable($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('HAVING expression subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
$sql = substr($sql, 0, -1);
return $sql;
}
}
?>

View File

@@ -0,0 +1,68 @@
<?php
/**
* InListBuilder.php
*
* Builds lists of values for the IN 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder list of values for the IN statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class InListBuilder implements Builder {
protected function buildSubTree($parsed, $delim) {
$builder = new SubTreeBuilder();
return $builder->build($parsed, $delim);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::IN_LIST) {
return "";
}
$sql = $this->buildSubTree($parsed, ", ");
return "(" . $sql . ")";
}
}
?>

View File

@@ -0,0 +1,91 @@
<?php
/**
* IndexAlgorithmBuilder.php
*
* Builds index algorithm part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 the index algorithm of CREATE INDEX statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexAlgorithmBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_ALGORITHM) {
return '';
}
$sql = '';
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildOperator($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE INDEX algorithm subtree', $k, $v, 'expr_type');
}
$sql .= ' ';
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,75 @@
<?php
/**
* IndexColumnBuilder.php
*
* Builds the column entries of the column-list parts of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for index column entries of the column-list
* parts of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexColumnBuilder implements Builder {
protected function buildLength($parsed) {
return ($parsed === false ? '' : ('(' . $parsed . ')'));
}
protected function buildDirection($parsed) {
return ($parsed === false ? '' : (' ' . $parsed));
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_COLUMN) {
return "";
}
$sql = $parsed['name'];
$sql .= $this->buildLength($parsed['length']);
$sql .= $this->buildDirection($parsed['dir']);
return $sql;
}
}
?>

View File

@@ -0,0 +1,85 @@
<?php
/**
* IndexCommentBuilder.php
*
* Builds index comment part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 the index comment of CREATE INDEX statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexCommentBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::COMMENT) {
return '';
}
$sql = '';
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE INDEX comment subtree', $k, $v, 'expr_type');
}
$sql .= ' ';
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,70 @@
<?php
/**
* IndexHintListBuilder.php
*
* Builds the index hint list of a table.
*
* 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 <andre.rothe@phosco.info>
* @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 index hint lists.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexHintListBuilder implements Builder {
public function hasHint($parsed) {
return isset($parsed['hints']);
}
// TODO: the hint list should be enhanced to get base_expr fro position calculation
public function build(array $parsed) {
if (!isset($parsed['hints']) || $parsed['hints'] === false) {
return "";
}
$sql = "";
foreach ($parsed['hints'] as $k => $v) {
$sql .= $v['hint_type'] . " " . $v['hint_list'] . " ";
}
return " " . substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,97 @@
<?php
/**
* IndexKeyBuilder.php
*
* Builds index key part of a CREATE TABLE 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 <andre.rothe@phosco.info>
* @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 the index key part of a CREATE TABLE statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexKeyBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildIndexType($parsed) {
$builder = new IndexTypeBuilder();
return $builder->build($parsed);
}
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildColumnList($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildIndexType($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE index key subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,91 @@
<?php
/**
* IndexLockBuilder.php
*
* Builds index lock part of a CREATE INDEX 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 <andre.rothe@phosco.info>
* @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 the index lock of CREATE INDEX statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexLockBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_LOCK) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildOperator($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE INDEX lock subtree', $k, $v, 'expr_type');
}
$sql .= ' ';
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* IndexParserBuilder.php
*
* Builds index parser part of a PRIMARY KEY statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the index parser of a PRIMARY KEY
* statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexParserBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_PARSER) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE primary key index parser subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* IndexSizeBuilder.php
*
* Builds index size part of a PRIMARY KEY statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the index size of a PRIMARY KEY
* statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexSizeBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_SIZE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE primary key index size subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,80 @@
<?php
/**
* IndexTypeBuilder.php
*
* Builds index type part of a PRIMARY KEY statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the index type of a PRIMARY KEY
* statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexTypeBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::INDEX_TYPE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE primary key index type subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,100 @@
<?php
/**
* InsertBuilder.php
*
* Builds the [INSERT] 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 <andre.rothe@phosco.info>
* @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 [INSERT] statement parts.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class InsertBuilder 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 InsertColumnListBuilder();
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('INSERT', $k, $v, 'expr_type');
}
$sql .= " ";
}
return 'INSERT ' . substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,80 @@
<?php
/**
* InsertColumnListBuilder.php
*
* Builds column-list parts of INSERT 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 <andre.rothe@phosco.info>
* @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 INSERT statements.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class InsertColumnListBuilder 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('INSERT column-list subtree', $k, $v, 'expr_type');
}
$sql .= ", ";
}
return "(" . substr($sql, 0, -2) . ")";
}
}
?>

View File

@@ -0,0 +1,89 @@
<?php
/**
* InsertStatement.php
*
* Builds the INSERT 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 <andre.rothe@phosco.info>
* @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 Insert statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class InsertStatementBuilder implements Builder {
protected function buildVALUES($parsed) {
$builder = new ValuesBuilder();
return $builder->build($parsed);
}
protected function buildINSERT($parsed) {
$builder = new InsertBuilder();
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 [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);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,76 @@
<?php
/**
* JoinBuilder.php
*
* Builds the JOIN statement parts (within FROM).
*
* 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 <andre.rothe@phosco.info>
* @author George Schneeloch <noisecapella@gmail.com>
* @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 JOIN statement parts (within FROM).
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @author George Schneeloch <noisecapella@gmail.com>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class JoinBuilder {
public function build($parsed) {
if ($parsed === 'CROSS') {
return ", ";
}
if ($parsed === 'JOIN') {
return " INNER JOIN ";
}
if ($parsed === 'LEFT') {
return " LEFT JOIN ";
}
if ($parsed === 'RIGHT') {
return " RIGHT JOIN ";
}
if ($parsed === 'STRAIGHT_JOIN') {
return " STRAIGHT_JOIN ";
}
// TODO: add more
throw new UnsupportedFeatureException($parsed);
}
}
?>

View File

@@ -0,0 +1,68 @@
<?php
/**
* LikeBuilder.php
*
* Builds the LIKE statement part of a CREATE TABLE 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 <andre.rothe@phosco.info>
* @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 LIKE statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class LikeBuilder implements Builder {
protected function buildTable($parsed, $index) {
$builder = new TableBuilder();
return $builder->build($parsed, $index);
}
public function build(array $parsed) {
$sql = $this->buildTable($parsed, 0);
if (strlen($sql) === 0) {
throw new UnableToCreateSQLException('LIKE', "", $parsed, 'table');
}
return "LIKE " . $sql;
}
}
?>

View File

@@ -0,0 +1,87 @@
<?php
/**
* LikeExpressionBuilder.php
*
* Builds the LIKE keyword within parenthesis.
*
* 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 <andre.rothe@phosco.info>
* @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 the (LIKE) keyword within a
* CREATE TABLE statement. There are difference to LIKE (without parenthesis),
* the latter is a top-level element of the output array.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class LikeExpressionBuilder implements Builder {
protected function buildTable($parsed, $index) {
$builder = new TableBuilder();
return $builder->build($parsed, $index);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::LIKE) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildTable($v, 0);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE create-def (like) subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* LimitBuilder.php
*
* Builds the LIMIT 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 <andre.rothe@phosco.info>
* @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 LIMIT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class LimitBuilder implements Builder {
public function build(array $parsed) {
$sql = ($parsed['rowcount']) . ($parsed['offset'] ? " OFFSET " . $parsed['offset'] : "");
if ($sql === "") {
throw new UnableToCreateSQLException('LIMIT', 'rowcount', $parsed, 'rowcount');
}
return "LIMIT " . $sql;
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* OperatorBuilder.php
*
* Builds operators.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for operators.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OperatorBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::OPERATOR) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,67 @@
<?php
/**
* OrderByAliasBuilder.php
*
* Builds an alias within an ORDER-BY clause.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for an alias within the ORDER-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByAliasBuilder implements Builder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::ALIAS) {
return "";
}
return $parsed['base_expr'] . $this->buildDirection($parsed);
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* OrderByBracketExpressionBuilder.php
*
* Builds bracket-expressions within the ORDER-BY 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 <andre.rothe@phosco.info>
* @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 bracket-expressions within the ORDER-BY part.
* It must contain the direction.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByBracketExpressionBuilder extends WhereBracketExpressionBuilder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = parent::build($parsed);
if ($sql !== '') {
$sql .= $this->buildDirection($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,113 @@
<?php
/**
* OrderByBuilder.php
*
* Builds the ORDERBY clause.
*
* 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 <andre.rothe@phosco.info>
* @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 the ORDER-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByBuilder implements Builder {
protected function buildFunction($parsed) {
$builder = new OrderByFunctionBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new OrderByReservedBuilder();
return $builder->build($parsed);
}
protected function buildColRef($parsed) {
$builder = new OrderByColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildAlias($parsed) {
$builder = new OrderByAliasBuilder();
return $builder->build($parsed);
}
protected function buildExpression($parsed) {
$builder = new OrderByExpressionBuilder();
return $builder->build($parsed);
}
protected function buildBracketExpression($parsed) {
$builder = new OrderByBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildPosition($parsed) {
$builder = new PositionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = "";
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildAlias($v);
$sql .= $this->buildColRef($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildExpression($v);
$sql .= $this->buildBracketExpression($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildPosition($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('ORDER', $k, $v, 'expr_type');
}
$sql .= ", ";
}
$sql = substr($sql, 0, -2);
return "ORDER BY " . $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* OrderByColumnReferenceBuilder.php
*
* Builds column references within the ORDER-BY 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 <andre.rothe@phosco.info>
* @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 column references within the ORDER-BY part.
* It must contain the direction.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByColumnReferenceBuilder extends ColumnReferenceBuilder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = parent::build($parsed);
if ($sql !== '') {
$sql .= $this->buildDirection($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* OrderByExpressionBuilder.php
*
* Builds expressions within the ORDER-BY 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 <andre.rothe@phosco.info>
* @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 expressions within the ORDER-BY part.
* It must contain the direction.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByExpressionBuilder extends WhereExpressionBuilder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = parent::build($parsed);
if ($sql !== '') {
$sql .= $this->buildDirection($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* OrderByFunctionBuilder.php
*
* Builds functions within the ORDER-BY 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 <andre.rothe@phosco.info>
* @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 functions within the ORDER-BY part.
* It must contain the direction.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByFunctionBuilder extends FunctionBuilder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = parent::build($parsed);
if ($sql !== '') {
$sql .= $this->buildDirection($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* OrderByReservedBuilder.php
*
* Builds reserved keywords within the ORDER-BY 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 <andre.rothe@phosco.info>
* @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 reserved keywords within the ORDER-BY part.
* It must contain the direction.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByReservedBuilder extends ReservedBuilder {
protected function buildDirection($parsed) {
$builder = new DirectionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = parent::build($parsed);
if ($sql !== '') {
$sql .= $this->buildDirection($parsed);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* PositionBuilder.php
*
* Builds positions of the GROUP BY clause.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for positions of the GROUP-BY clause.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class PositionBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::POSITION) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,109 @@
<?php
/**
* PrimaryKeyBuilder.php
*
* Builds the PRIMARY KEY statement part of CREATE TABLE.
*
* 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 <andre.rothe@phosco.info>
* @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 the PRIMARY KEY statement part of CREATE TABLE.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class PrimaryKeyBuilder implements Builder {
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
protected function buildConstraint($parsed) {
$builder = new ConstraintBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildIndexType($parsed) {
$builder = new IndexTypeBuilder();
return $builder->build($parsed);
}
protected function buildIndexSize($parsed) {
$builder = new IndexSizeBuilder();
return $builder->build($parsed);
}
protected function buildIndexParser($parsed) {
$builder = new IndexParserBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::PRIMARY_KEY) {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildConstraint($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildColumnList($v);
$sql .= $this->buildIndexType($v);
$sql .= $this->buildIndexSize($v);
$sql .= $this->buildIndexParser($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('CREATE TABLE primary key subtree', $k, $v, 'expr_type');
}
$sql .= " ";
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* Procedureuilder.php
*
* Builds the procedures within the SHOW 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for a procedure within SHOW statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ProcedureBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::PROCEDURE) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,97 @@
<?php
/**
* QueryBuilder.php
*
* Builds the SELECT statements within parentheses.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for queries within parentheses (no subqueries).
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class QueryBuilder implements Builder {
protected function buildRefClause($parsed) {
$builder = new RefClauseBuilder();
return $builder->build($parsed);
}
protected function buildRefType($parsed) {
$builder = new RefTypeBuilder();
return $builder->build($parsed);
}
protected function buildJoin($parsed) {
$builder = new JoinBuilder();
return $builder->build($parsed);
}
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
protected function buildSelectStatement($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}
public function build(array $parsed, $index = 0) {
if ($parsed['expr_type'] !== ExpressionType::QUERY) {
return '';
}
// TODO: should we add a numeric level (0) between sub_tree and SELECT?
$sql = $this->buildSelectStatement($parsed['sub_tree']);
$sql .= $this->buildAlias($parsed);
if ($index !== 0) {
$sql = $this->buildJoin($parsed['join_type']) . $sql;
$sql .= $this->buildRefType($parsed['ref_type']);
$sql .= $parsed['ref_clause'] === false ? '' : $this->buildRefClause($parsed['ref_clause']);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,99 @@
<?php
/**
* RecordBuilder.php
*
* Builds the records within the INSERT 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 <andre.rothe@phosco.info>
* @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 the records within INSERT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RecordBuilder implements Builder {
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::RECORD) {
return isset($parsed['base_expr']) ? $parsed['base_expr'] : '';
}
$sql = "";
foreach ($parsed['data'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildConstant($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildColRef($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException(ExpressionType::RECORD, $k, $v, 'expr_type');
}
$sql .= ", ";
}
$sql = substr($sql, 0, -2);
return "(" . $sql . ")";
}
}
?>

View File

@@ -0,0 +1,114 @@
<?php
/**
* RefClauseBuilder.php
*
* Builds reference clauses within a JOIN.
*
* 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 <andre.rothe@phosco.info>
* @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 references clause within a JOIN.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RefClauseBuilder implements Builder {
protected function buildInList($parsed) {
$builder = new InListBuilder();
return $builder->build($parsed);
}
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildColumnList($parsed) {
$builder = new ColumnListBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed === false) {
return '';
}
$sql = '';
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildBracketExpression($v);
$sql .= $this->buildInList($v);
$sql .= $this->buildColumnList($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('expression ref_clause', $k, $v, 'expr_type');
}
$sql .= ' ';
}
return substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,69 @@
<?php
/**
* RefTypeBuilder.php
*
* Builds reference type within a JOIN.
*
* 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 <andre.rothe@phosco.info>
* @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\UnsupportedFeatureException;
/**
* This class implements the references type within a JOIN.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RefTypeBuilder {
public function build($parsed) {
if ($parsed === false) {
return "";
}
if ($parsed === 'ON') {
return " ON ";
}
if ($parsed === 'USING') {
return " USING ";
}
// TODO: add more
throw new UnsupportedFeatureException($parsed);
}
}
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* RenameStatement.php
*
* Builds the RENAME 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 <andre.rothe@phosco.info>
* @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 RENAME statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RenameStatementBuilder implements Builder {
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function processSourceAndDestTable($v) {
if (!isset($v['source']) || !isset($v['destination'])) {
return '';
}
return $v['source']['base_expr'] . ' TO ' . $v['destination']['base_expr'] . ',';
}
public function build(array $parsed) {
$rename = $parsed['RENAME'];
$sql = '';
foreach ($rename['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->processSourceAndDestTable($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('RENAME subtree', $k, $v, 'expr_type');
}
$sql .= ' ';
}
$sql = trim('RENAME ' . $sql);
return (substr($sql, -1) === ',' ? substr($sql, 0, -1) : $sql);
}
}
?>

View File

@@ -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 <andre.rothe@phosco.info>
* @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 <andre.rothe@phosco.info>
* @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);
}
}
?>

View File

@@ -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 <andre.rothe@phosco.info>
* @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 <andre.rothe@phosco.info>
* @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) . ")";
}
}
?>

View File

@@ -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 <andre.rothe@phosco.info>
* @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 <andre.rothe@phosco.info>
* @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;
}
}
?>

View File

@@ -0,0 +1,66 @@
<?php
/**
* ReservedBuilder.php
*
* Builds reserved keywords.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for reserved keywords.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ReservedBuilder implements Builder {
public function isReserved($parsed) {
return (isset($parsed['expr_type']) && $parsed['expr_type'] === ExpressionType::RESERVED);
}
public function build(array $parsed) {
if (!$this->isReserved($parsed)) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* SchemaBuilder.php
*
* Builds the schema within the DROP 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for a schema within DROP statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SchemaBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::SCHEMA) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,78 @@
<?php
/**
* SelectBracketExpressionBuilder.php
*
* Builds the bracket expressions within a SELECT 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for bracket expressions within a SELECT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SelectBracketExpressionBuilder implements Builder {
protected function buildSubTree($parsed, $delim) {
$builder = new SubTreeBuilder();
return $builder->build($parsed, $delim);
}
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
return "";
}
return '('
. $this->buildSubTree(
$parsed,
!empty($parsed['delim']) ? $parsed['delim'] : ' '
)
. ')'
. $this->buildAlias($parsed);
}
}
?>

View File

@@ -0,0 +1,115 @@
<?php
/**
* SelectBuilder.php
*
* Builds the SELECT statement from the [SELECT] field.
*
* 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 <andre.rothe@phosco.info>
* @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 [SELECT] field. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SelectBuilder implements Builder {
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildSelectExpression($parsed) {
$builder = new SelectExpressionBuilder();
return $builder->build($parsed);
}
protected function buildSelectBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
/**
* Returns a well-formatted delimiter string. If you don't need nice SQL,
* you could simply return $parsed['delim'].
*
* @param array $parsed The part of the output array, which contains the current expression.
* @return a string, which is added right after the expression
*/
protected function getDelimiter($parsed) {
return (!isset($parsed['delim']) || $parsed['delim'] === false ? '' : (trim($parsed['delim']) . ' '));
}
public function build(array $parsed) {
$sql = "";
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildSelectBracketExpression($v);
$sql .= $this->buildSelectExpression($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildReserved($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('SELECT', $k, $v, 'expr_type');
}
$sql .= $this->getDelimiter($v);
}
return "SELECT " . $sql;
}
}
?>

View File

@@ -0,0 +1,74 @@
<?php
/**
* SelectExpressionBuilder.php
*
* Builds simple expressions within a SELECT 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for simple expressions within a SELECT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SelectExpressionBuilder implements Builder {
protected function buildSubTree($parsed, $delim) {
$builder = new SubTreeBuilder();
return $builder->build($parsed, $delim);
}
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
return "";
}
$sql = $this->buildSubTree($parsed, " ");
$sql .= $this->buildAlias($parsed);
return $sql;
}
}
?>

View File

@@ -0,0 +1,132 @@
<?php
/**
* SelectStatement.php
*
* Builds the SELECT 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 <andre.rothe@phosco.info>
* @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 Select statement. You can overwrite
* all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SelectStatementBuilder implements Builder {
protected function buildSELECT($parsed) {
$builder = new SelectBuilder();
return $builder->build($parsed);
}
protected function buildFROM($parsed) {
$builder = new FromBuilder();
return $builder->build($parsed);
}
protected function buildWHERE($parsed) {
$builder = new WhereBuilder();
return $builder->build($parsed);
}
protected function buildGROUP($parsed) {
$builder = new GroupByBuilder();
return $builder->build($parsed);
}
protected function buildHAVING($parsed) {
$builder = new HavingBuilder();
return $builder->build($parsed);
}
protected function buildORDER($parsed) {
$builder = new OrderByBuilder();
return $builder->build($parsed);
}
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 = "";
if (isset($parsed['SELECT'])) {
$sql .= $this->buildSELECT($parsed['SELECT']);
}
if (isset($parsed['FROM'])) {
$sql .= " " . $this->buildFROM($parsed['FROM']);
}
if (isset($parsed['WHERE'])) {
$sql .= " " . $this->buildWHERE($parsed['WHERE']);
}
if (isset($parsed['GROUP'])) {
$sql .= " " . $this->buildGROUP($parsed['GROUP']);
}
if (isset($parsed['HAVING'])) {
$sql .= " " . $this->buildHAVING($parsed['HAVING']);
}
if (isset($parsed['ORDER'])) {
$sql .= " " . $this->buildORDER($parsed['ORDER']);
}
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;
}
}
?>

View File

@@ -0,0 +1,75 @@
<?php
/**
* SetBuilder.php
*
* Builds the SET part of the INSERT 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 <andre.rothe@phosco.info>
* @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 SET part of INSERT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SetBuilder implements Builder {
protected function buildSetExpression($parsed) {
$builder = new SetExpressionBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = "";
foreach ($parsed as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildSetExpression($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('SET', $k, $v, 'expr_type');
}
$sql .= ",";
}
return "SET " . substr($sql, 0, -1);
}
}
?>

View File

@@ -0,0 +1,117 @@
<?php
/**
* SetExpressionBuilder.php
*
* Builds the SET part of the INSERT 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 <andre.rothe@phosco.info>
* @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 the SET part of INSERT statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SetExpressionBuilder implements Builder {
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildSign($parsed) {
$builder = new SignBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::EXPRESSION) {
return '';
}
$sql = '';
foreach ($parsed['sub_tree'] as $k => $v) {
$delim = ' ';
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildBracketExpression($v);
// we don't need whitespace between the sign and
// the following part
if ($this->buildSign($v) !== '') {
$delim = '';
}
$sql .= $this->buildSign($v);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('SET expression subtree', $k, $v, 'expr_type');
}
$sql .= $delim;
}
$sql = substr($sql, 0, -1);
return $sql;
}
}
?>

View File

@@ -0,0 +1,114 @@
<?php
/**
* ShowBuilder.php
*
* Builds the SHOW 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 <andre.rothe@phosco.info>
* @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 SHOW statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ShowBuilder implements Builder {
protected function buildTable($parsed, $delim) {
$builder = new TableBuilder();
return $builder->build($parsed, $delim);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildProcedure($parsed) {
$builder = new ProcedureBuilder();
return $builder->build($parsed);
}
protected function buildDatabase($parsed) {
$builder = new DatabaseBuilder();
return $builder->build($parsed);
}
protected function buildEngine($parsed) {
$builder = new EngineBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$show = $parsed['SHOW'];
$sql = "";
foreach ($show as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildReserved($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildEngine($v);
$sql .= $this->buildDatabase($v);
$sql .= $this->buildProcedure($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildTable($v, 0);
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('SHOW', $k, $v, 'expr_type');
}
$sql .= " ";
}
$sql = substr($sql, 0, -1);
return "SHOW " . $sql;
}
}
?>

View File

@@ -0,0 +1,72 @@
<?php
/**
* ShowStatementBuilder.php
*
* Builds the SHOW 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 <andre.rothe@phosco.info>
* @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 SHOW statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ShowStatementBuilder implements Builder {
protected function buildWHERE($parsed) {
$builder = new WhereBuilder();
return $builder->build($parsed);
}
protected function buildSHOW($parsed) {
$builder = new ShowBuilder();
return $builder->build($parsed);
}
public function build(array $parsed) {
$sql = $this->buildSHOW($parsed);
if (isset($parsed['WHERE'])) {
$sql .= " " . $this->buildWHERE($parsed['WHERE']);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,62 @@
<?php
/**
* SignBuilder.php
*
* Builds unary operators.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for unary operators.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SignBuilder implements Builder {
public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::SIGN) {
return "";
}
return $parsed['base_expr'];
}
}
?>

View File

@@ -0,0 +1,98 @@
<?php
/**
* SubQueryBuilder.php
*
* Builds the statements for sub-queries.
*
* 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 <andre.rothe@phosco.info>
* @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\utils\ExpressionType;
/**
* This class implements the builder for sub-queries.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SubQueryBuilder implements Builder {
protected function buildRefClause($parsed) {
$builder = new RefClauseBuilder();
return $builder->build($parsed);
}
protected function buildRefType($parsed) {
$builder = new RefTypeBuilder();
return $builder->build($parsed);
}
protected function buildJoin($parsed) {
$builder = new JoinBuilder();
return $builder->build($parsed);
}
protected function buildAlias($parsed) {
$builder = new AliasBuilder();
return $builder->build($parsed);
}
protected function buildSelectStatement($parsed) {
$builder = new SelectStatementBuilder();
return $builder->build($parsed);
}
public function build(array $parsed, $index = 0) {
if ($parsed['expr_type'] !== ExpressionType::SUBQUERY) {
return '';
}
// TODO: should we add a numeric level (0) between sub_tree and SELECT?
$sql = $this->buildSelectStatement($parsed['sub_tree']);
$sql = '(' . $sql . ')';
$sql .= $this->buildAlias($parsed);
if ($index !== 0) {
$sql = $this->buildJoin($parsed['join_type']) . $sql;
$sql .= $this->buildRefType($parsed['ref_type']);
$sql .= $parsed['ref_clause'] === false ? '' : $this->buildRefClause($parsed['ref_clause']);
}
return $sql;
}
}
?>

View File

@@ -0,0 +1,130 @@
<?php
/**
* SubTreeBuilder.php
*
* Builds the statements for [sub_tree] fields.
*
* 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 <andre.rothe@phosco.info>
* @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 [sub_tree] fields.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SubTreeBuilder implements Builder {
protected function buildColRef($parsed) {
$builder = new ColumnReferenceBuilder();
return $builder->build($parsed);
}
protected function buildFunction($parsed) {
$builder = new FunctionBuilder();
return $builder->build($parsed);
}
protected function buildOperator($parsed) {
$builder = new OperatorBuilder();
return $builder->build($parsed);
}
protected function buildConstant($parsed) {
$builder = new ConstantBuilder();
return $builder->build($parsed);
}
protected function buildReserved($parsed) {
$builder = new ReservedBuilder();
return $builder->build($parsed);
}
protected function buildSubQuery($parsed) {
$builder = new SubQueryBuilder();
return $builder->build($parsed);
}
protected function buildQuery($parsed) {
$builder = new QueryBuilder();
return $builder->build($parsed);
}
protected function buildSelectBracketExpression($parsed) {
$builder = new SelectBracketExpressionBuilder();
return $builder->build($parsed);
}
protected function buildSign($parsed) {
$builder = new SignBuilder();
return $builder->build($parsed);
}
public function build(array $parsed, $delim = " ") {
if ($parsed['sub_tree'] === '') {
return "";
}
$sql = "";
foreach ($parsed['sub_tree'] as $k => $v) {
$len = strlen($sql);
$sql .= $this->buildColRef($v);
$sql .= $this->buildFunction($v);
$sql .= $this->buildOperator($v);
$sql .= $this->buildConstant($v);
$sql .= $this->buildSubQuery($v);
$sql .= $this->buildSelectBracketExpression($v);
$sql .= $this->buildReserved($v);
$sql .= $this->buildQuery($v);
$sign = $this->buildSign($v);
$sql .= $sign;
if ($len == strlen($sql)) {
throw new UnableToCreateSQLException('expression subtree', $k, $v, 'expr_type');
}
// We don't need whitespace between a sign and the following part.
if ($sign === '') {
$sql .= $delim;
}
}
return substr($sql, 0, -strlen($delim));
}
}
?>

Some files were not shown because too many files have changed in this diff Show More