Skip to content

Commit 9a8d2ba

Browse files
committed
Implement PDO connection error reporting
Track driver diagnostics and honor PDO::ATTR_ERRMODE for query() and exec() while keeping the underlying SQLite connection in exception mode.
1 parent 29a107c commit 9a8d2ba

2 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* PDO uses camel case naming, enable non-snake case:
88
* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
9+
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
910
*
1011
* PDO uses $string as a parameter name, enable it:
1112
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
@@ -461,6 +462,30 @@ class WP_MySQL_On_SQLite extends PDO {
461462
*/
462463
private $connection;
463464

465+
/**
466+
* Caller-visible PDO error mode.
467+
*
468+
* The underlying SQLite connection always uses exceptions so internal
469+
* operations can handle errors reliably.
470+
*
471+
* @var int
472+
*/
473+
private $error_mode = PDO::ERRMODE_EXCEPTION;
474+
475+
/**
476+
* SQLSTATE associated with the last PDO operation.
477+
*
478+
* @var string|null
479+
*/
480+
private $error_code;
481+
482+
/**
483+
* Error information associated with the last PDO operation.
484+
*
485+
* @var array
486+
*/
487+
private $error_info = array( '', null, null );
488+
464489
/**
465490
* ID generated by the last user-issued INSERT or REPLACE statement.
466491
*
@@ -995,6 +1020,8 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
9951020

9961021
$stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $query, $this->last_affected_rows );
9971022
$stmt->setFetchMode( $fetch_mode, ...$fetch_mode_args );
1023+
$this->error_code = '00000';
1024+
$this->error_info = array( '00000', null, null );
9981025
return $stmt;
9991026
} catch ( Throwable $e ) {
10001027
try {
@@ -1003,12 +1030,14 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10031030
} catch ( Throwable $rollback_exception ) {
10041031
// Ignore rollback errors.
10051032
}
1006-
if ( $e instanceof WP_MySQL_On_SQLite_Exception ) {
1007-
throw $e;
1008-
} elseif ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
1009-
throw $this->convert_information_schema_exception( $e );
1033+
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
1034+
$e = $this->convert_information_schema_exception( $e );
1035+
}
1036+
if ( ! ( $e instanceof WP_MySQL_On_SQLite_Exception ) ) {
1037+
$e = $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
10101038
}
1011-
throw $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
1039+
1040+
return $this->handle_pdo_error( $e );
10121041
} finally {
10131042
// A query that doesn't return any rows or fails sets found rows to 0.
10141043
if ( ! $this->is_readonly || isset( $e ) ) {
@@ -1025,6 +1054,9 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10251054
#[ReturnTypeWillChange]
10261055
public function exec( $query ) {
10271056
$stmt = $this->query( $query );
1057+
if ( false === $stmt ) {
1058+
return false;
1059+
}
10281060
return $stmt->rowCount();
10291061
}
10301062

@@ -1039,6 +1071,24 @@ public function lastInsertId( $name = null ) {
10391071
return $this->last_insert_id;
10401072
}
10411073

1074+
/**
1075+
* PDO API: Fetch the SQLSTATE associated with the last operation.
1076+
*
1077+
* @return string|null The SQLSTATE error code, or null when unavailable.
1078+
*/
1079+
public function errorCode(): ?string {
1080+
return $this->error_code;
1081+
}
1082+
1083+
/**
1084+
* PDO API: Fetch error information associated with the last operation.
1085+
*
1086+
* @return array Error information from the last PDO operation.
1087+
*/
1088+
public function errorInfo(): array {
1089+
return $this->error_info;
1090+
}
1091+
10421092
/**
10431093
* PDO API: Quote a string for use in a MySQL query.
10441094
*
@@ -1176,6 +1226,16 @@ public function inTransaction(): bool {
11761226
* @return bool True on success, false on failure.
11771227
*/
11781228
public function setAttribute( $attribute, $value ): bool {
1229+
if ( PDO::ATTR_ERRMODE === $attribute ) {
1230+
$pdo = $this->connection->get_pdo();
1231+
$result = $pdo->setAttribute( $attribute, $value );
1232+
if ( ! $result ) {
1233+
return false;
1234+
}
1235+
$this->error_mode = $pdo->getAttribute( PDO::ATTR_ERRMODE );
1236+
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
1237+
return true;
1238+
}
11791239
return $this->connection->get_pdo()->setAttribute( $attribute, $value );
11801240
}
11811241

@@ -1191,6 +1251,9 @@ public function setAttribute( $attribute, $value ): bool {
11911251
*/
11921252
#[ReturnTypeWillChange]
11931253
public function getAttribute( $attribute ) {
1254+
if ( PDO::ATTR_ERRMODE === $attribute ) {
1255+
return $this->error_mode;
1256+
}
11941257
return $this->connection->get_pdo()->getAttribute( $attribute );
11951258
}
11961259

@@ -1516,6 +1579,26 @@ public function execute_sqlite_query( string $sql, array $params = array() ): PD
15161579
return $this->connection->query( $sql, $params );
15171580
}
15181581

1582+
/**
1583+
* Record and report a PDO operation error according to the configured mode.
1584+
*
1585+
* @param WP_MySQL_On_SQLite_Exception $exception The operation error.
1586+
* @return false Always false when the error is not thrown.
1587+
* @throws WP_MySQL_On_SQLite_Exception When exception mode is enabled.
1588+
*/
1589+
private function handle_pdo_error( WP_MySQL_On_SQLite_Exception $exception ): bool {
1590+
$this->error_info = $exception->errorInfo;
1591+
$this->error_code = $this->error_info[0];
1592+
1593+
if ( PDO::ERRMODE_EXCEPTION === $this->error_mode ) {
1594+
throw $exception;
1595+
}
1596+
if ( PDO::ERRMODE_WARNING === $this->error_mode ) {
1597+
trigger_error( $exception->getMessage(), E_USER_WARNING );
1598+
}
1599+
return false;
1600+
}
1601+
15191602
/**
15201603
* Translate and execute a MySQL query in SQLite.
15211604
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,54 @@ public function test_last_insert_id(): void {
422422
$this->assertSame( '1', $this->driver->lastInsertId() );
423423
}
424424

425+
public function test_connection_error_information(): void {
426+
$this->assertNull( $this->driver->errorCode() );
427+
$this->assertSame( array( '', null, null ), $this->driver->errorInfo() );
428+
429+
$this->driver->query( 'SELECT 1' );
430+
431+
$this->assertSame( '00000', $this->driver->errorCode() );
432+
$this->assertSame(
433+
array( '00000', null, null ),
434+
$this->driver->errorInfo()
435+
);
436+
}
437+
438+
public function test_silent_error_mode(): void {
439+
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
440+
441+
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
442+
$this->assertSame( 'HY000', $this->driver->errorCode() );
443+
$this->assertSame( 1, $this->driver->errorInfo()[1] );
444+
$this->assertSame( 'no such table: missing_table', $this->driver->errorInfo()[2] );
445+
$this->assertFalse( $this->driver->exec( 'SELECT * FROM missing_table' ) );
446+
447+
$this->assertInstanceOf( PDOStatement::class, $this->driver->query( 'SELECT 1' ) );
448+
$this->assertSame( '00000', $this->driver->errorCode() );
449+
}
450+
451+
public function test_warning_error_mode(): void {
452+
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
453+
$warning = null;
454+
set_error_handler(
455+
function ( $level, $message ) use ( &$warning ) {
456+
if ( E_USER_WARNING === $level ) {
457+
$warning = $message;
458+
return true;
459+
}
460+
return false;
461+
}
462+
);
463+
464+
try {
465+
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
466+
} finally {
467+
restore_error_handler();
468+
}
469+
470+
$this->assertStringContainsString( 'no such table: missing_table', $warning );
471+
}
472+
425473
public function test_quote_matches_mysql_escaping(): void {
426474
$backslash = chr( 92 );
427475
$value = chr( 0 ) . "\n\r{$backslash}'\"" . chr( 26 ) . "\tƮềʂᴛ🙂";

0 commit comments

Comments
 (0)