Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A wrapper around Android's SQLiteDatabase with restoring capability

License

NotificationsYou must be signed in to change notification settings

yaa110/RestorableSQLiteDatabase

Repository files navigation

DownloadLicense

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.

How to use

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>

Example: Undoing deleted rows

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");

Documentation

publicstaticRestorableSQLiteDatabasegetInstance(SQLiteDatabasemSQLiteDatabase,HashMap<String,String>tableRowid)

Constructs a new instance of theRestorableSQLiteDatabase only if no instance is constructed.

Parameters

  • mSQLiteDatabase the instance of theSQLiteDatabase 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 theSQLiteOpenHelper 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 theSQLiteDatabase 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 theSQLiteOpenHelper 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 theSQLiteDatabase to be wrapped.
public <TextendsSQLiteOpenHelper>voidreopen(Thelper)

Reopens the SQLite database.

Parameters

  • helper the instance of theSQLiteOpenHelper 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.

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


[8]ページ先頭

©2009-2025 Movatter.jp