class Statement

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Statement.php \Drupal\sqlite\Driver\Database\sqlite\Statement
  2. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Statement.php \Drupal\Core\Database\Driver\sqlite\Statement
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Statement.php \Drupal\Core\Database\Driver\sqlite\Statement
  2. 8.9.x core/lib/Drupal/Core/Database/Statement.php \Drupal\Core\Database\Statement
  3. 10 core/modules/sqlite/src/Driver/Database/sqlite/Statement.php \Drupal\sqlite\Driver\Database\sqlite\Statement
  4. 10 core/lib/Drupal/Core/Database/Driver/sqlite/Statement.php \Drupal\Core\Database\Driver\sqlite\Statement
  5. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Statement.php \Drupal\sqlite\Driver\Database\sqlite\Statement

Default implementation of StatementInterface.

\PDO allows us to extend the \PDOStatement class to provide additional functionality beyond that offered by default. We do need extra functionality. By default, this class is not driver-specific. If a given driver needs to set a custom statement class, it may do so in its constructor.

Hierarchy

Expanded class hierarchy of Statement

Deprecated

in drupal:9.1.0 and is removed from drupal:10.0.0. Database drivers should use or extend StatementWrapper instead, and encapsulate client-level statement objects.

See also

http://php.net/pdostatement

https://www.drupal.org/node/3177488

1 file declares its use of Statement
ConnectionTest.php in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php

File

core/lib/Drupal/Core/Database/Statement.php, line 24

Namespace

Drupal\Core\Database
View source
class Statement extends \PDOStatement implements StatementInterface {
    
    /**
     * Reference to the database connection object for this statement.
     *
     * The name $dbh is inherited from \PDOStatement.
     *
     * @var \Drupal\Core\Database\Connection
     */
    public $dbh;
    
    /**
     * Is rowCount() execution allowed.
     *
     * @var bool
     */
    public $allowRowCount = FALSE;
    protected function __construct(Connection $dbh) {
        $this->dbh = $dbh;
        $this->setFetchMode(\PDO::FETCH_OBJ);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConnectionTarget() : string {
        return $this->connection
            ->getTarget();
    }
    
    /**
     * {@inheritdoc}
     */
    public function execute($args = [], $options = []) {
        if (isset($options['fetch'])) {
            if (is_string($options['fetch'])) {
                // \PDO::FETCH_PROPS_LATE tells __construct() to run before properties
                // are added to the object.
                $this->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, $options['fetch']);
            }
            else {
                $this->setFetchMode($options['fetch']);
            }
        }
        $logger = $this->dbh
            ->getLogger();
        if (!empty($logger)) {
            $query_start = microtime(TRUE);
        }
        $return = parent::execute($args);
        if (!empty($logger)) {
            $query_end = microtime(TRUE);
            $logger->log($this, $args, $query_end - $query_start, $query_start);
        }
        return $return;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getQueryString() {
        return $this->queryString;
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchCol($index = 0) {
        return $this->fetchAll(\PDO::FETCH_COLUMN, $index);
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchAllAssoc($key, $fetch = NULL) {
        $return = [];
        if (isset($fetch)) {
            if (is_string($fetch)) {
                $this->setFetchMode(\PDO::FETCH_CLASS, $fetch);
            }
            else {
                $this->setFetchMode($fetch);
            }
        }
        foreach ($this as $record) {
            $record_key = is_object($record) ? $record->{$key} : $record[$key];
            $return[$record_key] = $record;
        }
        return $return;
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchAllKeyed($key_index = 0, $value_index = 1) {
        $return = [];
        $this->setFetchMode(\PDO::FETCH_NUM);
        foreach ($this as $record) {
            $return[$record[$key_index]] = $record[$value_index];
        }
        return $return;
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchField($index = 0) {
        // Call \PDOStatement::fetchColumn to fetch the field.
        return $this->fetchColumn($index);
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchAssoc() {
        // Call \PDOStatement::fetch to fetch the row.
        return $this->fetch(\PDO::FETCH_ASSOC);
    }
    
    /**
     * {@inheritdoc}
     */
    public function rowCount() {
        // SELECT query should not use the method.
        if ($this->allowRowCount) {
            return parent::rowCount();
        }
        else {
            throw new RowCountException();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
        // Call \PDOStatement::setFetchMode to set fetch mode.
        // \PDOStatement is picky about the number of arguments in some cases so we
        // need to be pass the exact number of arguments we where given.
        switch (func_num_args()) {
            case 1:
                return parent::setFetchMode($mode);
            case 2:
                return parent::setFetchMode($mode, $a1);
            case 3:
            default:
                return parent::setFetchMode($mode, $a1, $a2);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
        // Call \PDOStatement::fetchAll to fetch all rows.
        // \PDOStatement is picky about the number of arguments in some cases so we
        // need to be pass the exact number of arguments we where given.
        switch (func_num_args()) {
            case 0:
                return parent::fetchAll();
            case 1:
                return parent::fetchAll($mode);
            case 2:
                return parent::fetchAll($mode, $column_index);
            case 3:
            default:
                return parent::fetchAll($mode, $column_index, $constructor_arguments);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Statement::$allowRowCount public property Is rowCount() execution allowed.
Statement::$dbh public property Reference to the database connection object for this statement.
Statement::execute public function Executes a prepared statement. Overrides StatementInterface::execute
Statement::fetchAll public function Returns an array containing all of the result set rows. Overrides StatementInterface::fetchAll
Statement::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides StatementInterface::fetchAllAssoc
Statement::fetchAllKeyed public function Returns the entire result set as a single associative array. Overrides StatementInterface::fetchAllKeyed
Statement::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides StatementInterface::fetchAssoc
Statement::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides StatementInterface::fetchCol
Statement::fetchField public function Returns a single field from the next record of a result set. Overrides StatementInterface::fetchField
Statement::getConnectionTarget public function
Statement::getQueryString public function Gets the query string of this statement. Overrides StatementInterface::getQueryString
Statement::rowCount public function Returns the number of rows matched by the last SQL statement. Overrides StatementInterface::rowCount
Statement::setFetchMode public function Sets the default fetch mode for this statement. Overrides StatementInterface::setFetchMode
Statement::__construct protected function
StatementInterface::fetch public function Fetches the next row from a result set. 3
StatementInterface::fetchObject public function Fetches the next row and returns it as an object. 3

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