KQLite Database
This abstract class represents a KQLite database and manages the database file, its version, and the lifecycle of the database connection, including schema creation, upgrades, and downgrades. Subclasses must provide KQLiteDriver implementation, list of KQLiteTables to create database schema and optionally define migration paths.
The core responsibilities of this class are:
Connection Management: open and close the database connection using a KQLiteDriver.
Connection configuration: onConfigure is called on every connection open.
Schema Creation: Creating the initial database tables when the database is first created, based on the list provided by getKQLiteTables.
Versioning and Migration: Handling schema upgrades and downgrades by executing KQLiteMigration when the provided version differs from the one stored in the database file
PRAGMA user_versionTransaction Control: Providing methods like withTransaction and withSavepoint for atomic operations. Checking transactions using inTransaction
Access to Utilities: Offering access to KQLitePragma for configuration, KQLiteSchema for manual schema manipulation and getConnection for custom queries.
A typical implementation looks like this:
class MyDatabase(driver = MyBundledSQLiteDriver()) : KQLiteDatabase(driver) {
override fun getKQLiteTables(): List<KQLiteTable> {
// Define the tables for initial creation
return listOf(UserTable, ProductTable)
}Author
MOHAMMAD AZIM ANSARI
Parameters
The KQLiteDriver implementation for database operations. Upgrade/Downgrade depends upon this version. onConfigure and KQLiteMigrations will not be preformed for version = 0
See also
Functions
This method closes the SQLiteConnection that was opened by the open method and clears the internal cached reference to it.
Returns the SQLiteConnection instance that was most recently opened by the call to the open method. The connection is cached internally for the lifetime of the KQLiteDatabase instance or until close is called.
Used when database upgrade or downgrade is required by comparing PRAGMA user_version and given version to KQLiteDatabase.
Specifies the list of KQLiteTable to be created in the database for the first time (i.e., when the database file does not exist or its version is 0). Each KQLiteTable in the returned list will be used to generate and execute a CREATE TABLE statement.
Returns true if a transaction is active on the specified or last connection, false otherwise.
Called when the database is opened, after the connection is established but before the schema is upgraded or downgraded. It is invoked every time a database connection is opened except when KQLiteDatabase version is 0.
Called when the database is created for the first time. This invokes after all tables returned by getKQLiteTables have been created.
Called when the database has been opened.
Opens a connection to the database using KQLiteDriver.open and handles schema creation, upgrades, or downgrades if required.
Returns a KQLitePragma instance for configuring database PRAGMA settings.
Returns a KQLiteSchema instance for performing schema-related operations like creating or dropping tables.
Executes the given block of work within a named savepoint savepointName.
Executes the given block of work in a database transaction.