@@ -1191,11 +1191,15 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11911191 if ( $ e instanceof WP_SQLite_Information_Schema_Exception ) {
11921192 $ e = $ this ->convert_information_schema_exception ( $ e );
11931193 }
1194- if ( ! ( $ e instanceof WP_MySQL_On_SQLite_Exception ) ) {
1195- $ e = $ this ->new_driver_exception ( $ e ->getMessage (), $ e ->getCode (), $ e );
1194+ if ( $ e instanceof WP_MySQL_On_SQLite_Exception ) {
1195+ $ driver_exception = $ e ;
1196+ } elseif ( $ e instanceof PDOException ) {
1197+ $ driver_exception = $ this ->convert_sqlite_exception ( $ e );
1198+ } else {
1199+ $ driver_exception = $ this ->new_driver_exception ( $ e ->getMessage (), $ e ->getCode (), $ e );
11961200 }
11971201
1198- return $ this ->handle_pdo_error ( $ e );
1202+ return $ this ->handle_pdo_error ( $ driver_exception );
11991203 } finally {
12001204 // A query that doesn't return any rows or fails sets found rows to 0.
12011205 if ( ! $ this ->is_readonly || isset ( $ e ) ) {
@@ -2192,9 +2196,9 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21922196 sprintf ( 'SELECT 1 FROM %s LIMIT 0 ' , $ table_name )
21932197 );
21942198 } catch ( PDOException $ e ) {
2195- throw $ this ->new_driver_exception (
2196- sprintf ( " Table '%s.%s' doesn't exist " , $ this ->db_name , $ table_name ),
2197- ' 42S02 '
2199+ throw $ this ->new_table_not_found_exception (
2200+ sprintf ( '%s.%s ' , $ this ->db_name , $ table_name ),
2201+ $ e
21982202 );
21992203 }
22002204 }
@@ -3650,10 +3654,7 @@ private function execute_show_columns_statement( WP_Parser_Node $node ): void {
36503654 )->fetchColumn ();
36513655
36523656 if ( ! $ table_exists ) {
3653- throw $ this ->new_driver_exception (
3654- sprintf ( "Table '%s.%s' doesn't exist " , $ database , $ table_name ),
3655- '42S02 '
3656- );
3657+ throw $ this ->new_table_not_found_exception ( sprintf ( '%s.%s ' , $ database , $ table_name ) );
36573658 }
36583659
36593660 // LIKE and WHERE clauses.
@@ -4072,7 +4073,9 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
40724073 );
40734074 }
40744075 } catch ( PDOException $ e ) {
4075- if ( 'HY000 ' === $ e ->getCode () ) {
4076+ if ( '42S02 ' === $ e ->getCode () && isset ( $ e ->errorInfo [2 ] ) ) {
4077+ $ errors = array ( $ e ->errorInfo [2 ] );
4078+ } elseif ( 'HY000 ' === $ e ->getCode () ) {
40764079 $ errors = array ( "Table ' $ table_name' doesn't exist " );
40774080 } else {
40784081 $ errors = array ( $ e ->getMessage () );
@@ -5774,13 +5777,7 @@ private function translate_insert_or_replace_body(
57745777
57755778 // Check if the table exists.
57765779 if ( 0 === count ( $ columns ) ) {
5777- throw $ this ->new_driver_exception (
5778- sprintf (
5779- "SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exist " ,
5780- $ table_name
5781- ),
5782- '42S02 '
5783- );
5780+ throw $ this ->new_table_not_found_exception ( $ table_name );
57845781 }
57855782
57865783 // Get a list of columns that are targeted by the INSERT or REPLACE query.
@@ -6088,13 +6085,7 @@ private function translate_update_list( string $table_name, WP_Parser_Node $pare
60886085
60896086 // Check if the table exists.
60906087 if ( 0 === count ( $ columns ) ) {
6091- throw $ this ->new_driver_exception (
6092- sprintf (
6093- "SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exist " ,
6094- $ table_name
6095- ),
6096- '42S02 '
6097- );
6088+ throw $ this ->new_table_not_found_exception ( $ table_name );
60986089 }
60996090
61006091 $ column_map = array_combine ( array_column ( $ columns , 'COLUMN_NAME ' ), $ columns );
@@ -6833,10 +6824,7 @@ private function get_sqlite_create_table_statement(
68336824 )->fetch ( PDO ::FETCH_ASSOC );
68346825
68356826 if ( false === $ table_info ) {
6836- throw $ this ->new_driver_exception (
6837- sprintf ( "Table '%s' doesn't exist " , $ table_name ),
6838- '42S02 '
6839- );
6827+ throw $ this ->new_table_not_found_exception ( $ table_name );
68406828 }
68416829
68426830 // 2. Get column info.
@@ -7808,6 +7796,26 @@ private function new_not_supported_exception( string $cause ): WP_MySQL_On_SQLit
78087796 );
78097797 }
78107798
7799+ /**
7800+ * Create a MySQL-compatible table-not-found exception.
7801+ *
7802+ * @param string $table_name The missing table name.
7803+ * @param Throwable|null $previous The previous exception.
7804+ * @return WP_MySQL_On_SQLite_Exception
7805+ */
7806+ private function new_table_not_found_exception (
7807+ string $ table_name ,
7808+ ?Throwable $ previous = null
7809+ ): WP_MySQL_On_SQLite_Exception {
7810+ $ driver_message = sprintf ( "Table '%s' doesn't exist " , $ table_name );
7811+ return $ this ->new_driver_exception (
7812+ 'SQLSTATE[42S02]: Base table or view not found: 1146 ' . $ driver_message ,
7813+ '42S02 ' ,
7814+ $ previous ,
7815+ array ( '42S02 ' , 1146 , $ driver_message )
7816+ );
7817+ }
7818+
78117819 /**
78127820 * Create a MySQL-compatible exception for an invalid SQL mode value.
78137821 *
@@ -7836,6 +7844,25 @@ private function new_access_denied_to_information_schema_exception(): WP_MySQL_O
78367844 );
78377845 }
78387846
7847+ /**
7848+ * Convert a SQLite PDO exception to a MySQL-on-SQLite driver exception.
7849+ *
7850+ * @param PDOException $e The SQLite PDO exception.
7851+ * @return WP_MySQL_On_SQLite_Exception
7852+ */
7853+ private function convert_sqlite_exception ( PDOException $ e ): WP_MySQL_On_SQLite_Exception {
7854+ $ error_info = is_array ( $ e ->errorInfo ) ? $ e ->errorInfo : array ();
7855+ $ sqlstate = $ error_info [0 ] ?? null ;
7856+ $ error_message = $ error_info [2 ] ?? null ;
7857+
7858+ if ( 'HY000 ' === $ sqlstate && 0 === strpos ( $ error_message , 'no such table: ' ) ) {
7859+ $ table_name = substr ( $ error_message , strlen ( 'no such table: ' ) );
7860+ return $ this ->new_table_not_found_exception ( $ table_name , $ e );
7861+ }
7862+
7863+ return $ this ->new_driver_exception ( $ e ->getMessage (), $ e ->getCode (), $ e );
7864+ }
7865+
78397866 /**
78407867 * Convert an information schema exception to a MySQL-like driver exception.
78417868 *
0 commit comments