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