Skip to content

Commit fe1965d

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 87b17fa commit fe1965d

2 files changed

Lines changed: 138 additions & 5 deletions

File tree

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

Lines changed: 90 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
@@ -524,6 +525,30 @@ class WP_MySQL_On_SQLite extends PDO {
524525
*/
525526
private $connection;
526527

528+
/**
529+
* Caller-visible PDO error mode.
530+
*
531+
* The underlying SQLite connection always uses exceptions so internal
532+
* operations can handle errors reliably.
533+
*
534+
* @var int
535+
*/
536+
private $error_mode = PDO::ERRMODE_EXCEPTION;
537+
538+
/**
539+
* SQLSTATE associated with the last PDO operation.
540+
*
541+
* @var string|null
542+
*/
543+
private $error_code;
544+
545+
/**
546+
* Error information associated with the last PDO operation.
547+
*
548+
* @var array
549+
*/
550+
private $error_info = array( '', null, null );
551+
527552
/**
528553
* ID generated by the last user-issued INSERT or REPLACE statement.
529554
*
@@ -1052,6 +1077,8 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10521077

10531078
$stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $query, $this->last_affected_rows );
10541079
$stmt->setFetchMode( $fetch_mode, ...$fetch_mode_args );
1080+
$this->error_code = '00000';
1081+
$this->error_info = array( '00000', null, null );
10551082
return $stmt;
10561083
} catch ( Throwable $e ) {
10571084
try {
@@ -1060,12 +1087,14 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10601087
} catch ( Throwable $rollback_exception ) {
10611088
// Ignore rollback errors.
10621089
}
1063-
if ( $e instanceof WP_MySQL_On_SQLite_Exception ) {
1064-
throw $e;
1065-
} elseif ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
1066-
throw $this->convert_information_schema_exception( $e );
1090+
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
1091+
$e = $this->convert_information_schema_exception( $e );
10671092
}
1068-
throw $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
1093+
if ( ! ( $e instanceof WP_MySQL_On_SQLite_Exception ) ) {
1094+
$e = $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
1095+
}
1096+
1097+
return $this->handle_pdo_error( $e );
10691098
} finally {
10701099
// A query that doesn't return any rows or fails sets found rows to 0.
10711100
if ( ! $this->is_readonly || isset( $e ) ) {
@@ -1082,6 +1111,9 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10821111
#[ReturnTypeWillChange]
10831112
public function exec( $query ) {
10841113
$stmt = $this->query( $query );
1114+
if ( false === $stmt ) {
1115+
return false;
1116+
}
10851117
return $stmt->rowCount();
10861118
}
10871119

@@ -1116,6 +1148,24 @@ public function lastInsertId( $name = null ) {
11161148
return $this->last_insert_id;
11171149
}
11181150

1151+
/**
1152+
* PDO API: Fetch the SQLSTATE associated with the last operation.
1153+
*
1154+
* @return string|null The SQLSTATE error code, or null when unavailable.
1155+
*/
1156+
public function errorCode(): ?string {
1157+
return $this->error_code;
1158+
}
1159+
1160+
/**
1161+
* PDO API: Fetch error information associated with the last operation.
1162+
*
1163+
* @return array Error information from the last PDO operation.
1164+
*/
1165+
public function errorInfo(): array {
1166+
return $this->error_info;
1167+
}
1168+
11191169
/**
11201170
* PDO API: Quote a string for use in a MySQL query.
11211171
*
@@ -1256,6 +1306,17 @@ public function inTransaction(): bool {
12561306
* @return bool True on success, false on failure.
12571307
*/
12581308
public function setAttribute( $attribute, $value ): bool {
1309+
// Track the caller's error mode while keeping internal SQLite operations in exception mode.
1310+
if ( PDO::ATTR_ERRMODE === $attribute ) {
1311+
$pdo = $this->connection->get_pdo();
1312+
$result = $pdo->setAttribute( $attribute, $value );
1313+
if ( ! $result ) {
1314+
return false;
1315+
}
1316+
$this->error_mode = $pdo->getAttribute( PDO::ATTR_ERRMODE );
1317+
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
1318+
return true;
1319+
}
12591320
return $this->connection->get_pdo()->setAttribute( $attribute, $value );
12601321
}
12611322

@@ -1271,6 +1332,10 @@ public function setAttribute( $attribute, $value ): bool {
12711332
*/
12721333
#[ReturnTypeWillChange]
12731334
public function getAttribute( $attribute ) {
1335+
// Return the caller's error mode instead of the exception mode used internally.
1336+
if ( PDO::ATTR_ERRMODE === $attribute ) {
1337+
return $this->error_mode;
1338+
}
12741339
return $this->connection->get_pdo()->getAttribute( $attribute );
12751340
}
12761341

@@ -1601,6 +1666,26 @@ public function execute_sqlite_query( string $sql, array $params = array() ): PD
16011666
return $this->connection->query( $sql, $params );
16021667
}
16031668

1669+
/**
1670+
* Record and report a PDO operation error according to the configured mode.
1671+
*
1672+
* @param WP_MySQL_On_SQLite_Exception $exception The operation error.
1673+
* @return false Always false when the error is not thrown.
1674+
* @throws WP_MySQL_On_SQLite_Exception When exception mode is enabled.
1675+
*/
1676+
private function handle_pdo_error( WP_MySQL_On_SQLite_Exception $exception ): bool {
1677+
$this->error_info = $exception->errorInfo;
1678+
$this->error_code = $this->error_info[0];
1679+
1680+
if ( PDO::ERRMODE_EXCEPTION === $this->error_mode ) {
1681+
throw $exception;
1682+
}
1683+
if ( PDO::ERRMODE_WARNING === $this->error_mode ) {
1684+
trigger_error( $exception->getMessage(), E_USER_WARNING );
1685+
}
1686+
return false;
1687+
}
1688+
16041689
/**
16051690
* Translate and execute a MySQL query in SQLite.
16061691
*

‎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
@@ -435,6 +435,54 @@ public function test_last_insert_id_rejects_invalid_sequence_name(): void {
435435
$this->driver->lastInsertId( array() );
436436
}
437437

438+
public function test_connection_error_information(): void {
439+
$this->assertNull( $this->driver->errorCode() );
440+
$this->assertSame( array( '', null, null ), $this->driver->errorInfo() );
441+
442+
$this->driver->query( 'SELECT 1' );
443+
444+
$this->assertSame( '00000', $this->driver->errorCode() );
445+
$this->assertSame(
446+
array( '00000', null, null ),
447+
$this->driver->errorInfo()
448+
);
449+
}
450+
451+
public function test_silent_error_mode(): void {
452+
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
453+
454+
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
455+
$this->assertSame( 'HY000', $this->driver->errorCode() );
456+
$this->assertSame( 1, $this->driver->errorInfo()[1] );
457+
$this->assertSame( 'no such table: missing_table', $this->driver->errorInfo()[2] );
458+
$this->assertFalse( $this->driver->exec( 'SELECT * FROM missing_table' ) );
459+
460+
$this->assertInstanceOf( PDOStatement::class, $this->driver->query( 'SELECT 1' ) );
461+
$this->assertSame( '00000', $this->driver->errorCode() );
462+
}
463+
464+
public function test_warning_error_mode(): void {
465+
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
466+
$warning = null;
467+
set_error_handler(
468+
function ( $level, $message ) use ( &$warning ) {
469+
if ( E_USER_WARNING === $level ) {
470+
$warning = $message;
471+
return true;
472+
}
473+
return false;
474+
}
475+
);
476+
477+
try {
478+
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
479+
} finally {
480+
restore_error_handler();
481+
}
482+
483+
$this->assertStringContainsString( 'no such table: missing_table', $warning );
484+
}
485+
438486
public function test_quote_matches_mysql_escaping(): void {
439487
$backslash = chr( 92 );
440488
$value = chr( 0 ) . "\n\r{$backslash}'\"" . chr( 26 ) . "\tƮềʂᴛ🙂";

0 commit comments

Comments
 (0)