vendor/doctrine/orm/src/Query.php line 284

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM;
  4. use Doctrine\DBAL\LockMode;
  5. use Doctrine\DBAL\Result;
  6. use Doctrine\DBAL\Types\Type;
  7. use Doctrine\ORM\Mapping\ClassMetadata;
  8. use Doctrine\ORM\Query\AST\DeleteStatement;
  9. use Doctrine\ORM\Query\AST\SelectStatement;
  10. use Doctrine\ORM\Query\AST\UpdateStatement;
  11. use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
  12. use Doctrine\ORM\Query\Parameter;
  13. use Doctrine\ORM\Query\ParameterTypeInferer;
  14. use Doctrine\ORM\Query\Parser;
  15. use Doctrine\ORM\Query\ParserResult;
  16. use Doctrine\ORM\Query\QueryException;
  17. use Doctrine\ORM\Query\ResultSetMapping;
  18. use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
  19. use Psr\Cache\CacheItemPoolInterface;
  20. use function array_keys;
  21. use function array_values;
  22. use function assert;
  23. use function count;
  24. use function get_debug_type;
  25. use function in_array;
  26. use function ksort;
  27. use function md5;
  28. use function reset;
  29. use function serialize;
  30. use function sha1;
  31. use function stripos;
  32. /**
  33.  * A Query object represents a DQL query.
  34.  *
  35.  * @final
  36.  */
  37. class Query extends AbstractQuery
  38. {
  39.     /**
  40.      * A query object is in CLEAN state when it has NO unparsed/unprocessed DQL parts.
  41.      */
  42.     public const STATE_CLEAN 1;
  43.     /**
  44.      * A query object is in state DIRTY when it has DQL parts that have not yet been
  45.      * parsed/processed. This is automatically defined as DIRTY when addDqlQueryPart
  46.      * is called.
  47.      */
  48.     public const STATE_DIRTY 2;
  49.     /* Query HINTS */
  50.     /**
  51.      * The refresh hint turns any query into a refresh query with the result that
  52.      * any local changes in entities are overridden with the fetched values.
  53.      */
  54.     public const HINT_REFRESH 'doctrine.refresh';
  55.     public const HINT_CACHE_ENABLED 'doctrine.cache.enabled';
  56.     public const HINT_CACHE_EVICT 'doctrine.cache.evict';
  57.     /**
  58.      * Internal hint: is set to the proxy entity that is currently triggered for loading
  59.      */
  60.     public const HINT_REFRESH_ENTITY 'doctrine.refresh.entity';
  61.     /**
  62.      * The includeMetaColumns query hint causes meta columns like foreign keys and
  63.      * discriminator columns to be selected and returned as part of the query result.
  64.      *
  65.      * This hint does only apply to non-object queries.
  66.      */
  67.     public const HINT_INCLUDE_META_COLUMNS 'doctrine.includeMetaColumns';
  68.     /**
  69.      * An array of class names that implement \Doctrine\ORM\Query\TreeWalker and
  70.      * are iterated and executed after the DQL has been parsed into an AST.
  71.      */
  72.     public const HINT_CUSTOM_TREE_WALKERS 'doctrine.customTreeWalkers';
  73.     /**
  74.      * A string with a class name that implements \Doctrine\ORM\Query\TreeWalker
  75.      * and is used for generating the target SQL from any DQL AST tree.
  76.      */
  77.     public const HINT_CUSTOM_OUTPUT_WALKER 'doctrine.customOutputWalker';
  78.     /**
  79.      * Marks queries as creating only read only objects.
  80.      *
  81.      * If the object retrieved from the query is already in the identity map
  82.      * then it does not get marked as read only if it wasn't already.
  83.      */
  84.     public const HINT_READ_ONLY 'doctrine.readOnly';
  85.     public const HINT_INTERNAL_ITERATION 'doctrine.internal.iteration';
  86.     public const HINT_LOCK_MODE 'doctrine.lockMode';
  87.     /**
  88.      * The current state of this query.
  89.      *
  90.      * @psalm-var self::STATE_*
  91.      */
  92.     private int $state self::STATE_DIRTY;
  93.     /**
  94.      * A snapshot of the parameter types the query was parsed with.
  95.      *
  96.      * @var array<string,Type>
  97.      */
  98.     private array $parsedTypes = [];
  99.     /**
  100.      * Cached DQL query.
  101.      */
  102.     private string|null $dql null;
  103.     /**
  104.      * The parser result that holds DQL => SQL information.
  105.      */
  106.     private ParserResult $parserResult;
  107.     /**
  108.      * The first result to return (the "offset").
  109.      */
  110.     private int $firstResult 0;
  111.     /**
  112.      * The maximum number of results to return (the "limit").
  113.      */
  114.     private int|null $maxResults null;
  115.     /**
  116.      * The cache driver used for caching queries.
  117.      */
  118.     private CacheItemPoolInterface|null $queryCache null;
  119.     /**
  120.      * Whether or not expire the query cache.
  121.      */
  122.     private bool $expireQueryCache false;
  123.     /**
  124.      * The query cache lifetime.
  125.      */
  126.     private int|null $queryCacheTTL null;
  127.     /**
  128.      * Whether to use a query cache, if available. Defaults to TRUE.
  129.      */
  130.     private bool $useQueryCache true;
  131.     /**
  132.      * Gets the SQL query/queries that correspond to this DQL query.
  133.      *
  134.      * @return list<string>|string The built sql query or an array of all sql queries.
  135.      */
  136.     public function getSQL(): string|array
  137.     {
  138.         return $this->parse()->getSqlExecutor()->getSqlStatements();
  139.     }
  140.     /**
  141.      * Returns the corresponding AST for this DQL query.
  142.      */
  143.     public function getAST(): SelectStatement|UpdateStatement|DeleteStatement
  144.     {
  145.         $parser = new Parser($this);
  146.         return $parser->getAST();
  147.     }
  148.     protected function getResultSetMapping(): ResultSetMapping
  149.     {
  150.         // parse query or load from cache
  151.         if ($this->resultSetMapping === null) {
  152.             $this->resultSetMapping $this->parse()->getResultSetMapping();
  153.         }
  154.         return $this->resultSetMapping;
  155.     }
  156.     /**
  157.      * Parses the DQL query, if necessary, and stores the parser result.
  158.      *
  159.      * Note: Populates $this->_parserResult as a side-effect.
  160.      */
  161.     private function parse(): ParserResult
  162.     {
  163.         $types = [];
  164.         foreach ($this->parameters as $parameter) {
  165.             /** @var Query\Parameter $parameter */
  166.             $types[$parameter->getName()] = $parameter->getType();
  167.         }
  168.         // Return previous parser result if the query and the filter collection are both clean
  169.         if ($this->state === self::STATE_CLEAN && $this->parsedTypes === $types && $this->em->isFiltersStateClean()) {
  170.             return $this->parserResult;
  171.         }
  172.         $this->state       self::STATE_CLEAN;
  173.         $this->parsedTypes $types;
  174.         $queryCache $this->queryCache ?? $this->em->getConfiguration()->getQueryCache();
  175.         // Check query cache.
  176.         if (! ($this->useQueryCache && $queryCache)) {
  177.             $parser = new Parser($this);
  178.             $this->parserResult $parser->parse();
  179.             return $this->parserResult;
  180.         }
  181.         $cacheItem $queryCache->getItem($this->getQueryCacheId());
  182.         if (! $this->expireQueryCache && $cacheItem->isHit()) {
  183.             $cached $cacheItem->get();
  184.             if ($cached instanceof ParserResult) {
  185.                 // Cache hit.
  186.                 $this->parserResult $cached;
  187.                 return $this->parserResult;
  188.             }
  189.         }
  190.         // Cache miss.
  191.         $parser = new Parser($this);
  192.         $this->parserResult $parser->parse();
  193.         $queryCache->save($cacheItem->set($this->parserResult)->expiresAfter($this->queryCacheTTL));
  194.         return $this->parserResult;
  195.     }
  196.     protected function _doExecute(): Result|int
  197.     {
  198.         $executor $this->parse()->getSqlExecutor();
  199.         if ($this->queryCacheProfile) {
  200.             $executor->setQueryCacheProfile($this->queryCacheProfile);
  201.         } else {
  202.             $executor->removeQueryCacheProfile();
  203.         }
  204.         if ($this->resultSetMapping === null) {
  205.             $this->resultSetMapping $this->parserResult->getResultSetMapping();
  206.         }
  207.         // Prepare parameters
  208.         $paramMappings $this->parserResult->getParameterMappings();
  209.         $paramCount    count($this->parameters);
  210.         $mappingCount  count($paramMappings);
  211.         if ($paramCount $mappingCount) {
  212.             throw QueryException::tooManyParameters($mappingCount$paramCount);
  213.         }
  214.         if ($paramCount $mappingCount) {
  215.             throw QueryException::tooFewParameters($mappingCount$paramCount);
  216.         }
  217.         // evict all cache for the entity region
  218.         if ($this->hasCache && isset($this->hints[self::HINT_CACHE_EVICT]) && $this->hints[self::HINT_CACHE_EVICT]) {
  219.             $this->evictEntityCacheRegion();
  220.         }
  221.         [$sqlParams$types] = $this->processParameterMappings($paramMappings);
  222.         $this->evictResultSetCache(
  223.             $executor,
  224.             $sqlParams,
  225.             $types,
  226.             $this->em->getConnection()->getParams(),
  227.         );
  228.         return $executor->execute($this->em->getConnection(), $sqlParams$types);
  229.     }
  230.     /**
  231.      * @param array<string,mixed> $sqlParams
  232.      * @param array<string,Type>  $types
  233.      * @param array<string,mixed> $connectionParams
  234.      */
  235.     private function evictResultSetCache(
  236.         AbstractSqlExecutor $executor,
  237.         array $sqlParams,
  238.         array $types,
  239.         array $connectionParams,
  240.     ): void {
  241.         if ($this->queryCacheProfile === null || ! $this->getExpireResultCache()) {
  242.             return;
  243.         }
  244.         $cache $this->queryCacheProfile->getResultCache();
  245.         assert($cache !== null);
  246.         $statements = (array) $executor->getSqlStatements(); // Type casted since it can either be a string or an array
  247.         foreach ($statements as $statement) {
  248.             $cacheKeys $this->queryCacheProfile->generateCacheKeys($statement$sqlParams$types$connectionParams);
  249.             $cache->deleteItem(reset($cacheKeys));
  250.         }
  251.     }
  252.     /**
  253.      * Evict entity cache region
  254.      */
  255.     private function evictEntityCacheRegion(): void
  256.     {
  257.         $AST $this->getAST();
  258.         if ($AST instanceof SelectStatement) {
  259.             throw new QueryException('The hint "HINT_CACHE_EVICT" is not valid for select statements.');
  260.         }
  261.         $className $AST instanceof DeleteStatement
  262.             $AST->deleteClause->abstractSchemaName
  263.             $AST->updateClause->abstractSchemaName;
  264.         $this->em->getCache()->evictEntityRegion($className);
  265.     }
  266.     /**
  267.      * Processes query parameter mappings.
  268.      *
  269.      * @param array<list<int>> $paramMappings
  270.      *
  271.      * @return mixed[][]
  272.      * @psalm-return array{0: list<mixed>, 1: array}
  273.      *
  274.      * @throws Query\QueryException
  275.      */
  276.     private function processParameterMappings(array $paramMappings): array
  277.     {
  278.         $sqlParams = [];
  279.         $types     = [];
  280.         foreach ($this->parameters as $parameter) {
  281.             $key $parameter->getName();
  282.             if (! isset($paramMappings[$key])) {
  283.                 throw QueryException::unknownParameter($key);
  284.             }
  285.             [$value$type] = $this->resolveParameterValue($parameter);
  286.             foreach ($paramMappings[$key] as $position) {
  287.                 $types[$position] = $type;
  288.             }
  289.             $sqlPositions $paramMappings[$key];
  290.             // optimized multi value sql positions away for now,
  291.             // they are not allowed in DQL anyways.
  292.             $value      = [$value];
  293.             $countValue count($value);
  294.             for ($i 0$l count($sqlPositions); $i $l$i++) {
  295.                 $sqlParams[$sqlPositions[$i]] = $value[$i $countValue];
  296.             }
  297.         }
  298.         if (count($sqlParams) !== count($types)) {
  299.             throw QueryException::parameterTypeMismatch();
  300.         }
  301.         if ($sqlParams) {
  302.             ksort($sqlParams);
  303.             $sqlParams array_values($sqlParams);
  304.             ksort($types);
  305.             $types array_values($types);
  306.         }
  307.         return [$sqlParams$types];
  308.     }
  309.     /**
  310.      * @return mixed[] tuple of (value, type)
  311.      * @psalm-return array{0: mixed, 1: mixed}
  312.      */
  313.     private function resolveParameterValue(Parameter $parameter): array
  314.     {
  315.         if ($parameter->typeWasSpecified()) {
  316.             return [$parameter->getValue(), $parameter->getType()];
  317.         }
  318.         $key           $parameter->getName();
  319.         $originalValue $parameter->getValue();
  320.         $value         $originalValue;
  321.         $rsm           $this->getResultSetMapping();
  322.         if ($value instanceof ClassMetadata && isset($rsm->metadataParameterMapping[$key])) {
  323.             $value $value->getMetadataValue($rsm->metadataParameterMapping[$key]);
  324.         }
  325.         if ($value instanceof ClassMetadata && isset($rsm->discriminatorParameters[$key])) {
  326.             $value array_keys(HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($value$this->em));
  327.         }
  328.         $processedValue $this->processParameterValue($value);
  329.         return [
  330.             $processedValue,
  331.             $originalValue === $processedValue
  332.                 $parameter->getType()
  333.                 : ParameterTypeInferer::inferType($processedValue),
  334.         ];
  335.     }
  336.     /**
  337.      * Defines a cache driver to be used for caching queries.
  338.      *
  339.      * @return $this
  340.      */
  341.     public function setQueryCache(CacheItemPoolInterface|null $queryCache): self
  342.     {
  343.         $this->queryCache $queryCache;
  344.         return $this;
  345.     }
  346.     /**
  347.      * Defines whether the query should make use of a query cache, if available.
  348.      *
  349.      * @return $this
  350.      */
  351.     public function useQueryCache(bool $bool): self
  352.     {
  353.         $this->useQueryCache $bool;
  354.         return $this;
  355.     }
  356.     /**
  357.      * Defines how long the query cache will be active before expire.
  358.      *
  359.      * @param int|null $timeToLive How long the cache entry is valid.
  360.      *
  361.      * @return $this
  362.      */
  363.     public function setQueryCacheLifetime(int|null $timeToLive): self
  364.     {
  365.         $this->queryCacheTTL $timeToLive;
  366.         return $this;
  367.     }
  368.     /**
  369.      * Retrieves the lifetime of resultset cache.
  370.      */
  371.     public function getQueryCacheLifetime(): int|null
  372.     {
  373.         return $this->queryCacheTTL;
  374.     }
  375.     /**
  376.      * Defines if the query cache is active or not.
  377.      *
  378.      * @return $this
  379.      */
  380.     public function expireQueryCache(bool $expire true): self
  381.     {
  382.         $this->expireQueryCache $expire;
  383.         return $this;
  384.     }
  385.     /**
  386.      * Retrieves if the query cache is active or not.
  387.      */
  388.     public function getExpireQueryCache(): bool
  389.     {
  390.         return $this->expireQueryCache;
  391.     }
  392.     public function free(): void
  393.     {
  394.         parent::free();
  395.         $this->dql   null;
  396.         $this->state self::STATE_CLEAN;
  397.     }
  398.     /**
  399.      * Sets a DQL query string.
  400.      */
  401.     public function setDQL(string $dqlQuery): self
  402.     {
  403.         $this->dql   $dqlQuery;
  404.         $this->state self::STATE_DIRTY;
  405.         return $this;
  406.     }
  407.     /**
  408.      * Returns the DQL query that is represented by this query object.
  409.      */
  410.     public function getDQL(): string|null
  411.     {
  412.         return $this->dql;
  413.     }
  414.     /**
  415.      * Returns the state of this query object
  416.      * By default the type is Doctrine_ORM_Query_Abstract::STATE_CLEAN but if it appears any unprocessed DQL
  417.      * part, it is switched to Doctrine_ORM_Query_Abstract::STATE_DIRTY.
  418.      *
  419.      * @see AbstractQuery::STATE_CLEAN
  420.      * @see AbstractQuery::STATE_DIRTY
  421.      *
  422.      * @return int The query state.
  423.      * @psalm-return self::STATE_* The query state.
  424.      */
  425.     public function getState(): int
  426.     {
  427.         return $this->state;
  428.     }
  429.     /**
  430.      * Method to check if an arbitrary piece of DQL exists
  431.      *
  432.      * @param string $dql Arbitrary piece of DQL to check for.
  433.      */
  434.     public function contains(string $dql): bool
  435.     {
  436.         return stripos($this->getDQL(), $dql) !== false;
  437.     }
  438.     /**
  439.      * Sets the position of the first result to retrieve (the "offset").
  440.      *
  441.      * @param int $firstResult The first result to return.
  442.      *
  443.      * @return $this
  444.      */
  445.     public function setFirstResult(int $firstResult): self
  446.     {
  447.         $this->firstResult $firstResult;
  448.         $this->state       self::STATE_DIRTY;
  449.         return $this;
  450.     }
  451.     /**
  452.      * Gets the position of the first result the query object was set to retrieve (the "offset").
  453.      * Returns 0 if {@link setFirstResult} was not applied to this query.
  454.      *
  455.      * @return int The position of the first result.
  456.      */
  457.     public function getFirstResult(): int
  458.     {
  459.         return $this->firstResult;
  460.     }
  461.     /**
  462.      * Sets the maximum number of results to retrieve (the "limit").
  463.      *
  464.      * @return $this
  465.      */
  466.     public function setMaxResults(int|null $maxResults): self
  467.     {
  468.         $this->maxResults $maxResults;
  469.         $this->state      self::STATE_DIRTY;
  470.         return $this;
  471.     }
  472.     /**
  473.      * Gets the maximum number of results the query object was set to retrieve (the "limit").
  474.      * Returns NULL if {@link setMaxResults} was not applied to this query.
  475.      *
  476.      * @return int|null Maximum number of results.
  477.      */
  478.     public function getMaxResults(): int|null
  479.     {
  480.         return $this->maxResults;
  481.     }
  482.     /** {@inheritDoc} */
  483.     public function toIterable(iterable $parameters = [], $hydrationMode self::HYDRATE_OBJECT): iterable
  484.     {
  485.         $this->setHint(self::HINT_INTERNAL_ITERATIONtrue);
  486.         return parent::toIterable($parameters$hydrationMode);
  487.     }
  488.     public function setHint(string $namemixed $value): static
  489.     {
  490.         $this->state self::STATE_DIRTY;
  491.         return parent::setHint($name$value);
  492.     }
  493.     public function setHydrationMode(string|int $hydrationMode): static
  494.     {
  495.         $this->state self::STATE_DIRTY;
  496.         return parent::setHydrationMode($hydrationMode);
  497.     }
  498.     /**
  499.      * Set the lock mode for this Query.
  500.      *
  501.      * @see \Doctrine\DBAL\LockMode
  502.      *
  503.      * @psalm-param LockMode::* $lockMode
  504.      *
  505.      * @return $this
  506.      *
  507.      * @throws TransactionRequiredException
  508.      */
  509.     public function setLockMode(LockMode|int $lockMode): self
  510.     {
  511.         if (in_array($lockMode, [LockMode::NONELockMode::PESSIMISTIC_READLockMode::PESSIMISTIC_WRITE], true)) {
  512.             if (! $this->em->getConnection()->isTransactionActive()) {
  513.                 throw TransactionRequiredException::transactionRequired();
  514.             }
  515.         }
  516.         $this->setHint(self::HINT_LOCK_MODE$lockMode);
  517.         return $this;
  518.     }
  519.     /**
  520.      * Get the current lock mode for this query.
  521.      *
  522.      * @return int|null The current lock mode of this query or NULL if no specific lock mode is set.
  523.      */
  524.     public function getLockMode(): int|null
  525.     {
  526.         $lockMode $this->getHint(self::HINT_LOCK_MODE);
  527.         if ($lockMode === false) {
  528.             return null;
  529.         }
  530.         return $lockMode;
  531.     }
  532.     /**
  533.      * Generate a cache id for the query cache - reusing the Result-Cache-Id generator.
  534.      */
  535.     protected function getQueryCacheId(): string
  536.     {
  537.         ksort($this->hints);
  538.         return md5(
  539.             $this->getDQL() . serialize($this->hints) .
  540.             '&platform=' get_debug_type($this->getEntityManager()->getConnection()->getDatabasePlatform()) .
  541.             ($this->em->hasFilters() ? $this->em->getFilters()->getHash() : '') .
  542.             '&firstResult=' $this->firstResult '&maxResult=' $this->maxResults .
  543.             '&hydrationMode=' $this->hydrationMode '&types=' serialize($this->parsedTypes) . 'DOCTRINE_QUERY_CACHE_SALT',
  544.         );
  545.     }
  546.     protected function getHash(): string
  547.     {
  548.         return sha1(parent::getHash() . '-' $this->firstResult '-' $this->maxResults);
  549.     }
  550.     /**
  551.      * Cleanup Query resource when clone is called.
  552.      */
  553.     public function __clone()
  554.     {
  555.         parent::__clone();
  556.         $this->state self::STATE_DIRTY;
  557.     }
  558. }