Class SecureDatabaseStore
-
- All Implemented Interfaces:
public final class SecureDatabaseStore
-
-
Field Summary
Fields Modifier and Type Field Description private final String
autoEncryptionKeyAlias
private final Boolean
isOpen
private final Boolean
isInTransaction
private final SQLiteDatabase
database
private final String
databaseName
-
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 String
getAutoEncryptionKeyAlias()
final Boolean
isOpen()
Checks if the database exists and has been opened, that is, . final Boolean
isInTransaction()
Checks if a transaction is in progress. final SQLiteDatabase
getDatabase()
Returns the underlying net.sqlcipher.database.SQLiteDatabase
instance assuming .final String
getDatabaseName()
final Unit
open(ByteArray encryptionKey)
Opens the encrypted database with the encryption key. final Unit
changeEncryptionKey(ByteArray newEncryptionKey)
Changes the encryption key of the database. final Boolean
deleteAutoEncryptionKey()
final Boolean
existsAutoEncryptionKey()
final Unit
deleteStore(Context context)
Deletes the database file. final Unit
close()
Closes the database. final Unit
executeUpdate(String sql)
Executes a non- SELECT
statement that does contain parameters ('?').final Unit
executeUpdate(String sql, Object values)
Executes a non- SELECT
statement that contains parameters ('?').final Unit
executeInsert(String tableName, ContentValues values)
Executes an INSERT
statement by filling the column names and the associated values in aContentValues
object.final Unit
executeStatements(String sqlStatements)
Executes multiple SQL statements, each SQL statement does not contain parameters ('?'). final SecureDatabaseResultSet
executeQuery(String sql, String values)
Executes a SELECT
statement that contains parameters ('?').final SecureDatabaseResultSet
executeQuery(String sql)
Executes a SELECT
statement that does not contains parameters ('?').final Unit
beginExclusiveTransaction()
Begins a transaction. final Unit
commit()
Commits the transaction, and ends the current transaction. final Unit
rollback()
Rolls back the current open transaction. final Boolean
storeExists()
Checks if the database file exists. final static CharArray
toChars(ByteArray bytes)
Converts a non-null/non-empty
byte[] to char[].final static ByteArray
toBytes(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:
true
if the database is open,false
otherwise.
-
isInTransaction
final Boolean isInTransaction()
Checks if a transaction is in progress.
- Returns:
true
if a transaction is in progress,false
otherwise.
-
getDatabase
final SQLiteDatabase getDatabase()
Returns the underlying
net.sqlcipher.database.SQLiteDatabase
instance assuming .SecureDatabaseStore and .open have been called.- Returns:
The underlying
net.sqlcipher.database.SQLiteDatabase
instance, ornull
if this instance ofSecureDatabaseStore
is 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-
SELECT
statement 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-
SELECT
statement 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
INSERT
statement by filling the column names and the associated values in aContentValues
object.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
SELECT
statement that contains parameters ('?').- Parameters:
sql
- aSELECT
statementvalues
- one or more String values to match the '?- Returns:
a
non-null
result set from the query which may or may not contain rows.
-
executeQuery
final SecureDatabaseResultSet executeQuery(String sql)
Executes a
SELECT
statement that does not contains parameters ('?').As a good practice, use .executeQuery if possible.
- Parameters:
sql
- aSELECT
statement- Returns:
a
non-null
result 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:
True
if the database file exists,false
otherwise.
-
toChars
final static CharArray toChars(ByteArray bytes)
Converts a
non-null/non-empty
byte[] to char[].- Parameters:
bytes
- anon-null/non-empty
byte[]- Returns:
A char[] converted from the given byte[].
-
toBytes
final static ByteArray toBytes(CharArray chars)
-
-
-
-