QueryException.php 1.6KB

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