diff --git a/tests/WP_SQLite_Query_Tests.php b/tests/WP_SQLite_Query_Tests.php index 059fed9..119f1f2 100644 --- a/tests/WP_SQLite_Query_Tests.php +++ b/tests/WP_SQLite_Query_Tests.php @@ -505,6 +505,21 @@ public function testRecoverSerialized() { $this->assertEquals( $obj, $unserialized ); } + public function testOnDuplicateKey() { + $this->assertQuery( + 'CREATE TABLE `test` ( + `id` INT PRIMARY KEY, + `text` VARCHAR(255), + );' + ); + // The order is deliberate to test that the query works with the keys in any order. + $this->assertQuery( + 'INSERT INTO test (`text`, `id`) + VALUES ("test", 1) + ON DUPLICATE KEY UPDATE `text` = "test1"' + ); + } + public function testShowColumns() { $query = 'SHOW COLUMNS FROM wp_posts'; diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 542ee6c..0558842 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -2803,9 +2803,10 @@ private function translate_on_duplicate_key( $table_name ) { $this->rewriter->add( new WP_SQLite_Token( '(', WP_SQLite_Token::TYPE_OPERATOR ) ); $max = count( $conflict_columns ); - foreach ( $conflict_columns as $i => $conflict_column ) { + $i = 0; + foreach ( $conflict_columns as $conflict_column ) { $this->rewriter->add( new WP_SQLite_Token( '"' . $conflict_column . '"', WP_SQLite_Token::TYPE_KEYWORD, WP_SQLite_Token::FLAG_KEYWORD_KEY ) ); - if ( $i !== $max - 1 ) { + if ( ++$i < $max ) { $this->rewriter->add( new WP_SQLite_Token( ',', WP_SQLite_Token::TYPE_OPERATOR ) ); $this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_WHITESPACE ) ); }