A wrapper around Android's SQLiteDatabase with restoring capability

Overview

Restorable SQLiteDatabase

Download License

RestorableSQLiteDatabase is a wrapper to replicate android's SQLiteDatabase class with restoring capability. This wrapper makes it possible to undo changes made after execution of SQL queries.

How to use

Use Gradle

Reference library using this dependency in your module's build.gradle file:

dependencies {
    compile 'github.yaa110.db:restorablesqlitedatabase:0.1.0'
}

or Use Maven

<dependency>
	<groupId>github.yaa110.db</groupId>
	<artifactId>restorablesqlitedatabase</artifactId>
	<version>0.1.0</version>
	<type>aar</type>
</dependency>

Example: Undoing deleted rows

First, create a subclass of SQLiteOpenHelper:

public class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                        COLUMN_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        COLUMN_TITLE + " TEXT" +
                        ");"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int old_version, int new_version) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

}

Then, use RestorableSQLiteDatabase:

HashMap<String, String> tableRowid = new HashMap<>();
tableRowid.put(TABLE_NAME, COLUMN_ROWID);

DbHelper helper = new DbHelper(this);

RestorableSQLiteDatabase db = new RestorableSQLiteDatabase(helper, tableRowid);

// Delete some rows
db.delete(
        TABLE_NAME,
        COLUMN_TITLE + " = ?",
        new String[] {"demo"},
        "DELETION_TAG"
);

// Undoing deletion
db.restore("DELETION_TAG");

Documentation

public static RestorableSQLiteDatabase getInstance(SQLiteDatabase mSQLiteDatabase, HashMap<String, String> tableRowid)

Constructs a new instance of the RestorableSQLiteDatabase only if no instance is constructed.

Parameters

  • mSQLiteDatabase the instance of the SQLiteDatabase to be wrapped.
  • tableRowid maps the table name to its ROWID column name.
public static <T extends SQLiteOpenHelper> RestorableSQLiteDatabase getInstance(T helper, HashMap<String, String> tableRowid)

Constructs a new instance of the RestorableSQLiteDatabase only if no instance is constructed.

Parameters

  • helper the instance of the SQLiteOpenHelper to open a database using its getWritableDatabase method.
  • tableRowid maps the table name to its ROWID column name.
public static RestorableSQLiteDatabase getNewInstance(SQLiteDatabase mSQLiteDatabase, HashMap<String, String> tableRowid)

Constructs a new instance of the RestorableSQLiteDatabase.

Parameters

  • mSQLiteDatabase the instance of the SQLiteDatabase to be wrapped.
  • tableRowid maps the table name to its ROWID column name.
public static <T extends SQLiteOpenHelper> RestorableSQLiteDatabase getNewInstance(T helper, HashMap<String, String> tableRowid)

Constructs a new instance of the RestorableSQLiteDatabase.

Parameters

  • helper the instance of the SQLiteOpenHelper to open a database using its getWritableDatabase method.
  • tableRowid maps the table name to its ROWID column name.
public void close()

Closes the SQLite database. Use the reopen methods to reopen the SQLite database.

public boolean containsTag(String tag)

Checks if the hash table contains the tag.

Parameters

  • tag possible tag of restoring query.

Returns

True if the hash table contains the tag; false otherwise.

Throws

  • IllegalArgumentException if the tag is null.
public int delete(String table, String whereClause, String[] whereArgs, String tag)

Replicates the [delete](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#delete(java.lang.String, java.lang.String, java.lang.String[])) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public ArrayList<String> getQueries(String tag)

Provides the query to which the tag is mapped.

Parameters

  • tag possible tag of restoring query.

Returns

The queries to which the tag is mapped, or null if the hash table contains no mapping for the tag.

Throws

  • IllegalArgumentException if the tag is null.
public SQLiteDatabase getSQLiteDatabase()

Provides the instance of wrapped SQLiteDatabase.

Returns

The instance of wrapped SQLiteDatabase.

public Hashtable<String, ArrayList<String[]>> getTagQueryParameters()

Provides the parameters hash table.

Returns

The parameters hash table.

public Hashtable<String, ArrayList<String>> getTagQueryTable()

Provides the hash table.

Returns

The hash table.

public long insert(String table, String nullColumnHack, ContentValues values, String tag)

Replicates the [insert](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String, java.lang.String, android.content.ContentValues)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public long insertOrThrow(String table, String nullColumnHack, ContentValues values, String tag) throws SQLException

Replicates the [insertOrThrow](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insertOrThrow(java.lang.String, java.lang.String, android.content.ContentValues)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm, String tag)

Replicates the [insertWithOnConflict](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insertWithOnConflict(java.lang.String, java.lang.String, android.content.ContentValues, int)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public Cursor rawQuery(String sql, String[] selectionArgs, String tag) throws JSQLParserException, ClassCastException

Replicates the [rawQuery](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[])) method of the SQLiteDatabase.

Unlike the rawQuery of the SQLiteDatabase, there is no need to call the moveToFirst method of the returned Cursor to apply SQL query.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public Cursor rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal, String tag) throws JSQLParserException, ClassCastException

Replicates the [rawQuery](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)) method of the SQLiteDatabase.

Unlike the rawQuery of the SQLiteDatabase, there is no need to call the moveToFirst method of the returned Cursor to apply SQL query.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public void reopen(SQLiteDatabase mSqLiteDatabase)

Reopens the SQLite database.

Parameters

  • mSqLiteDatabase the instance of the SQLiteDatabase to be wrapped.
public <T extends SQLiteOpenHelper> void reopen(T helper)

Reopens the SQLite database.

Parameters

  • helper the instance of the SQLiteOpenHelper to open a database using its getWritableDatabase method.
public long replace(String table, String nullColumnHack, ContentValues initialValues, String tag)

Replicates the [replace](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#replace(java.lang.String, java.lang.String, android.content.ContentValues)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public long replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues, String tag) throws SQLException

Replicates the [replaceOrThrow](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#replaceOrThrow(java.lang.String, java.lang.String, android.content.ContentValues)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public int restore(String tag)

Restores the SQL queries to which the tag is mapped.

Parameters

  • tag the tag mapped to restoring queries.

Returns

Possible number of restored queries to which tag is mapped.

public int restore(String[] tags)

Restores the queries to which each tag is mapped.

Parameters

  • tags an array of tags mapped to restoring SQL queries.

Returns

Possible number of restored queries to which tag is mapped.

public int restore(Set<String> tags)

Restores the queries to which each tag is mapped.

Parameters

  • tags a set of tags mapped to restoring SQL queries.

Returns

Possible number of restored queries to which tag is mapped.

public int restoreAll()

Restores all restoring SQL queries.

Returns

Possible number of restored queries to which tag is mapped.

public void setTagQueryParameters(Hashtable<String, ArrayList<String[]>> tagQueryParameters)

Changes the parameters hash table.

Parameters

  • tagQueryParameters the substitute hash table.
public void setTagQueryTable(Hashtable<String, ArrayList<String>> tagQueryTable)

Changes the hash table.

Parameters

  • tagQueryTable the substitute hash table.
public Set<String> tagSet()

Provides a Set view of the tags contained in the hash table.

Returns

a Set view of the tags contained in the hash table.

public int update(String table, ContentValues values, String whereClause, String[] whereArgs, String tag)

Replicates the [update](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#update(java.lang.String, android.content.ContentValues, java.lang.String, java.lang.String[])) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.
public int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm, String tag)

Replicates the [updateWithOnConflict](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#updateWithOnConflict(java.lang.String, android.content.ContentValues, java.lang.String, java.lang.String[], int)) method of the SQLiteDatabase.

Parameters

  • tag the tag to be mapped to the restoring query.

Throws

  • IllegalArgumentException if the tag is null.

Dependencies

JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. JSqlParser is licensed under the LGPL V2.1.

License

RestorableSQLiteDatabase is licensed under the MIT License.

You might also like...
The purpose is to share the Internet capability of one device to the entire Bluetooth LAN.
The purpose is to share the Internet capability of one device to the entire Bluetooth LAN.

bluenet The purpose is to share the Internet capability of one device to the entire Bluetooth LAN. To make a prototype of a soft bus, or actually, I w

UE Capability parser used by smartphonecombo.it and cacombos.com

UE Capability Parser Warning Work In progress UE Capability parser used by smartphonecombo.it and cacombos.com $ java -jar uecapabilityparser.jar --he

A lightweight wrapper around SQLiteOpenHelper which introduces reactive stream semantics to SQL operations.

SQL Brite A lightweight wrapper around SupportSQLiteOpenHelper and ContentResolver which introduces reactive stream semantics to queries. Deprecated T

Wrapper around the android Camera class that simplifies its usage

EasyCamera Wrapper around the android Camera class that simplifies its usage (read more about the process) Usage: // the surface where the preview wil

A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.
A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.

MaterialDrawerKt Create navigation drawers in your Activities and Fragments without having to write any XML, in pure Kotlin code, with access to all t

Android Java wrapper around ffmpeg command line binary

FFMPEG Library for Android This project is a Java wrapper around an ffmpeg command line binary for use in Android applications. It depends on the andr

Wrapper around the android Camera class that simplifies its usage

EasyCamera Wrapper around the android Camera class that simplifies its usage (read more about the process) Usage: // the surface where the preview wil

A simple Kotlin wrapper around Anvil.

AnvilKotlin A simple Kotlin wrapper around Anvil. The only purpose of this library is to provide type safety to Anvil through Kotlin. Nothing more, no

A lightweight Kotlin friendly wrapper around Couchbase lite for Android.

CouchBaseKtx 🚧 Work In-Progress 🚧 A lightweight Kotlin friendly wrapper around Couchbase-lite for Android Read up a little bit of documentation abou

Android Studio project wrapper around the Elixir TodoApp Desktop app to run on Android including the Erlang runtime
Android Studio project wrapper around the Elixir TodoApp Desktop app to run on Android including the Erlang runtime

TodoApp Android: An Android Sample App This Android Studio project wraps the Desktop Sample App to run on an Android phone. How to build & run Install

SavedStateFlow - A Kotlin StateFlow wrapper around SavedStateHandle
SavedStateFlow - A Kotlin StateFlow wrapper around SavedStateHandle

SavedStateFlow SavedStateFlow is a Kotlin StateFlow wrapper around SavedStateHan

Xoxo is a simple wrapper around org.w3c.dom to parse XML using nice Kotlin APIs

Xoxo 😘 Xoxo is a simple wrapper around org.w3c.dom to parse XML using nice Kotlin APIs. No more NodeList, .item(), etc... just use .children, .filter

Wynncraft API Wrapper - Simple wrapper to get Wynncraft Stats of a player or a guild and more in Java

WynncraftAPIWrapper Simple wrapper to get Wynncraft Stats of a player or a guild

An android library to display a progressbar that goes around an image.
An android library to display a progressbar that goes around an image.

android-square-progressbar First things first This library is setup to work with the Android Studio and Gradle. If you're using the Eclipse environmen

Material progress circle around any FloatingActionButton. 100% Guidelines.
Material progress circle around any FloatingActionButton. 100% Guidelines.

FABProgressCircle Android library to provide a material progress circle around your FloatingActionButton. This component is compatible with any existe

Material progress circle around any FloatingActionButton. 100% Guidelines.
Material progress circle around any FloatingActionButton. 100% Guidelines.

FABProgressCircle Android library to provide a material progress circle around your FloatingActionButton. This component is compatible with any existe

[] Explorations around Android custom layouts

android-layout-samples Explorations around Android custom layouts, including off main thread View measure/layout passes. Sample code for: Composite Vi

Custom ImageView for moving image around the screen (Android)
Custom ImageView for moving image around the screen (Android)

MovingImageView Create a custom ImageView for moving image around the screen. Usage To use MovingImageView, add the module into your project and start

A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.
A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.

Pdf Viewer For Android A Simple PDF Viewer library which only occupies around 125kb while most of the Pdf viewer occupies upto 16MB space. How to inte

Owner
Navid
Navid
A MySQL connector wrapper that supports mapping to Kotlin classes.

Racoon Racoon is a wrapper for the MySQL connector. It makes communicating with the database easier by providing a bunch of functionalities: Mapping q

null 1 Jun 3, 2022
A wrapper around Android's SQLiteDatabase with restoring capability

Restorable SQLiteDatabase RestorableSQLiteDatabase is a wrapper to replicate android's SQLiteDatabase class with restoring capability. This wrapper ma

Navid 21 Oct 21, 2022
A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and okio

Store A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisatio

Isuru Rajapakse 98 Jan 3, 2023
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
Androids EditText that animates the typed text. EditText is extended to create AnimatedEditText and a PinEntryEditText.

AnimatedEditText for Android This repository contains AnimatedEditText and TextDrawable all of which extend the behaviour of EditText and implement fe

Ali Muzaffar 439 Nov 29, 2022
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
Tsunami - An open-source SMS & Call flooding Android application with unlimited OTP bombing capability

An open-source OTP & Call flooding android application with unlimited sending capability

Utsanjan 83 Jan 2, 2023
A Simple movies app using Kotllin, MVVM, and with an offline caching capability.

IMDB-CLONE A simple imdb clone using KOTLIN,MVVM with searching and bookmarking ability with offline caching ability Libraries used:- Kotlin Coroutine

saiteja janjirala 13 Aug 16, 2022
JetCam - Add camera capability to app built with Jetpack Compose

JetCam Add camera capability to your app with just a single method - JetCam To g

Pankaj Rai 6 Jul 8, 2022