Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ON DUPLICATE KEY: Prevent adding a comma if key isn't in the first position #113

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/WP_SQLite_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 3 additions & 2 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
Expand Down
Loading