function Connection::handleQueryException

Same name in this branch
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::handleQueryException()
Same name in other branches
  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()
  3. 9 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

2 calls to Connection::handleQueryException()
Connection::handleQueryException in core/lib/Drupal/Core/Database/Driver/sqlite/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.
1 method overrides Connection::handleQueryException()
Connection::handleQueryException in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Wraps and re-throws any PDO exception thrown by static::query().

File

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

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

protected function handleQueryException(\PDOException $e, $query, array $args = [], $options = []) {
    if ($options['throw_exception']) {
        // Wrap the exception in another exception, because PHP does not allow
        // overriding Exception::getMessage(). Its message is the extra database
        // debug information.
        $query_string = $query instanceof StatementInterface ? $query->getQueryString() : $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.