1<?php 2 3namespace Illuminate\Database; 4 5use Illuminate\Support\Str; 6use PDOException; 7use Throwable; 8 9class QueryException extends PDOException 10{ 11 /** 12 * The SQL for the query. 13 * 14 * @var string 15 */ 16 protected $sql; 17 18 /** 19 * The bindings for the query. 20 * 21 * @var array 22 */ 23 protected $bindings; 24 25 /** 26 * Create a new query exception instance. 27 * 28 * @param string $sql 29 * @param array $bindings 30 * @param \Throwable $previous 31 * @return void 32 */ 33 public function __construct($sql, array $bindings, Throwable $previous) 34 { 35 parent::__construct('', 0, $previous); 36 37 $this->sql = $sql; 38 $this->bindings = $bindings; 39 $this->code = $previous->getCode(); 40 $this->message = $this->formatMessage($sql, $bindings, $previous); 41 42 if ($previous instanceof PDOException) { 43 $this->errorInfo = $previous->errorInfo; 44 } 45 } 46 47 /** 48 * Format the SQL error message. 49 * 50 * @param string $sql 51 * @param array $bindings 52 * @param \Throwable $previous 53 * @return string 54 */ 55 protected function formatMessage($sql, $bindings, Throwable $previous) 56 { 57 return $previous->getMessage().' (SQL: '.Str::replaceArray('?', $bindings, $sql).')'; 58 } 59 60 /** 61 * Get the SQL for the query. 62 * 63 * @return string 64 */ 65 public function getSql() 66 { 67 return $this->sql; 68 } 69 70 /** 71 * Get the bindings for the query. 72 * 73 * @return array 74 */ 75 public function getBindings() 76 { 77 return $this->bindings; 78 } 79} 80