Skip to content

Commit 1b1a0be

Browse files
alex02JanJakes
andauthored
Report the right column name when an INSERT or REPLACE names an unknown column (#634)
Fixes #633. array_diff() keeps the keys of its first argument, so when the unknown column is not the first one in the column list, $unknown_columns has no key 0: PHP warns "Undefined array key 0" and the error says Unknown column ''. Using reset( $unknown_columns ) reports the first unknown column, as MySQL does, and adds no work to successful inserts. Tests: testInsertErrors() and testInsertErrorsInNonStrictMode() now also cover an unknown column after an existing one, the INSERT ... SET form and REPLACE. Both fail without the fix ("Undefined array key 0") and pass with it; the full mysql-on-sqlite suite passes (1822 tests). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected the missing-column name reported for some `INSERT` and `REPLACE` statements. The error now identifies the first missing column in the statement, including when valid columns appear before missing ones. This applies to column-list and `INSERT ... SET` forms, in both strict and non-strict SQL modes. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent 973810c commit 1b1a0be

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6144,7 +6144,7 @@ private function translate_insert_or_replace_body(
61446144
throw $this->new_driver_exception(
61456145
sprintf(
61466146
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '%s' in 'field list'",
6147-
$unknown_columns[0]
6147+
reset( $unknown_columns )
61486148
),
61496149
'42S22'
61506150
);

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17695,6 +17695,30 @@ public function testInsertErrors(): void {
1769517695
'INSERT INTO t (missing_column) VALUES (1)',
1769617696
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
1769717697
);
17698+
17699+
// Missing column after an existing one.
17700+
$this->assertQueryError(
17701+
'INSERT INTO t (value, missing_column) VALUES (1, 2)',
17702+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17703+
);
17704+
17705+
// Multiple missing columns. The first one is reported.
17706+
$this->assertQueryError(
17707+
'INSERT INTO t (value, missing_b, missing_a) VALUES (1, 2, 3)',
17708+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_b' in 'field list'"
17709+
);
17710+
17711+
// Missing column in the SET form.
17712+
$this->assertQueryError(
17713+
'INSERT INTO t SET value = 1, missing_column = 2',
17714+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17715+
);
17716+
17717+
// Missing column in REPLACE.
17718+
$this->assertQueryError(
17719+
'REPLACE INTO t (value, missing_column) VALUES (1, 2)',
17720+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17721+
);
1769817722
}
1769917723

1770017724
public function testInsertErrorsInNonStrictMode(): void {
@@ -17712,6 +17736,30 @@ public function testInsertErrorsInNonStrictMode(): void {
1771217736
'INSERT INTO t (missing_column) VALUES (1)',
1771317737
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
1771417738
);
17739+
17740+
// Missing column after an existing one.
17741+
$this->assertQueryError(
17742+
'INSERT INTO t (value, missing_column) VALUES (1, 2)',
17743+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17744+
);
17745+
17746+
// Multiple missing columns. The first one is reported.
17747+
$this->assertQueryError(
17748+
'INSERT INTO t (value, missing_b, missing_a) VALUES (1, 2, 3)',
17749+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_b' in 'field list'"
17750+
);
17751+
17752+
// Missing column in the SET form.
17753+
$this->assertQueryError(
17754+
'INSERT INTO t SET value = 1, missing_column = 2',
17755+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17756+
);
17757+
17758+
// Missing column in REPLACE.
17759+
$this->assertQueryError(
17760+
'REPLACE INTO t (value, missing_column) VALUES (1, 2)',
17761+
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'missing_column' in 'field list'"
17762+
);
1771517763
}
1771617764

1771717765
public function testUpdateErrors(): void {

0 commit comments

Comments
 (0)