function Connection::query

Same name in this branch
  1. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
Same name and namespace in other branches
  1. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
  2. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::query()
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::query()
  5. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  6. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
  7. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()

Executes a query string against the database.

This method provides a central handler for the actual execution of every query. All queries executed by Drupal are executed as prepared statements.

Parameters

string $query: The query to execute. This is a string containing an SQL query with placeholders.

array $args: The associative array of arguments for the prepared statement.

array $options: An associative array of options to control how the query is run. The given options will be merged with self::defaultOptions(). See the documentation for self::defaultOptions() for details. Typically, $options['return'] will be set by a default or by a query builder, and should not be set by a user.

Return value

\Drupal\Core\Database\StatementInterface|int|string|null This method will return one of the following:

  • If either $options['return'] === self::RETURN_STATEMENT, or $options['return'] is not set (due to self::defaultOptions()), returns the executed statement.
  • If $options['return'] === self::RETURN_AFFECTED, returns the number of rows matched by the query (not the number affected).
  • If $options['return'] === self::RETURN_INSERT_ID, returns the generated insert ID of the last query as a string.
  • If $options['return'] === self::RETURN_NULL, returns NULL.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper

\Drupal\Core\Database\IntegrityConstraintViolationException

\InvalidArgumentException

See also

\Drupal\Core\Database\Connection::defaultOptions()

4 calls to Connection::query()
Connection::hasJson in core/lib/Drupal/Core/Database/Connection.php
Runs a simple query to validate json datatype support.
Connection::popCommittableTransactions in core/lib/Drupal/Core/Database/Connection.php
Commit all the transaction layers that can commit.
Connection::pushTransaction in core/lib/Drupal/Core/Database/Connection.php
Increases the depth of transaction nesting.
Connection::rollBack in core/lib/Drupal/Core/Database/Connection.php
Rolls back the transaction entirely or to a named savepoint.

File

core/lib/Drupal/Core/Database/Connection.php, line 835

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function query($query, array $args = [], $options = []) {
    assert(is_string($query), 'The \'$query\' argument to ' . __METHOD__ . '() must be a string');
    // Use default values if not already set.
    $options += $this->defaultOptions();
    if (isset($options['return'])) {
        @trigger_error('Passing "return" option to ' . __METHOD__ . '() is deprecated in drupal:9.4.0 and is removed in drupal:11.0.0. For data manipulation operations, use dynamic queries instead. See https://www.drupal.org/node/3185520', E_USER_DEPRECATED);
    }
    assert(!isset($options['target']), 'Passing "target" option to query() has no effect. See https://www.drupal.org/node/2993033');
    $this->expandArguments($query, $args);
    $stmt = $this->prepareStatement($query, $options);
    try {
        $stmt->execute($args, $options);
        // Depending on the type of query we may need to return a different value.
        // See DatabaseConnection::defaultOptions() for a description of each
        // value.
        // @todo the block below is deprecated and as of Drupal 11 will be
        //   removed, query() will only return a StatementInterface object.
        // @see https://www.drupal.org/project/drupal/issues/3256524
        switch ($options['return'] ?? Database::RETURN_STATEMENT) {
            case Database::RETURN_STATEMENT:
                return $stmt;
            // Database::RETURN_AFFECTED should not be used; enable row counting
            // by passing the appropriate argument to the constructor instead.
            // @see https://www.drupal.org/node/3186368
            case Database::RETURN_AFFECTED:
                $stmt->allowRowCount = TRUE;
                return $stmt->rowCount();
            case Database::RETURN_INSERT_ID:
                $sequence_name = $options['sequence_name'] ?? NULL;
                return $this->lastInsertId($sequence_name);
            case Database::RETURN_NULL:
                return NULL;
            default:
                throw new \PDOException('Invalid return directive: ' . $options['return']);
        }
    } catch (\Exception $e) {
        $this->exceptionHandler()
            ->handleExecutionException($e, $stmt, $args, $options);
    }
}

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