Skip to content

Commit 59cc7cb

Browse files
authored
Preserve aliases in UPDATE JOIN translation (#462)
## Summary - resolve unqualified UPDATE targets back to their unique source alias - preserve the target alias and exclude it from SQLite FROM clauses - cover the Action Scheduler derived-table claim query from #461 ## Testing - composer run test -- --filter WP_MySQL_On_SQLite_Translation_Tests - composer run test - composer run check-cs Fixes #461 ## AI assistance OpenAI model `openai/gpt-5.6-sol` via OpenCode analyzed the generic translator path, implemented the alias-resolution, and drafted the focused PDO regression coverage. Chris Huber reviews and owns the change.
1 parent 0c3e8f9 commit 59cc7cb

2 files changed

Lines changed: 82 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,10 +2216,24 @@ private function execute_update_statement( WP_Parser_Node $node ): void {
22162216
)->fetchAll( PDO::FETCH_COLUMN );
22172217
}
22182218

2219-
$matched_tables = array_merge( $matched_temporary_tables, $matched_persistent_tables );
2220-
$updates_multiple_tables = count( $matched_tables ) > 1;
2221-
if ( 1 === count( $matched_tables ) ) {
2222-
$table_or_alias = $matched_tables[0];
2219+
$matched_tables = array_merge( $matched_temporary_tables, $matched_persistent_tables );
2220+
$matched_aliases = array();
2221+
foreach ( $table_alias_map as $alias => $data ) {
2222+
// Derived tables do not have a table name.
2223+
if ( null === $data['table_name'] ) {
2224+
continue;
2225+
}
2226+
2227+
foreach ( $matched_tables as $matched_table ) {
2228+
if ( 0 === strcasecmp( $data['table_name'], $matched_table ) ) {
2229+
$matched_aliases[] = $alias;
2230+
break;
2231+
}
2232+
}
2233+
}
2234+
$updates_multiple_tables = count( $matched_aliases ) > 1;
2235+
if ( 1 === count( $matched_aliases ) ) {
2236+
$table_or_alias = $matched_aliases[0];
22232237
} else {
22242238
break;
22252239
}

‎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)