function StatementPrefetchIterator::execute

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

Overrides StatementInterface::execute

1 call to StatementPrefetchIterator::execute()
Statement::execute in core/modules/sqlite/src/Driver/Database/sqlite/Statement.php
1 method overrides StatementPrefetchIterator::execute()
Statement::execute in core/modules/sqlite/src/Driver/Database/sqlite/Statement.php

File

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

Class

StatementPrefetchIterator
An implementation of StatementInterface that prefetches 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 and execute the statement.
    try {
        $statement = $this->getStatement($this->queryString, $args);
        $return = $statement->execute($args);
    } catch (\Exception $e) {
        if (isset($startEvent) && $this->connection
            ->isEventEnabled(StatementExecutionFailureEvent::class)) {
            $this->connection
                ->dispatchEvent(new StatementExecutionFailureEvent($startEvent->statementObjectId, $startEvent->key, $startEvent->target, $startEvent->queryString, $startEvent->args, $startEvent->caller, $startEvent->time, get_class($e), $e->getCode(), $e->getMessage()));
        }
        throw $e;
    }
    // Fetch all the data from the reply, in order to release any lock as soon
    // as possible.
    $this->data = $statement->fetchAll(\PDO::FETCH_ASSOC);
    $this->rowCount = $this->rowCountEnabled ? $statement->rowCount() : NULL;
    // Destroy the statement as soon as possible. See the documentation of
    // \Drupal\sqlite\Driver\Database\sqlite\Statement for an explanation.
    unset($statement);
    $this->markResultsetIterable($return);
    $this->columnNames = count($this->data) > 0 ? array_keys($this->data[0]) : [];
    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));
    }
    return $return;
}

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