@@ -4417,6 +4417,17 @@ private function translate_insert_or_replace_body(
44174417 array ( $ database , $ table_name )
44184418 )->fetchAll ( PDO ::FETCH_ASSOC );
44194419
4420+ // Check if the table exists.
4421+ if ( 0 === count ( $ columns ) ) {
4422+ throw $ this ->new_driver_exception (
4423+ sprintf (
4424+ "SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exists " ,
4425+ $ table_name
4426+ ),
4427+ '42S02 '
4428+ );
4429+ }
4430+
44204431 // Get a list of columns that are targeted by the INSERT or REPLACE query.
44214432 // This is either an explicit column list, or all columns of the table.
44224433 $ insert_list = array ();
@@ -4442,6 +4453,18 @@ private function translate_insert_or_replace_body(
44424453 }
44434454 }
44444455
4456+ // Check if all listed columns exist.
4457+ $ unkwnown_columns = array_diff ( $ insert_list , array_column ( $ columns , 'COLUMN_NAME ' ) );
4458+ if ( count ( $ unkwnown_columns ) > 0 ) {
4459+ throw $ this ->new_driver_exception (
4460+ sprintf (
4461+ "SQLSTATE[42S22]: Column not found: 1054 Unknown column '%s' in 'field list' " ,
4462+ $ unkwnown_columns [0 ]
4463+ ),
4464+ '42S22 '
4465+ );
4466+ }
4467+
44454468 // Prepare a helper map of columns that are included in the INSERT list.
44464469 $ insert_map = array_combine ( $ insert_list , $ insert_list );
44474470
@@ -4617,7 +4640,19 @@ private function translate_update_list( string $table_name, WP_Parser_Node $node
46174640 ' ,
46184641 array ( $ database , $ table_name )
46194642 )->fetchAll ( PDO ::FETCH_ASSOC );
4620- $ column_map = array_combine ( array_column ( $ columns , 'COLUMN_NAME ' ), $ columns );
4643+
4644+ // Check if the table exists.
4645+ if ( 0 === count ( $ columns ) ) {
4646+ throw $ this ->new_driver_exception (
4647+ sprintf (
4648+ "SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exists " ,
4649+ $ table_name
4650+ ),
4651+ '42S02 '
4652+ );
4653+ }
4654+
4655+ $ column_map = array_combine ( array_column ( $ columns , 'COLUMN_NAME ' ), $ columns );
46214656
46224657 // Translate the UPDATE list, emulating IMPLICIT DEFAULTs for NULL values.
46234658 $ fragment = '' ;
@@ -4628,7 +4663,17 @@ private function translate_update_list( string $table_name, WP_Parser_Node $node
46284663
46294664 // Get column info.
46304665 $ column_name = $ this ->unquote_sqlite_identifier ( $ this ->translate ( end ( $ column_ref_parts ) ) );
4631- $ column_info = $ column_map [ strtolower ( $ column_name ) ];
4666+ $ column_info = $ column_map [ strtolower ( $ column_name ) ] ?? null ;
4667+ if ( ! $ column_info ) {
4668+ throw $ this ->new_driver_exception (
4669+ sprintf (
4670+ "SQLSTATE[42S22]: Column not found: 1054 Unknown column '%s' in 'field list' " ,
4671+ $ column_name
4672+ ),
4673+ '42S22 '
4674+ );
4675+ }
4676+
46324677 $ data_type = $ column_info ['DATA_TYPE ' ];
46334678 $ is_nullable = 'YES ' === $ column_info ['IS_NULLABLE ' ];
46344679 $ default = $ column_info ['COLUMN_DEFAULT ' ];
0 commit comments