diff --git a/src/SQLParser/Node/NodeFactory.php b/src/SQLParser/Node/NodeFactory.php index 174511f..b93e122 100644 --- a/src/SQLParser/Node/NodeFactory.php +++ b/src/SQLParser/Node/NodeFactory.php @@ -620,15 +620,16 @@ public static function simplify($nodes) */ if (isset(self::$OPERATOR_TO_CLASS[$operation]) && is_subclass_of(self::$OPERATOR_TO_CLASS[$operation], 'SQLParser\Node\AbstractTwoOperandsOperator')) { + if (count($operands) != 2) { + throw new MagicQueryException('An error occured while parsing SQL statement. Invalid character found next to "'.$operation.'"'); + } + $leftOperand = array_shift($operands); - while (!empty($operands)) { - $rightOperand = array_shift($operands); + $rightOperand = array_shift($operands); - $instance = new self::$OPERATOR_TO_CLASS[$operation](); - $instance->setLeftOperand($leftOperand); - $instance->setRightOperand($rightOperand); - $leftOperand = $instance; - } + $instance = new self::$OPERATOR_TO_CLASS[$operation](); + $instance->setLeftOperand($leftOperand); + $instance->setRightOperand($rightOperand); return $instance; } elseif (isset(self::$OPERATOR_TO_CLASS[$operation]) && is_subclass_of(self::$OPERATOR_TO_CLASS[$operation], 'SQLParser\Node\AbstractManyInstancesOperator')) { diff --git a/tests/Mouf/Database/MagicQueryTest.php b/tests/Mouf/Database/MagicQueryTest.php index 9cf2cb2..c2f677b 100644 --- a/tests/Mouf/Database/MagicQueryTest.php +++ b/tests/Mouf/Database/MagicQueryTest.php @@ -116,6 +116,16 @@ public function testInNullException() { $magicQuery->build($sql, ['statuses' => NULL]); } + /** + * @expectedException \Mouf\Database\MagicQueryException + */ + public function testInvalidSql() { + $magicQuery = new MagicQuery(); + + $sql = 'SELECT * FROM users WHERE date_end => :startDate'; + $this->assertEquals('SELECT * FROM users WHERE date_end => \'2014-06-06\'', self::simplifySql($magicQuery->build($sql, ['startDate' => '2014-06-06']))); + } + public function testWithCache() { global $db_url;