Skip to content

Commit f7428c2

Browse files
committed
1 parent ec6eee8 commit f7428c2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/SQLParser/Node/NodeFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,15 +620,16 @@ public static function simplify($nodes)
620620
*/
621621

622622
if (isset(self::$OPERATOR_TO_CLASS[$operation]) && is_subclass_of(self::$OPERATOR_TO_CLASS[$operation], 'SQLParser\Node\AbstractTwoOperandsOperator')) {
623+
if (count($operands) != 2) {
624+
throw new MagicQueryException('An error occured while parsing SQL statement. Invalid character found next to "'.$operation.'"');
625+
}
626+
623627
$leftOperand = array_shift($operands);
624-
while (!empty($operands)) {
625-
$rightOperand = array_shift($operands);
628+
$rightOperand = array_shift($operands);
626629

627-
$instance = new self::$OPERATOR_TO_CLASS[$operation]();
628-
$instance->setLeftOperand($leftOperand);
629-
$instance->setRightOperand($rightOperand);
630-
$leftOperand = $instance;
631-
}
630+
$instance = new self::$OPERATOR_TO_CLASS[$operation]();
631+
$instance->setLeftOperand($leftOperand);
632+
$instance->setRightOperand($rightOperand);
632633

633634
return $instance;
634635
} elseif (isset(self::$OPERATOR_TO_CLASS[$operation]) && is_subclass_of(self::$OPERATOR_TO_CLASS[$operation], 'SQLParser\Node\AbstractManyInstancesOperator')) {

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ public function testInNullException() {
116116
$magicQuery->build($sql, ['statuses' => NULL]);
117117
}
118118

119+
/**
120+
* @expectedException \Mouf\Database\MagicQueryException
121+
*/
122+
public function testInvalidSql() {
123+
$magicQuery = new MagicQuery();
124+
125+
$sql = 'SELECT * FROM users WHERE date_end => :startDate';
126+
$this->assertEquals('SELECT * FROM users WHERE date_end => \'2014-06-06\'', self::simplifySql($magicQuery->build($sql, ['startDate' => '2014-06-06'])));
127+
}
128+
119129
public function testWithCache()
120130
{
121131
global $db_url;

0 commit comments

Comments
 (0)