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