1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Kdyby (http://www.kdyby.org) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2008 Filip Procházka ([email protected]) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the file license.txt that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kdyby\Doctrine; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
14
|
|
|
use Doctrine\DBAL\SQLParserUtils; |
15
|
|
|
use Doctrine\DBAL\SQLParserUtilsException; |
16
|
|
|
use Doctrine\ORM\AbstractQuery; |
17
|
|
|
use Doctrine\ORM\NativeQuery; |
18
|
|
|
use Doctrine\ORM\Query; |
19
|
|
|
use Kdyby; |
20
|
|
|
use Nette; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Filip Procházka <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class NativeQueryWrapper extends AbstractQuery |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Doctrine\ORM\NativeQuery |
32
|
|
|
*/ |
33
|
|
|
private $nativeQuery; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int|NULL |
37
|
|
|
*/ |
38
|
|
|
private $firstResult; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int|NULL |
42
|
|
|
*/ |
43
|
|
|
private $maxResults; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function __construct(NativeQuery $nativeQuery) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($nativeQuery->getEntityManager()); |
50
|
|
|
$this->nativeQuery = $nativeQuery; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return int|NULL |
57
|
|
|
*/ |
58
|
|
|
public function getFirstResult() |
59
|
|
|
{ |
60
|
|
|
return $this->firstResult; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int|NULL $firstResult |
67
|
|
|
* @return NativeQueryWrapper |
68
|
|
|
*/ |
69
|
|
|
public function setFirstResult($firstResult) |
70
|
|
|
{ |
71
|
|
|
$this->firstResult = $firstResult; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int|NULL |
79
|
|
|
*/ |
80
|
|
|
public function getMaxResults() |
81
|
|
|
{ |
82
|
|
|
return $this->maxResults; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int|NULL $maxResults |
89
|
|
|
* @return NativeQueryWrapper |
90
|
|
|
*/ |
91
|
|
|
public function setMaxResults($maxResults) |
92
|
|
|
{ |
93
|
|
|
$this->maxResults = $maxResults; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return NativeQuery |
101
|
|
|
*/ |
102
|
|
|
protected function getLimitedQuery() |
103
|
|
|
{ |
104
|
|
|
$copy = clone $this->nativeQuery; |
105
|
|
|
$copy->setParameters([]); |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
$params = $types = []; |
109
|
|
|
/** @var Query\Parameter $param */ |
110
|
|
|
foreach ($this->nativeQuery->getParameters() as $param) { |
111
|
|
|
$params[$param->getName()] = $param->getValue(); |
112
|
|
|
$types[$param->getName()] = $param->getType(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
list($query, $params, $types) = SQLParserUtils::expandListParameters($copy->getSQL(), $params, $types); |
116
|
|
|
|
117
|
|
|
$copy->setSQL($query); |
118
|
|
|
foreach ($params as $i => $value) { |
119
|
|
|
$copy->setParameter($i, $value, isset($types[$i]) ? $types[$i] : NULL); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} catch (SQLParserUtilsException $e) { |
123
|
|
|
$copy->setParameters(clone $this->nativeQuery->getParameters()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->maxResults !== NULL || $this->firstResult !== NULL) { |
127
|
|
|
$em = $this->nativeQuery->getEntityManager(); |
128
|
|
|
$platform = $em->getConnection()->getDatabasePlatform(); |
129
|
|
|
|
130
|
|
|
$copy->setSQL($platform->modifyLimitQuery($copy->getSQL(), $this->maxResults, $this->firstResult)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $copy; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function iterate($parameters = null, $hydrationMode = null) |
139
|
|
|
{ |
140
|
|
|
return $this->getLimitedQuery()->iterate($parameters, $hydrationMode); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
public function execute($parameters = null, $hydrationMode = null) |
146
|
|
|
{ |
147
|
|
|
return $this->getLimitedQuery()->execute($parameters, $hydrationMode); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
|
152
|
|
|
public function getResult($hydrationMode = self::HYDRATE_OBJECT) |
153
|
|
|
{ |
154
|
|
|
return $this->getLimitedQuery()->getResult($hydrationMode); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
public function getArrayResult() |
160
|
|
|
{ |
161
|
|
|
return $this->getLimitedQuery()->getArrayResult(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
public function getScalarResult() |
167
|
|
|
{ |
168
|
|
|
return $this->getLimitedQuery()->getScalarResult(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
public function getOneOrNullResult($hydrationMode = null) |
174
|
|
|
{ |
175
|
|
|
return $this->getLimitedQuery()->getOneOrNullResult($hydrationMode); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
public function getSingleResult($hydrationMode = null) |
181
|
|
|
{ |
182
|
|
|
return $this->getLimitedQuery()->getSingleResult($hydrationMode); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
public function getSingleScalarResult() |
188
|
|
|
{ |
189
|
|
|
return $this->getLimitedQuery()->getSingleScalarResult(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
|
194
|
|
|
public function setSQL($sql) |
195
|
|
|
{ |
196
|
|
|
$this->nativeQuery->setSQL($sql); |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
public function getSQL() |
203
|
|
|
{ |
204
|
|
|
return $this->nativeQuery->getSQL(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
public function setCacheable($cacheable) |
210
|
|
|
{ |
211
|
|
|
$this->nativeQuery->setCacheable($cacheable); |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
public function isCacheable() |
218
|
|
|
{ |
219
|
|
|
return $this->nativeQuery->isCacheable(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
|
224
|
|
|
public function setCacheRegion($cacheRegion) |
225
|
|
|
{ |
226
|
|
|
$this->nativeQuery->setCacheRegion($cacheRegion); |
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
|
232
|
|
|
public function getCacheRegion() |
233
|
|
|
{ |
234
|
|
|
return $this->nativeQuery->getCacheRegion(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
|
239
|
|
|
protected function isCacheEnabled() |
240
|
|
|
{ |
241
|
|
|
return $this->nativeQuery->isCacheEnabled(); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
|
246
|
|
|
public function getLifetime() |
247
|
|
|
{ |
248
|
|
|
return $this->nativeQuery->getLifetime(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
|
253
|
|
|
public function setLifetime($lifetime) |
254
|
|
|
{ |
255
|
|
|
$this->nativeQuery->setLifetime($lifetime); |
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
|
261
|
|
|
public function getCacheMode() |
262
|
|
|
{ |
263
|
|
|
return $this->nativeQuery->getCacheMode(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
|
268
|
|
|
public function setCacheMode($cacheMode) |
269
|
|
|
{ |
270
|
|
|
$this->nativeQuery->setCacheMode($cacheMode); |
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
|
276
|
|
|
public function getEntityManager() |
277
|
|
|
{ |
278
|
|
|
return $this->nativeQuery->getEntityManager(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
|
283
|
|
|
public function free() |
284
|
|
|
{ |
285
|
|
|
$this->nativeQuery->free(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
|
290
|
|
|
public function getParameters() |
291
|
|
|
{ |
292
|
|
|
return $this->nativeQuery->getParameters(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
|
297
|
|
|
public function getParameter($key) |
298
|
|
|
{ |
299
|
|
|
return $this->nativeQuery->getParameter($key); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
|
304
|
|
|
public function setParameters($parameters) |
305
|
|
|
{ |
306
|
|
|
$this->nativeQuery->setParameters($parameters); |
307
|
|
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
|
312
|
|
|
public function setParameter($key, $value, $type = null) |
313
|
|
|
{ |
314
|
|
|
$this->nativeQuery->setParameter($key, $value, $type); |
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
|
320
|
|
|
public function processParameterValue($value) |
321
|
|
|
{ |
322
|
|
|
return $this->nativeQuery->processParameterValue($value); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
|
327
|
|
|
public function setResultSetMapping(Query\ResultSetMapping $rsm) |
328
|
|
|
{ |
329
|
|
|
$this->nativeQuery->setResultSetMapping($rsm); |
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
|
334
|
|
|
|
335
|
|
|
protected function getResultSetMapping() |
336
|
|
|
{ |
337
|
|
|
return $this->nativeQuery->getResultSetMapping(); |
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
|
342
|
|
|
public function setHydrationCacheProfile(QueryCacheProfile $profile = null) |
343
|
|
|
{ |
344
|
|
|
$this->nativeQuery->setHydrationCacheProfile($profile); |
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
|
350
|
|
|
public function getHydrationCacheProfile() |
351
|
|
|
{ |
352
|
|
|
return $this->nativeQuery->getHydrationCacheProfile(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
|
357
|
|
|
public function setResultCacheProfile(QueryCacheProfile $profile = null) |
358
|
|
|
{ |
359
|
|
|
$this->nativeQuery->setResultCacheProfile($profile); |
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
|
365
|
|
|
public function setResultCacheDriver($resultCacheDriver = null) |
366
|
|
|
{ |
367
|
|
|
$this->nativeQuery->setResultCacheDriver($resultCacheDriver); |
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
|
373
|
|
|
public function getResultCacheDriver() |
374
|
|
|
{ |
375
|
|
|
return $this->nativeQuery->getResultCacheDriver(); |
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
|
380
|
|
|
public function useResultCache($bool, $lifetime = null, $resultCacheId = null) |
381
|
|
|
{ |
382
|
|
|
$this->nativeQuery->useResultCache($bool, $lifetime, $resultCacheId); |
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
|
387
|
|
|
|
388
|
|
|
public function setResultCacheLifetime($lifetime) |
389
|
|
|
{ |
390
|
|
|
$this->nativeQuery->setResultCacheLifetime($lifetime); |
391
|
|
|
return $this; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
|
396
|
|
|
public function getResultCacheLifetime() |
397
|
|
|
{ |
398
|
|
|
return $this->nativeQuery->getResultCacheLifetime(); |
|
|
|
|
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
|
402
|
|
|
|
403
|
|
|
public function expireResultCache($expire = true) |
404
|
|
|
{ |
405
|
|
|
$this->nativeQuery->expireResultCache($expire); |
406
|
|
|
return $this; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
|
410
|
|
|
|
411
|
|
|
public function getExpireResultCache() |
412
|
|
|
{ |
413
|
|
|
return $this->nativeQuery->getExpireResultCache(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
|
418
|
|
|
public function getQueryCacheProfile() |
419
|
|
|
{ |
420
|
|
|
return $this->nativeQuery->getQueryCacheProfile(); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
|
424
|
|
|
|
425
|
|
|
public function setFetchMode($class, $assocName, $fetchMode) |
426
|
|
|
{ |
427
|
|
|
$this->nativeQuery->setFetchMode($class, $assocName, $fetchMode); |
428
|
|
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
|
432
|
|
|
|
433
|
|
|
public function setHydrationMode($hydrationMode) |
434
|
|
|
{ |
435
|
|
|
$this->nativeQuery->setHydrationMode($hydrationMode); |
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
|
440
|
|
|
|
441
|
|
|
public function getHydrationMode() |
442
|
|
|
{ |
443
|
|
|
return $this->nativeQuery->getHydrationMode(); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
|
447
|
|
|
|
448
|
|
|
public function setHint($name, $value) |
449
|
|
|
{ |
450
|
|
|
$this->nativeQuery->setHint($name, $value); |
451
|
|
|
return $this; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
|
455
|
|
|
|
456
|
|
|
public function getHint($name) |
457
|
|
|
{ |
458
|
|
|
return $this->nativeQuery->getHint($name); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
|
462
|
|
|
|
463
|
|
|
public function hasHint($name) |
464
|
|
|
{ |
465
|
|
|
return $this->nativeQuery->hasHint($name); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
|
470
|
|
|
public function getHints() |
471
|
|
|
{ |
472
|
|
|
return $this->nativeQuery->getHints(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
|
476
|
|
|
|
477
|
|
|
protected function getHydrationCacheId() |
478
|
|
|
{ |
479
|
|
|
return $this->nativeQuery->getHydrationCacheId(); |
|
|
|
|
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
|
483
|
|
|
|
484
|
|
|
public function setResultCacheId($id) |
485
|
|
|
{ |
486
|
|
|
$this->nativeQuery->setResultCacheId($id); |
487
|
|
|
return $this; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
|
491
|
|
|
|
492
|
|
|
public function getResultCacheId() |
493
|
|
|
{ |
494
|
|
|
return $this->nativeQuery->getResultCacheId(); |
|
|
|
|
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
|
499
|
|
|
protected function getHash() |
500
|
|
|
{ |
501
|
|
|
return $this->nativeQuery->getHash(); |
|
|
|
|
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
|
505
|
|
|
|
506
|
|
|
protected function _doExecute() |
507
|
|
|
{ |
508
|
|
|
throw new NotImplementedException; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
|
512
|
|
|
|
513
|
|
|
public function __clone() |
514
|
|
|
{ |
515
|
|
|
$this->nativeQuery = clone $this->nativeQuery; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
} |
519
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.