1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
6
|
|
|
use Rougin\Windstorm\QueryInterface; |
7
|
|
|
use Rougin\Windstorm\ResultInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Query |
11
|
|
|
* |
12
|
|
|
* @package Windstorm |
13
|
|
|
* @author Rougin Gutib <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Query implements QueryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Doctrine\DBAL\Query\QueryBuilder |
19
|
|
|
*/ |
20
|
|
|
protected $builder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $initial = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $table = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
protected $type = self::TYPE_SELECT; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Clones the builder instance. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
12 |
|
public function __clone() |
43
|
|
|
{ |
44
|
12 |
|
$this->builder = clone $this->builder; |
45
|
12 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the query instance. |
49
|
|
|
* |
50
|
|
|
* @param \Doctrine\DBAL\Query\QueryBuilder $builder |
51
|
|
|
*/ |
52
|
228 |
|
public function __construct(QueryBuilder $builder) |
53
|
|
|
{ |
54
|
228 |
|
$this->builder = $builder; |
55
|
228 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the safe and compiled SQL. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
5 |
|
public function __toString() |
63
|
|
|
{ |
64
|
5 |
|
return $this->sql(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Generates an AND HAVING query. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
72
|
|
|
*/ |
73
|
6 |
|
public function andHaving($key) |
74
|
|
|
{ |
75
|
6 |
|
return new Having($this, $this->builder, $key, $this->initial, 'AND'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Generates a multiple ORDER BY query. |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @return \Rougin\Windstorm\OrderInterface |
83
|
|
|
*/ |
84
|
6 |
|
public function andOrderBy($key) |
85
|
|
|
{ |
86
|
6 |
|
return new Order($this, $this->builder, $key, $this->initial, 'ADD'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generates an AND WHERE query. |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
94
|
|
|
*/ |
95
|
6 |
|
public function andWhere($key) |
96
|
|
|
{ |
97
|
6 |
|
return new Where($this, $this->builder, $key, $this->initial, 'AND'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the SQL bindings specified. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
42 |
|
public function bindings() |
106
|
|
|
{ |
107
|
42 |
|
return $this->builder->getParameters(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the Builder instance. |
112
|
|
|
* |
113
|
|
|
* @param \Doctrine\DBAL\Query\QueryBuilder $builder |
114
|
|
|
* @return self |
115
|
|
|
*/ |
116
|
156 |
|
public function builder(QueryBuilder $builder) |
117
|
|
|
{ |
118
|
156 |
|
$this->builder = $builder; |
119
|
|
|
|
120
|
156 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Generates a DELETE FROM query. |
125
|
|
|
* |
126
|
|
|
* @param string $table |
127
|
|
|
* @param string|null $alias |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
30 |
|
public function deleteFrom($table, $alias = null) |
131
|
|
|
{ |
132
|
30 |
|
$this->reset(); |
133
|
|
|
|
134
|
30 |
|
$this->initial = $alias; |
135
|
|
|
|
136
|
30 |
|
$this->table = $table; |
137
|
|
|
|
138
|
30 |
|
$this->type = self::TYPE_DELETE; |
139
|
|
|
|
140
|
30 |
|
$this->builder->delete($table, $this->initial); |
141
|
|
|
|
142
|
30 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generates a FROM query. |
147
|
|
|
* |
148
|
|
|
* @param string $table |
149
|
|
|
* @param string|null $alias |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
162 |
|
public function from($table, $alias = null) |
153
|
|
|
{ |
154
|
162 |
|
$this->initial = $alias; |
155
|
|
|
|
156
|
162 |
|
$this->table = $table; |
157
|
|
|
|
158
|
162 |
|
$this->type = self::TYPE_SELECT; |
159
|
|
|
|
160
|
162 |
|
$this->builder->from($table, $alias); |
161
|
|
|
|
162
|
162 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generates a GROUP BY query. |
167
|
|
|
* |
168
|
|
|
* @param array|string $fields |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
6 |
|
public function groupBy($fields) |
172
|
|
|
{ |
173
|
6 |
|
$this->builder->groupBy($fields); |
174
|
|
|
|
175
|
6 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Generates a HAVING query. |
180
|
|
|
* |
181
|
|
|
* @param string $key |
182
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
183
|
|
|
*/ |
184
|
18 |
|
public function having($key) |
185
|
|
|
{ |
186
|
18 |
|
return new Having($this, $this->builder, $key, $this->initial); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Generates an INNER JOIN query. |
191
|
|
|
* |
192
|
|
|
* @param string $table |
193
|
|
|
* @param string $local |
194
|
|
|
* @param string $foreign |
195
|
|
|
* @return self |
196
|
|
|
*/ |
197
|
9 |
|
public function innerJoin($table, $local, $foreign) |
198
|
|
|
{ |
199
|
9 |
|
list($alias, $where) = $this->condition($table, $local, $foreign); |
200
|
|
|
|
201
|
9 |
|
$this->builder->innerJoin($this->initial, $table, $alias, $where); |
202
|
|
|
|
203
|
9 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Generates an INSERT INTO query. |
208
|
|
|
* |
209
|
|
|
* @param string $table |
210
|
|
|
* @return \Rougin\Windstorm\InsertInterface |
211
|
|
|
*/ |
212
|
9 |
|
public function insertInto($table) |
213
|
|
|
{ |
214
|
9 |
|
$this->reset(); |
215
|
|
|
|
216
|
9 |
|
$this->type = self::TYPE_INSERT; |
217
|
|
|
|
218
|
9 |
|
return new Insert($this, $this->builder, $table); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the instance of the query builder, if available. |
223
|
|
|
* |
224
|
|
|
* @return mixed |
225
|
|
|
*/ |
226
|
9 |
|
public function instance() |
227
|
|
|
{ |
228
|
9 |
|
return $this->builder; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Generates a LEFT JOIN query. |
233
|
|
|
* |
234
|
|
|
* @param string $table |
235
|
|
|
* @param string $local |
236
|
|
|
* @param string $foreign |
237
|
|
|
* @return self |
238
|
|
|
*/ |
239
|
6 |
|
public function leftJoin($table, $local, $foreign) |
240
|
|
|
{ |
241
|
6 |
|
list($alias, $where) = $this->condition($table, $local, $foreign); |
242
|
|
|
|
243
|
6 |
|
$this->builder->leftJoin($this->initial, $table, $alias, $where); |
244
|
|
|
|
245
|
6 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Performs a LIMIT query. |
250
|
|
|
* |
251
|
|
|
* @param integer $limit |
252
|
|
|
* @param integer|null $offset |
253
|
|
|
* @return self |
254
|
|
|
*/ |
255
|
18 |
|
public function limit($limit, $offset = null) |
256
|
|
|
{ |
257
|
18 |
|
$this->builder->setMaxResults($limit); |
258
|
|
|
|
259
|
18 |
|
if ($offset !== null) |
260
|
12 |
|
{ |
261
|
18 |
|
$this->builder->setFirstResult($offset); |
262
|
12 |
|
} |
263
|
|
|
|
264
|
18 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Generates an OR HAVING query. |
269
|
|
|
* |
270
|
|
|
* @param string $key |
271
|
|
|
* @return \Rougin\Windstorm\HavingInterface |
272
|
|
|
*/ |
273
|
6 |
|
public function orHaving($key) |
274
|
|
|
{ |
275
|
6 |
|
return new Having($this, $this->builder, $key, $this->initial, 'OR'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Generates an OR WHERE query. |
280
|
|
|
* |
281
|
|
|
* @param string $key |
282
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
283
|
|
|
*/ |
284
|
6 |
|
public function orWhere($key) |
285
|
|
|
{ |
286
|
6 |
|
return new Where($this, $this->builder, $key, $this->initial, 'OR'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Generates an ORDER BY query. |
291
|
|
|
* |
292
|
|
|
* @param string $key |
293
|
|
|
* @return \Rougin\Windstorm\OrderInterface |
294
|
|
|
*/ |
295
|
24 |
|
public function orderBy($key) |
296
|
|
|
{ |
297
|
24 |
|
return new Order($this, $this->builder, $key, $this->initial); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Generates a RIGHT JOIN query. |
302
|
|
|
* |
303
|
|
|
* @param string $table |
304
|
|
|
* @param string $local |
305
|
|
|
* @param string $foreign |
306
|
|
|
* @return self |
307
|
|
|
*/ |
308
|
6 |
|
public function rightJoin($table, $local, $foreign) |
309
|
|
|
{ |
310
|
6 |
|
list($alias, $where) = $this->condition($table, $local, $foreign); |
311
|
|
|
|
312
|
6 |
|
$this->builder->rightJoin($this->initial, $table, $alias, $where); |
313
|
|
|
|
314
|
6 |
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Generates a SELECT query. |
319
|
|
|
* |
320
|
|
|
* @param array|string $fields |
321
|
|
|
* @return self |
322
|
|
|
*/ |
323
|
168 |
|
public function select($fields) |
324
|
|
|
{ |
325
|
168 |
|
$this->reset(); |
326
|
|
|
|
327
|
168 |
|
$this->type = self::TYPE_SELECT; |
328
|
|
|
|
329
|
168 |
|
$this->builder->select($fields); |
330
|
|
|
|
331
|
168 |
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Returns the safe and compiled SQL. |
336
|
|
|
* |
337
|
|
|
* @return string |
338
|
|
|
*/ |
339
|
204 |
|
public function sql() |
340
|
|
|
{ |
341
|
204 |
|
return $this->builder->getSql(); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Returns the table name from the query. |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
6 |
|
public function table() |
350
|
|
|
{ |
351
|
6 |
|
return $this->table; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Returns the type of the query. |
356
|
|
|
* |
357
|
|
|
* @return integer |
358
|
|
|
*/ |
359
|
36 |
|
public function type() |
360
|
|
|
{ |
361
|
36 |
|
return $this->type; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Generates an UPDATE query. |
366
|
|
|
* |
367
|
|
|
* @param string $table |
368
|
|
|
* @param string|null $alias |
369
|
|
|
* @return \Rougin\Windstorm\UpdateInterface |
370
|
|
|
*/ |
371
|
15 |
|
public function update($table, $alias = null) |
372
|
|
|
{ |
373
|
15 |
|
$this->reset(); |
374
|
|
|
|
375
|
15 |
|
$this->type = self::TYPE_UPDATE; |
376
|
|
|
|
377
|
15 |
|
list($this->initial, $this->table) = array($alias, $table); |
378
|
|
|
|
379
|
15 |
|
return new Update($this, $this->builder, $table, $alias); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Generates a WHERE query. |
384
|
|
|
* |
385
|
|
|
* @param string $key |
386
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
387
|
|
|
*/ |
388
|
105 |
|
public function where($key) |
389
|
|
|
{ |
390
|
105 |
|
return new Where($this, $this->builder, $key, $this->initial); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Returns an available table alias. |
395
|
|
|
* |
396
|
|
|
* @param string $table |
397
|
|
|
* @return string |
398
|
|
|
*/ |
399
|
21 |
|
protected function alias($table) |
400
|
|
|
{ |
401
|
21 |
|
$characters = str_split($table); |
402
|
|
|
|
403
|
21 |
|
$result = $characters[0]; |
404
|
|
|
|
405
|
21 |
|
foreach ($characters as $character) |
406
|
|
|
{ |
407
|
21 |
|
$character = strtolower($character); |
408
|
|
|
|
409
|
21 |
|
if ($this->initial !== $character) |
410
|
14 |
|
{ |
411
|
21 |
|
$result = $character; break; |
412
|
|
|
} |
413
|
14 |
|
} |
414
|
|
|
|
415
|
21 |
|
return (string) $result; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Returns a JOIN condition. |
420
|
|
|
* |
421
|
|
|
* @param string $table |
422
|
|
|
* @param string $local |
423
|
|
|
* @param string $foreign |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
21 |
|
protected function condition($table, $local, $foreign) |
427
|
|
|
{ |
428
|
21 |
|
$condition = $this->initial . '.' . $local; |
429
|
|
|
|
430
|
21 |
|
$alias = $this->alias((string) $table); |
431
|
|
|
|
432
|
21 |
|
$condition .= ' = ' . $alias . '.' . $foreign; |
433
|
|
|
|
434
|
21 |
|
return array($alias, (string) $condition); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Resets the whole query builder. |
439
|
|
|
* |
440
|
|
|
* @return self |
441
|
|
|
*/ |
442
|
222 |
|
protected function reset() |
443
|
|
|
{ |
444
|
222 |
|
$this->builder->setMaxResults(null); |
445
|
|
|
|
446
|
222 |
|
$this->builder->setFirstResult(null); |
447
|
|
|
|
448
|
222 |
|
$this->initial = (string) ''; |
449
|
|
|
|
450
|
222 |
|
$this->builder->resetQueryParts(); |
451
|
|
|
|
452
|
222 |
|
$this->table = (string) ''; |
453
|
|
|
|
454
|
222 |
|
$this->builder->setParameters(array()); |
455
|
|
|
|
456
|
222 |
|
$this->type = self::TYPE_SELECT; |
457
|
|
|
|
458
|
222 |
|
return $this; |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|