1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* expression-types.php. |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2010-2013, Justin Swanhart |
8
|
|
|
* with contributions by André Rothe <[email protected], [email protected]> |
9
|
|
|
* and David Négrier <[email protected]> |
10
|
|
|
* |
11
|
|
|
* All rights reserved. |
12
|
|
|
* |
13
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
14
|
|
|
* are permitted provided that the following conditions are met: |
15
|
|
|
* |
16
|
|
|
* * Redistributions of source code must retain the above copyright notice, |
17
|
|
|
* this list of conditions and the following disclaimer. |
18
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, |
19
|
|
|
* this list of conditions and the following disclaimer in the documentation |
20
|
|
|
* and/or other materials provided with the distribution. |
21
|
|
|
* |
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
23
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
24
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
25
|
|
|
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
26
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
27
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
28
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
30
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
31
|
|
|
* DAMAGE. |
32
|
|
|
*/ |
33
|
|
|
namespace SQLParser\Node; |
34
|
|
|
|
35
|
|
|
use Doctrine\DBAL\Connection; |
36
|
|
|
use Mouf\MoufInstanceDescriptor; |
37
|
|
|
use Mouf\MoufManager; |
38
|
|
|
use SQLParser\Node\Traverser\NodeTraverser; |
39
|
|
|
use SQLParser\Node\Traverser\VisitorInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This class represents a node that is an SQL expression. |
43
|
|
|
* |
44
|
|
|
* @author David Négrier <[email protected]> |
45
|
|
|
*/ |
46
|
|
|
class Expression implements NodeInterface |
47
|
|
|
{ |
48
|
|
|
private $baseExpression; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the base expression (the string that generated this expression). |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getBaseExpression() |
56
|
|
|
{ |
57
|
|
|
return $this->baseExpression; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the base expression (the string that generated this expression). |
62
|
|
|
* |
63
|
|
|
* @param string $baseExpression |
64
|
|
|
*/ |
65
|
|
|
public function setBaseExpression($baseExpression) |
66
|
|
|
{ |
67
|
|
|
$this->baseExpression = $baseExpression; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private $subTree; |
71
|
|
|
|
72
|
|
|
public function getSubTree() |
73
|
|
|
{ |
74
|
|
|
return $this->subTree; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the subtree. |
79
|
|
|
* |
80
|
|
|
* @Important |
81
|
|
|
* |
82
|
|
|
* @param array<NodeInterface>|NodeInterface $subTree |
83
|
|
|
*/ |
84
|
|
|
public function setSubTree($subTree) |
85
|
|
|
{ |
86
|
|
|
$this->subTree = $subTree; |
87
|
|
|
$this->subTree = NodeFactory::simplify($this->subTree); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private $alias; |
91
|
|
|
|
92
|
|
|
public function getAlias() |
93
|
|
|
{ |
94
|
|
|
return $this->alias; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the alias. |
99
|
|
|
* |
100
|
|
|
* @Important |
101
|
|
|
* |
102
|
|
|
* @param string $alias |
103
|
|
|
*/ |
104
|
|
|
public function setAlias($alias) |
105
|
|
|
{ |
106
|
|
|
$this->alias = $alias; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private $direction; |
110
|
|
|
|
111
|
|
|
public function getDiretion() |
112
|
|
|
{ |
113
|
|
|
return $this->direction; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets the direction. |
118
|
|
|
* |
119
|
|
|
* @Important |
120
|
|
|
* |
121
|
|
|
* @param string $direction |
122
|
|
|
*/ |
123
|
|
|
public function setDirection($direction) |
124
|
|
|
{ |
125
|
|
|
$this->direction = $direction; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private $brackets = false; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns true if the expression is between brackets. |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function hasBrackets() |
136
|
|
|
{ |
137
|
|
|
return $this->brackets; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets to true if the expression is between brackets. |
142
|
|
|
* |
143
|
|
|
* @Important |
144
|
|
|
* |
145
|
|
|
* @param bool $brackets |
146
|
|
|
*/ |
147
|
|
|
public function setBrackets($brackets) |
148
|
|
|
{ |
149
|
|
|
$this->brackets = $brackets; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns a Mouf instance descriptor describing this object. |
154
|
|
|
* |
155
|
|
|
* @param MoufManager $moufManager |
156
|
|
|
* |
157
|
|
|
* @return MoufInstanceDescriptor |
158
|
|
|
*/ |
159
|
|
|
public function toInstanceDescriptor(MoufManager $moufManager) |
160
|
|
|
{ |
161
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
162
|
|
|
$instanceDescriptor->getProperty('baseExpression')->setValue(NodeFactory::nodeToInstanceDescriptor($this->baseExpression, $moufManager)); |
163
|
|
|
$instanceDescriptor->getProperty('subTree')->setValue(NodeFactory::nodeToInstanceDescriptor($this->subTree, $moufManager)); |
164
|
|
|
$instanceDescriptor->getProperty('alias')->setValue(NodeFactory::nodeToInstanceDescriptor($this->alias, $moufManager)); |
165
|
|
|
$instanceDescriptor->getProperty('direction')->setValue(NodeFactory::nodeToInstanceDescriptor($this->direction, $moufManager)); |
166
|
|
|
$instanceDescriptor->getProperty('brackets')->setValue(NodeFactory::nodeToInstanceDescriptor($this->brackets, $moufManager)); |
167
|
|
|
|
168
|
|
|
return $instanceDescriptor; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Renders the object as a SQL string. |
173
|
|
|
* |
174
|
|
|
* @param Connection $dbConnection |
175
|
|
|
* @param array $parameters |
176
|
|
|
* @param number $indent |
177
|
|
|
* @param int $conditionsMode |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
182
|
|
|
{ |
183
|
|
|
$sql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
184
|
|
|
|
185
|
|
|
if ($this->alias) { |
186
|
|
|
$sql .= ' AS '.$this->alias; |
187
|
|
|
} |
188
|
|
|
if ($this->direction) { |
189
|
|
|
$sql .= ' '.$this->direction; |
190
|
|
|
} |
191
|
|
|
if ($this->brackets) { |
192
|
|
|
$sql = '('.$sql.')'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $sql; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
200
|
|
|
* |
201
|
|
|
* @param VisitorInterface $visitor |
202
|
|
|
*/ |
203
|
|
View Code Duplication |
public function walk(VisitorInterface $visitor) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$node = $this; |
206
|
|
|
$result = $visitor->enterNode($node); |
207
|
|
|
if ($result instanceof NodeInterface) { |
208
|
|
|
$node = $result; |
209
|
|
|
} |
210
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
211
|
|
|
if (is_array($this->subTree)) { |
212
|
|
|
foreach ($this->subTree as $key => $operand) { |
213
|
|
|
$result2 = $operand->walk($visitor); |
214
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
215
|
|
|
unset($this->subTree[$key]); |
216
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
217
|
|
|
$this->subTree[$key] = $result2; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} else { |
221
|
|
|
$result2 = $this->subTree->walk($visitor); |
222
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
223
|
|
|
$this->subTree = []; |
224
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
225
|
|
|
$this->subTree = $result2; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $visitor->leaveNode($node); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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.