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