sqlite-database-integration
/
wp-includes
/
database
/
sqlite
/
class-wp-mysql-on-sqlite-exception.php
class-wp-mysql-on-sqlite-exception.php in SQLite Database Integration 3.0.1, at wp-includes/database/sqlite/class-wp-mysql-on-sqlite-exception.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * PDO uses camel case naming, enable non-snake case: |
| 5 | * phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Exception raised by the MySQL-on-SQLite driver. |
| 10 | * |
| 11 | * Provides PDO-style error information and access to the driver that originated |
| 12 | * the exception. |
| 13 | */ |
| 14 | class WP_MySQL_On_SQLite_Exception extends PDOException { |
| 15 | /** |
| 16 | * The MySQL-on-SQLite driver that originated the exception. |
| 17 | * |
| 18 | * @var WP_MySQL_On_SQLite |
| 19 | */ |
| 20 | private $driver; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param WP_MySQL_On_SQLite $driver The MySQL-on-SQLite driver that originated the exception. |
| 26 | * @param string $message The exception message. |
| 27 | * @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE. |
| 28 | * @param Throwable|null $previous The previous throwable used for the exception chaining. |
| 29 | * @param array|null $error_info PDO-style error information. |
| 30 | */ |
| 31 | public function __construct( |
| 32 | WP_MySQL_On_SQLite $driver, |
| 33 | string $message, |
| 34 | $code = 0, |
| 35 | ?Throwable $previous = null, |
| 36 | ?array $error_info = null |
| 37 | ) { |
| 38 | parent::__construct( $message, 0, $previous ); |
| 39 | $this->code = $code; |
| 40 | $this->driver = $driver; |
| 41 | $this->errorInfo = $error_info ?? $this->create_error_info( $message, $code, $previous ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the MySQL-on-SQLite driver that originated the exception. |
| 46 | * |
| 47 | * @return WP_MySQL_On_SQLite The originating driver. |
| 48 | */ |
| 49 | public function get_driver(): WP_MySQL_On_SQLite { |
| 50 | return $this->driver; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create PDO-style error information from an originating exception or from |
| 55 | * the emulated driver error. |
| 56 | * |
| 57 | * @param string $message The exception message. |
| 58 | * @param int|string $code The exception code. |
| 59 | * @param Throwable|null $previous The previous throwable. |
| 60 | * @return array PDO-style error information. |
| 61 | */ |
| 62 | private function create_error_info( string $message, $code, ?Throwable $previous ): array { |
| 63 | if ( $previous instanceof PDOException && is_array( $previous->errorInfo ) ) { |
| 64 | return $previous->errorInfo; |
| 65 | } |
| 66 | |
| 67 | $sqlstate = is_string( $code ) && 5 === strlen( $code ) ? $code : 'HY000'; |
| 68 | return array( $sqlstate, 1105, $message ); |
| 69 | } |
| 70 | } |
| 71 |