You are here

public function Connection::ThrowPdoException in Drupal driver for SQL Server and SQL Azure 8.2

This is a helper method to rethrow an Exception if the execution of a PDOStatement fails.

Sometimes, as a result of a PDO Statement execution error the error itself will be found in the connection and no in the statement.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/PDO/Connection.php, line 294

Class

Connection

Namespace

Drupal\Driver\Database\sqlsrv\PDO

Code

public function ThrowPdoException(Statement &$statement = null, \PDOException $e = null) {

  // This is what a SQL Server PDO "no error" looks like.
  $null_error = array(
    0 => '00000',
    1 => null,
    2 => null,
  );
  $error_info_connection = $this
    ->errorInfo();
  if ($error_info_connection == $null_error && $e !== null) {
    throw $e;
  }
  $error_info_statement = !empty($statement) ? $statement
    ->errorInfo() : $null_error;

  // TODO: Concatenate error information when both connection
  // and statement error info are valid.
  // We rebuild a message formatted in the same way as PDO.
  $error_info = $error_info_connection === $null_error ? $error_info_statement : $error_info_connection;
  $code = $e && is_numeric($e
    ->getCode()) ? $e
    ->getCode() : 0;
  $exception = new PDOException("SQLSTATE[" . $error_info[0] . "]: General error " . $error_info[1] . ": " . $error_info[2], $code, $e);
  $exception->errorInfo = $error_info;
  unset($statement);
  throw $exception;
}