Class SecureDatabaseStore
-
- All Implemented Interfaces:
public final class SecureDatabaseStore
-
-
Field Summary
Fields Modifier and Type Field Description private final StringautoEncryptionKeyAliasprivate final BooleanisOpenprivate final BooleanisInTransactionprivate final SQLiteDatabasedatabaseprivate final StringdatabaseName
-
Constructor Summary
Constructors Constructor Description SecureDatabaseStore(Context context, String databaseName, Integer databaseVersion, CreateDatabaseCallback callback, Boolean enableWriteAheadLogging)SecureDatabaseStore(Context context, String databaseName, Integer databaseVersion, CreateDatabaseCallback callback)
-
Method Summary
Modifier and Type Method Description final StringgetAutoEncryptionKeyAlias()final BooleanisOpen()Checks if the database exists and has been opened, that is, . final BooleanisInTransaction()Checks if a transaction is in progress. final SQLiteDatabasegetDatabase()Returns the underlying net.sqlcipher.database.SQLiteDatabaseinstance assuming .final StringgetDatabaseName()final Unitopen(ByteArray encryptionKey)Opens the encrypted database with the encryption key. final UnitchangeEncryptionKey(ByteArray newEncryptionKey)Changes the encryption key of the database. final BooleandeleteAutoEncryptionKey()final BooleanexistsAutoEncryptionKey()final UnitdeleteStore(Context context)Deletes the database file. final Unitclose()Closes the database. final UnitexecuteUpdate(String sql)Executes a non- SELECTstatement that does contain parameters ('?').final UnitexecuteUpdate(String sql, Object values)Executes a non- SELECTstatement that contains parameters ('?').final UnitexecuteInsert(String tableName, ContentValues values)Executes an INSERTstatement by filling the column names and the associated values in aContentValuesobject.final UnitexecuteStatements(String sqlStatements)Executes multiple SQL statements, each SQL statement does not contain parameters ('?'). final SecureDatabaseResultSetexecuteQuery(String sql, String values)Executes a SELECTstatement that contains parameters ('?').final SecureDatabaseResultSetexecuteQuery(String sql)Executes a SELECTstatement that does not contains parameters ('?').final UnitbeginExclusiveTransaction()Begins a transaction. final Unitcommit()Commits the transaction, and ends the current transaction. final Unitrollback()Rolls back the current open transaction. final BooleanstoreExists()Checks if the database file exists. final static CharArraytoChars(ByteArray bytes)Converts a non-null/non-emptybyte[] to char[].final static ByteArraytoBytes(CharArray chars)-
-
Constructor Detail
-
SecureDatabaseStore
SecureDatabaseStore(Context context, String databaseName, Integer databaseVersion, CreateDatabaseCallback callback, Boolean enableWriteAheadLogging)
-
SecureDatabaseStore
SecureDatabaseStore(Context context, String databaseName, Integer databaseVersion, CreateDatabaseCallback callback)
-
-
Method Detail
-
getAutoEncryptionKeyAlias
final String getAutoEncryptionKeyAlias()
-
isOpen
final Boolean isOpen()
Checks if the database exists and has been opened, that is, .open )} has been called and .close has not been called.
- Returns:
trueif the database is open,falseotherwise.
-
isInTransaction
final Boolean isInTransaction()
Checks if a transaction is in progress.
- Returns:
trueif a transaction is in progress,falseotherwise.
-
getDatabase
final SQLiteDatabase getDatabase()
Returns the underlying
net.sqlcipher.database.SQLiteDatabaseinstance assuming .SecureDatabaseStore and .open have been called.- Returns:
The underlying
net.sqlcipher.database.SQLiteDatabaseinstance, ornullif this instance ofSecureDatabaseStoreis not opened, that is, either .open was not called or .close has been called.
-
getDatabaseName
final String getDatabaseName()
-
open
final Unit open(ByteArray encryptionKey)
Opens the encrypted database with the encryption key.
You must open the store before you can interact with it. The store can fail to open if the encryption key is incorrect. When the store is opened for the first time, the provided key is used to encrypt the database.
- Parameters:
encryptionKey- the key to encrypt the store.
-
changeEncryptionKey
final Unit changeEncryptionKey(ByteArray newEncryptionKey)
Changes the encryption key of the database.
Database should have been opened with the old encryption key.
- Parameters:
newEncryptionKey- new encryption key.
-
deleteAutoEncryptionKey
final Boolean deleteAutoEncryptionKey()
-
existsAutoEncryptionKey
final Boolean existsAutoEncryptionKey()
-
deleteStore
final Unit deleteStore(Context context)
Deletes the database file. Note that this instance is no longer usable after the call, any operation performed on the instance will get FileMissingException.
- Parameters:
context- Android application context
-
close
final Unit close()
Closes the database. Note that once the database is closed, no operations are allowed.
-
executeUpdate
final Unit executeUpdate(String sql)
Executes a non-
SELECTstatement that does contain parameters ('?').Any SQL statement that is not a SELECT statement qualifies as an update, including CREATE, UPDATE, INSERT, ALTER, COMMIT, BEGIN, DETACH, DELETE, DROP, END, EXPLAIN, VACUUM, and REPLACE statements and many more. Basically, if your SQL statement does not begin with SELECT, it is an update statement.
As a good practice, should use .executeUpdate if possible.
- Parameters:
sql- a SQL statement that does not contains parameters ('?
-
executeUpdate
final Unit executeUpdate(String sql, Object values)
Executes a non-
SELECTstatement that contains parameters ('?').Any SQL statement that is not a SELECT statement qualifies as an update, including CREATE, UPDATE, INSERT, ALTER, COMMIT, BEGIN, DETACH, DELETE, DROP, END, EXPLAIN, VACUUM, and REPLACE statements and many more. Basically, if your SQL statement does not begin with SELECT, it is an update statement.
- Parameters:
sql- a valid SQL statement that does not begin with SELECT and contains '?values- array of objects to match the '?
-
executeInsert
final Unit executeInsert(String tableName, ContentValues values)
Executes an
INSERTstatement by filling the column names and the associated values in aContentValuesobject.Example:
<pre>`static final String TABLE_NAME = "account_user"; static final String COLUMN_EMAIL = "email"; static final String COLUMN_FULL_NAME = "fullName"; static final String COLUMN_ACCOUNT_NAME = "accountName"; // Fills each column with value. ContentValues cv = new ContentValues(); cv.put(COLUMN_EMAIL, "john.smith@mycompany.com"); cv.put(COLUMN_FULL_NAME, "John Smith"); cv.put(COLUMN_ACCOUNT_NAME, "JSmith032"); // databaseStore was defined, instantiated and opened already. databaseStore.executeInsert(TABLE_NAME, cv); ` * </pre> *- Parameters:
tableName- table namevalues- a ContentValues object that contains column names and the associated values
-
executeStatements
final Unit executeStatements(String sqlStatements)
Executes multiple SQL statements, each SQL statement does not contain parameters ('?'). The multiple SQL statements will be executed inside a transaction, any SQL statement execution failure will cause the changes to be rolled back.
- Parameters:
sqlStatements- avararg(array) of SQL statements
-
executeQuery
final SecureDatabaseResultSet executeQuery(String sql, String values)
Executes a
SELECTstatement that contains parameters ('?').- Parameters:
sql- aSELECTstatementvalues- one or more String values to match the '?- Returns:
a
non-nullresult set from the query which may or may not contain rows.
-
executeQuery
final SecureDatabaseResultSet executeQuery(String sql)
Executes a
SELECTstatement that does not contains parameters ('?').As a good practice, use .executeQuery if possible.
- Parameters:
sql- aSELECTstatement- Returns:
a
non-nullresult set from the query which may or may not contain rows.
-
beginExclusiveTransaction
final Unit beginExclusiveTransaction()
Begins a transaction.
-
commit
final Unit commit()
Commits the transaction, and ends the current transaction.
-
rollback
final Unit rollback()
Rolls back the current open transaction.
-
storeExists
@Synchronized() final Boolean storeExists()
Checks if the database file exists.
- Returns:
Trueif the database file exists,falseotherwise.
-
toChars
final static CharArray toChars(ByteArray bytes)
Converts a
non-null/non-emptybyte[] to char[].- Parameters:
bytes- anon-null/non-emptybyte[]- Returns:
A char[] converted from the given byte[].
-
toBytes
final static ByteArray toBytes(CharArray chars)
-
-
-
-