function Connection::handleQueryException

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::handleQueryException()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::handleQueryException()
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::handleQueryException()
  2. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::handleQueryException()

Wraps and re-throws any PDO exception thrown by static::query().

Parameters

\PDOException $e: The exception thrown by static::query().

$query: The query executed by static::query().

array $args: An array of arguments for the prepared statement.

array $options: An associative array of options to control how the query is run.

Return value

\Drupal\Core\Database\StatementInterface|int|null Most database drivers will return NULL when a PDO exception is thrown for a query, but some of them may need to re-run the query, so they can also return a \Drupal\Core\Database\StatementInterface object or an integer.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper

\Drupal\Core\Database\IntegrityConstraintViolationException

Deprecated

in drupal:9.2.0 and is removed from drupal:10.0.0. Get a handler through $this->exceptionHandler() instead, and use one of its methods.

See also

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

3 calls to Connection::handleQueryException()
Connection::handleQueryException in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Wraps and re-throws any PDO exception thrown by static::query().
Connection::handleQueryException in core/modules/mysql/src/Driver/Database/mysql/Connection.php
Wraps and re-throws any PDO exception thrown by static::query().
Connection::query in core/lib/Drupal/Core/Database/Connection.php
Executes a query string against the database.
2 methods override Connection::handleQueryException()
Connection::handleQueryException in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Wraps and re-throws any PDO exception thrown by static::query().
Connection::handleQueryException in core/modules/mysql/src/Driver/Database/mysql/Connection.php
Wraps and re-throws any PDO exception thrown by static::query().

File

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

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

protected function handleQueryException(\PDOException $e, $query, array $args = [], $options = []) {
    @trigger_error('Connection::handleQueryException() is deprecated in drupal:9.2.0 and is removed in drupal:10.0.0. Get a handler through $this->exceptionHandler() instead, and use one of its methods. See https://www.drupal.org/node/3187222', E_USER_DEPRECATED);
    if ($options['throw_exception'] ?? TRUE) {
        // Wrap the exception in another exception, because PHP does not allow
        // overriding Exception::getMessage(). Its message is the extra database
        // debug information.
        // @todo in Drupal 10, remove checking if $query is a statement object.
        // @see https://www.drupal.org/node/3154439
        if ($query instanceof StatementInterface) {
            $query_string = $query->getQueryString();
        }
        elseif ($query instanceof \PDOStatement) {
            $query_string = $query->queryString;
        }
        else {
            $query_string = $query;
        }
        $message = $e->getMessage() . ": " . $query_string . "; " . print_r($args, TRUE);
        // Match all SQLSTATE 23xxx errors.
        if (substr($e->getCode(), -6, -3) == '23') {
            $exception = new IntegrityConstraintViolationException($message, $e->getCode(), $e);
        }
        else {
            $exception = new DatabaseExceptionWrapper($message, 0, $e);
        }
        throw $exception;
    }
    return NULL;
}

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