Skip to content

Commit

Permalink
Merge 22470bb into bc9b2f5
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Sep 11, 2019
2 parents bc9b2f5 + 22470bb commit 040460a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 50 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Expand Up @@ -35,3 +35,10 @@ jobs:
- *composerupdate
script:
- *phpunit
- stage: test
php: 7.3
env: PREFER_LOWEST=""
before_script:
- *composerupdate
script:
- *phpunit
61 changes: 61 additions & 0 deletions src/SQLParser/Node/AbstractInListOperator.php
@@ -0,0 +1,61 @@
<?php

namespace SQLParser\Node;
use Doctrine\DBAL\Connection;
use Mouf\Database\MagicQueryException;

/**
* This class represents an In operation in an SQL expression.
*
* @author David Négrier <d.negrier@thecodingmachine.com>
*/
abstract class AbstractInListOperator extends AbstractTwoOperandsOperator
{
protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
{
$rightOperand = $this->getRightOperand();

$rightOperand = $this->refactorParameterToExpression($rightOperand);

$this->setRightOperand($rightOperand);

$parameterNode = $this->getParameter($rightOperand);

if ($parameterNode !== null) {
if (!isset($parameters[$parameterNode->getName()])) {
throw new MagicQueryException("Missing parameter '" . $parameterNode->getName() . "' for 'IN' operand.");
}
if ($parameters[$parameterNode->getName()] === []) {
return "FALSE";
}
}

return parent::getSql($parameters, $dbConnection, $indent, $conditionsMode, $extrapolateParameters);
}

protected function refactorParameterToExpression(NodeInterface $rightOperand): NodeInterface
{
if ($rightOperand instanceof Parameter) {
$expression = new Expression();
$expression->setSubTree([$rightOperand]);
$expression->setBrackets(true);
return $expression;
}
return $rightOperand;
}

protected function getParameter(NodeInterface $operand): ?Parameter
{
if (!$operand instanceof Expression) {
return null;
}
$subtree = $operand->getSubTree();
if (!isset($subtree[0])) {
return null;
}
if ($subtree[0] instanceof Parameter) {
return $subtree[0];
}
return null;
}
}
50 changes: 1 addition & 49 deletions src/SQLParser/Node/In.php
Expand Up @@ -9,7 +9,7 @@
*
* @author David Négrier <d.negrier@thecodingmachine.com>
*/
class In extends AbstractTwoOperandsOperator
class In extends AbstractInListOperator
{
/**
* Returns the symbol for this operator.
Expand All @@ -20,52 +20,4 @@ protected function getOperatorSymbol()
{
return 'IN';
}

protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
{
$rightOperand = $this->getRightOperand();

$rightOperand = $this->refactorParameterToExpression($rightOperand);

$this->setRightOperand($rightOperand);

$parameterNode = $this->getParameter($rightOperand);

if ($parameterNode !== null) {
if (!isset($parameters[$parameterNode->getName()])) {
throw new MagicQueryException("Missing parameter '" . $parameterNode->getName() . "' for 'IN' operand.");
}
if ($parameters[$parameterNode->getName()] === []) {
return "FALSE";
}
}

return parent::getSql($parameters, $dbConnection, $indent, $conditionsMode, $extrapolateParameters);
}

protected function refactorParameterToExpression(NodeInterface $rightOperand): NodeInterface
{
if ($rightOperand instanceof Parameter) {
$expression = new Expression();
$expression->setSubTree([$rightOperand]);
$expression->setBrackets(true);
return $expression;
}
return $rightOperand;
}

protected function getParameter(NodeInterface $operand): ?Parameter
{
if (!$operand instanceof Expression) {
return null;
}
$subtree = $operand->getSubTree();
if (!isset($subtree[0])) {
return null;
}
if ($subtree[0] instanceof Parameter) {
return $subtree[0];
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/SQLParser/Node/NotIn.php
Expand Up @@ -7,7 +7,7 @@
*
* @author David Négrier <d.negrier@thecodingmachine.com>
*/
class NotIn extends AbstractTwoOperandsOperator
class NotIn extends AbstractInListOperator
{
/**
* Returns the symbol for this operator.
Expand Down
10 changes: 10 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Expand Up @@ -78,6 +78,12 @@ public function testStandardSelect()
$sql = 'SELECT * FROM users WHERE status IN :statuses';
$this->assertEquals('SELECT * FROM users WHERE status IN (\'1\',\'2\')', self::simplifySql($magicQuery->build($sql, ['statuses' => [1, 2]])));

$sql = 'SELECT * FROM users WHERE status not in :status';
$this->assertEquals("SELECT * FROM users WHERE status NOT IN ('2','4')", self::simplifySql($magicQuery->build($sql, ['status' => [2, 4]])));

$sql = 'SELECT * FROM users WHERE status not in (:status)';
$this->assertEquals("SELECT * FROM users WHERE status NOT IN ('2','4')", self::simplifySql($magicQuery->build($sql, ['status' => [2, 4]])));

$sql = 'SELECT * FROM myTable where someField BETWEEN :value1 AND :value2';
$this->assertEquals("SELECT * FROM myTable WHERE someField BETWEEN '2' AND '4'", self::simplifySql($magicQuery->build($sql, ['value1' => 2, 'value2' => 4])));
$this->assertEquals("SELECT * FROM myTable WHERE someField >= '2'", self::simplifySql($magicQuery->build($sql, ['value1' => 2])));
Expand Down Expand Up @@ -433,5 +439,9 @@ public function testBuildPreparedStatement()
// Let's check that MagicQuery is cleverly adding parenthesis if the user forgot those in the "IN" statement.
$sql = 'SELECT id FROM users WHERE status IN :status';
$this->assertEquals("SELECT id FROM users WHERE status IN (:status)", self::simplifySql($magicQuery->buildPreparedStatement($sql, ['status' => [1,2]])));

// Let's check that MagicQuery is cleverly adding parenthesis if the user forgot those in the "NOT IN" statement.
$sql = 'SELECT id FROM users WHERE status NOT IN :status';
$this->assertEquals("SELECT id FROM users WHERE status NOT IN (:status)", self::simplifySql($magicQuery->buildPreparedStatement($sql, ['status' => [1,2]])));
}
}

0 comments on commit 040460a

Please sign in to comment.