|
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\Platforms\AbstractPlatform; |
|
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 table (and optionally a JOIN .. ON expression) in a SQL expression. |
|
43
|
|
|
* |
|
44
|
|
|
* @author David Négrier <[email protected]> |
|
45
|
|
|
*/ |
|
46
|
|
|
class Table implements NodeInterface |
|
47
|
|
|
{ |
|
48
|
|
|
private $database; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the name of the database. |
|
52
|
|
|
* |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getDatabase() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->database; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Sets the name of the database |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $database |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setDatabase($database) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->database = $database; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private $table; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the table name. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getTable() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->table; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets the table name. |
|
84
|
|
|
* |
|
85
|
|
|
* @Important |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $table |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setTable($table) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->table = $table; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private $alias; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the alias. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getAlias() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->alias; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Sets the alias. |
|
108
|
|
|
* |
|
109
|
|
|
* @Important |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $alias |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setAlias($alias) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->alias = $alias; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private $joinType; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns the join type. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getJoinType() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->joinType; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets the join type (JOIN, LEFT JOIN, RIGHT JOIN, etc...). |
|
132
|
|
|
* |
|
133
|
|
|
* @Important |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $joinType |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setJoinType($joinType) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->joinType = $joinType; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private $refClause; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the list of refClause statements. |
|
146
|
|
|
* |
|
147
|
|
|
* @return NodeInterface[]|NodeInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getRefClause() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->refClause; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Sets the list of refClause statements. |
|
156
|
|
|
* |
|
157
|
|
|
* @Important |
|
158
|
|
|
* |
|
159
|
|
|
* @param NodeInterface[]|NodeInterface $refClause |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setRefClause($refClause) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->refClause = $refClause; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @var Hint[] |
|
168
|
|
|
*/ |
|
169
|
|
|
private $hints; |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Return the list of table hints |
|
173
|
|
|
* |
|
174
|
|
|
* @return Hint[] |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getHints(): array |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->hints; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Set a list of table hints |
|
183
|
|
|
* |
|
184
|
|
|
* @param Hint[] $hints |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setHints(array $hints): void |
|
187
|
|
|
{ |
|
188
|
|
|
$this->hints = $hints; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Returns a Mouf instance descriptor describing this object. |
|
193
|
|
|
* |
|
194
|
|
|
* @param MoufManager $moufManager |
|
195
|
|
|
* |
|
196
|
|
|
* @return MoufInstanceDescriptor |
|
197
|
|
|
*/ |
|
198
|
|
|
public function toInstanceDescriptor(MoufManager $moufManager) |
|
199
|
|
|
{ |
|
200
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
|
201
|
|
|
$instanceDescriptor->getProperty('database')->setValue($this->database); |
|
202
|
|
|
$instanceDescriptor->getProperty('table')->setValue($this->table); |
|
203
|
|
|
$instanceDescriptor->getProperty('alias')->setValue($this->alias); |
|
204
|
|
|
$instanceDescriptor->getProperty('joinType')->setValue($this->joinType); |
|
205
|
|
|
$instanceDescriptor->getProperty('refClause')->setValue(NodeFactory::nodeToInstanceDescriptor($this->refClause, $moufManager)); |
|
206
|
|
|
|
|
207
|
|
|
return $instanceDescriptor; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Renders the object as a SQL string. |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $parameters |
|
214
|
|
|
* @param AbstractPlatform $platform |
|
215
|
|
|
* @param int $indent |
|
216
|
|
|
* @param int $conditionsMode |
|
217
|
|
|
* |
|
218
|
|
|
* @param bool $extrapolateParameters |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string |
|
222
|
|
|
{ |
|
223
|
|
|
$sql = ''; |
|
224
|
|
|
if ($this->refClause || $this->joinType === 'CROSS JOIN') { |
|
225
|
|
|
$sql .= "\n ".$this->joinType.' '; |
|
226
|
|
|
} |
|
227
|
|
|
if ($this->database) { |
|
228
|
|
|
$sql .= $platform->quoteSingleIdentifier($this->database).'.'; |
|
229
|
|
|
} |
|
230
|
|
|
$sql .= $platform->quoteSingleIdentifier($this->table); |
|
231
|
|
|
if ($this->alias) { |
|
232
|
|
|
$sql .= ' AS '.$platform->quoteSingleIdentifier($this->alias); |
|
233
|
|
|
} |
|
234
|
|
|
if ($this->hints) { |
|
|
|
|
|
|
235
|
|
|
foreach ($this->hints as $hint) { |
|
236
|
|
|
$sql .= ' ' . $hint->getType() . ' ' . $hint->getList(); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
if ($this->refClause) { |
|
240
|
|
|
$sql .= ' ON '; |
|
241
|
|
|
$sql .= NodeFactory::toSql($this->refClause, $platform, $parameters, ' ', true, $indent, $conditionsMode, $extrapolateParameters); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $sql; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
|
249
|
|
|
* |
|
250
|
|
|
* @param VisitorInterface $visitor |
|
251
|
|
|
*/ |
|
252
|
|
|
public function walk(VisitorInterface $visitor) |
|
253
|
|
|
{ |
|
254
|
|
|
$node = $this; |
|
255
|
|
|
$result = $visitor->enterNode($node); |
|
256
|
|
|
if ($result instanceof NodeInterface) { |
|
257
|
|
|
$node = $result; |
|
258
|
|
|
} |
|
259
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
|
260
|
|
|
if (is_array($this->refClause)) { |
|
261
|
|
|
foreach ($this->refClause as $key => $operand) { |
|
262
|
|
|
$result2 = $operand->walk($visitor); |
|
263
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
|
264
|
|
|
unset($this->refClause[$key]); |
|
265
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
|
266
|
|
|
$this->refClause[$key] = $result2; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
} elseif ($this->refClause) { |
|
270
|
|
|
$result2 = $this->refClause->walk($visitor); |
|
271
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
|
272
|
|
|
$this->refClause = null; |
|
273
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
|
274
|
|
|
$this->refClause = $result2; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return $visitor->leaveNode($node); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.