function StatementPrefetch::execute

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/StatementPrefetch.php \Drupal\Core\Database\StatementPrefetch::execute()
  2. 8.9.x core/lib/Drupal/Core/Database/StatementPrefetch.php \Drupal\Core\Database\StatementPrefetch::execute()

Overrides StatementInterface::execute

File

core/lib/Drupal/Core/Database/StatementPrefetch.php, line 172

Class

StatementPrefetch
An implementation of StatementInterface that pre-fetches all data.

Namespace

Drupal\Core\Database

Code

public function execute($args = [], $options = []) {
    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(\PDO::FETCH_CLASS, $options['fetch']);
        }
        else {
            $this->setFetchMode($options['fetch']);
        }
    }
    if ($this->connection
        ->isEventEnabled(StatementExecutionStartEvent::class)) {
        $startEvent = new StatementExecutionStartEvent(spl_object_id($this), $this->connection
            ->getKey(), $this->connection
            ->getTarget(), $this->getQueryString(), $args ?? [], $this->connection
            ->findCallerFromDebugBacktrace());
        $this->connection
            ->dispatchEvent($startEvent);
    }
    // Prepare the query.
    $statement = $this->getStatement($this->queryString, $args);
    if (!$statement) {
        $this->throwPDOException();
    }
    $return = $statement->execute($args);
    if (!$return) {
        $this->throwPDOException();
    }
    if ($this->rowCountEnabled) {
        $this->rowCount = $statement->rowCount();
    }
    // Fetch all the data from the reply, in order to release any lock
    // as soon as possible.
    $this->data = $statement->fetchAll(\PDO::FETCH_ASSOC);
    // Destroy the statement as soon as possible. See the documentation of
    // \Drupal\sqlite\Driver\Database\sqlite\Statement for an explanation.
    unset($statement);
    $this->resultRowCount = count($this->data);
    if ($this->resultRowCount) {
        $this->columnNames = array_keys($this->data[0]);
    }
    else {
        $this->columnNames = [];
    }
    if (isset($startEvent) && $this->connection
        ->isEventEnabled(StatementExecutionEndEvent::class)) {
        $this->connection
            ->dispatchEvent(new StatementExecutionEndEvent($startEvent->statementObjectId, $startEvent->key, $startEvent->target, $startEvent->queryString, $startEvent->args, $startEvent->caller, $startEvent->time));
    }
    // Initialize the first row in $this->currentRow.
    $this->next();
    return $return;
}

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