class StatementPrefetchIterator

Same name in other branches
  1. 10 core/lib/Drupal/Core/Database/StatementPrefetchIterator.php \Drupal\Core\Database\StatementPrefetchIterator

An implementation of StatementInterface that prefetches all data.

This class behaves very similar to a StatementWrapperIterator of a \PDOStatement but as it always fetches every row it is possible to manipulate those results.

Hierarchy

Expanded class hierarchy of StatementPrefetchIterator

3 files declare their use of StatementPrefetchIterator
ConnectionTest.php in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
FetchTest.php in core/tests/Drupal/KernelTests/Core/Database/FetchTest.php
Statement.php in core/modules/sqlite/src/Driver/Database/sqlite/Statement.php

File

core/lib/Drupal/Core/Database/StatementPrefetchIterator.php, line 16

Namespace

Drupal\Core\Database
View source
class StatementPrefetchIterator extends StatementBase {
    
    /**
     * Main data store.
     *
     * The resultset is stored as a FetchAs::Associative array.
     *
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     * the methods provided by Drupal\Core\Database\Statement\PrefetchedResult
     * instead.
     *
     * @see https://www.drupal.org/node/3510455
     */
    protected array $data = [];
    
    /**
     * The list of column names in this result set.
     *
     * @var string[]
     *
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     * the methods provided by Drupal\Core\Database\Statement\PrefetchedResult
     * instead.
     *
     * @see https://www.drupal.org/node/3510455
     */
    protected ?array $columnNames = NULL;
    
    /**
     * The number of rows matched by the last query.
     *
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     * the methods provided by Drupal\Core\Database\Statement\PrefetchedResult
     * instead.
     *
     * @see https://www.drupal.org/node/3510455
     */
    protected ?int $rowCount = NULL;
    
    /**
     * Holds the default fetch style.
     *
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     * $fetchMode instead.
     *
     * @see https://www.drupal.org/node/3488338
     */
    protected int $defaultFetchStyle = \PDO::FETCH_OBJ;
    
    /**
     * Holds the default fetch mode.
     *
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     * $fetchMode instead.
     *
     * @see https://www.drupal.org/node/3510455
     */
    protected FetchAs $defaultFetchMode = FetchAs::Object;
    
    /**
     * Constructs a StatementPrefetchIterator object.
     *
     * @param object $clientConnection
     *   Client database connection object, for example \PDO.
     * @param \Drupal\Core\Database\Connection $connection
     *   The database connection.
     * @param string $queryString
     *   The query string.
     * @param array $driverOptions
     *   Driver-specific options.
     * @param bool $rowCountEnabled
     *   (optional) Enables counting the rows matched. Defaults to FALSE.
     */
    public function __construct(object $clientConnection, Connection $connection, string $queryString, array $driverOptions = [], bool $rowCountEnabled = FALSE) {
        parent::__construct($connection, $clientConnection, $queryString, $rowCountEnabled);
    }
    
    /**
     * {@inheritdoc}
     */
    public function execute($args = [], $options = []) {
        if (isset($options['fetch']) && is_int($options['fetch'])) {
            @trigger_error("Passing the 'fetch' key as an integer to \$options in execute() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use a case of \\Drupal\\Core\\Database\\FetchAs enum instead. See https://www.drupal.org/node/3488338", E_USER_DEPRECATED);
        }
        $startEvent = $this->dispatchStatementExecutionStartEvent($args ?? []);
        // Prepare and execute the statement.
        try {
            $this->clientStatement = $this->getStatement($this->queryString, $args);
            $return = $this->clientExecute($args, $options);
        } catch (\Exception $e) {
            $this->dispatchStatementExecutionFailureEvent($startEvent, $e);
            unset($this->clientStatement);
            throw $e;
        }
        // Fetch all the data from the reply, in order to release any lock as soon
        // as possible. Then, destroy the client statement. See the documentation
        // of \Drupal\sqlite\Driver\Database\sqlite\Statement for an explanation.
        $this->result = new PrefetchedResult($this->fetchMode, $this->fetchOptions, $this->clientFetchAll(FetchAs::Associative), $this->rowCountEnabled ? $this->clientRowCount() : NULL);
        unset($this->clientStatement);
        $this->markResultsetIterable($return);
        if (isset($options['fetch'])) {
            if (is_string($options['fetch'])) {
                // Default to an object. Note: db fields will be added to the object
                // before the constructor is run. If you need to assign fields after
                // the constructor is run. See https://www.drupal.org/node/315092.
                $this->setFetchMode(FetchAs::ClassObject, $options['fetch']);
            }
            else {
                $this->setFetchMode($options['fetch']);
            }
        }
        $this->dispatchStatementExecutionEndEvent($startEvent);
        return $return;
    }
    
    /**
     * Grab a PDOStatement object from a given query and its arguments.
     *
     * Some drivers (including SQLite) will need to perform some preparation
     * themselves to get the statement right.
     *
     * @param string $query
     *   The query.
     * @param array|null $args
     *   An array of arguments. This can be NULL.
     *
     * @return object
     *   A PDOStatement object.
     */
    protected function getStatement(string $query, ?array &$args = []) : object {
        return $this->connection
            ->prepare($query, $this->driverOptions);
    }
    
    /**
     * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
     *   ::fetchField() instead.
     *
     * @see https://www.drupal.org/node/3490312
     */
    public function fetchColumn($index = 0) {
        @trigger_error(__METHOD__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use ::fetchField() instead. See https://www.drupal.org/node/3490312', E_USER_DEPRECATED);
        return $this->fetchField($index);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
FetchModeTrait::$fetchModeLiterals protected property Map FETCH_* modes to their literal for inclusion in messages.
FetchModeTrait::$supportedFetchModes protected property The fetch modes supported.
FetchModeTrait::assocToClass protected function Converts a row of data in FETCH_ASSOC format to FETCH_CLASS.
FetchModeTrait::assocToColumn protected function Converts a row of data in FETCH_ASSOC format to FETCH_COLUMN.
FetchModeTrait::assocToFetchMode protected function Converts a row of data in associative format to a specified format.
FetchModeTrait::assocToNum protected function Converts a row of data in FETCH_ASSOC format to FETCH_NUM.
FetchModeTrait::assocToObj protected function Converts a row of data in FETCH_ASSOC format to FETCH_OBJ.
PdoTrait::clientExecute protected function Executes the prepared PDO statement.
PdoTrait::clientFetch protected function Fetches the next row from the PDO statement.
PdoTrait::clientFetchAll protected function
PdoTrait::clientFetchColumn protected function Returns a single column from the next row of a result set.
PdoTrait::clientFetchObject protected function Fetches the next row and returns it as an object.
PdoTrait::clientQueryString protected function Returns the query string used to prepare the statement.
PdoTrait::clientRowCount protected function Returns the number of rows affected by the last SQL statement.
PdoTrait::clientSetFetchMode protected function Sets the default fetch mode for the PDO statement.
PdoTrait::fetchAsToPdo protected function Converts a FetchAs mode to a \PDO::FETCH_* constant value.
PdoTrait::getClientStatement public function Returns the client-level database PDO statement object.
PdoTrait::pdoToFetchAs protected function Converts a \PDO::FETCH_* constant value to a FetchAs mode.
StatementBase::$clientStatement protected property The client database Statement object.
StatementBase::$fetchMode protected property Holds the default fetch mode.
StatementBase::$fetchOptions protected property Holds fetch options.
StatementBase::$result protected property The results of a data query language (DQL) statement.
StatementBase::dispatchStatementExecutionEndEvent protected function Dispatches an event informing that the statement execution succeeded.
StatementBase::dispatchStatementExecutionFailureEvent protected function Dispatches an event informing of the statement execution failure.
StatementBase::dispatchStatementExecutionStartEvent protected function Dispatches an event informing that the statement execution begins.
StatementBase::fetch public function Fetches the next row from a result set. Overrides StatementInterface::fetch
StatementBase::fetchAll public function Returns an array containing all of the result set rows. Overrides StatementInterface::fetchAll
StatementBase::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides StatementInterface::fetchAllAssoc
StatementBase::fetchAllKeyed public function Returns the entire result set as a single associative array. Overrides StatementInterface::fetchAllKeyed
StatementBase::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides StatementInterface::fetchAssoc
StatementBase::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides StatementInterface::fetchCol
StatementBase::fetchField public function Returns a single field from the next record of a result set. Overrides StatementInterface::fetchField
StatementBase::fetchObject public function Fetches the next row and returns it as an object. Overrides StatementInterface::fetchObject
StatementBase::getConnectionTarget public function Returns the target connection this statement is associated with. Overrides StatementInterface::getConnectionTarget
StatementBase::getQueryString public function Gets the query string of this statement. Overrides StatementInterface::getQueryString
StatementBase::rowCount public function Returns the number of rows matched by the last SQL statement. Overrides StatementInterface::rowCount
StatementBase::setFetchMode public function Sets the default fetch mode for this statement. Overrides StatementInterface::setFetchMode
StatementIteratorTrait::$isResultsetIterable private property Traces if rows can be fetched from the resultset.
StatementIteratorTrait::$resultsetKey private property The key of the current row.
StatementIteratorTrait::$resultsetRow private property The current row, retrieved in the current fetch format.
StatementIteratorTrait::current public function Returns the current element.
StatementIteratorTrait::getResultsetCurrentRowIndex protected function Returns the row index of the current element in the resultset.
StatementIteratorTrait::key public function Returns the key of the current element.
StatementIteratorTrait::markResultsetFetchingComplete protected function Informs the iterator that no more rows can be fetched from the resultset.
StatementIteratorTrait::markResultsetIterable protected function Informs the iterator whether rows can be fetched from the resultset.
StatementIteratorTrait::next public function Moves the current position to the next element.
StatementIteratorTrait::rewind public function Rewinds back to the first element of the Iterator.
StatementIteratorTrait::setResultsetCurrentRow protected function Sets the current resultset row for the iterator, and increments the key.
StatementIteratorTrait::valid public function Checks if current position is valid.
StatementPrefetchIterator::$columnNames Deprecated protected property The list of column names in this result set.
StatementPrefetchIterator::$data Deprecated protected property Main data store.
StatementPrefetchIterator::$defaultFetchMode Deprecated protected property Holds the default fetch mode.
StatementPrefetchIterator::$defaultFetchStyle Deprecated protected property Holds the default fetch style.
StatementPrefetchIterator::$rowCount Deprecated protected property The number of rows matched by the last query.
StatementPrefetchIterator::execute public function Executes a prepared statement. Overrides StatementBase::execute 1
StatementPrefetchIterator::fetchColumn Deprecated public function
StatementPrefetchIterator::getStatement protected function Grab a PDOStatement object from a given query and its arguments. 1
StatementPrefetchIterator::__construct public function Constructs a StatementPrefetchIterator object. Overrides StatementBase::__construct

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.