Skip to content

Commit 4b63633

Browse files
committed
Apply type casting and implicit defaults to ON DUPLICATE KEY UPDATE clause
1 parent d7c3309 commit 4b63633

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10522,6 +10522,25 @@ public function testCastValuesOnInsertInNonStrictMode(): void {
1052210522
$this->assertQuery( 'DROP TABLE t' );
1052310523
}
1052410524

10525+
public function testCastValuesOnDuplicateKeyUpdate(): void {
10526+
$this->assertQuery( 'CREATE TABLE t (value TEXT UNIQUE)' );
10527+
$this->assertQuery( "INSERT INTO t VALUES ('test')" );
10528+
10529+
// Ensure that type casting is applied to ON DUPLICATE KEY UPDATE clause.
10530+
$this->assertQuery( "INSERT INTO t VALUES ('test') ON DUPLICATE KEY UPDATE value = 0x61" );
10531+
$this->assertSame( 'a', $this->assertQuery( 'SELECT * FROM t' )[0]->value );
10532+
}
10533+
10534+
public function testCastValuesOnDuplicateKeyUpdateInNonStrictMode(): void {
10535+
$this->assertQuery( "SET SESSION sql_mode = ''" );
10536+
$this->assertQuery( 'CREATE TABLE t (value INT UNIQUE)' );
10537+
$this->assertQuery( "INSERT INTO t VALUES (123)" );
10538+
10539+
// Ensure that type casting is applied to ON DUPLICATE KEY UPDATE clause.
10540+
$this->assertQuery( "INSERT INTO t VALUES (123) ON DUPLICATE KEY UPDATE value = 'test'" );
10541+
$this->assertSame( '0', $this->assertQuery( 'SELECT * FROM t' )[0]->value );
10542+
}
10543+
1052510544
public function testCastValuesOnUpdate(): void {
1052610545
// INTEGER
1052710546
$this->assertQuery( 'CREATE TABLE t (value INT)' );

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,13 @@ private function execute_insert_or_replace_statement( WP_Parser_Node $node ): vo
15311531
$table_ref = $node->get_first_child_node( 'tableRef' );
15321532
$table_name = $this->unquote_sqlite_identifier( $this->translate( $table_ref ) );
15331533
$parts[] = $this->translate_insert_or_replace_body( $table_name, $child );
1534+
} elseif ( $is_node && 'insertUpdateList' === $child->rule_name ) {
1535+
// Translate "ON DUPLICATE KEY UPDATE" to "ON CONFLICT DO UPDATE SET".
1536+
$parts[] = 'ON CONFLICT DO UPDATE SET ';
1537+
$parts[] = $this->translate_update_list(
1538+
$table_name,
1539+
$child->get_first_child_node( 'updateList' )
1540+
);
15341541
} else {
15351542
$parts[] = $this->translate( $child );
15361543
}
@@ -3241,12 +3248,6 @@ private function translate( $node ): ?string {
32413248
return null;
32423249
}
32433250
return $this->translate_sequence( $node->get_children() );
3244-
case 'insertUpdateList':
3245-
// Translate "ON DUPLICATE KEY UPDATE" to "ON CONFLICT DO UPDATE SET".
3246-
return sprintf(
3247-
'ON CONFLICT DO UPDATE SET %s',
3248-
$this->translate( $node->get_first_child_node( 'updateList' ) )
3249-
);
32503251
case 'simpleExpr':
32513252
return $this->translate_simple_expr( $node );
32523253
case 'predicateOperations':

0 commit comments

Comments
 (0)