Skip to content

Commit 759b9f2

Browse files
committed
Reset state for PDO transaction methods
Treat direct transaction calls as independent operations so insert IDs and SQLite query traces do not leak across operation boundaries.
1 parent e607fe5 commit 759b9f2

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ public function beginTransaction(): bool {
11931193
if ( $this->inTransaction() ) {
11941194
throw $this->new_driver_exception( 'There is already an active transaction' );
11951195
}
1196+
$this->flush();
11961197
$this->begin_user_transaction();
11971198
return true;
11981199
}
@@ -1206,6 +1207,7 @@ public function commit(): bool {
12061207
if ( ! $this->inTransaction() ) {
12071208
throw $this->new_driver_exception( 'There is no active transaction' );
12081209
}
1210+
$this->flush();
12091211
$this->commit_user_transaction();
12101212
return true;
12111213
}
@@ -1219,6 +1221,7 @@ public function rollBack(): bool {
12191221
if ( ! $this->inTransaction() ) {
12201222
throw $this->new_driver_exception( 'There is no active transaction' );
12211223
}
1224+
$this->flush();
12221225
$this->rollback_user_transaction();
12231226
return true;
12241227
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,34 @@ public function test_rollback_no_active_transaction(): void {
446446
$this->driver->rollBack();
447447
}
448448

449+
public function test_transaction_methods_flush_operation_state(): void {
450+
$this->driver->query( 'CREATE TABLE t (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)' );
451+
$this->driver->query( 'INSERT INTO t (id) VALUES (NULL)' );
452+
453+
$this->assertSame( '1', $this->driver->lastInsertId() );
454+
$this->assertTrue( $this->driver->beginTransaction() );
455+
$this->assertSame( '0', $this->driver->lastInsertId() );
456+
$this->assertSame( '', $this->driver->get_last_mysql_query() );
457+
$this->assertSame( array( 'BEGIN IMMEDIATE' ), array_column( $this->driver->get_last_sqlite_queries(), 'sql' ) );
458+
459+
$this->driver->query( 'INSERT INTO t (id) VALUES (NULL)' );
460+
461+
$this->assertSame( '2', $this->driver->lastInsertId() );
462+
$this->assertTrue( $this->driver->commit() );
463+
$this->assertSame( '0', $this->driver->lastInsertId() );
464+
$this->assertSame( '', $this->driver->get_last_mysql_query() );
465+
$this->assertSame( array( 'COMMIT' ), array_column( $this->driver->get_last_sqlite_queries(), 'sql' ) );
466+
467+
$this->driver->beginTransaction();
468+
$this->driver->query( 'INSERT INTO t (id) VALUES (NULL)' );
469+
470+
$this->assertSame( '3', $this->driver->lastInsertId() );
471+
$this->assertTrue( $this->driver->rollBack() );
472+
$this->assertSame( '0', $this->driver->lastInsertId() );
473+
$this->assertSame( '', $this->driver->get_last_mysql_query() );
474+
$this->assertSame( array( 'ROLLBACK' ), array_column( $this->driver->get_last_sqlite_queries(), 'sql' ) );
475+
}
476+
449477
public function test_fetch_default(): void {
450478
// Default fetch mode is PDO::FETCH_BOTH.
451479
$result = $this->driver->query( "SELECT 1, 'abc', 2" );

0 commit comments

Comments
 (0)