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 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 $table; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the table name. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getTable() |
56
|
|
|
{ |
57
|
|
|
return $this->table; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the table name. |
62
|
|
|
* |
63
|
|
|
* @Important |
64
|
|
|
* |
65
|
|
|
* @param string $table |
66
|
|
|
*/ |
67
|
|
|
public function setTable($table) |
68
|
|
|
{ |
69
|
|
|
$this->table = $table; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private $alias; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the alias. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getAlias() |
80
|
|
|
{ |
81
|
|
|
return $this->alias; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the alias. |
86
|
|
|
* |
87
|
|
|
* @Important |
88
|
|
|
* |
89
|
|
|
* @param string $alias |
90
|
|
|
*/ |
91
|
|
|
public function setAlias($alias) |
92
|
|
|
{ |
93
|
|
|
$this->alias = $alias; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private $joinType; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the join type. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getJoinType() |
104
|
|
|
{ |
105
|
|
|
return $this->joinType; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets the join type (JOIN, LEFT JOIN, RIGHT JOIN, etc...). |
110
|
|
|
* |
111
|
|
|
* @Important |
112
|
|
|
* |
113
|
|
|
* @param string $joinType |
114
|
|
|
*/ |
115
|
|
|
public function setJoinType($joinType) |
116
|
|
|
{ |
117
|
|
|
$this->joinType = $joinType; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private $refClause; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the list of refClause statements. |
124
|
|
|
* |
125
|
|
|
* @return NodeInterface[]|NodeInterface |
126
|
|
|
*/ |
127
|
|
|
public function getRefClause() |
128
|
|
|
{ |
129
|
|
|
return $this->refClause; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the list of refClause statements. |
134
|
|
|
* |
135
|
|
|
* @Important |
136
|
|
|
* |
137
|
|
|
* @param NodeInterface[]|NodeInterface $refClause |
138
|
|
|
*/ |
139
|
|
|
public function setRefClause($refClause) |
140
|
|
|
{ |
141
|
|
|
$this->refClause = $refClause; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns a Mouf instance descriptor describing this object. |
146
|
|
|
* |
147
|
|
|
* @param MoufManager $moufManager |
148
|
|
|
* |
149
|
|
|
* @return MoufInstanceDescriptor |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function toInstanceDescriptor(MoufManager $moufManager) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
154
|
|
|
$instanceDescriptor->getProperty('table')->setValue($this->table); |
155
|
|
|
$instanceDescriptor->getProperty('alias')->setValue($this->alias); |
156
|
|
|
$instanceDescriptor->getProperty('joinType')->setValue($this->joinType); |
157
|
|
|
$instanceDescriptor->getProperty('refClause')->setValue(NodeFactory::nodeToInstanceDescriptor($this->refClause, $moufManager)); |
158
|
|
|
|
159
|
|
|
return $instanceDescriptor; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Renders the object as a SQL string. |
164
|
|
|
* |
165
|
|
|
* @param Connection $dbConnection |
166
|
|
|
* @param array $parameters |
167
|
|
|
* @param number $indent |
168
|
|
|
* @param int $conditionsMode |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
173
|
|
|
{ |
174
|
|
|
$sql = ''; |
175
|
|
|
if ($this->refClause) { |
176
|
|
|
$sql .= "\n ".$this->joinType.' '; |
177
|
|
|
} |
178
|
|
|
$sql .= NodeFactory::escapeDBItem($this->table, $dbConnection); |
179
|
|
|
if ($this->alias) { |
180
|
|
|
$sql .= ' AS '.NodeFactory::escapeDBItem($this->alias, $dbConnection); |
181
|
|
|
} |
182
|
|
View Code Duplication |
if ($this->refClause) { |
|
|
|
|
183
|
|
|
$sql .= ' ON '; |
184
|
|
|
$sql .= NodeFactory::toSql($this->refClause, $dbConnection, $parameters, ' ', true, $indent, $conditionsMode); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $sql; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
192
|
|
|
* |
193
|
|
|
* @param VisitorInterface $visitor |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function walk(VisitorInterface $visitor) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$node = $this; |
198
|
|
|
$result = $visitor->enterNode($node); |
199
|
|
|
if ($result instanceof NodeInterface) { |
200
|
|
|
$node = $result; |
201
|
|
|
} |
202
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
203
|
|
|
if (is_array($this->refClause)) { |
204
|
|
|
foreach ($this->refClause as $key => $operand) { |
205
|
|
|
$result2 = $operand->walk($visitor); |
206
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
207
|
|
|
unset($this->refClause[$key]); |
208
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
209
|
|
|
$this->refClause[$key] = $result2; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} elseif ($this->refClause) { |
213
|
|
|
$result2 = $this->refClause->walk($visitor); |
214
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
215
|
|
|
$this->refClause = null; |
216
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
217
|
|
|
$this->refClause = $result2; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $visitor->leaveNode($node); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
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.