AuthorizationException.php
2 weeks ago
InvalidCastException.php
2 weeks ago
InvalidMiddlewareException.php
2 weeks ago
InvalidRoutActionException.php
2 weeks ago
InvalidValidationRuleException.php
2 weeks ago
MassAssignmentException.php
2 weeks ago
ModelNotFoundException.php
2 weeks ago
MultipleRecordsFoundException.php
2 weeks ago
NotFoundException.php
2 weeks ago
QueryException.php
2 weeks ago
RecordNotFoundException.php
2 weeks ago
UniqueConstraintViolationException.php
2 weeks ago
ValidationException.php
2 weeks ago
QueryException.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Exception thrown when a query fails. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Exceptions |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Exceptions; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Exception; |
| 14 | use Throwable; |
| 15 | class QueryException extends Exception |
| 16 | { |
| 17 | /** |
| 18 | * The sql. |
| 19 | * |
| 20 | * @var mixed |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | protected $sql; |
| 25 | /** |
| 26 | * The bindings. |
| 27 | * |
| 28 | * @var mixed |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | protected $bindings; |
| 33 | /** |
| 34 | * Create a new instance. |
| 35 | * |
| 36 | * @param mixed $sql The sql. |
| 37 | * @param array $bindings The bindings. |
| 38 | * @param Throwable $error The error. |
| 39 | * |
| 40 | * @return void |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | public function __construct($sql, array $bindings, Throwable $error) |
| 45 | { |
| 46 | $this->sql = $sql; |
| 47 | $this->bindings = $bindings; |
| 48 | $this->code = $error->getCode(); |
| 49 | $this->message = $this->format_message($sql, $bindings, $error); |
| 50 | } |
| 51 | /** |
| 52 | * Format message. |
| 53 | * |
| 54 | * @param mixed $sql The sql. |
| 55 | * @param mixed $bindings The bindings. |
| 56 | * @param Throwable $error The error. |
| 57 | * |
| 58 | * @return void |
| 59 | * |
| 60 | * @since 1.0.0 |
| 61 | */ |
| 62 | protected function format_message($sql, $bindings, Throwable $error) |
| 63 | { |
| 64 | return $error->getMessage() . ' (Connection: MySQL, SQL: ' . $this->prepare($sql, $bindings) . ')'; |
| 65 | } |
| 66 | /** |
| 67 | * Get the sql. |
| 68 | * |
| 69 | * @return mixed |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | public function get_sql() |
| 74 | { |
| 75 | return $this->sql; |
| 76 | } |
| 77 | /** |
| 78 | * Get the bindings. |
| 79 | * |
| 80 | * @return mixed |
| 81 | * |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | public function get_bindings() |
| 85 | { |
| 86 | return $this->bindings; |
| 87 | } |
| 88 | /** |
| 89 | * Prepare. |
| 90 | * |
| 91 | * @param mixed $sql The sql. |
| 92 | * @param mixed $bindings The bindings. |
| 93 | * |
| 94 | * @return void |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | protected function prepare($sql, $bindings) |
| 99 | { |
| 100 | $count = 1; |
| 101 | foreach ($bindings as $binding) { |
| 102 | $sql = \str_replace('?', $binding, $sql, $count); |
| 103 | } |
| 104 | return $sql; |
| 105 | } |
| 106 | } |
| 107 |