@@ -923,6 +923,90 @@ public function test_transaction_methods_flush_operation_state(): void {
923923 $ this ->assertSame ( array ( 'ROLLBACK ' ), array_column ( $ this ->driver ->get_last_sqlite_queries (), 'sql ' ) );
924924 }
925925
926+ public function test_standalone_write_uses_wrapper_transaction (): void {
927+ $ this ->driver ->query ( 'CREATE TABLE t (id INT PRIMARY KEY, value INT) ' );
928+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1, 1) ' );
929+
930+ $ statement = $ this ->driver ->query ( 'UPDATE t SET value = 2 WHERE id = 1 ' );
931+ $ queries = array_column ( $ this ->driver ->get_last_sqlite_queries (), 'sql ' );
932+
933+ $ this ->assertSame ( 1 , $ statement ->rowCount () );
934+ $ this ->assertSame ( 'BEGIN IMMEDIATE ' , $ queries [0 ] );
935+ $ this ->assertSame ( 'COMMIT ' , end ( $ queries ) );
936+ }
937+
938+ public function test_write_inside_savepoint_commits_on_release (): void {
939+ $ this ->driver ->query ( 'CREATE TABLE t (id INT PRIMARY KEY, value INT) ' );
940+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1, 1) ' );
941+
942+ $ this ->driver ->query ( 'SAVEPOINT outer_transaction ' );
943+ $ statement = $ this ->driver ->query ( 'UPDATE t SET value = 2 WHERE id = 1 ' );
944+ $ queries = array_column ( $ this ->driver ->get_last_sqlite_queries (), 'sql ' );
945+
946+ $ this ->assertSame ( 1 , $ statement ->rowCount () );
947+ $ this ->assertNotContains ( 'BEGIN IMMEDIATE ' , $ queries );
948+ $ this ->assertTrue ( $ this ->driver ->inTransaction () );
949+
950+ $ this ->driver ->query ( 'RELEASE SAVEPOINT outer_transaction ' );
951+
952+ $ this ->assertFalse ( $ this ->driver ->inTransaction () );
953+ $ this ->assertSame ( '2 ' , $ this ->driver ->query ( 'SELECT value FROM t ' )->fetchColumn () );
954+ }
955+
956+ public function test_write_inside_savepoint_can_be_rolled_back (): void {
957+ $ this ->driver ->query ( 'CREATE TABLE t (id INT PRIMARY KEY, value INT) ' );
958+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1, 1) ' );
959+
960+ $ this ->driver ->query ( 'SAVEPOINT outer_transaction ' );
961+ $ this ->driver ->query ( 'UPDATE t SET value = 2 WHERE id = 1 ' );
962+ $ this ->driver ->query ( 'ROLLBACK TO SAVEPOINT outer_transaction ' );
963+ $ this ->driver ->query ( 'RELEASE SAVEPOINT outer_transaction ' );
964+
965+ $ this ->assertFalse ( $ this ->driver ->inTransaction () );
966+ $ this ->assertSame ( '1 ' , $ this ->driver ->query ( 'SELECT value FROM t ' )->fetchColumn () );
967+ }
968+
969+ public function test_writes_inside_nested_savepoints_preserve_outer_changes (): void {
970+ $ this ->driver ->query ( 'CREATE TABLE t (id INT PRIMARY KEY, value INT) ' );
971+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1, 1) ' );
972+
973+ $ this ->driver ->query ( 'SAVEPOINT outer_transaction ' );
974+ $ this ->driver ->query ( 'UPDATE t SET value = 2 WHERE id = 1 ' );
975+ $ this ->driver ->query ( 'SAVEPOINT inner_transaction ' );
976+ $ this ->driver ->query ( 'UPDATE t SET value = 3 WHERE id = 1 ' );
977+ $ this ->driver ->query ( 'ROLLBACK TO SAVEPOINT inner_transaction ' );
978+ $ this ->driver ->query ( 'RELEASE SAVEPOINT inner_transaction ' );
979+
980+ $ this ->assertTrue ( $ this ->driver ->inTransaction () );
981+ $ this ->assertSame ( '2 ' , $ this ->driver ->query ( 'SELECT value FROM t ' )->fetchColumn () );
982+
983+ $ this ->driver ->query ( 'RELEASE SAVEPOINT outer_transaction ' );
984+
985+ $ this ->assertFalse ( $ this ->driver ->inTransaction () );
986+ $ this ->assertSame ( '2 ' , $ this ->driver ->query ( 'SELECT value FROM t ' )->fetchColumn () );
987+ }
988+
989+ public function test_failed_write_cleans_up_savepoint_transaction_state (): void {
990+ $ this ->driver ->query ( 'CREATE TABLE t (id INT PRIMARY KEY) ' );
991+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1) ' );
992+ $ this ->driver ->query ( 'SAVEPOINT outer_transaction ' );
993+
994+ try {
995+ $ this ->driver ->query ( 'INSERT INTO t VALUES (1) ' );
996+ $ this ->fail ( 'Expected the duplicate insert to fail. ' );
997+ } catch ( PDOException $ e ) {
998+ $ this ->assertStringContainsString ( 'UNIQUE constraint failed ' , $ e ->getMessage () );
999+ }
1000+
1001+ $ this ->assertFalse ( $ this ->driver ->inTransaction () );
1002+ $ this ->assertSame ( '1 ' , $ this ->driver ->query ( 'SELECT COUNT(*) FROM t ' )->fetchColumn () );
1003+
1004+ $ this ->driver ->query ( 'INSERT INTO t VALUES (2) ' );
1005+ $ queries = array_column ( $ this ->driver ->get_last_sqlite_queries (), 'sql ' );
1006+ $ this ->assertSame ( 'BEGIN IMMEDIATE ' , $ queries [0 ] );
1007+ $ this ->assertSame ( 'COMMIT ' , end ( $ queries ) );
1008+ }
1009+
9261010 public function test_fetch_default (): void {
9271011 // Default fetch mode is PDO::FETCH_BOTH.
9281012 $ result = $ this ->driver ->query ( "SELECT 1, 'abc', 2 " );
0 commit comments