|
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\LimitInterface; |
|
12
|
|
|
use Aura\SqlQuery\Common\LimitOffsetInterface; |
|
13
|
|
|
use Aura\SqlQuery\Common\SubselectInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* Abstract query object. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Aura.SqlQuery |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractQuery |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* Data to be bound to the query. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $bind_values = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* The list of WHERE conditions. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
* |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $where = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* ORDER BY these columns. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
* |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $order_by = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* The number of rows to select |
|
54
|
|
|
* |
|
55
|
|
|
* @var int |
|
56
|
|
|
* |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $limit = 0; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
* Return rows after this offset. |
|
63
|
|
|
* |
|
64
|
|
|
* @var int |
|
65
|
|
|
* |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $offset = 0; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* |
|
71
|
|
|
* The list of flags. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
* |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $flags = array(); |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* |
|
80
|
|
|
* A helper for quoting identifier names. |
|
81
|
|
|
* |
|
82
|
|
|
* @var Quoter |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $quoter; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* Prefix to use on placeholders for "sequential" bound values; used for |
|
90
|
|
|
* deconfliction when merging bound values from sub-selects, etc. |
|
91
|
|
|
* |
|
92
|
|
|
* @var mixed |
|
93
|
|
|
* |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $seq_bind_prefix = ''; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* Constructor. |
|
100
|
|
|
* |
|
101
|
|
|
* @param Quoter $quoter A helper for quoting identifier names. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $seq_bind_prefix A prefix for rewritten sequential-binding |
|
104
|
|
|
* placeholders (@see getSeqPlaceholder()). |
|
105
|
|
|
* |
|
106
|
|
|
*/ |
|
107
|
397 |
|
public function __construct(Quoter $quoter, $seq_bind_prefix = '') |
|
108
|
|
|
{ |
|
109
|
397 |
|
$this->quoter = $quoter; |
|
110
|
397 |
|
$this->seq_bind_prefix = $seq_bind_prefix; |
|
111
|
397 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* |
|
115
|
|
|
* Returns the prefix for rewritten sequential-binding placeholders |
|
116
|
|
|
* (@see getSeqPlaceholder()). |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
* |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function getSeqBindPrefix() |
|
122
|
|
|
{ |
|
123
|
1 |
|
return $this->seq_bind_prefix; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* |
|
128
|
|
|
* Returns this query object as an SQL statement string. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
* |
|
132
|
|
|
*/ |
|
133
|
242 |
|
public function __toString() |
|
134
|
|
|
{ |
|
135
|
242 |
|
return $this->getStatement(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* |
|
140
|
|
|
* Returns this query object as an SQL statement string. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
* |
|
144
|
|
|
*/ |
|
145
|
68 |
|
public function getStatement() |
|
146
|
|
|
{ |
|
147
|
68 |
|
return $this->build(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* |
|
152
|
|
|
* Builds this query object into a string. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
* |
|
156
|
|
|
*/ |
|
157
|
|
|
abstract protected function build(); |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* |
|
161
|
|
|
* Returns the prefix to use when quoting identifier names. |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
* |
|
165
|
|
|
*/ |
|
166
|
251 |
|
public function getQuoteNamePrefix() |
|
167
|
|
|
{ |
|
168
|
251 |
|
return $this->quoter->getQuoteNamePrefix(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* |
|
173
|
|
|
* Returns the suffix to use when quoting identifier names. |
|
174
|
|
|
* |
|
175
|
|
|
* @return string |
|
176
|
|
|
* |
|
177
|
|
|
*/ |
|
178
|
251 |
|
public function getQuoteNameSuffix() |
|
179
|
|
|
{ |
|
180
|
251 |
|
return $this->quoter->getQuoteNameSuffix(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* |
|
185
|
|
|
* Returns an array as an indented comma-separated values string. |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $list The values to convert. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
* |
|
191
|
|
|
*/ |
|
192
|
219 |
|
protected function indentCsv(array $list) |
|
193
|
|
|
{ |
|
194
|
219 |
|
return PHP_EOL . ' ' |
|
195
|
219 |
|
. implode(',' . PHP_EOL . ' ', $list); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* |
|
200
|
|
|
* Returns an array as an indented string. |
|
201
|
|
|
* |
|
202
|
|
|
* @param array $list The values to convert. |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
* |
|
206
|
|
|
*/ |
|
207
|
71 |
|
protected function indent(array $list) |
|
208
|
|
|
{ |
|
209
|
71 |
|
return PHP_EOL . ' ' |
|
210
|
71 |
|
. implode(PHP_EOL . ' ', $list); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* |
|
215
|
|
|
* Binds multiple values to placeholders; merges with existing values. |
|
216
|
|
|
* |
|
217
|
|
|
* @param array $bind_values Values to bind to placeholders. |
|
218
|
|
|
* |
|
219
|
|
|
* @return $this |
|
220
|
|
|
* |
|
221
|
|
|
*/ |
|
222
|
31 |
|
public function bindValues(array $bind_values) |
|
223
|
|
|
{ |
|
224
|
|
|
// array_merge() renumbers integer keys, which is bad for |
|
225
|
|
|
// question-mark placeholders |
|
226
|
31 |
|
foreach ($bind_values as $key => $val) { |
|
227
|
31 |
|
$this->bindValue($key, $val); |
|
228
|
|
|
} |
|
229
|
31 |
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* |
|
234
|
|
|
* Binds a single value to the query. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $name The placeholder name or number. |
|
237
|
|
|
* |
|
238
|
|
|
* @param mixed $value The value to bind to the placeholder. |
|
239
|
|
|
* |
|
240
|
|
|
* @return $this |
|
241
|
|
|
* |
|
242
|
|
|
*/ |
|
243
|
72 |
|
public function bindValue($name, $value) |
|
244
|
|
|
{ |
|
245
|
72 |
|
$this->bind_values[$name] = $value; |
|
246
|
72 |
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* |
|
251
|
|
|
* Gets the values to bind to placeholders. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
* |
|
255
|
|
|
*/ |
|
256
|
126 |
|
public function getBindValues() |
|
257
|
|
|
{ |
|
258
|
126 |
|
return $this->bind_values; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* |
|
263
|
|
|
* Reset all values bound to named placeholders. |
|
264
|
|
|
* |
|
265
|
|
|
* @return $this |
|
266
|
|
|
* |
|
267
|
|
|
*/ |
|
268
|
|
|
public function resetBindValues() |
|
269
|
|
|
{ |
|
270
|
|
|
$this->bind_values = array(); |
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* |
|
276
|
|
|
* Builds the flags as a space-separated string. |
|
277
|
|
|
* |
|
278
|
|
|
* @return string |
|
279
|
|
|
* |
|
280
|
|
|
*/ |
|
281
|
252 |
|
protected function buildFlags() |
|
282
|
|
|
{ |
|
283
|
252 |
|
if (empty($this->flags)) { |
|
284
|
214 |
|
return ''; // not applicable |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
38 |
|
return ' ' . implode(' ', array_keys($this->flags)); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* |
|
292
|
|
|
* Sets or unsets specified flag. |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $flag Flag to set or unset |
|
295
|
|
|
* |
|
296
|
|
|
* @param bool $enable Flag status - enabled or not (default true) |
|
297
|
|
|
* |
|
298
|
|
|
* @return null |
|
299
|
|
|
* |
|
300
|
|
|
*/ |
|
301
|
43 |
|
protected function setFlag($flag, $enable = true) |
|
302
|
|
|
{ |
|
303
|
43 |
|
if ($enable) { |
|
304
|
43 |
|
$this->flags[$flag] = true; |
|
305
|
|
|
} else { |
|
306
|
5 |
|
unset($this->flags[$flag]); |
|
307
|
|
|
} |
|
308
|
43 |
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* |
|
312
|
|
|
* Returns true if the specified flag was enabled by setFlag(). |
|
313
|
|
|
* |
|
314
|
|
|
* @param string $flag Flag to check |
|
315
|
|
|
* |
|
316
|
|
|
* @return bool |
|
317
|
|
|
* |
|
318
|
|
|
*/ |
|
319
|
10 |
|
protected function hasFlag($flag) |
|
320
|
|
|
{ |
|
321
|
10 |
|
return isset($this->flags[$flag]); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* |
|
326
|
|
|
* Reset all query flags. |
|
327
|
|
|
* |
|
328
|
|
|
* @return $this |
|
329
|
|
|
* |
|
330
|
|
|
*/ |
|
331
|
15 |
|
public function resetFlags() |
|
332
|
|
|
{ |
|
333
|
15 |
|
$this->flags = array(); |
|
334
|
15 |
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* |
|
339
|
|
|
* Adds a WHERE condition to the query by AND or OR. If the condition has |
|
340
|
|
|
* ?-placeholders, additional arguments to the method will be bound to |
|
341
|
|
|
* those placeholders sequentially. |
|
342
|
|
|
* |
|
343
|
|
|
* @param string $andor Add the condition using this operator, typically |
|
344
|
|
|
* 'AND' or 'OR'. |
|
345
|
|
|
* |
|
346
|
|
|
* @param string $cond The WHERE condition. |
|
347
|
|
|
* |
|
348
|
|
|
* @param array ...$bind arguments to bind to placeholders |
|
349
|
|
|
* |
|
350
|
|
|
* @return $this |
|
351
|
|
|
* |
|
352
|
|
|
*/ |
|
353
|
60 |
|
protected function addWhere($andor, $cond, ...$bind) |
|
354
|
|
|
{ |
|
355
|
60 |
|
$this->addClauseCondWithBind('where', $andor, $cond, $bind); |
|
356
|
60 |
|
return $this; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* |
|
361
|
|
|
* Adds conditions and binds values to a clause. |
|
362
|
|
|
* |
|
363
|
|
|
* @param string $clause The clause to work with, typically 'where' or |
|
364
|
|
|
* 'having'. |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $andor Add the condition using this operator, typically |
|
367
|
|
|
* 'AND' or 'OR'. |
|
368
|
|
|
* |
|
369
|
|
|
* @param string $cond The WHERE condition. |
|
370
|
|
|
|
|
371
|
|
|
* @param array $bind arguments to bind to placeholders |
|
372
|
|
|
* |
|
373
|
|
|
* @return null |
|
374
|
|
|
* |
|
375
|
|
|
*/ |
|
376
|
70 |
|
protected function addClauseCondWithBind($clause, $andor, $cond, $bind) |
|
377
|
|
|
{ |
|
378
|
70 |
|
$cond = $this->rebuildCondAndBindValues($cond, $bind); |
|
379
|
|
|
|
|
380
|
|
|
// add condition to clause; eg $this->where or $this->having |
|
381
|
70 |
|
$clause =& $this->$clause; |
|
382
|
70 |
|
if ($clause) { |
|
383
|
49 |
|
$clause[] = "$andor $cond"; |
|
384
|
|
|
} else { |
|
385
|
70 |
|
$clause[] = $cond; |
|
386
|
|
|
} |
|
387
|
70 |
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* |
|
391
|
|
|
* Rebuilds a condition string, replacing sequential placeholders with |
|
392
|
|
|
* named placeholders, and binding the sequential values to the named |
|
393
|
|
|
* placeholders. |
|
394
|
|
|
* |
|
395
|
|
|
* @param string $cond The condition with sequential placeholders. |
|
396
|
|
|
* |
|
397
|
|
|
* @param array $bind_values The values to bind to the sequential |
|
398
|
|
|
* placeholders under their named versions. |
|
399
|
|
|
* |
|
400
|
|
|
* @return string The rebuilt condition string. |
|
401
|
|
|
* |
|
402
|
|
|
*/ |
|
403
|
120 |
|
protected function rebuildCondAndBindValues($cond, array $bind_values) |
|
404
|
|
|
{ |
|
405
|
120 |
|
$cond = $this->quoter->quoteNamesIn($cond); |
|
406
|
|
|
|
|
407
|
|
|
// bind values against ?-mark placeholders, but because PDO is finicky |
|
408
|
|
|
// about the numbering of sequential placeholders, convert each ?-mark |
|
409
|
|
|
// to a named placeholder |
|
410
|
120 |
|
$parts = preg_split('/(\?)/', $cond, null, PREG_SPLIT_DELIM_CAPTURE); |
|
411
|
120 |
|
foreach ($parts as $key => $val) { |
|
412
|
120 |
|
if ($val != '?') { |
|
413
|
120 |
|
continue; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
80 |
|
$bind_value = array_shift($bind_values); |
|
417
|
80 |
|
if ($bind_value instanceof SubselectInterface) { |
|
418
|
10 |
|
$parts[$key] = $bind_value->getStatement(); |
|
419
|
10 |
|
$this->bind_values = array_merge( |
|
420
|
10 |
|
$this->bind_values, |
|
421
|
10 |
|
$bind_value->getBindValues() |
|
422
|
|
|
); |
|
423
|
10 |
|
continue; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
75 |
|
$placeholder = $this->getSeqPlaceholder(); |
|
427
|
75 |
|
$parts[$key] = ':' . $placeholder; |
|
428
|
75 |
|
$this->bind_values[$placeholder] = $bind_value; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
120 |
|
$cond = implode('', $parts); |
|
432
|
120 |
|
return $cond; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* |
|
437
|
|
|
* Gets the current sequential placeholder name. |
|
438
|
|
|
* |
|
439
|
|
|
* @return string |
|
440
|
|
|
* |
|
441
|
|
|
*/ |
|
442
|
75 |
|
protected function getSeqPlaceholder() |
|
443
|
|
|
{ |
|
444
|
75 |
|
$i = count($this->bind_values) + 1; |
|
445
|
75 |
|
return $this->seq_bind_prefix . "_{$i}_"; |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
/** |
|
449
|
|
|
* |
|
450
|
|
|
* Builds the `WHERE` clause of the statement. |
|
451
|
|
|
* |
|
452
|
|
|
* @return string |
|
453
|
|
|
* |
|
454
|
|
|
*/ |
|
455
|
206 |
|
protected function buildWhere() |
|
456
|
|
|
{ |
|
457
|
206 |
|
if (empty($this->where)) { |
|
458
|
151 |
|
return ''; // not applicable |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
60 |
|
return PHP_EOL . 'WHERE' . $this->indent($this->where); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* |
|
466
|
|
|
* Adds a column order to the query. |
|
467
|
|
|
* |
|
468
|
|
|
* @param array $spec The columns and direction to order by. |
|
469
|
|
|
* |
|
470
|
|
|
* @return $this |
|
471
|
|
|
* |
|
472
|
|
|
*/ |
|
473
|
9 |
|
protected function addOrderBy(array $spec) |
|
474
|
|
|
{ |
|
475
|
9 |
|
foreach ($spec as $col) { |
|
476
|
9 |
|
$this->order_by[] = $this->quoter->quoteNamesIn($col); |
|
477
|
|
|
} |
|
478
|
9 |
|
return $this; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* |
|
483
|
|
|
* Builds the `ORDER BY ...` clause of the statement. |
|
484
|
|
|
* |
|
485
|
|
|
* @return string |
|
486
|
|
|
* |
|
487
|
|
|
*/ |
|
488
|
206 |
|
protected function buildOrderBy() |
|
489
|
|
|
{ |
|
490
|
206 |
|
if (empty($this->order_by)) { |
|
491
|
197 |
|
return ''; // not applicable |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
9 |
|
return PHP_EOL . 'ORDER BY' . $this->indentCsv($this->order_by); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* |
|
499
|
|
|
* Builds the `LIMIT ... OFFSET` clause of the statement. |
|
500
|
|
|
* |
|
501
|
|
|
* Note that this will allow OFFSET values with a LIMIT. |
|
502
|
|
|
* |
|
503
|
|
|
* @return string |
|
504
|
|
|
* |
|
505
|
|
|
*/ |
|
506
|
172 |
|
protected function buildLimit() |
|
507
|
|
|
{ |
|
508
|
172 |
|
$clause = ''; |
|
509
|
172 |
|
$limit = $this instanceof LimitInterface && $this->limit; |
|
510
|
172 |
|
$offset = $this instanceof LimitOffsetInterface && $this->offset; |
|
511
|
|
|
|
|
512
|
172 |
|
if ($limit) { |
|
513
|
19 |
|
$clause .= "LIMIT {$this->limit}"; |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
172 |
|
if ($offset) { |
|
517
|
10 |
|
$clause .= " OFFSET {$this->offset}"; |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
172 |
|
if ($clause) { |
|
521
|
19 |
|
$clause = PHP_EOL . trim($clause); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
172 |
|
return $clause; |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
|