You are here

public function Connection::query in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  2. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::query()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\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 PDO prepared statements.

Parameters

string|\Drupal\Core\Database\StatementInterface|\PDOStatement $query: The query to execute. This is a string containing an SQL query with placeholders. (deprecated) An already-prepared instance of StatementInterface or of \PDOStatement may also be passed in order to allow calling code to manually bind variables to a query. In such cases, the content of the $args array will be ignored. It is extremely rare that module code will need to pass a statement object to this method. It is used primarily for database drivers for databases that require special LOB field handling.

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 affected by the query (not the number matched).
  • 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

Overrides Connection::query

See also

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

3 calls to Connection::query()
Connection::nextId in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Retrieve a the next id in a sequence.
Connection::queryRange in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Runs a limited-range query on this database object.
Connection::queryTemporary in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Runs a SELECT query and stores its results in a temporary table.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php, line 155

Class

Connection
PostgreSQL implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function query($query, array $args = [], $options = []) {
  $options += $this
    ->defaultOptions();

  // The PDO PostgreSQL driver has a bug which doesn't type cast booleans
  // correctly when parameters are bound using associative arrays.
  // @see http://bugs.php.net/bug.php?id=48383
  foreach ($args as &$value) {
    if (is_bool($value)) {
      $value = (int) $value;
    }
  }

  // We need to wrap queries with a savepoint if:
  // - Currently in a transaction.
  // - A 'mimic_implicit_commit' does not exist already.
  // - The query is not a savepoint query.
  $wrap_with_savepoint = $this
    ->inTransaction() && !isset($this->transactionLayers['mimic_implicit_commit']) && !(is_string($query) && (stripos($query, 'ROLLBACK TO SAVEPOINT ') === 0 || stripos($query, 'RELEASE SAVEPOINT ') === 0 || stripos($query, 'SAVEPOINT ') === 0));
  if ($wrap_with_savepoint) {

    // Create a savepoint so we can rollback a failed query. This is so we can
    // mimic MySQL and SQLite transactions which don't fail if a single query
    // fails. This is important for tables that are created on demand. For
    // example, \Drupal\Core\Cache\DatabaseBackend.
    $this
      ->addSavepoint();
    try {
      $return = parent::query($query, $args, $options);
      $this
        ->releaseSavepoint();
    } catch (\Exception $e) {
      $this
        ->rollbackSavepoint();
      throw $e;
    }
  }
  else {
    $return = parent::query($query, $args, $options);
  }
  return $return;
}