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 *
0 commit comments