Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 24929cd4 authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Distinguish ROLLBACK from ROLLBACK TO

The former aborts the current transaction, the latter restores the
state to a specified savepoint.

Test: CtsDatabaseTestCases
Test: DatabaseGeneralTest
Bug: 36957161
Change-Id: Ia0b189e8aac4687f10d8fbc07143a452f5f719c9
parent 4c8d5a72
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1408,6 +1408,12 @@ public class DatabaseUtils {
        } else if (prefixSql.equals("END")) {
            return STATEMENT_COMMIT;
        } else if (prefixSql.equals("ROL")) {
            boolean isRollbackToSavepoint = sql.toUpperCase(Locale.ROOT).contains(" TO ");
            if (isRollbackToSavepoint) {
                Log.w(TAG, "Statement '" + sql
                        + "' may not work on API levels 16-27, use ';" + sql + "' instead");
                return STATEMENT_OTHER;
            }
            return STATEMENT_ABORT;
        } else if (prefixSql.equals("BEG")) {
            return STATEMENT_BEGIN;
+15 −0
Original line number Diff line number Diff line
@@ -1221,4 +1221,19 @@ public class DatabaseGeneralTest extends AndroidTestCase implements PerformanceT
                InstrumentationRegistry.getInstrumentation()).executeShellCommand(cmd);
    }

    @SmallTest
    public void testSavepointRollbacks() {
        try (SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null)) {
            db.execSQL("drop table if exists data");
            db.execSQL("create table if not exists data (id INTEGER PRIMARY KEY, val TEXT)");
            db.execSQL("begin deferred transaction");
            db.execSQL("insert into data (val) values('row 1')");
            db.execSQL("savepoint foo");
            db.execSQL("insert into data (val) values('row 2')");
            db.execSQL("rollback to foo");
            db.execSQL("commit transaction");
            long rowCount = DatabaseUtils.longForQuery(db, "select count(*) from data", null);
            assertEquals(1, rowCount);
        }
    }
}