You are here

public function Connection::query in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  2. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::query()
  3. 8 core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\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 $query: The query to execute. In most cases this will be a string containing an SQL query with placeholders. An already-prepared instance of StatementInterface may also be passed in order to allow calling code to manually bind variables to a query. If a StatementInterface is passed, 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: An array of arguments for the prepared statement. If the prepared statement uses ? placeholders, this array must be an indexed array. If it contains named placeholders, it must be an associative array.

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 either $options['return'] === self::RETURN_NULL, or an exception occurs and $options['throw_exception'] evaluates to FALSE, returns NULL.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper

\Drupal\Core\Database\IntegrityConstraintViolationException

\InvalidArgumentException

Overrides Connection::query

See also

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

5 calls to Connection::query()
Connection::nextId in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
Retrieves an unique ID from a given sequence.
Connection::nextIdDelete in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
Connection::popCommittableTransactions in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
Overridden to work around issues to MySQL not supporting transactional DDL.
Connection::queryRange in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
Runs a limited-range query on this database object.
Connection::queryTemporary in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
Runs a SELECT query and stores its results in a temporary table.

File

core/lib/Drupal/Core/Database/Driver/mysql/Connection.php, line 355

Class

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

Namespace

Drupal\Core\Database\Driver\mysql

Code

public function query($query, array $args = [], $options = []) {
  try {
    return parent::query($query, $args, $options);
  } catch (DatabaseException $e) {
    if ($e
      ->getPrevious()->errorInfo[1] == 1153) {

      // If a max_allowed_packet error occurs the message length is truncated.
      // This should prevent the error from recurring if the exception is
      // logged to the database using dblog or the like.
      $message = Unicode::truncateBytes($e
        ->getMessage(), self::MIN_MAX_ALLOWED_PACKET);
      $e = new DatabaseExceptionWrapper($message, $e
        ->getCode(), $e
        ->getPrevious());
    }
    throw $e;
  }
}