- Notifications
You must be signed in to change notification settings - Fork2
A wrapper around Android's SQLiteDatabase with restoring capability
License
yaa110/RestorableSQLiteDatabase
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
RestorableSQLiteDatabase is a wrapper to replicate android'sSQLiteDatabase class with restoring capability. This wrapper makes it possible to undo changes made after execution of SQL queries.
Use Gradle
Reference library using this dependency in your module'sbuild.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>
First, create a subclass ofSQLiteOpenHelper
:
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, useRestorableSQLiteDatabase
:
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");
publicstaticRestorableSQLiteDatabasegetInstance(SQLiteDatabasemSQLiteDatabase,HashMap<String,String>tableRowid)
Constructs a new instance of theRestorableSQLiteDatabase
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 theRestorableSQLiteDatabase
only if no instance is constructed.
Parameters
- helper the instance of the
SQLiteOpenHelper
to open a database using itsgetWritableDatabase method. - tableRowid maps the table name to its ROWID column name.
publicstaticRestorableSQLiteDatabasegetNewInstance(SQLiteDatabasemSQLiteDatabase,HashMap<String,String>tableRowid)
Constructs a new instance of theRestorableSQLiteDatabase
.
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 theRestorableSQLiteDatabase
.
Parameters
- helper the instance of the
SQLiteOpenHelper
to open a database using itsgetWritableDatabase method. - tableRowid maps the table name to its ROWID column name.
publicvoidclose()
Closes the SQLite database. Use thereopen
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 theSQLiteDatabase
.
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 wrappedSQLiteDatabase
.
Returns
The instance of wrappedSQLiteDatabase
.
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 theSQLiteDatabase
.
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 theSQLiteDatabase
.
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 theSQLiteDatabase
.
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 theSQLiteDatabase
.
Unlike therawQuery
of theSQLiteDatabase
, there is no need to call themoveToFirst
method of the returnedCursor
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 theSQLiteDatabase
.
Unlike therawQuery
of theSQLiteDatabase
, there is no need to call themoveToFirst
method of the returnedCursor
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 itsgetWritableDatabase 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 theSQLiteDatabase
.
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 theSQLiteDatabase
.
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 aSet
view of the tags contained in the hash table.
Returns
aSet
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 theSQLiteDatabase
.
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 theSQLiteDatabase
.
Parameters
- tag the tag to be mapped to the restoring query.
Throws
- IllegalArgumentException if the tag is null.
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. JSqlParser is licensed under the LGPL V2.1.
RestorableSQLiteDatabase is licensed under the MIT License.