KQLiteDatabase

abstract class KQLiteDatabase(kqLiteDriver: KQLiteDriver) : SQLiteFunc, AutoCloseable

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_version

  • Transaction 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

kqLiteDriver

The KQLiteDriver implementation for database operations. Upgrade/Downgrade depends upon this version. onConfigure and KQLiteMigrations will not be preformed for version = 0

See also

Constructors

Link copied to clipboard
constructor(kqLiteDriver: KQLiteDriver)

Functions

Link copied to clipboard
open override fun close()

This method closes the SQLiteConnection that was opened by the open method and clears the internal cached reference to it.

Link copied to clipboard

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.

Link copied to clipboard
protected open fun getKQLiteMigrations(): List<KQLiteMigration>?

Used when database upgrade or downgrade is required by comparing PRAGMA user_version and given version to KQLiteDatabase.

Link copied to clipboard

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.

Link copied to clipboard

Returns true if a transaction is active on the specified or last connection, false otherwise.

Link copied to clipboard
protected open fun onConfigure(pragma: KQLitePragma)

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.

Link copied to clipboard
protected open fun onCreate(connection: SQLiteConnection)

Called when the database is created for the first time. This invokes after all tables returned by getKQLiteTables have been created.

Link copied to clipboard
protected open fun onOpen(connection: SQLiteConnection)

Called when the database has been opened.

Link copied to clipboard
fun open(flags: Int? = null): SQLiteConnection

Opens a connection to the database using KQLiteDriver.open and handles schema creation, upgrades, or downgrades if required.

Link copied to clipboard

Returns a KQLitePragma instance for configuring database PRAGMA settings.

Link copied to clipboard

Returns a KQLiteSchema instance for performing schema-related operations like creating or dropping tables.

Link copied to clipboard
fun <R> withSavepoint(savepointName: String, connection: SQLiteConnection? = null, work: () -> R): R

Executes the given block of work within a named savepoint savepointName.

Link copied to clipboard
fun <R> withTransaction(type: TransactionType? = null, connection: SQLiteConnection? = null, work: () -> R): R

Executes the given block of work in a database transaction.