Skip to content

Commit 012a257

Browse files
committed
Preserve aliases in UPDATE JOIN translation
Resolve unqualified UPDATE targets to their table-reference alias so joined targets are not repeated in SQLite FROM clauses. Fixes #461
1 parent 96db8f0 commit 012a257

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,9 +2148,17 @@ private function execute_update_statement( WP_Parser_Node $node ): void {
21482148
}
21492149

21502150
$matched_tables = array_merge( $matched_temporary_tables, $matched_persistent_tables );
2151-
$updates_multiple_tables = count( $matched_tables ) > 1;
2152-
if ( 1 === count( $matched_tables ) ) {
2153-
$table_or_alias = $matched_tables[0];
2151+
$matched_aliases = array_keys(
2152+
array_filter(
2153+
$table_alias_map,
2154+
function ( $data ) use ( $matched_tables ) {
2155+
return in_array( $data['table_name'], $matched_tables, true );
2156+
}
2157+
)
2158+
);
2159+
$updates_multiple_tables = count( $matched_aliases ) > 1;
2160+
if ( 1 === count( $matched_aliases ) ) {
2161+
$table_or_alias = $matched_aliases[0];
21542162
} else {
21552163
break;
21562164
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,70 @@ public function testUpdate(): void {
415415
);
416416
}
417417

418+
public function testUpdateJoinWithDerivedTablePreservesTargetAlias(): void {
419+
$sqlite_version = $this->driver->get_sqlite_version();
420+
if ( version_compare( $sqlite_version, '3.33.0', '<' ) ) {
421+
$this->markTestSkipped(
422+
sprintf( "SQLite version %s doesn't support UPDATE with FROM clause.", $sqlite_version )
423+
);
424+
return;
425+
}
426+
427+
$this->driver->query( 'CREATE TABLE actions (action_id INT, claim_id INT, status TEXT)' );
428+
$this->driver->query( "INSERT INTO actions VALUES (1, 0, 'pending'), (2, 0, 'pending'), (3, 0, 'complete'), (4, 0, 'canceled')" );
429+
430+
$statement = $this->driver->query(
431+
"UPDATE actions t1
432+
JOIN (
433+
SELECT action_id
434+
FROM actions
435+
WHERE claim_id = 0
436+
AND status = 'pending'
437+
ORDER BY action_id ASC
438+
LIMIT 1
439+
FOR UPDATE
440+
) t2 ON t1.action_id = t2.action_id
441+
SET claim_id = 99"
442+
);
443+
444+
$this->assertSame( 1, $statement->rowCount() );
445+
$queries = array_map(
446+
function ( $query ) {
447+
return trim( preg_replace( '/\s+/', ' ', $query ) );
448+
},
449+
array_column( $this->driver->get_last_sqlite_queries(), 'sql' )
450+
);
451+
$this->assertContains(
452+
"UPDATE `actions` AS `t1` SET `claim_id` = 99 FROM ( SELECT `action_id` FROM `actions` WHERE `claim_id` = 0 AND `status` = 'pending' ORDER BY `action_id` ASC LIMIT 1 ) AS `t2` WHERE `t1`.`action_id` = `t2`.`action_id`",
453+
$queries
454+
);
455+
$this->assertSame(
456+
array(
457+
array(
458+
'action_id' => '1',
459+
'claim_id' => '99',
460+
'status' => 'pending',
461+
),
462+
),
463+
$this->driver->query( 'SELECT * FROM actions WHERE claim_id = 99' )->fetchAll( PDO::FETCH_ASSOC )
464+
);
465+
$this->assertSame(
466+
array(
467+
array(
468+
'action_id' => '3',
469+
'claim_id' => '0',
470+
'status' => 'complete',
471+
),
472+
array(
473+
'action_id' => '4',
474+
'claim_id' => '0',
475+
'status' => 'canceled',
476+
),
477+
),
478+
$this->driver->query( "SELECT * FROM actions WHERE status IN ( 'complete', 'canceled' ) ORDER BY action_id" )->fetchAll( PDO::FETCH_ASSOC )
479+
);
480+
}
481+
418482
public function testDelete(): void {
419483
$this->assertQuery(
420484
'DELETE FROM `t`',

0 commit comments

Comments
 (0)