From 42e75c04b5482807bb8ad0280497e771f48760dd Mon Sep 17 00:00:00 2001 From: bgrgicak Date: Fri, 17 May 2024 13:49:38 +0200 Subject: [PATCH 1/2] Prevent adding a comma if key isn't in the first position --- tests/WP_SQLite_Query_Tests.php | 14 ++++++++++++++ wp-includes/sqlite/class-wp-sqlite-translator.php | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/WP_SQLite_Query_Tests.php b/tests/WP_SQLite_Query_Tests.php index 059fed9..8f12110 100644 --- a/tests/WP_SQLite_Query_Tests.php +++ b/tests/WP_SQLite_Query_Tests.php @@ -505,6 +505,20 @@ 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..7ec8c3f 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 ) ); } From 7973d6935087f73aea8a4e59adccf295fb3ae95d Mon Sep 17 00:00:00 2001 From: bgrgicak Date: Fri, 17 May 2024 13:58:25 +0200 Subject: [PATCH 2/2] Linter fixes --- tests/WP_SQLite_Query_Tests.php | 15 ++++++++------- wp-includes/sqlite/class-wp-sqlite-translator.php | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/WP_SQLite_Query_Tests.php b/tests/WP_SQLite_Query_Tests.php index 8f12110..119f1f2 100644 --- a/tests/WP_SQLite_Query_Tests.php +++ b/tests/WP_SQLite_Query_Tests.php @@ -506,16 +506,17 @@ public function testRecoverSerialized() { } public function testOnDuplicateKey() { - $this->assertQuery(" - CREATE TABLE `test` ( + $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'" + $this->assertQuery( + 'INSERT INTO test (`text`, `id`) + VALUES ("test", 1) + ON DUPLICATE KEY UPDATE `text` = "test1"' ); } diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 7ec8c3f..0558842 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -2803,7 +2803,7 @@ private function translate_on_duplicate_key( $table_name ) { $this->rewriter->add( new WP_SQLite_Token( '(', WP_SQLite_Token::TYPE_OPERATOR ) ); $max = count( $conflict_columns ); - $i = 0; + $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 ) {