You are here

protected function Connection::handleQueryException in Drupal 9

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

Overrides Connection::handleQueryException

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

File

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

Class

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

Namespace

Drupal\Core\Database\Driver\mysql

Code

protected function handleQueryException(\PDOException $e, $query, array $args = [], $options = []) {

  // In case of attempted INSERT of a record with an undefined column and no
  // default value indicated in schema, MySql returns a 1364 error code.
  // Throw an IntegrityConstraintViolationException here like the other
  // drivers do, to avoid the parent class to throw a generic
  // DatabaseExceptionWrapper instead.
  if (!empty($e->errorInfo[1]) && $e->errorInfo[1] === 1364) {
    @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);
    $query_string = $query instanceof StatementInterface ? $query
      ->getQueryString() : $query;
    $message = $e
      ->getMessage() . ": " . $query_string . "; " . print_r($args, TRUE);
    throw new IntegrityConstraintViolationException($message, is_int($e
      ->getCode()) ? $e
      ->getCode() : 0, $e);
  }
  parent::handleQueryException($e, $query, $args, $options);
}