Package-level declarations

import androidx.sqlite.SQLiteConnection
import androidx.sqlite.driver.bundled.SQLITE_OPEN_READONLY
import com.kqlite.database.KQLiteDatabase
import com.kqlite.database.KQLiteDriver
import com.kqlite.demo.contacts.model.ContactType
import com.kqlite.demo.contacts.platform.getDatabaseAbsolutePath
import com.kqlite.functions.DATE
import com.kqlite.functions.JSON_ARRAY
import com.kqlite.migration.KQLiteMigration
import com.kqlite.pragma.JournalMode
import com.kqlite.pragma.KQLitePragma
import com.kqlite.statement.quickInsert
import com.kqlite.table.KQLiteTable

class ContactsDatabase(
kqLiteDriver: KQLiteDriver = ContactsDatabaseDriver(getDatabaseAbsolutePath(NAME))
) : KQLiteDatabase(kqLiteDriver = kqLiteDriver) {

companion object {
const val NAME = "contacts.db"
const val VERSION = 2
}

override fun getKQLiteTables(): List<KQLiteTable> {
return listOf(TblContact)
}

override fun onConfigure(pragma: KQLitePragma) {
super.onConfigure(pragma)
pragma.foreignKeys.set(true)
pragma.journalMode.set(JournalMode.WAL)
}

override fun getKQLiteMigrations(): List<KQLiteMigration> {
return listOf(Migration12())
}

fun openReadWrite() = this.open(null)

fun openReadOnly() = this.open(SQLITE_OPEN_READONLY)

override fun onCreate(connection: SQLiteConnection) {
super.onCreate(connection)
println("Database created.")
TblContact.quickInsert {
it.firstName.bind("John")
it.lastName.bind("Doe")
it.phone.bind(JSON_ARRAY("1234567890"))
it.type.bind(ContactType.Family)
it.birthDate.bind(DATE("2000-01-01"))
}
}

override fun onOpen(connection: SQLiteConnection) {
super.open()
println("Database opened.")
}

override fun close() {
super.close()
println("Database closed.")
}
}

Types

Link copied to clipboard
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.

Link copied to clipboard
interface KQLiteDriver

A driver for creating and managing connections to a KQLiteDatabase.

Link copied to clipboard
sealed class SQLiteFunc

Provides a collection of utility functions that correspond to SQLite's built-in core functions. These functions allow for retrieving metadata and status information directly from the SQLite engine, such as the library version, change counts, compile-time options, and more.

Link copied to clipboard

Specifies the behavior of a database transaction.