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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SQLParser/ExpressionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class ExpressionType
const RESERVED = 'reserved';
const CONSTANT = 'const';

const LIMIT_CONST = 'limit_const';

const AGGREGATE_FUNCTION = 'aggregate_function';
const SIMPLE_FUNCTION = 'function';

Expand Down
121 changes: 121 additions & 0 deletions src/SQLParser/Node/LimitNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* expression-types.php.
*
*
* Copyright (c) 2010-2013, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* and David Négrier <d.negrier@thecodingmachine.com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/

namespace SQLParser\Node;

use Doctrine\DBAL\Connection;
use Mouf\MoufManager;
use Mouf\MoufInstanceDescriptor;
use SQLParser\Node\Traverser\VisitorInterface;

/**
* This class represents a constant of a LIMIT in an SQL expression.
*
* @author David MAECHLER <d.maechler@thecodingmachine.com>
*/
class LimitNode implements NodeInterface
{
private $value;

public function getValue()
{
return $this->value;
}

/**
* Sets the value.
*
* @Important
*
* @param string $value
*/
public function setValue($value)
{
$this->value = $value;
}

/**
* Returns a Mouf instance descriptor describing this object.
*
* @param MoufManager $moufManager
*
* @return MoufInstanceDescriptor
*/
public function toInstanceDescriptor(MoufManager $moufManager)
{
$instanceDescriptor = $moufManager->createInstance(get_called_class());
$instanceDescriptor->getProperty('value')->setValue(NodeFactory::nodeToInstanceDescriptor($this->value, $moufManager));

return $instanceDescriptor;
}

/**
* Renders the object as a SQL string.
*
* @param array $parameters
* @param Connection $dbConnection
* @param int|number $indent
* @param int $conditionsMode
* @return string
* @throws \Exception
*/
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
{
if($this->value === null) {
throw new \Exception('A limit parameter must be an integer');
}

if(is_numeric($this->value)) {
return (int) $this->value;
} else if ($dbConnection != null) {
return $dbConnection->quote($this->value);
} else {
return addslashes($this->value);
}
}

/**
* Walks the tree of nodes, calling the visitor passed in parameter.
*
* @param VisitorInterface $visitor
* @return NodeInterface|null|string Can return null if nothing is to be done or a node that should replace this node, or NodeTraverser::REMOVE_NODE to remove the node
*/
public function walk(VisitorInterface $visitor) {
$node = $this;
$result = $visitor->enterNode($node);
if ($result instanceof NodeInterface) {
$node = $result;
}
return $visitor->leaveNode($node);
}
}
36 changes: 31 additions & 5 deletions src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ public static function toObject(array $desc)
}

switch ($desc['expr_type']) {
case ExpressionType::LIMIT_CONST:
if (substr($desc['base_expr'], 0, 1) == ':') {
$instance = new UnquotedParameter();
$instance->setName(substr($desc['base_expr'], 1));
} else {
$instance = new LimitNode();
$expr = $desc['base_expr'];
if (strpos($expr, "'") === 0) {
$expr = substr($expr, 1);
}
if (strrpos($expr, "'") === strlen($expr) - 1) {
$expr = substr($expr, 0, strlen($expr) - 1);
}
$expr = stripslashes($expr);

$instance->setValue($expr);
}
// Debug:
unset($desc['base_expr']);
unset($desc['expr_type']);
unset($desc['sub_tree']);
if (!empty($desc)) {
throw new \InvalidArgumentException('Unexpected parameters in exception: '.var_export($desc, true));
}

return $instance;
case ExpressionType::CONSTANT:
$const = new ConstNode();
$expr = $desc['base_expr'];
Expand Down Expand Up @@ -108,10 +134,10 @@ public static function toObject(array $desc)
if (!empty($desc['alias'])) {
$instance->setAlias($desc['alias']['name']);
}
}

if (!empty($desc['direction'])) {
$instance->setDirection($desc['direction']);
if (!empty($desc['direction'])) {
$instance->setDirection($desc['direction']);
}
}

// Debug:
Expand Down Expand Up @@ -687,12 +713,12 @@ public static function toSql($nodes, Connection $dbConnection = null, array $par
$item = $nodes;
if ($item instanceof SqlRenderInterface) {
$itemSql = $item->toSql($parameters, $dbConnection, $indent, $conditionsMode);
if ($itemSql == null) {
if ($itemSql === null || $itemSql === '') {
return;
}
$sql = str_repeat(' ', $indent).$itemSql;
} else {
if ($item == null) {
if ($item === null || $item === '') {
return;
}
$sql = str_repeat(' ', $indent).$item;
Expand Down
11 changes: 5 additions & 6 deletions src/SQLParser/Node/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
*/
class Parameter implements NodeInterface
{
private $name;
private $discardedOnNull = true;
protected $name;
protected $discardedOnNull = true;

/**
* Returns the name.
Expand Down Expand Up @@ -80,12 +80,12 @@ public function setName($name)
/**
* @var string
*/
private $autoPrepend;
protected $autoPrepend;

/**
* @var string
*/
private $autoAppend;
protected $autoAppend;

/**
* @return string
Expand Down Expand Up @@ -180,9 +180,8 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
return "'".addslashes($this->autoPrepend.$item.$this->autoAppend)."'";
}, $parameters[$this->name])).')';
} else{
return "'".addslashes($this->autoPrepend.$parameters[$this->name].$this->autoAppend)."'";
return "'".addslashes($this->autoPrepend.$parameters[$this->name].$this->autoAppend)."'";
}

}
}
} elseif (!$this->isDiscardedOnNull()) {
Expand Down
2 changes: 1 addition & 1 deletion src/SQLParser/Node/SimpleFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
if (!empty($this->baseExpression)) {
$sql .= $this->baseExpression.'(';
}
$sql .= NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
$sql .= NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ',', false, $indent, $conditionsMode);
if (!empty($this->baseExpression)) {
$sql .= ')';
}
Expand Down
63 changes: 63 additions & 0 deletions src/SQLParser/Node/UnquotedParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* expression-types.php.
*
*
* Copyright (c) 2010-2013, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* and David Négrier <d.negrier@thecodingmachine.com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/

namespace SQLParser\Node;

use Doctrine\DBAL\Connection;
use Mouf\MoufInstanceDescriptor;
use Mouf\MoufManager;
use SQLParser\Node\Traverser\VisitorInterface;

/**
* This class represents a parameter (as in parameterized query).
*
* @author David MAECHLER <d.maechler@thecodingmachine.com>
*/
class UnquotedParameter extends Parameter
{
/**
* Renders the object as a SQL string without quote if its a numeric
*
* @param array $parameters
* @param Connection $dbConnection
* @param int|number $indent
* @param int $conditionsMode
* @return string
*/
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
{
$name = parent::toSql($parameters, $dbConnection, $indent, $conditionsMode);
$name = str_replace("'", "", $name);
return $name;
}
}
Loading