1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Exception\PrimeException; |
7
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor; |
8
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
9
|
|
|
use Bdf\Prime\Query\Contract\Paginable; |
10
|
|
|
use Bdf\Prime\Query\Contract\ReadOperation; |
11
|
|
|
use Bdf\Prime\Query\Contract\WriteOperation; |
12
|
|
|
use Bdf\Prime\Query\Expression\Raw; |
13
|
|
|
use Bdf\Prime\Query\Extension\EntityJoinTrait; |
14
|
|
|
use Bdf\Prime\Query\Extension\LimitableTrait; |
15
|
|
|
use Bdf\Prime\Query\Extension\LockableTrait; |
16
|
|
|
use Bdf\Prime\Query\Extension\OrderableTrait; |
17
|
|
|
use Bdf\Prime\Query\Extension\PaginableTrait; |
18
|
|
|
use Bdf\Prime\Query\Extension\SimpleJoinTrait; |
19
|
|
|
use Doctrine\DBAL\Query\Expression\CompositeExpression; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sql Query |
23
|
|
|
* |
24
|
|
|
* @package Bdf\Prime\Query |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @todo comment reset un statement (ex ecraser les orders). Prendre en compte le reset du compiler |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
class Query extends AbstractQuery implements SqlQueryInterface, Paginable |
29
|
|
|
{ |
30
|
|
|
use EntityJoinTrait; |
31
|
|
|
use PaginableTrait; |
32
|
|
|
use LimitableTrait; |
33
|
|
|
use OrderableTrait; |
34
|
|
|
use SimpleJoinTrait; |
35
|
|
|
use LockableTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes a new <tt>Query</tt>. |
39
|
|
|
* |
40
|
|
|
* @param ConnectionInterface $connection The DBAL Connection. |
41
|
|
|
* @param PreprocessorInterface|null $preprocessor |
42
|
|
|
*/ |
43
|
733 |
|
public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null) |
44
|
|
|
{ |
45
|
733 |
|
parent::__construct($connection, $preprocessor ?: new DefaultPreprocessor()); |
|
|
|
|
46
|
|
|
|
47
|
733 |
|
$this->statements = [ |
48
|
|
|
'ignore' => null, |
49
|
|
|
'replace' => null, |
50
|
|
|
'values' => [], |
51
|
|
|
'columns' => [], |
52
|
|
|
'distinct' => null, |
53
|
|
|
'tables' => [], |
54
|
|
|
'joins' => [], |
55
|
|
|
'where' => [], |
56
|
|
|
'groups' => [], |
57
|
|
|
'having' => [], |
58
|
|
|
'orders' => [], |
59
|
|
|
'limit' => null, |
60
|
|
|
'offset' => null, |
61
|
|
|
'aggregate' => null, |
62
|
|
|
'lock' => null, |
63
|
|
|
]; |
64
|
733 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
583 |
|
public function getBindings() |
70
|
|
|
{ |
71
|
583 |
|
return $this->compiler->getBindings($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function raw($sql) |
78
|
|
|
{ |
79
|
1 |
|
return new Raw($sql); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function quote($value, $type = null) |
86
|
|
|
{ |
87
|
|
|
return $this->compiler->quote($value, $type); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
|
|
|
|
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @todo Utile ? |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function quoteIdentifier($column) |
96
|
|
|
{ |
97
|
|
|
return $this->compiler->quoteIdentifier($this, $column); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
#[WriteOperation] |
|
|
|
|
104
|
26 |
|
public function delete() |
|
|
|
|
105
|
|
|
{ |
106
|
26 |
|
return $this->executeUpdate(self::TYPE_DELETE); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
#[WriteOperation] |
|
|
|
|
113
|
21 |
|
public function update(array $data = [], array $types = []) |
|
|
|
|
114
|
|
|
{ |
115
|
21 |
|
if ($data) { |
|
|
|
|
116
|
11 |
|
$this->statements['values'] = [ |
117
|
11 |
|
'data' => $data, |
118
|
11 |
|
'types' => $types, |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
21 |
|
return $this->executeUpdate(self::TYPE_UPDATE); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
#[WriteOperation] |
|
|
|
|
129
|
443 |
|
public function insert(array $data = []) |
|
|
|
|
130
|
|
|
{ |
131
|
443 |
|
if ($data) { |
|
|
|
|
132
|
443 |
|
$this->statements['values'] = [ |
133
|
443 |
|
'data' => $data, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
443 |
|
return $this->executeUpdate(self::TYPE_INSERT); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
|
|
|
|
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
1 |
|
public function ignore($flag = true) |
144
|
|
|
{ |
145
|
1 |
|
$this->statements['ignore'] = (bool)$flag; |
|
|
|
|
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
#[WriteOperation] |
|
|
|
|
154
|
2 |
|
public function replace(array $values = []) |
|
|
|
|
155
|
|
|
{ |
156
|
2 |
|
$this->statements['replace'] = true; |
157
|
|
|
|
158
|
2 |
|
return $this->insert($values); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set values to insert or update |
163
|
|
|
* |
164
|
|
|
* @todo Remonter sur une interface ? |
|
|
|
|
165
|
|
|
* |
166
|
|
|
* <code> |
167
|
|
|
* // Perform a INSERT INTO ... SELECT ... query |
168
|
|
|
* $query |
169
|
|
|
* ->from('users_bck') |
170
|
|
|
* ->values($connection->builder()->from('users')) |
171
|
|
|
* ->insert() |
172
|
|
|
* ; |
173
|
|
|
* |
174
|
|
|
* // Perform a REPLACE INTO ... SELECT ... with column mapping |
175
|
|
|
* $query |
176
|
|
|
* ->from('users_bck') |
177
|
|
|
* ->values( |
178
|
|
|
* $connection->builder() |
179
|
|
|
* ->from('users') |
180
|
|
|
* ->select([ |
181
|
|
|
* 'backup_name' => 'name', // Map "name" column from select table to "backup_name" column to insert table |
182
|
|
|
* 'id' // Use "id" column without mapping other than ORM data mapping |
183
|
|
|
* ]) |
184
|
|
|
* ) |
185
|
|
|
* ->replace() // Replace can also be used |
186
|
|
|
* ; |
187
|
|
|
* |
188
|
|
|
* // Simple UPDATE query |
189
|
|
|
* $query->values(['foo' => 'bar'])->update(); |
190
|
|
|
* </code> |
191
|
|
|
* |
192
|
|
|
* @param QueryInterface|array $data The values, in form [column] => [value], or the SELECT query |
|
|
|
|
193
|
|
|
* @param array $types The binding types Do not works with INSERT INTO SELECT query |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
19 |
|
public function values($data = [], array $types = []) |
198
|
|
|
{ |
199
|
19 |
|
$this->statements['values'] = [ |
200
|
19 |
|
'data' => $data, |
201
|
19 |
|
'type' => $types |
|
|
|
|
202
|
|
|
]; |
203
|
|
|
|
204
|
19 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Executes this query as an update query |
209
|
|
|
* |
210
|
|
|
* @param string $type The query type |
211
|
|
|
* |
212
|
|
|
* @return int The number of updated rows |
|
|
|
|
213
|
|
|
* |
214
|
|
|
* @throws PrimeException |
|
|
|
|
215
|
|
|
*/ |
216
|
456 |
|
protected function executeUpdate($type) |
217
|
|
|
{ |
218
|
456 |
|
$this->setType($type); |
219
|
|
|
|
220
|
456 |
|
$nb = $this->connection->execute($this)->count(); |
221
|
|
|
|
222
|
456 |
|
if ($nb > 0) { |
223
|
447 |
|
$this->clearCacheOnWrite(); |
224
|
|
|
} |
225
|
|
|
|
226
|
456 |
|
return $nb; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
* |
232
|
|
|
* @todo Return statement instead of array ? |
|
|
|
|
233
|
|
|
*/ |
234
|
|
|
#[ReadOperation] |
|
|
|
|
235
|
502 |
|
public function execute($columns = null) |
|
|
|
|
236
|
|
|
{ |
237
|
502 |
|
if (!empty($columns)) { |
238
|
52 |
|
$this->select($columns); |
239
|
|
|
} |
240
|
|
|
|
241
|
502 |
|
$this->setType(self::TYPE_SELECT); |
242
|
|
|
|
243
|
502 |
|
return $this->executeCached(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
10 |
|
protected function cacheKey() |
250
|
|
|
{ |
251
|
10 |
|
return sha1($this->toSql().'-'.serialize($this->getBindings())); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
|
|
*/ |
257
|
|
|
#[ReadOperation] |
|
|
|
|
258
|
13 |
|
public function paginationCount($columns = null) |
|
|
|
|
259
|
|
|
{ |
260
|
13 |
|
$statements = $this->statements; |
261
|
|
|
|
262
|
13 |
|
$this->compilerState->invalidate(['columns', 'orders']); |
263
|
|
|
|
264
|
13 |
|
$this->statements['orders'] = []; |
265
|
13 |
|
$this->statements['limit'] = null; |
266
|
13 |
|
$this->statements['offset'] = null; |
267
|
13 |
|
$this->statements['aggregate'] = ['pagination', $this->getPaginationColumns($columns)]; |
268
|
|
|
|
269
|
13 |
|
$count = (int)$this->execute()[0]['aggregate']; |
|
|
|
|
270
|
|
|
|
271
|
13 |
|
$this->compilerState->invalidate(['columns', 'orders']); |
272
|
13 |
|
$this->statements = $statements; |
273
|
|
|
|
274
|
13 |
|
return $count; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the column to count for pagination |
279
|
|
|
* @todo Voir pour count sur PK quand une entité est liée ? |
|
|
|
|
280
|
|
|
* |
281
|
|
|
* @param array|string|null $column |
|
|
|
|
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
13 |
|
protected function getPaginationColumns($column) |
286
|
|
|
{ |
287
|
13 |
|
if (!empty($column)) { |
288
|
|
|
return $column; |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// If distinct is on and no column are given, we use the current column |
292
|
13 |
|
if ($this->statements['distinct'] && !empty($this->statements['columns'])) { |
293
|
3 |
|
return $this->statements['columns'][0]['column']; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// If group by we use the columns of the group by |
297
|
11 |
|
if ($this->statements['groups']) { |
298
|
2 |
|
$this->statements['distinct'] = true; |
299
|
2 |
|
$column = $this->statements['groups'][0]; |
300
|
2 |
|
$this->statements['groups'] = []; |
301
|
|
|
|
302
|
2 |
|
return $column; |
303
|
|
|
} |
304
|
|
|
|
305
|
10 |
|
return '*'; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritdoc} |
310
|
|
|
*/ |
311
|
|
|
#[ReadOperation] |
|
|
|
|
312
|
346 |
|
public function count($column = null) |
|
|
|
|
313
|
|
|
{ |
314
|
346 |
|
return (int)$this->aggregate(__FUNCTION__, $column); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* {@inheritdoc} |
319
|
|
|
*/ |
320
|
|
|
#[ReadOperation] |
|
|
|
|
321
|
2 |
|
public function avg($column = null) |
|
|
|
|
322
|
|
|
{ |
323
|
2 |
|
return (float)$this->aggregate(__FUNCTION__, $column); |
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* {@inheritdoc} |
328
|
|
|
*/ |
329
|
|
|
#[ReadOperation] |
|
|
|
|
330
|
2 |
|
public function min($column = null) |
|
|
|
|
331
|
|
|
{ |
332
|
2 |
|
return (float)$this->aggregate(__FUNCTION__, $column); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* {@inheritdoc} |
337
|
|
|
*/ |
338
|
|
|
#[ReadOperation] |
|
|
|
|
339
|
3 |
|
public function max($column = null) |
|
|
|
|
340
|
|
|
{ |
341
|
3 |
|
return (float)$this->aggregate(__FUNCTION__, $column); |
|
|
|
|
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* {@inheritdoc} |
346
|
|
|
*/ |
347
|
|
|
#[ReadOperation] |
|
|
|
|
348
|
2 |
|
public function sum($column = null) |
|
|
|
|
349
|
|
|
{ |
350
|
2 |
|
return (float)$this->aggregate(__FUNCTION__, $column); |
|
|
|
|
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* {@inheritdoc} |
355
|
|
|
*/ |
356
|
|
|
#[ReadOperation] |
|
|
|
|
357
|
355 |
|
public function aggregate($function, $column = null) |
|
|
|
|
358
|
|
|
{ |
359
|
355 |
|
$statements = $this->statements; |
360
|
|
|
|
361
|
355 |
|
$this->compilerState->invalidate('columns'); |
362
|
|
|
|
363
|
355 |
|
$this->statements['aggregate'] = [$function, $column ?: '*']; |
|
|
|
|
364
|
|
|
|
365
|
355 |
|
$aggregate = $this->execute()[0]['aggregate']; |
366
|
|
|
|
367
|
355 |
|
$this->compilerState->invalidate('columns'); |
368
|
355 |
|
$this->statements = $statements; |
369
|
|
|
|
370
|
355 |
|
return $aggregate; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
|
|
|
|
374
|
|
|
* {@inheritdoc} |
375
|
|
|
*/ |
376
|
18 |
|
public function distinct($flag = true) |
377
|
|
|
{ |
378
|
18 |
|
$this->compilerState->invalidate('columns'); |
379
|
|
|
|
380
|
18 |
|
$this->statements['distinct'] = (bool)$flag; |
|
|
|
|
381
|
|
|
|
382
|
18 |
|
return $this; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
|
|
|
|
386
|
|
|
* {@inheritdoc} |
387
|
|
|
*/ |
388
|
699 |
|
public function from($from, $alias = null) |
389
|
|
|
{ |
390
|
699 |
|
$this->compilerState->invalidate('from'); |
391
|
|
|
|
392
|
|
|
$table = [ |
393
|
699 |
|
'table' => $from, |
394
|
699 |
|
'alias' => $alias, |
395
|
|
|
]; |
396
|
699 |
|
$key = $alias ?: $from; |
|
|
|
|
397
|
|
|
|
398
|
699 |
|
if (is_string($key)) { |
399
|
699 |
|
$this->statements['tables'][$key] = $table; |
400
|
|
|
} else { |
401
|
|
|
$this->statements['tables'][] = $table; |
402
|
|
|
} |
403
|
|
|
|
404
|
699 |
|
return $this; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Change a FROM alias for a table (or previously defined alias) |
409
|
|
|
* |
410
|
|
|
* Usage: |
411
|
|
|
* <code> |
412
|
|
|
* // Change alias of the current table : FROM my_table as my_alias |
413
|
|
|
* $query->from('my_table')->fromAlias('my_alias'); |
414
|
|
|
* |
415
|
|
|
* // Change alias of the foo table : FROM foo as my_alias, bar |
416
|
|
|
* $query->from('foo')->from('bar')->fromAlias('my_alias', 'foo'); |
417
|
|
|
* |
418
|
|
|
* // Redefine alias of foo : FROM foo as my_alias, bar |
419
|
|
|
* $query->from('foo', 'f')->from('bar')->fromAlias('my_alias', 'f'); |
420
|
|
|
* </code> |
421
|
|
|
* |
422
|
|
|
* @param string $alias The new alias name |
423
|
|
|
* @param string|null $table The last alias / table name. If null, will define the last table |
424
|
|
|
* |
425
|
|
|
* @return $this |
426
|
|
|
*/ |
427
|
3 |
|
public function fromAlias(string $alias, ?string $table = null) |
428
|
|
|
{ |
429
|
3 |
|
$this->compilerState->invalidate('from'); |
430
|
|
|
|
431
|
3 |
|
$table = $table ?? key($this->statements['tables']); |
|
|
|
|
432
|
|
|
|
433
|
3 |
|
$this->statements['tables'][$alias] = $this->statements['tables'][$table]; |
434
|
3 |
|
$this->statements['tables'][$alias]['alias'] = $alias; |
435
|
|
|
|
436
|
3 |
|
unset($this->statements['tables'][$table]); |
437
|
|
|
|
438
|
3 |
|
return $this; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
|
|
|
|
442
|
|
|
* {@inheritdoc} |
443
|
|
|
*/ |
444
|
5 |
|
public function group($column) |
445
|
|
|
{ |
446
|
5 |
|
$this->compilerState->invalidate('groups'); |
447
|
|
|
|
448
|
5 |
|
$this->statements['groups'] = is_array($column) ? $column : func_get_args(); |
449
|
|
|
|
450
|
5 |
|
return $this; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
|
|
|
|
454
|
|
|
* {@inheritdoc} |
455
|
|
|
*/ |
456
|
|
|
public function addGroup($column) |
457
|
|
|
{ |
458
|
|
|
$this->compilerState->invalidate('groups'); |
459
|
|
|
|
460
|
|
|
$this->statements['groups'] = array_merge($this->statements['groups'], is_array($column) ? $column : func_get_args()); |
461
|
|
|
|
462
|
|
|
return $this; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
|
|
|
|
466
|
|
|
* {@inheritdoc} |
467
|
|
|
*/ |
468
|
2 |
|
public function having($column, $operator = null, $value = null) |
469
|
|
|
{ |
470
|
2 |
|
$this->compilerState->invalidate('having'); |
471
|
|
|
|
472
|
2 |
|
return $this->buildClause('having', $column, $operator, $value); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
|
|
|
|
476
|
|
|
* {@inheritdoc} |
477
|
|
|
*/ |
478
|
3 |
|
public function orHaving($column, $operator = null, $value = null) |
479
|
|
|
{ |
480
|
3 |
|
$this->compilerState->invalidate('having'); |
481
|
|
|
|
482
|
3 |
|
return $this->buildClause('having', $column, $operator, $value, CompositeExpression::TYPE_OR); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
|
|
|
|
486
|
|
|
* {@inheritdoc} |
487
|
|
|
*/ |
488
|
2 |
|
public function havingNull($column, $type = CompositeExpression::TYPE_AND) |
489
|
|
|
{ |
490
|
2 |
|
$this->compilerState->invalidate('having'); |
491
|
|
|
|
492
|
2 |
|
return $this->buildClause('having', $column, '=', null, $type); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
|
|
|
|
496
|
|
|
* {@inheritdoc} |
497
|
|
|
*/ |
498
|
2 |
|
public function havingNotNull($column, $type = CompositeExpression::TYPE_AND) |
499
|
|
|
{ |
500
|
2 |
|
$this->compilerState->invalidate('having'); |
501
|
|
|
|
502
|
2 |
|
return $this->buildClause('having', $column, '!=', null, $type); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
|
|
|
|
506
|
|
|
* {@inheritdoc} |
507
|
|
|
*/ |
508
|
1 |
|
public function orHavingNull($column) |
509
|
|
|
{ |
510
|
1 |
|
return $this->havingNull($column, CompositeExpression::TYPE_OR); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
|
|
|
|
514
|
|
|
* {@inheritdoc} |
515
|
|
|
*/ |
516
|
1 |
|
public function orHavingNotNull($column) |
517
|
|
|
{ |
518
|
1 |
|
return $this->havingNotNull($column, CompositeExpression::TYPE_OR); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
|
|
|
|
522
|
|
|
* {@inheritdoc} |
523
|
|
|
*/ |
524
|
2 |
|
public function havingRaw($raw, $type = CompositeExpression::TYPE_AND) |
525
|
|
|
{ |
526
|
2 |
|
$this->compilerState->invalidate('having'); |
527
|
|
|
|
528
|
2 |
|
return $this->buildRaw('having', $raw, $type); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
|
|
|
|
532
|
|
|
* {@inheritdoc} |
533
|
|
|
*/ |
534
|
1 |
|
public function orHavingRaw($raw) |
535
|
|
|
{ |
536
|
1 |
|
return $this->havingRaw($raw, CompositeExpression::TYPE_OR); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
|
|
|
|
540
|
|
|
* {@inheritdoc} |
541
|
|
|
*/ |
542
|
6 |
|
public function addCommand($command, $value) |
543
|
|
|
{ |
544
|
|
|
switch ($command) { |
545
|
6 |
|
case ':limit': |
546
|
4 |
|
if (is_array($value)) { |
547
|
|
|
$this->limit($value[0], $value[1]); |
548
|
|
|
} else { |
549
|
4 |
|
$this->limit($value); |
550
|
|
|
} |
551
|
4 |
|
break; |
|
|
|
|
552
|
|
|
|
553
|
4 |
|
case ':limitPage': |
554
|
1 |
|
if (is_array($value)) { |
555
|
1 |
|
$this->limitPage($value[0], $value[1]); |
556
|
|
|
} else { |
557
|
|
|
$this->limitPage($value); |
558
|
|
|
} |
559
|
1 |
|
break; |
|
|
|
|
560
|
|
|
|
561
|
3 |
|
case ':offset': |
562
|
|
|
$this->offset($value); |
563
|
|
|
break; |
|
|
|
|
564
|
|
|
|
565
|
3 |
|
case ':order': |
566
|
2 |
|
$this->order($value); |
567
|
2 |
|
break; |
|
|
|
|
568
|
|
|
|
569
|
1 |
|
case ':distinct': |
570
|
1 |
|
$this->distinct($value); |
571
|
1 |
|
break; |
|
|
|
|
572
|
|
|
|
573
|
|
|
case ':group': |
574
|
|
|
$this->group($value); |
575
|
|
|
break; |
|
|
|
|
576
|
|
|
|
577
|
|
|
case ':having': |
578
|
|
|
$this->having($value); |
579
|
|
|
break; |
|
|
|
|
580
|
|
|
} |
|
|
|
|
581
|
|
|
|
582
|
6 |
|
return $this; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* {@inheritdoc} |
587
|
|
|
*/ |
588
|
133 |
|
public function toSql() |
589
|
|
|
{ |
590
|
133 |
|
return $this->compile(); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* {@inheritdoc} |
595
|
|
|
* @todo A reprendre: utiliser les types des bindings |
|
|
|
|
596
|
|
|
*/ |
597
|
15 |
|
public function toRawSql() |
598
|
|
|
{ |
599
|
15 |
|
$keys = []; |
600
|
15 |
|
$sql = $this->toSql(); |
601
|
15 |
|
$values = $this->compiler->getBindings($this); |
602
|
|
|
|
603
|
|
|
# build a regular expression for each parameter |
|
|
|
|
604
|
15 |
|
foreach ($values as $key => $value) { |
605
|
15 |
|
if (is_string($key)) { |
606
|
|
|
$keys[] = '/:' . $key . '/'; |
607
|
|
|
} else { |
608
|
15 |
|
$keys[] = '/[?]/'; |
609
|
|
|
} |
610
|
|
|
|
611
|
15 |
|
if (is_array($value)) { |
612
|
|
|
$values[$key] = implode(',', $this->connection->quote($value)); |
|
|
|
|
613
|
15 |
|
} elseif (is_null($value)) { |
|
|
|
|
614
|
|
|
$values[$key] = 'NULL'; |
615
|
15 |
|
} elseif ($value instanceof \DateTimeInterface) { |
|
|
|
|
616
|
|
|
$values[$key] = $value->format($this->connection->platform()->grammar()->getDateTimeFormatString()); |
617
|
15 |
|
} elseif (is_string($value)) { |
|
|
|
|
618
|
8 |
|
$values[$key] = $this->connection->quote($value); |
619
|
|
|
} else { |
620
|
15 |
|
$values[$key] = $value; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
15 |
|
return preg_replace($keys, $values, $sql, 1); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Gets a string representation of this Query which corresponds to |
629
|
|
|
* the final SQL query being constructed. |
|
|
|
|
630
|
|
|
* |
631
|
|
|
* @return string The string representation of this Query. |
632
|
|
|
*/ |
633
|
|
|
public function __toString() |
634
|
|
|
{ |
635
|
|
|
return $this->toSql(); |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
|