1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SQLParser\Node; |
4
|
|
|
|
5
|
|
|
use Mouf\Utils\Common\ConditionInterface\ConditionTrait; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Mouf\MoufManager; |
8
|
|
|
use Mouf\MoufInstanceDescriptor; |
9
|
|
|
use SQLParser\Node\Traverser\NodeTraverser; |
10
|
|
|
use SQLParser\Node\Traverser\VisitorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class represents an operation that takes 2 operands (for instance =, <, >, etc...) in an SQL expression. |
14
|
|
|
* |
15
|
|
|
* @author David Négrier <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractTwoOperandsOperator implements NodeInterface |
18
|
|
|
{ |
19
|
|
|
use ConditionTrait; |
20
|
|
|
|
21
|
|
|
private $leftOperand; |
22
|
|
|
|
23
|
|
|
public function getLeftOperand() |
24
|
|
|
{ |
25
|
|
|
return $this->leftOperand; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets the leftOperand. |
30
|
|
|
* |
31
|
|
|
* @Important |
32
|
|
|
* |
33
|
|
|
* @param NodeInterface|NodeInterface[]|string $leftOperand |
34
|
|
|
*/ |
35
|
|
|
public function setLeftOperand($leftOperand) |
36
|
|
|
{ |
37
|
|
|
$this->leftOperand = $leftOperand; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private $rightOperand; |
41
|
|
|
|
42
|
|
|
public function getRightOperand() |
43
|
|
|
{ |
44
|
|
|
return $this->rightOperand; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets the rightOperand. |
49
|
|
|
* |
50
|
|
|
* @Important |
51
|
|
|
* |
52
|
|
|
* @param string|NodeInterface|NodeInterface[] $rightOperand |
53
|
|
|
*/ |
54
|
|
|
public function setRightOperand($rightOperand) |
55
|
|
|
{ |
56
|
|
|
$this->rightOperand = $rightOperand; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a Mouf instance descriptor describing this object. |
61
|
|
|
* |
62
|
|
|
* @param MoufManager $moufManager |
63
|
|
|
* |
64
|
|
|
* @return MoufInstanceDescriptor |
65
|
|
|
*/ |
66
|
|
|
public function toInstanceDescriptor(MoufManager $moufManager) |
67
|
|
|
{ |
68
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
69
|
|
|
$instanceDescriptor->getProperty('leftOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->leftOperand, $moufManager)); |
70
|
|
|
$instanceDescriptor->getProperty('rightOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->rightOperand, $moufManager)); |
71
|
|
|
|
72
|
|
View Code Duplication |
if ($this->leftOperand instanceof Parameter) { |
|
|
|
|
73
|
|
|
// Let's add a condition on the parameter. |
74
|
|
|
$conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition'); |
75
|
|
|
$conditionDescriptor->getProperty('parameterName')->setValue($this->leftOperand->getName()); |
76
|
|
|
$instanceDescriptor->getProperty('condition')->setValue($conditionDescriptor); |
77
|
|
|
} |
78
|
|
|
// TODO: manage cases where both leftOperand and rightOperand are parameters. |
79
|
|
View Code Duplication |
if ($this->rightOperand instanceof Parameter) { |
|
|
|
|
80
|
|
|
// Let's add a condition on the parameter. |
81
|
|
|
$conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition'); |
82
|
|
|
$conditionDescriptor->getProperty('parameterName')->setValue($this->rightOperand->getName()); |
83
|
|
|
$instanceDescriptor->getProperty('condition')->setValue($conditionDescriptor); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $instanceDescriptor; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Renders the object as a SQL string. |
91
|
|
|
* |
92
|
|
|
* @param Connection $dbConnection |
93
|
|
|
* @param array $parameters |
94
|
|
|
* @param number $indent |
95
|
|
|
* @param int $conditionsMode |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
100
|
|
|
{ |
101
|
|
|
if ($conditionsMode == self::CONDITION_GUESS) { |
102
|
|
|
$bypass = false; |
103
|
|
View Code Duplication |
if ($this->leftOperand instanceof Parameter) { |
|
|
|
|
104
|
|
|
if ($this->leftOperand->isDiscardedOnNull() && !isset($parameters[$this->leftOperand->getName()])) { |
105
|
|
|
$bypass = true; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
View Code Duplication |
if ($this->rightOperand instanceof Parameter) { |
|
|
|
|
109
|
|
|
if ($this->rightOperand->isDiscardedOnNull() && !isset($parameters[$this->rightOperand->getName()])) { |
110
|
|
|
$bypass = true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
if ($bypass === true) { |
114
|
|
|
return; |
115
|
|
|
} else { |
116
|
|
|
$conditionsMode = self::CONDITION_IGNORE; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
if ($conditionsMode == self::CONDITION_IGNORE || !$this->condition || $this->condition->isOk($parameters)) { |
120
|
|
|
$sql = $this->getSql($parameters, $dbConnection, $indent, $conditionsMode); |
121
|
|
|
} else { |
122
|
|
|
$sql = null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $sql; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
129
|
|
|
{ |
130
|
|
|
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
131
|
|
|
$sql .= ' '.$this->getOperatorSymbol().' '; |
132
|
|
|
$sql .= NodeFactory::toSql($this->rightOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
133
|
|
|
|
134
|
|
|
return $sql; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
139
|
|
|
* |
140
|
|
|
* @param VisitorInterface $visitor |
141
|
|
|
*/ |
142
|
|
|
public function walk(VisitorInterface $visitor) |
143
|
|
|
{ |
144
|
|
|
$node = $this; |
145
|
|
|
$result = $visitor->enterNode($node); |
146
|
|
|
if ($result instanceof NodeInterface) { |
147
|
|
|
$node = $result; |
148
|
|
|
} |
149
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
150
|
|
|
$result2 = $this->leftOperand->walk($visitor); |
151
|
|
View Code Duplication |
if ($result2 === NodeTraverser::REMOVE_NODE) { |
|
|
|
|
152
|
|
|
return NodeTraverser::REMOVE_NODE; |
153
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
154
|
|
|
$this->leftOperand = $result2; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$result2 = $this->rightOperand->walk($visitor); |
158
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
159
|
|
|
return NodeTraverser::REMOVE_NODE; |
160
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
161
|
|
|
$this->rightOperand = $result2; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $visitor->leaveNode($node); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the symbol for this operator. |
170
|
|
|
*/ |
171
|
|
|
abstract protected function getOperatorSymbol(); |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.