diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 5ffad92..94c1e52 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -519,4 +519,43 @@ public function getSchemaBuilder() return $builder; } + + /** + * Run a SQL statement. + * + * @param string $query + * @param array $bindings + * @param \Closure $callback + * @return mixed + * + * @throws \Illuminate\Database\QueryException + */ + protected function runQueryCallback($query, $bindings, Closure $callback) + { + try { + $result = $callback($query, $bindings); + + if ($result instanceof \PDOStatement) { + $errorInfo = $result->errorInfo(); + if (isset($errorInfo[0]) && $errorInfo[0] !== '00000') { + $finalErrorMessage = sprintf( + 'SQLSTATE[%s] [%d] %s', + $errorInfo[0], + (int)$errorInfo[1], + trim(preg_replace(['/^\[\d+\]\s\(severity\s\d+\)\s/', '/\s+/'], ['', ' '], $errorInfo[2])) + ); + throw new \PDOException($finalErrorMessage, (int)$errorInfo[1]); + } + } + return $result; + + } catch (Throwable $e) { + throw new QueryException( + $this->getName(), + $query, + $this->prepareBindings($bindings), + $e + ); + } + } }