Completed
Push — 1.4 ( ab36f3...357412 )
by David
22s queued 11s
created

AbstractInListOperator::getParameter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace SQLParser\Node;
4
use Doctrine\DBAL\Connection;
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Mouf\Database\MagicQueryException;
7
8
/**
9
 * This class represents an In operation in an SQL expression.
10
 *
11
 * @author David Négrier <[email protected]>
12
 */
13
abstract class AbstractInListOperator extends AbstractTwoOperandsOperator
14
{
15
    protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
16
    {
17
        $rightOperand = $this->getRightOperand();
18
19
        $rightOperand = $this->refactorParameterToExpression($rightOperand);
20
21
        $this->setRightOperand($rightOperand);
22
23
        $parameterNode = $this->getParameter($rightOperand);
24
25
        if ($parameterNode !== null) {
26
            if (!isset($parameters[$parameterNode->getName()])) {
27
                throw new MagicQueryException("Missing parameter '" . $parameterNode->getName() . "' for 'IN' operand.");
28
            }
29
            if ($parameters[$parameterNode->getName()] === []) {
30
                return "FALSE";
31
            }
32
        }
33
34
        return parent::getSql($parameters, $platform, $indent, $conditionsMode, $extrapolateParameters);
35
    }
36
37
    protected function refactorParameterToExpression(NodeInterface $rightOperand): NodeInterface
38
    {
39
        if ($rightOperand instanceof Parameter) {
40
            $expression = new Expression();
41
            $expression->setSubTree([$rightOperand]);
42
            $expression->setBrackets(true);
43
            return $expression;
44
        }
45
        return $rightOperand;
46
    }
47
48
    protected function getParameter(NodeInterface $operand): ?Parameter
49
    {
50
        if (!$operand instanceof Expression) {
51
            return null;
52
        }
53
        $subtree = $operand->getSubTree();
54
        if (!isset($subtree[0])) {
55
            return null;
56
        }
57
        if ($subtree[0] instanceof Parameter) {
58
            return $subtree[0];
59
        }
60
        return null;
61
    }
62
}
63