@@ -10407,6 +10407,183 @@ public function testTransactionSavepoints(): void {
1040710407 $this->assertSame( array(), (array) array_column( $result, 'id' ) );
1040810408 }
1040910409
10410+ public function testFailedStatementKeepsTransactionActive(): void {
10411+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10412+ $this->assertQuery( 'BEGIN' );
10413+ $this->assertQuery( 'INSERT INTO t VALUES (1)' );
10414+
10415+ // A failed write and a failed read keep the transaction active.
10416+ $this->assertQueryError(
10417+ 'INSERT INTO t VALUES (1)',
10418+ 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
10419+ );
10420+ $this->assertTrue( $this->engine->inTransaction() );
10421+ $this->assertQueryError(
10422+ 'SELECT * FROM missing_table',
10423+ "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'missing_table' doesn't exist"
10424+ );
10425+ $this->assertTrue( $this->engine->inTransaction() );
10426+
10427+ $this->assertQuery( 'INSERT INTO t VALUES (2)' );
10428+ $this->assertQuery( 'COMMIT' );
10429+ $result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
10430+ $this->assertSame( array( '1', '2' ), array_column( $result, 'id' ) );
10431+ }
10432+
10433+ public function testFailedStatementIsRolledBackInTransaction(): void {
10434+ $this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
10435+ $this->assertQuery( 'CREATE TABLE t2 (id INT PRIMARY KEY)' );
10436+ $this->assertQuery( 'CREATE TABLE t3 (id INT PRIMARY KEY, t2_id INT, FOREIGN KEY (t2_id) REFERENCES t2 (id))' );
10437+ $this->assertQuery( 'INSERT INTO t1 VALUES (1)' );
10438+ $this->assertQuery( 'INSERT INTO t2 VALUES (1)' );
10439+ $this->assertQuery( 'INSERT INTO t3 VALUES (1, 1)' );
10440+ $this->assertQuery( 'BEGIN' );
10441+ $this->assertQuery( 'INSERT INTO t1 VALUES (2)' );
10442+
10443+ /*
10444+ * A multi-table DELETE runs a DELETE query for each table. Deleting
10445+ * from t1 succeeds, and then deleting from t2 fails on the foreign key.
10446+ */
10447+ $this->assertQueryError(
10448+ 'DELETE t1, t2 FROM t1 JOIN t2 ON t2.id = t1.id',
10449+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
10450+ );
10451+ $this->assertTrue( $this->engine->inTransaction() );
10452+ $this->assertQuery( 'COMMIT' );
10453+
10454+ // The changes of the failed statement are undone, and the rest is kept.
10455+ $result = $this->assertQuery( 'SELECT id FROM t1 ORDER BY id' );
10456+ $this->assertSame( array( '1', '2' ), array_column( $result, 'id' ) );
10457+ $result = $this->assertQuery( 'SELECT id FROM t2' );
10458+ $this->assertSame( array( '1' ), array_column( $result, 'id' ) );
10459+ }
10460+
10461+ public function testRollbackToSavepointAfterFailedStatement(): void {
10462+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10463+ $this->assertQuery( 'BEGIN' );
10464+ $this->assertQuery( 'INSERT INTO t VALUES (1)' );
10465+ $this->assertQuery( 'SAVEPOINT sp1' );
10466+ $this->assertQuery( 'INSERT INTO t VALUES (2)' );
10467+ $this->assertQueryError(
10468+ 'INSERT INTO t VALUES (1)',
10469+ 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
10470+ );
10471+
10472+ // The savepoint still exists after the failed statement.
10473+ $this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
10474+ $this->assertQuery( 'COMMIT' );
10475+ $result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
10476+ $this->assertSame( array( '1' ), array_column( $result, 'id' ) );
10477+ }
10478+
10479+ public function testFailedStatementKeepsTableLock(): void {
10480+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10481+ $this->assertQuery( 'LOCK TABLES t WRITE' );
10482+ $this->assertQuery( 'INSERT INTO t VALUES (1)' );
10483+ $this->assertQueryError(
10484+ 'INSERT INTO t VALUES (1)',
10485+ 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
10486+ );
10487+
10488+ // UNLOCK TABLES keeps the rows written before the failed statement.
10489+ $this->assertQuery( 'UNLOCK TABLES' );
10490+ $this->assertFalse( $this->engine->inTransaction() );
10491+ $result = $this->assertQuery( 'SELECT id FROM t' );
10492+ $this->assertSame( array( '1' ), array_column( $result, 'id' ) );
10493+ }
10494+
10495+ /**
10496+ * @dataProvider dataFailedCommits
10497+ */
10498+ public function testFailedCommitRollsBackTransaction( string $begin_query, string $commit_query ): void {
10499+ $this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
10500+ $this->assertQuery( 'CREATE TABLE c (id INT PRIMARY KEY, p_id INT, FOREIGN KEY (p_id) REFERENCES p (id))' );
10501+ $this->assertQuery( $begin_query );
10502+
10503+ // Make the commit fail by deferring a foreign key check to it.
10504+ $this->sqlite->exec( 'PRAGMA defer_foreign_keys = ON' );
10505+ $this->assertQuery( 'INSERT INTO c VALUES (1, 1)' );
10506+ $this->assertQueryError(
10507+ $commit_query,
10508+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
10509+ );
10510+
10511+ // Like MySQL, the failed commit rolls back the transaction.
10512+ $this->assertFalse( $this->engine->inTransaction() );
10513+ $result = $this->assertQuery( 'SELECT id FROM c' );
10514+ $this->assertSame( array(), array_column( $result, 'id' ) );
10515+
10516+ // The table lock is released, so UNLOCK TABLES doesn't commit a new transaction.
10517+ $this->assertQuery( 'BEGIN' );
10518+ $this->assertQuery( 'UNLOCK TABLES' );
10519+ $this->assertTrue( $this->engine->inTransaction() );
10520+ }
10521+
10522+ public static function dataFailedCommits(): array {
10523+ return array(
10524+ 'COMMIT' => array( 'BEGIN', 'COMMIT' ),
10525+ 'UNLOCK TABLES' => array( 'LOCK TABLES c WRITE', 'UNLOCK TABLES' ),
10526+ 'implicit START TRANSACTION' => array( 'BEGIN', 'START TRANSACTION' ),
10527+ 'implicit BEGIN' => array( 'BEGIN', 'BEGIN' ),
10528+ 'implicit LOCK TABLES' => array( 'BEGIN', 'LOCK TABLES p WRITE' ),
10529+ );
10530+ }
10531+
10532+ public function testFailedStatementRolledBackBySqliteEndsTransaction(): void {
10533+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10534+
10535+ // Some SQLite errors roll back the whole transaction. Emulate them with a trigger.
10536+ $this->sqlite->exec(
10537+ "CREATE TRIGGER t_rollback BEFORE INSERT ON t WHEN NEW.id = 2 BEGIN SELECT RAISE(ROLLBACK, 'Rolled back'); END"
10538+ );
10539+
10540+ $this->assertQuery( 'BEGIN' );
10541+ $this->assertQuery( 'INSERT INTO t VALUES (1)' );
10542+ $this->assertQuery( 'SAVEPOINT sp1' );
10543+ $this->assertQueryError(
10544+ 'INSERT INTO t VALUES (2)',
10545+ 'SQLSTATE[23000]: Integrity constraint violation: 19 Rolled back'
10546+ );
10547+
10548+ // The transaction and its savepoints are gone.
10549+ $this->assertFalse( $this->engine->inTransaction() );
10550+ $this->assertQueryError(
10551+ 'ROLLBACK TO SAVEPOINT sp1',
10552+ 'SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT sp1 does not exist'
10553+ );
10554+
10555+ // Without a transaction, the same error leaves the connection usable.
10556+ $this->assertQueryError(
10557+ 'INSERT INTO t VALUES (2)',
10558+ 'SQLSTATE[23000]: Integrity constraint violation: 19 Rolled back'
10559+ );
10560+ $this->assertFalse( $this->engine->inTransaction() );
10561+ $this->assertQuery( 'INSERT INTO t VALUES (3)' );
10562+ $result = $this->assertQuery( 'SELECT id FROM t' );
10563+ $this->assertSame( array( '3' ), array_column( $result, 'id' ) );
10564+ }
10565+
10566+ public function testFailedStatementRolledBackBySqliteReleasesTableLock(): void {
10567+ $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10568+
10569+ // Some SQLite errors roll back the whole transaction. Emulate them with a trigger.
10570+ $this->sqlite->exec(
10571+ "CREATE TRIGGER t_rollback BEFORE INSERT ON t WHEN NEW.id = 2 BEGIN SELECT RAISE(ROLLBACK, 'Rolled back'); END"
10572+ );
10573+
10574+ $this->assertQuery( 'LOCK TABLES t WRITE' );
10575+ $this->assertQueryError(
10576+ 'INSERT INTO t VALUES (2)',
10577+ 'SQLSTATE[23000]: Integrity constraint violation: 19 Rolled back'
10578+ );
10579+ $this->assertFalse( $this->engine->inTransaction() );
10580+
10581+ // The table lock is released, so UNLOCK TABLES doesn't commit a new transaction.
10582+ $this->assertQuery( 'BEGIN' );
10583+ $this->assertQuery( 'UNLOCK TABLES' );
10584+ $this->assertTrue( $this->engine->inTransaction() );
10585+ }
10586+
1041010587 public function testSavepointWithoutTransactionDoesNotStartTransaction(): void {
1041110588 $this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, v INT)' );
1041210589 $this->assertQuery( 'INSERT INTO t (id, v) VALUES (1, 1)' );
0 commit comments