SQLiteStorage

Base implementation for SQLite storage that uses SQLCipher for encrypted storage. This class provides a secure storage solution with a generic approach for different data types. It supports multiple tables through a table registration mechanism.

This is an abstract base class that provides common SQLite database functionality. Subclasses should implement their specific storage operations based on their table structure.

Parameters

config

Configuration object containing all database settings. See SQLiteStorageConfig for details.

Constructors

Link copied to clipboard
constructor(config: SQLiteStorageConfig)

Types

Link copied to clipboard
object Companion
Link copied to clipboard
protected open inner class DatabaseHelper(context: Context, databaseName: String, version: Int) : SQLiteOpenHelper

Helper class for creating and managing the SQLite database. This class extends SQLiteOpenHelper which is required for proper SQLite database management in Android. It provides hooks for database creation and version upgrades, but delegates the actual table creation to the registered table creator functions.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
protected val backupOnError: Boolean
Link copied to clipboard
protected val context: Context
Link copied to clipboard
val database: SQLiteDatabase
Link copied to clipboard
protected val databaseName: String
Link copied to clipboard
protected val databaseVersion: Int
Link copied to clipboard
protected lateinit var dbHelper: SQLiteOpenHelper
Link copied to clipboard
protected open val logger: Logger
Link copied to clipboard
protected val maxBackupCount: Int
Link copied to clipboard
protected val onDatabaseError: suspend (Exception, Boolean) -> Unit?
Link copied to clipboard

Functions

Link copied to clipboard
open suspend fun attemptBackupRestoration(): Boolean

Attempt to restore the database from the most recent backup.

Link copied to clipboard

Begin a database transaction.

Link copied to clipboard
protected fun checkDatabase()

Check if the database is open and throw an exception if it's not. This provides a more informative error message than the default UninitializedPropertyAccessException.

Link copied to clipboard
protected open suspend fun cleanOldBackups()

Remove old backup files exceeding the configured maximum backup count.

Link copied to clipboard
suspend fun closeDatabase()

Close the database connection.

Link copied to clipboard
open suspend fun createDatabaseBackup(): File?

Create a backup of the current database file. The backup file will be encrypted with the same passphrase as the original database.

Link copied to clipboard
protected open fun createDatabaseHelper(context: Context, databaseName: String, version: Int): SQLiteOpenHelper

Factory method to create the appropriate database helper. This method follows the factory method pattern to allow subclasses to provide their own DatabaseHelper implementation without modifying the base class.

Link copied to clipboard

End the current transaction.

Link copied to clipboard
protected suspend fun <T> executeOnIO(operation: suspend () -> T): T

Execute a database operation on the IO dispatcher. This is a helper method for subclasses to use when implementing their own database operations.

Link copied to clipboard
fun executeSQL(sql: String, args: Array<Any?>)

Execute SQL with the given arguments. This is a convenience method for executing SQL statements.

Link copied to clipboard
protected open fun getBackupFileName(timestamp: Long): String

Generate a backup filename with timestamp. Format: {databaseName}backup{timestamp}.db

Link copied to clipboard
suspend fun initializeDatabase()

Initialize the SQL database storage. This method creates the database and tables if they don't exist. If the database cannot be opened, it will attempt to restore from backup if available.

Link copied to clipboard
open suspend fun listBackupFiles(): List<File>

List all backup files for the current database, sorted by timestamp (newest first).

Link copied to clipboard
protected open fun parseBackupTimestamp(filename: String): Long?

Parse the timestamp from a backup filename.

Link copied to clipboard
fun <T> query(sql: String, args: Array<String>?, mapper: (Cursor) -> T): List<T>

Execute a query with the given arguments and map the results.

Link copied to clipboard
fun registerTableCreator(creator: (SQLiteDatabase) -> Unit)

Register a table creator function that will be called when the database is created. This allows multiple tables to be created in the same database.

Link copied to clipboard

Mark the current transaction as successful.