1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\SqlQuery; |
10
|
|
|
|
11
|
|
|
use Aura\SqlQuery\Common\SubselectInterface; |
12
|
|
|
use Aura\SqlQuery\Common\QuoterInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* Abstract query object. |
17
|
|
|
* |
18
|
|
|
* @package Aura.SqlQuery |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractQuery |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* Data to be bound to the query. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
protected $bind_values = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* The list of WHERE conditions. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
protected $where = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* ORDER BY these columns. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
|
|
protected $order_by = array(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* The list of flags. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
protected $flags = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* A helper for quoting identifier names. |
62
|
|
|
* |
63
|
|
|
* @var Quoter |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
protected $quoter; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* Constructor. |
71
|
|
|
* |
72
|
|
|
* @param Quoter $quoter A helper for quoting identifier names. |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
407 |
|
public function __construct(QuoterInterface $quoter, $builder) |
76
|
|
|
{ |
77
|
407 |
|
$this->quoter = $quoter; |
|
|
|
|
78
|
407 |
|
$this->builder = $builder; |
|
|
|
|
79
|
407 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* Returns this query object as an SQL statement string. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
243 |
|
public function __toString() |
89
|
|
|
{ |
90
|
243 |
|
return $this->getStatement(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* Returns this query object as an SQL statement string. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
69 |
|
public function getStatement() |
101
|
|
|
{ |
102
|
69 |
|
return $this->build(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* Builds this query object into a string. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
* |
111
|
|
|
*/ |
112
|
|
|
abstract protected function build(); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
* Returns the prefix to use when quoting identifier names. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
* |
120
|
|
|
*/ |
121
|
257 |
|
public function getQuoteNamePrefix() |
122
|
|
|
{ |
123
|
257 |
|
return $this->quoter->getQuoteNamePrefix(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* Returns the suffix to use when quoting identifier names. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
257 |
|
public function getQuoteNameSuffix() |
134
|
|
|
{ |
135
|
257 |
|
return $this->quoter->getQuoteNameSuffix(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* Binds multiple values to placeholders; merges with existing values. |
141
|
|
|
* |
142
|
|
|
* @param array $bind_values Values to bind to placeholders. |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
* |
146
|
|
|
*/ |
147
|
31 |
|
public function bindValues(array $bind_values) |
148
|
|
|
{ |
149
|
|
|
// array_merge() renumbers integer keys, which is bad for |
150
|
|
|
// question-mark placeholders |
151
|
31 |
|
foreach ($bind_values as $key => $val) { |
152
|
31 |
|
$this->bindValue($key, $val); |
153
|
31 |
|
} |
154
|
31 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* |
159
|
|
|
* Binds a single value to the query. |
160
|
|
|
* |
161
|
|
|
* @param string $name The placeholder name or number. |
162
|
|
|
* |
163
|
|
|
* @param mixed $value The value to bind to the placeholder. |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
* |
167
|
|
|
*/ |
168
|
136 |
|
public function bindValue($name, $value) |
169
|
|
|
{ |
170
|
136 |
|
$this->bind_values[$name] = $value; |
171
|
136 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* Gets the values to bind to placeholders. |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
* |
180
|
|
|
*/ |
181
|
126 |
|
public function getBindValues() |
182
|
|
|
{ |
183
|
126 |
|
return $this->bind_values; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* Reset all values bound to named placeholders. |
189
|
|
|
* |
190
|
|
|
* @return $this |
191
|
|
|
* |
192
|
|
|
*/ |
193
|
15 |
|
public function resetBindValues() |
194
|
|
|
{ |
195
|
15 |
|
$this->bind_values = array(); |
196
|
15 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* |
201
|
|
|
* Sets or unsets specified flag. |
202
|
|
|
* |
203
|
|
|
* @param string $flag Flag to set or unset |
204
|
|
|
* |
205
|
|
|
* @param bool $enable Flag status - enabled or not (default true) |
206
|
|
|
* |
207
|
|
|
* @return null |
208
|
|
|
* |
209
|
|
|
*/ |
210
|
43 |
|
protected function setFlag($flag, $enable = true) |
211
|
|
|
{ |
212
|
43 |
|
if ($enable) { |
213
|
43 |
|
$this->flags[$flag] = true; |
214
|
43 |
|
} else { |
215
|
5 |
|
unset($this->flags[$flag]); |
216
|
|
|
} |
217
|
43 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* |
221
|
|
|
* Returns true if the specified flag was enabled by setFlag(). |
222
|
|
|
* |
223
|
|
|
* @param string $flag Flag to check |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
* |
227
|
|
|
*/ |
228
|
10 |
|
protected function hasFlag($flag) |
229
|
|
|
{ |
230
|
10 |
|
return isset($this->flags[$flag]); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* |
235
|
|
|
* Reset all query flags. |
236
|
|
|
* |
237
|
|
|
* @return $this |
238
|
|
|
* |
239
|
|
|
*/ |
240
|
20 |
|
public function resetFlags() |
241
|
|
|
{ |
242
|
20 |
|
$this->flags = array(); |
243
|
20 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* |
248
|
|
|
* Adds conditions and binds values to a clause. |
249
|
|
|
* |
250
|
|
|
* @param string $clause The clause to work with, typically 'where' or |
251
|
|
|
* 'having'. |
252
|
|
|
* |
253
|
|
|
* @param string $andor Add the condition using this operator, typically |
254
|
|
|
* 'AND' or 'OR'. |
255
|
|
|
* |
256
|
|
|
* @param string $cond The WHERE condition. |
257
|
|
|
* |
258
|
|
|
* @param array $bind arguments to bind to placeholders |
259
|
|
|
* |
260
|
|
|
* @return null |
261
|
|
|
* |
262
|
|
|
*/ |
263
|
70 |
|
protected function addClauseCondWithBind($clause, $andor, $cond, $bind) |
264
|
|
|
{ |
265
|
70 |
|
$cond = $this->quoter->quoteNamesIn($cond); |
266
|
70 |
|
$cond = $this->fixCondWithBind($cond, $bind); |
267
|
|
|
|
268
|
70 |
|
$clause =& $this->$clause; |
269
|
70 |
|
if ($clause) { |
270
|
49 |
|
$clause[] = "$andor $cond"; |
271
|
49 |
|
} else { |
272
|
70 |
|
$clause[] = $cond; |
273
|
|
|
} |
274
|
|
|
|
275
|
70 |
|
} |
276
|
|
|
|
277
|
120 |
|
protected function fixCondWithBind($cond, array $bind) |
278
|
|
|
{ |
279
|
120 |
|
$selects = []; |
280
|
|
|
|
281
|
120 |
|
foreach ($bind as $key => $val) { |
282
|
80 |
|
if ($val instanceof SubselectInterface) { |
283
|
10 |
|
$selects[":{$key}"] = $val; |
284
|
10 |
|
} else { |
285
|
75 |
|
$this->bindValue($key, $val); |
286
|
|
|
} |
287
|
120 |
|
} |
288
|
|
|
|
289
|
120 |
|
foreach ($selects as $key => $select) { |
290
|
10 |
|
$selects[$key] = $select->getStatement(); |
291
|
10 |
|
$this->bind_values = array_merge( |
292
|
10 |
|
$this->bind_values, |
293
|
10 |
|
$select->getBindValues() |
294
|
10 |
|
); |
295
|
120 |
|
} |
296
|
|
|
|
297
|
120 |
|
$cond = strtr($cond, $selects); |
298
|
120 |
|
return $cond; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* |
303
|
|
|
* Adds a column order to the query. |
304
|
|
|
* |
305
|
|
|
* @param array $spec The columns and direction to order by. |
306
|
|
|
* |
307
|
|
|
* @return $this |
308
|
|
|
* |
309
|
|
|
*/ |
310
|
9 |
|
protected function addOrderBy(array $spec) |
311
|
|
|
{ |
312
|
9 |
|
foreach ($spec as $col) { |
313
|
9 |
|
$this->order_by[] = $this->quoter->quoteNamesIn($col); |
314
|
9 |
|
} |
315
|
9 |
|
return $this; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..