Skip to content

yaa110/RestorableSQLiteDatabase

Repository files navigation

Restorable SQLiteDatabase

DownloadLicense

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:

publicclassDbHelperextendsSQLiteOpenHelper{publicDbHelper(Contextcontext){super(context, DB_NAME, null, DB_VERSION)} @OverridepublicvoidonCreate(SQLiteDatabasedb){db.execSQL( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + COLUMN_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_TITLE + " TEXT" + ");" )} @OverridepublicvoidonUpgrade(SQLiteDatabasedb, intold_version, intnew_version){db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db)} }

Then, use RestorableSQLiteDatabase:

HashMap<String, String> tableRowid = newHashMap<>(); tableRowid.put(TABLE_NAME, COLUMN_ROWID); DbHelperhelper = newDbHelper(this); RestorableSQLiteDatabasedb = newRestorableSQLiteDatabase(helper, tableRowid); // Delete some rowsdb.delete( TABLE_NAME, COLUMN_TITLE + " = ?", newString[]{"demo"}, "DELETION_TAG" ); // Undoing deletiondb.restore("DELETION_TAG");

Documentation

publicstaticRestorableSQLiteDatabasegetInstance(SQLiteDatabasemSQLiteDatabase, 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.
publicstatic <TextendsSQLiteOpenHelper> RestorableSQLiteDatabasegetInstance(Thelper, 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.
publicstaticRestorableSQLiteDatabasegetNewInstance(SQLiteDatabasemSQLiteDatabase, 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.
publicstatic <TextendsSQLiteOpenHelper> RestorableSQLiteDatabasegetNewInstance(Thelper, 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.
publicvoidclose()

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

publicbooleancontainsTag(Stringtag)

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.
publicintdelete(Stringtable, StringwhereClause, String[] whereArgs, Stringtag)

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.
publicArrayList<String> getQueries(Stringtag)

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.
publicSQLiteDatabasegetSQLiteDatabase()

Provides the instance of wrapped SQLiteDatabase.

Returns

The instance of wrapped SQLiteDatabase.

publicHashtable<String, ArrayList<String[]>> getTagQueryParameters()

Provides the parameters hash table.

Returns

The parameters hash table.

publicHashtable<String, ArrayList<String>> getTagQueryTable()

Provides the hash table.

Returns

The hash table.

publiclonginsert(Stringtable, StringnullColumnHack, ContentValuesvalues, Stringtag)

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.
publiclonginsertOrThrow(Stringtable, StringnullColumnHack, ContentValuesvalues, Stringtag) throwsSQLException

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.
publiclonginsertWithOnConflict(Stringtable, StringnullColumnHack, ContentValuesinitialValues, intconflictAlgorithm, Stringtag)

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.
publicCursorrawQuery(Stringsql, String[] selectionArgs, Stringtag) throwsJSQLParserException, 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.
publicCursorrawQuery(Stringsql, String[] selectionArgs, CancellationSignalcancellationSignal, Stringtag) throwsJSQLParserException, 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.
publicvoidreopen(SQLiteDatabasemSqLiteDatabase)

Reopens the SQLite database.

Parameters

  • mSqLiteDatabase the instance of the SQLiteDatabase to be wrapped.
public <TextendsSQLiteOpenHelper> voidreopen(Thelper)

Reopens the SQLite database.

Parameters

  • helper the instance of the SQLiteOpenHelper to open a database using its getWritableDatabase method.
publiclongreplace(Stringtable, StringnullColumnHack, ContentValuesinitialValues, Stringtag)

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.
publiclongreplaceOrThrow(Stringtable, StringnullColumnHack, ContentValuesinitialValues, Stringtag) throwsSQLException

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.
publicintrestore(Stringtag)

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.

publicintrestore(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.

publicintrestore(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.

publicintrestoreAll()

Restores all restoring SQL queries.

Returns

Possible number of restored queries to which tag is mapped.

publicvoidsetTagQueryParameters(Hashtable<String, ArrayList<String[]>> tagQueryParameters)

Changes the parameters hash table.

Parameters

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

Changes the hash table.

Parameters

  • tagQueryTable the substitute hash table.
publicSet<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.

publicintupdate(Stringtable, ContentValuesvalues, StringwhereClause, String[] whereArgs, Stringtag)

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.
publicintupdateWithOnConflict(Stringtable, ContentValuesvalues, StringwhereClause, String[] whereArgs, intconflictAlgorithm, Stringtag)

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.

About

A wrapper around Android's SQLiteDatabase with restoring capability

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages