Cache
9 months ago
Decorator
9 months ago
Event
9 months ago
Exception
9 months ago
Id
9 months ago
Internal
9 months ago
Mapping
9 months ago
Persisters
8 months ago
Proxy
9 months ago
Query
9 months ago
Repository
9 months ago
Tools
9 months ago
Utility
9 months ago
AbstractQuery.php
9 months ago
Cache.php
9 months ago
Configuration.php
9 months ago
EntityManager.php
9 months ago
EntityManagerInterface.php
9 months ago
EntityNotFoundException.php
9 months ago
EntityRepository.php
9 months ago
Events.php
9 months ago
LazyCriteriaCollection.php
9 months ago
NativeQuery.php
9 months ago
NoResultException.php
9 months ago
NonUniqueResultException.php
9 months ago
ORMException.php
9 months ago
ORMInvalidArgumentException.php
9 months ago
ORMSetup.php
9 months ago
OptimisticLockException.php
9 months ago
PersistentCollection.php
9 months ago
PessimisticLockException.php
9 months ago
Query.php
9 months ago
QueryBuilder.php
9 months ago
TransactionRequiredException.php
9 months ago
UnexpectedResultException.php
9 months ago
UnitOfWork.php
9 months ago
Version.php
9 months ago
index.php
9 months ago
AbstractQuery.php
645 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use BackedEnum; |
| 6 | use Countable; |
| 7 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\CacheAdapter; |
| 8 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\DoctrineProvider; |
| 9 | use MailPoetVendor\Doctrine\Common\Collections\ArrayCollection; |
| 10 | use MailPoetVendor\Doctrine\Common\Collections\Collection; |
| 11 | use MailPoetVendor\Doctrine\DBAL\Cache\QueryCacheProfile; |
| 12 | use MailPoetVendor\Doctrine\DBAL\Result; |
| 13 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 14 | use MailPoetVendor\Doctrine\ORM\Cache\Exception\InvalidResultCacheDriver; |
| 15 | use MailPoetVendor\Doctrine\ORM\Cache\Logging\CacheLogger; |
| 16 | use MailPoetVendor\Doctrine\ORM\Cache\QueryCacheKey; |
| 17 | use MailPoetVendor\Doctrine\ORM\Cache\TimestampCacheKey; |
| 18 | use MailPoetVendor\Doctrine\ORM\Internal\Hydration\IterableResult; |
| 19 | use MailPoetVendor\Doctrine\ORM\Mapping\MappingException as ORMMappingException; |
| 20 | use MailPoetVendor\Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
| 21 | use MailPoetVendor\Doctrine\ORM\Query\Parameter; |
| 22 | use MailPoetVendor\Doctrine\ORM\Query\ResultSetMapping; |
| 23 | use MailPoetVendor\Doctrine\Persistence\Mapping\MappingException; |
| 24 | use LogicException; |
| 25 | use MailPoetVendor\Psr\Cache\CacheItemPoolInterface; |
| 26 | use Traversable; |
| 27 | use function array_map; |
| 28 | use function array_shift; |
| 29 | use function assert; |
| 30 | use function count; |
| 31 | use function func_num_args; |
| 32 | use function in_array; |
| 33 | use function is_array; |
| 34 | use function is_numeric; |
| 35 | use function is_object; |
| 36 | use function is_scalar; |
| 37 | use function is_string; |
| 38 | use function iterator_count; |
| 39 | use function iterator_to_array; |
| 40 | use function ksort; |
| 41 | use function method_exists; |
| 42 | use function reset; |
| 43 | use function serialize; |
| 44 | use function sha1; |
| 45 | abstract class AbstractQuery |
| 46 | { |
| 47 | public const HYDRATE_OBJECT = 1; |
| 48 | public const HYDRATE_ARRAY = 2; |
| 49 | public const HYDRATE_SCALAR = 3; |
| 50 | public const HYDRATE_SINGLE_SCALAR = 4; |
| 51 | public const HYDRATE_SIMPLEOBJECT = 5; |
| 52 | public const HYDRATE_SCALAR_COLUMN = 6; |
| 53 | protected $parameters; |
| 54 | protected $_resultSetMapping; |
| 55 | protected $_em; |
| 56 | protected $_hints = []; |
| 57 | protected $_hydrationMode = self::HYDRATE_OBJECT; |
| 58 | protected $_queryCacheProfile; |
| 59 | protected $_expireResultCache = \false; |
| 60 | protected $_hydrationCacheProfile; |
| 61 | protected $cacheable = \false; |
| 62 | protected $hasCache = \false; |
| 63 | protected $cacheRegion; |
| 64 | protected $cacheMode; |
| 65 | protected $cacheLogger; |
| 66 | protected $lifetime = 0; |
| 67 | public function __construct(EntityManagerInterface $em) |
| 68 | { |
| 69 | $this->_em = $em; |
| 70 | $this->parameters = new ArrayCollection(); |
| 71 | $this->_hints = $em->getConfiguration()->getDefaultQueryHints(); |
| 72 | $this->hasCache = $this->_em->getConfiguration()->isSecondLevelCacheEnabled(); |
| 73 | if ($this->hasCache) { |
| 74 | $this->cacheLogger = $em->getConfiguration()->getSecondLevelCacheConfiguration()->getCacheLogger(); |
| 75 | } |
| 76 | } |
| 77 | public function setCacheable($cacheable) |
| 78 | { |
| 79 | $this->cacheable = (bool) $cacheable; |
| 80 | return $this; |
| 81 | } |
| 82 | public function isCacheable() |
| 83 | { |
| 84 | return $this->cacheable; |
| 85 | } |
| 86 | public function setCacheRegion($cacheRegion) |
| 87 | { |
| 88 | $this->cacheRegion = (string) $cacheRegion; |
| 89 | return $this; |
| 90 | } |
| 91 | public function getCacheRegion() |
| 92 | { |
| 93 | return $this->cacheRegion; |
| 94 | } |
| 95 | protected function isCacheEnabled() |
| 96 | { |
| 97 | return $this->cacheable && $this->hasCache; |
| 98 | } |
| 99 | public function getLifetime() |
| 100 | { |
| 101 | return $this->lifetime; |
| 102 | } |
| 103 | public function setLifetime($lifetime) |
| 104 | { |
| 105 | $this->lifetime = (int) $lifetime; |
| 106 | return $this; |
| 107 | } |
| 108 | public function getCacheMode() |
| 109 | { |
| 110 | return $this->cacheMode; |
| 111 | } |
| 112 | public function setCacheMode($cacheMode) |
| 113 | { |
| 114 | $this->cacheMode = (int) $cacheMode; |
| 115 | return $this; |
| 116 | } |
| 117 | public abstract function getSQL(); |
| 118 | public function getEntityManager() |
| 119 | { |
| 120 | return $this->_em; |
| 121 | } |
| 122 | public function free() |
| 123 | { |
| 124 | $this->parameters = new ArrayCollection(); |
| 125 | $this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints(); |
| 126 | } |
| 127 | public function getParameters() |
| 128 | { |
| 129 | return $this->parameters; |
| 130 | } |
| 131 | public function getParameter($key) |
| 132 | { |
| 133 | $key = Query\Parameter::normalizeName($key); |
| 134 | $filteredParameters = $this->parameters->filter(static function (Query\Parameter $parameter) use($key) : bool { |
| 135 | $parameterName = $parameter->getName(); |
| 136 | return $key === $parameterName; |
| 137 | }); |
| 138 | return !$filteredParameters->isEmpty() ? $filteredParameters->first() : null; |
| 139 | } |
| 140 | public function setParameters($parameters) |
| 141 | { |
| 142 | if (is_array($parameters)) { |
| 143 | $parameterCollection = new ArrayCollection(); |
| 144 | foreach ($parameters as $key => $value) { |
| 145 | $parameterCollection->add(new Parameter($key, $value)); |
| 146 | } |
| 147 | $parameters = $parameterCollection; |
| 148 | } |
| 149 | $this->parameters = $parameters; |
| 150 | return $this; |
| 151 | } |
| 152 | public function setParameter($key, $value, $type = null) |
| 153 | { |
| 154 | $existingParameter = $this->getParameter($key); |
| 155 | if ($existingParameter !== null) { |
| 156 | $existingParameter->setValue($value, $type); |
| 157 | return $this; |
| 158 | } |
| 159 | $this->parameters->add(new Parameter($key, $value, $type)); |
| 160 | return $this; |
| 161 | } |
| 162 | public function processParameterValue($value) |
| 163 | { |
| 164 | if (is_scalar($value)) { |
| 165 | return $value; |
| 166 | } |
| 167 | if ($value instanceof Collection) { |
| 168 | $value = iterator_to_array($value); |
| 169 | } |
| 170 | if (is_array($value)) { |
| 171 | $value = $this->processArrayParameterValue($value); |
| 172 | return $value; |
| 173 | } |
| 174 | if ($value instanceof Mapping\ClassMetadata) { |
| 175 | return $value->name; |
| 176 | } |
| 177 | if ($value instanceof BackedEnum) { |
| 178 | return $value->value; |
| 179 | } |
| 180 | if (!is_object($value)) { |
| 181 | return $value; |
| 182 | } |
| 183 | try { |
| 184 | $class = DefaultProxyClassNameResolver::getClass($value); |
| 185 | $value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value); |
| 186 | if ($value === null) { |
| 187 | throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class); |
| 188 | } |
| 189 | } catch (MappingException|ORMMappingException $e) { |
| 190 | $value = $this->potentiallyProcessIterable($value); |
| 191 | } |
| 192 | return $value; |
| 193 | } |
| 194 | private function potentiallyProcessIterable($value) |
| 195 | { |
| 196 | if ($value instanceof Traversable) { |
| 197 | $value = iterator_to_array($value); |
| 198 | $value = $this->processArrayParameterValue($value); |
| 199 | } |
| 200 | return $value; |
| 201 | } |
| 202 | private function processArrayParameterValue(array $value) : array |
| 203 | { |
| 204 | foreach ($value as $key => $paramValue) { |
| 205 | $paramValue = $this->processParameterValue($paramValue); |
| 206 | $value[$key] = is_array($paramValue) ? reset($paramValue) : $paramValue; |
| 207 | } |
| 208 | return $value; |
| 209 | } |
| 210 | public function setResultSetMapping(Query\ResultSetMapping $rsm) |
| 211 | { |
| 212 | $this->translateNamespaces($rsm); |
| 213 | $this->_resultSetMapping = $rsm; |
| 214 | return $this; |
| 215 | } |
| 216 | protected function getResultSetMapping() |
| 217 | { |
| 218 | return $this->_resultSetMapping; |
| 219 | } |
| 220 | private function translateNamespaces(Query\ResultSetMapping $rsm) : void |
| 221 | { |
| 222 | $translate = function ($alias) : string { |
| 223 | return $this->_em->getClassMetadata($alias)->getName(); |
| 224 | }; |
| 225 | $rsm->aliasMap = array_map($translate, $rsm->aliasMap); |
| 226 | $rsm->declaringClasses = array_map($translate, $rsm->declaringClasses); |
| 227 | } |
| 228 | public function setHydrationCacheProfile(?QueryCacheProfile $profile = null) |
| 229 | { |
| 230 | if ($profile === null) { |
| 231 | if (func_num_args() < 1) { |
| 232 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__); |
| 233 | } |
| 234 | $this->_hydrationCacheProfile = null; |
| 235 | return $this; |
| 236 | } |
| 237 | // DBAL 2 |
| 238 | if (!method_exists(QueryCacheProfile::class, 'setResultCache')) { |
| 239 | // @phpstan-ignore method.deprecated |
| 240 | if (!$profile->getResultCacheDriver()) { |
| 241 | $defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache(); |
| 242 | if ($defaultHydrationCacheImpl) { |
| 243 | // @phpstan-ignore method.deprecated |
| 244 | $profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultHydrationCacheImpl)); |
| 245 | } |
| 246 | } |
| 247 | } elseif (!$profile->getResultCache()) { |
| 248 | $defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache(); |
| 249 | if ($defaultHydrationCacheImpl) { |
| 250 | $profile = $profile->setResultCache($defaultHydrationCacheImpl); |
| 251 | } |
| 252 | } |
| 253 | $this->_hydrationCacheProfile = $profile; |
| 254 | return $this; |
| 255 | } |
| 256 | public function getHydrationCacheProfile() |
| 257 | { |
| 258 | return $this->_hydrationCacheProfile; |
| 259 | } |
| 260 | public function setResultCacheProfile(?QueryCacheProfile $profile = null) |
| 261 | { |
| 262 | if ($profile === null) { |
| 263 | if (func_num_args() < 1) { |
| 264 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__); |
| 265 | } |
| 266 | $this->_queryCacheProfile = null; |
| 267 | return $this; |
| 268 | } |
| 269 | // DBAL 2 |
| 270 | if (!method_exists(QueryCacheProfile::class, 'setResultCache')) { |
| 271 | // @phpstan-ignore method.deprecated |
| 272 | if (!$profile->getResultCacheDriver()) { |
| 273 | $defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCache(); |
| 274 | if ($defaultResultCacheDriver) { |
| 275 | // @phpstan-ignore method.deprecated |
| 276 | $profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultResultCacheDriver)); |
| 277 | } |
| 278 | } |
| 279 | } elseif (!$profile->getResultCache()) { |
| 280 | $defaultResultCache = $this->_em->getConfiguration()->getResultCache(); |
| 281 | if ($defaultResultCache) { |
| 282 | $profile = $profile->setResultCache($defaultResultCache); |
| 283 | } |
| 284 | } |
| 285 | $this->_queryCacheProfile = $profile; |
| 286 | return $this; |
| 287 | } |
| 288 | public function setResultCacheDriver($resultCacheDriver = null) |
| 289 | { |
| 290 | if ($resultCacheDriver !== null && !$resultCacheDriver instanceof \MailPoetVendor\Doctrine\Common\Cache\Cache) { |
| 291 | throw InvalidResultCacheDriver::create(); |
| 292 | } |
| 293 | return $this->setResultCache($resultCacheDriver ? CacheAdapter::wrap($resultCacheDriver) : null); |
| 294 | } |
| 295 | public function setResultCache(?CacheItemPoolInterface $resultCache = null) |
| 296 | { |
| 297 | if ($resultCache === null) { |
| 298 | if (func_num_args() < 1) { |
| 299 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9791', 'Calling %s without arguments is deprecated, pass null instead.', __METHOD__); |
| 300 | } |
| 301 | if ($this->_queryCacheProfile) { |
| 302 | $this->_queryCacheProfile = new QueryCacheProfile($this->_queryCacheProfile->getLifetime(), $this->_queryCacheProfile->getCacheKey()); |
| 303 | } |
| 304 | return $this; |
| 305 | } |
| 306 | // DBAL 2 |
| 307 | if (!method_exists(QueryCacheProfile::class, 'setResultCache')) { |
| 308 | $resultCacheDriver = DoctrineProvider::wrap($resultCache); |
| 309 | $this->_queryCacheProfile = $this->_queryCacheProfile ? $this->_queryCacheProfile->setResultCacheDriver($resultCacheDriver) : new QueryCacheProfile(0, null, $resultCacheDriver); |
| 310 | return $this; |
| 311 | } |
| 312 | $this->_queryCacheProfile = $this->_queryCacheProfile ? $this->_queryCacheProfile->setResultCache($resultCache) : new QueryCacheProfile(0, null, $resultCache); |
| 313 | return $this; |
| 314 | } |
| 315 | public function getResultCacheDriver() |
| 316 | { |
| 317 | if ($this->_queryCacheProfile && $this->_queryCacheProfile->getResultCacheDriver()) { |
| 318 | return $this->_queryCacheProfile->getResultCacheDriver(); |
| 319 | } |
| 320 | return $this->_em->getConfiguration()->getResultCacheImpl(); |
| 321 | } |
| 322 | public function useResultCache($useCache, $lifetime = null, $resultCacheId = null) |
| 323 | { |
| 324 | return $useCache ? $this->enableResultCache($lifetime, $resultCacheId) : $this->disableResultCache(); |
| 325 | } |
| 326 | public function enableResultCache(?int $lifetime = null, ?string $resultCacheId = null) : self |
| 327 | { |
| 328 | $this->setResultCacheLifetime($lifetime); |
| 329 | $this->setResultCacheId($resultCacheId); |
| 330 | return $this; |
| 331 | } |
| 332 | public function disableResultCache() : self |
| 333 | { |
| 334 | $this->_queryCacheProfile = null; |
| 335 | return $this; |
| 336 | } |
| 337 | public function setResultCacheLifetime($lifetime) |
| 338 | { |
| 339 | $lifetime = (int) $lifetime; |
| 340 | if ($this->_queryCacheProfile) { |
| 341 | $this->_queryCacheProfile = $this->_queryCacheProfile->setLifetime($lifetime); |
| 342 | return $this; |
| 343 | } |
| 344 | $this->_queryCacheProfile = new QueryCacheProfile($lifetime); |
| 345 | $cache = $this->_em->getConfiguration()->getResultCache(); |
| 346 | if (!$cache) { |
| 347 | return $this; |
| 348 | } |
| 349 | // Compatibility for DBAL 2 |
| 350 | if (!method_exists($this->_queryCacheProfile, 'setResultCache')) { |
| 351 | // @phpstan-ignore method.deprecated |
| 352 | $this->_queryCacheProfile = $this->_queryCacheProfile->setResultCacheDriver(DoctrineProvider::wrap($cache)); |
| 353 | return $this; |
| 354 | } |
| 355 | $this->_queryCacheProfile = $this->_queryCacheProfile->setResultCache($cache); |
| 356 | return $this; |
| 357 | } |
| 358 | public function getResultCacheLifetime() |
| 359 | { |
| 360 | return $this->_queryCacheProfile ? $this->_queryCacheProfile->getLifetime() : 0; |
| 361 | } |
| 362 | public function expireResultCache($expire = \true) |
| 363 | { |
| 364 | $this->_expireResultCache = $expire; |
| 365 | return $this; |
| 366 | } |
| 367 | public function getExpireResultCache() |
| 368 | { |
| 369 | return $this->_expireResultCache; |
| 370 | } |
| 371 | public function getQueryCacheProfile() |
| 372 | { |
| 373 | return $this->_queryCacheProfile; |
| 374 | } |
| 375 | public function setFetchMode($class, $assocName, $fetchMode) |
| 376 | { |
| 377 | if (!in_array($fetchMode, [Mapping\ClassMetadata::FETCH_EAGER, Mapping\ClassMetadata::FETCH_LAZY], \true)) { |
| 378 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9777', 'Calling %s() with something else than ClassMetadata::FETCH_EAGER or ClassMetadata::FETCH_LAZY is deprecated.', __METHOD__); |
| 379 | $fetchMode = Mapping\ClassMetadata::FETCH_LAZY; |
| 380 | } |
| 381 | $this->_hints['fetchMode'][$class][$assocName] = $fetchMode; |
| 382 | return $this; |
| 383 | } |
| 384 | public function setHydrationMode($hydrationMode) |
| 385 | { |
| 386 | $this->_hydrationMode = $hydrationMode; |
| 387 | return $this; |
| 388 | } |
| 389 | public function getHydrationMode() |
| 390 | { |
| 391 | return $this->_hydrationMode; |
| 392 | } |
| 393 | public function getResult($hydrationMode = self::HYDRATE_OBJECT) |
| 394 | { |
| 395 | return $this->execute(null, $hydrationMode); |
| 396 | } |
| 397 | public function getArrayResult() |
| 398 | { |
| 399 | return $this->execute(null, self::HYDRATE_ARRAY); |
| 400 | } |
| 401 | public function getSingleColumnResult() |
| 402 | { |
| 403 | return $this->execute(null, self::HYDRATE_SCALAR_COLUMN); |
| 404 | } |
| 405 | public function getScalarResult() |
| 406 | { |
| 407 | return $this->execute(null, self::HYDRATE_SCALAR); |
| 408 | } |
| 409 | public function getOneOrNullResult($hydrationMode = null) |
| 410 | { |
| 411 | try { |
| 412 | $result = $this->execute(null, $hydrationMode); |
| 413 | } catch (NoResultException $e) { |
| 414 | return null; |
| 415 | } |
| 416 | if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && !$result) { |
| 417 | return null; |
| 418 | } |
| 419 | if (!is_array($result)) { |
| 420 | return $result; |
| 421 | } |
| 422 | if (count($result) > 1) { |
| 423 | throw new NonUniqueResultException(); |
| 424 | } |
| 425 | return array_shift($result); |
| 426 | } |
| 427 | public function getSingleResult($hydrationMode = null) |
| 428 | { |
| 429 | $result = $this->execute(null, $hydrationMode); |
| 430 | if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && !$result) { |
| 431 | throw new NoResultException(); |
| 432 | } |
| 433 | if (!is_array($result)) { |
| 434 | return $result; |
| 435 | } |
| 436 | if (count($result) > 1) { |
| 437 | throw new NonUniqueResultException(); |
| 438 | } |
| 439 | return array_shift($result); |
| 440 | } |
| 441 | public function getSingleScalarResult() |
| 442 | { |
| 443 | return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR); |
| 444 | } |
| 445 | public function setHint($name, $value) |
| 446 | { |
| 447 | $this->_hints[$name] = $value; |
| 448 | return $this; |
| 449 | } |
| 450 | public function getHint($name) |
| 451 | { |
| 452 | return $this->_hints[$name] ?? \false; |
| 453 | } |
| 454 | public function hasHint($name) |
| 455 | { |
| 456 | return isset($this->_hints[$name]); |
| 457 | } |
| 458 | public function getHints() |
| 459 | { |
| 460 | return $this->_hints; |
| 461 | } |
| 462 | public function iterate($parameters = null, $hydrationMode = null) |
| 463 | { |
| 464 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/issues/8463', 'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use toIterable() instead.', __METHOD__); |
| 465 | if ($hydrationMode !== null) { |
| 466 | $this->setHydrationMode($hydrationMode); |
| 467 | } |
| 468 | if (!empty($parameters)) { |
| 469 | $this->setParameters($parameters); |
| 470 | } |
| 471 | $rsm = $this->getResultSetMapping(); |
| 472 | if ($rsm === null) { |
| 473 | throw new LogicException('Uninitialized result set mapping.'); |
| 474 | } |
| 475 | $stmt = $this->_doExecute(); |
| 476 | return $this->_em->newHydrator($this->_hydrationMode)->iterate($stmt, $rsm, $this->_hints); |
| 477 | } |
| 478 | public function toIterable(iterable $parameters = [], $hydrationMode = null) : iterable |
| 479 | { |
| 480 | if ($hydrationMode !== null) { |
| 481 | $this->setHydrationMode($hydrationMode); |
| 482 | } |
| 483 | if ($this->isCountable($parameters) && count($parameters) !== 0 || $parameters instanceof Traversable && iterator_count($parameters) !== 0) { |
| 484 | $this->setParameters($parameters); |
| 485 | } |
| 486 | $rsm = $this->getResultSetMapping(); |
| 487 | if ($rsm === null) { |
| 488 | throw new LogicException('Uninitialized result set mapping.'); |
| 489 | } |
| 490 | $stmt = $this->_doExecute(); |
| 491 | return $this->_em->newHydrator($this->_hydrationMode)->toIterable($stmt, $rsm, $this->_hints); |
| 492 | } |
| 493 | public function execute($parameters = null, $hydrationMode = null) |
| 494 | { |
| 495 | if ($this->cacheable && $this->isCacheEnabled()) { |
| 496 | return $this->executeUsingQueryCache($parameters, $hydrationMode); |
| 497 | } |
| 498 | return $this->executeIgnoreQueryCache($parameters, $hydrationMode); |
| 499 | } |
| 500 | private function executeIgnoreQueryCache($parameters = null, $hydrationMode = null) |
| 501 | { |
| 502 | if ($hydrationMode !== null) { |
| 503 | $this->setHydrationMode($hydrationMode); |
| 504 | } |
| 505 | if (!empty($parameters)) { |
| 506 | $this->setParameters($parameters); |
| 507 | } |
| 508 | $setCacheEntry = static function ($data) : void { |
| 509 | }; |
| 510 | if ($this->_hydrationCacheProfile !== null) { |
| 511 | [$cacheKey, $realCacheKey] = $this->getHydrationCacheId(); |
| 512 | $cache = $this->getHydrationCache(); |
| 513 | $cacheItem = $cache->getItem($cacheKey); |
| 514 | $result = $cacheItem->isHit() ? $cacheItem->get() : []; |
| 515 | if (isset($result[$realCacheKey])) { |
| 516 | return $result[$realCacheKey]; |
| 517 | } |
| 518 | if (!$result) { |
| 519 | $result = []; |
| 520 | } |
| 521 | $setCacheEntry = static function ($data) use($cache, $result, $cacheItem, $realCacheKey) : void { |
| 522 | $cache->save($cacheItem->set($result + [$realCacheKey => $data])); |
| 523 | }; |
| 524 | } |
| 525 | $stmt = $this->_doExecute(); |
| 526 | if (is_numeric($stmt)) { |
| 527 | $setCacheEntry($stmt); |
| 528 | return $stmt; |
| 529 | } |
| 530 | $rsm = $this->getResultSetMapping(); |
| 531 | if ($rsm === null) { |
| 532 | throw new LogicException('Uninitialized result set mapping.'); |
| 533 | } |
| 534 | $data = $this->_em->newHydrator($this->_hydrationMode)->hydrateAll($stmt, $rsm, $this->_hints); |
| 535 | $setCacheEntry($data); |
| 536 | return $data; |
| 537 | } |
| 538 | private function getHydrationCache() : CacheItemPoolInterface |
| 539 | { |
| 540 | assert($this->_hydrationCacheProfile !== null); |
| 541 | // Support for DBAL 2 |
| 542 | if (!method_exists($this->_hydrationCacheProfile, 'getResultCache')) { |
| 543 | // @phpstan-ignore method.deprecated |
| 544 | $cacheDriver = $this->_hydrationCacheProfile->getResultCacheDriver(); |
| 545 | assert($cacheDriver !== null); |
| 546 | return CacheAdapter::wrap($cacheDriver); |
| 547 | } |
| 548 | $cache = $this->_hydrationCacheProfile->getResultCache(); |
| 549 | assert($cache !== null); |
| 550 | return $cache; |
| 551 | } |
| 552 | private function executeUsingQueryCache($parameters = null, $hydrationMode = null) |
| 553 | { |
| 554 | $rsm = $this->getResultSetMapping(); |
| 555 | if ($rsm === null) { |
| 556 | throw new LogicException('Uninitialized result set mapping.'); |
| 557 | } |
| 558 | $queryCache = $this->_em->getCache()->getQueryCache($this->cacheRegion); |
| 559 | $queryKey = new QueryCacheKey($this->getHash(), $this->lifetime, $this->cacheMode ?: Cache::MODE_NORMAL, $this->getTimestampKey()); |
| 560 | $result = $queryCache->get($queryKey, $rsm, $this->_hints); |
| 561 | if ($result !== null) { |
| 562 | if ($this->cacheLogger) { |
| 563 | $this->cacheLogger->queryCacheHit($queryCache->getRegion()->getName(), $queryKey); |
| 564 | } |
| 565 | return $result; |
| 566 | } |
| 567 | $result = $this->executeIgnoreQueryCache($parameters, $hydrationMode); |
| 568 | $cached = $queryCache->put($queryKey, $rsm, $result, $this->_hints); |
| 569 | if ($this->cacheLogger) { |
| 570 | $this->cacheLogger->queryCacheMiss($queryCache->getRegion()->getName(), $queryKey); |
| 571 | if ($cached) { |
| 572 | $this->cacheLogger->queryCachePut($queryCache->getRegion()->getName(), $queryKey); |
| 573 | } |
| 574 | } |
| 575 | return $result; |
| 576 | } |
| 577 | private function getTimestampKey() : ?TimestampCacheKey |
| 578 | { |
| 579 | assert($this->_resultSetMapping !== null); |
| 580 | $entityName = reset($this->_resultSetMapping->aliasMap); |
| 581 | if (empty($entityName)) { |
| 582 | return null; |
| 583 | } |
| 584 | $metadata = $this->_em->getClassMetadata($entityName); |
| 585 | return new Cache\TimestampCacheKey($metadata->rootEntityName); |
| 586 | } |
| 587 | protected function getHydrationCacheId() |
| 588 | { |
| 589 | $parameters = []; |
| 590 | $types = []; |
| 591 | foreach ($this->getParameters() as $parameter) { |
| 592 | $parameters[$parameter->getName()] = $this->processParameterValue($parameter->getValue()); |
| 593 | $types[$parameter->getName()] = $parameter->getType(); |
| 594 | } |
| 595 | $sql = $this->getSQL(); |
| 596 | assert(is_string($sql)); |
| 597 | $queryCacheProfile = $this->getHydrationCacheProfile(); |
| 598 | $hints = $this->getHints(); |
| 599 | $hints['hydrationMode'] = $this->getHydrationMode(); |
| 600 | ksort($hints); |
| 601 | assert($queryCacheProfile !== null); |
| 602 | return $queryCacheProfile->generateCacheKeys($sql, $parameters, $types, $hints); |
| 603 | } |
| 604 | public function setResultCacheId($id) |
| 605 | { |
| 606 | if (!$this->_queryCacheProfile) { |
| 607 | return $this->setResultCacheProfile(new QueryCacheProfile(0, $id)); |
| 608 | } |
| 609 | $this->_queryCacheProfile = $this->_queryCacheProfile->setCacheKey($id); |
| 610 | return $this; |
| 611 | } |
| 612 | public function getResultCacheId() |
| 613 | { |
| 614 | return $this->_queryCacheProfile ? $this->_queryCacheProfile->getCacheKey() : null; |
| 615 | } |
| 616 | protected abstract function _doExecute(); |
| 617 | public function __clone() |
| 618 | { |
| 619 | $this->parameters = new ArrayCollection(); |
| 620 | $this->_hints = []; |
| 621 | $this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints(); |
| 622 | } |
| 623 | protected function getHash() |
| 624 | { |
| 625 | $query = $this->getSQL(); |
| 626 | assert(is_string($query)); |
| 627 | $hints = $this->getHints(); |
| 628 | $params = array_map(function (Parameter $parameter) { |
| 629 | $value = $parameter->getValue(); |
| 630 | // Small optimization |
| 631 | // Does not invoke processParameterValue for scalar value |
| 632 | if (is_scalar($value)) { |
| 633 | return $value; |
| 634 | } |
| 635 | return $this->processParameterValue($value); |
| 636 | }, $this->parameters->getValues()); |
| 637 | ksort($hints); |
| 638 | return sha1($query . '-' . serialize($params) . '-' . serialize($hints)); |
| 639 | } |
| 640 | private function isCountable(iterable $subject) : bool |
| 641 | { |
| 642 | return $subject instanceof Countable || is_array($subject); |
| 643 | } |
| 644 | } |
| 645 |