SQLiteDatabase

SQLite Database

Describes the concept of a database as driven by the SQLite engine; SQLite databases can be held in-memory, or backed by a file which is persisted to disk.

The SQLiteDatabase is the primary interface through which all database operations are performed, including queries, transactions, and schema operations.

 

Database Lifecycle

There are essentially three phases to a SQLite database as understood by Elide: initialized, opened, and closed.

When the database is in an initialized state, it has not yet loaded data from disk or memory (via serialization).

Initialization takes place immediately when the database object is created, and is guided by the configuration parameters which can be provided to the constructor.

The database moves almost immediately to an opened state, where it is ready to accept queries, transactions, or deserialization from in-memory data.

When the database is closed (via the close method), all associated resources are freed, and the database may no longer be used for queries or transactions of any kind; such transactions (placed after close) throw unconditional exceptions.

 

Managed Resources

All resources associated with a given SQLite database are managed by that database; this includes query ephemeral, like prepared statements, transactions, and associated objects.

When a database is closed, this tree of objects is closed (where applicable) in a reasonable order.

After closing, resources are freed, with the only exception being result objects de-serialized for use in the user's application.

These objects remain alive so long as the user's application maintains references to them; database references held by ephemeral objects are always weakly referenced.

 

Guest Usage

SQLite leverages a constrained set of primitive data types (see SQLitePrimitiveType) to represent data within user tables; as a result, SQLite data types work well with guest code out of the box.

Wrapping and boxing types, where required, use proxy interfaces so that they interoperate as expected with guest code; by and large, Elide's SQLite API for guests follows Bun's API in JavaScript.

See also

Module-level SQLite API

Statement API

Transaction API

Transactor Interface

SQLite Types

Types

Link copied to clipboard
object Defaults

Hard-coded defaults for SQLite.

Properties

Link copied to clipboard
abstract val active: Boolean

Indicate whether the database is open and active; if false, queries will throw.

Functions

Link copied to clipboard
open override fun close()

abstract fun close(throwOnError: Boolean)

Close the underlying connection backing this database, and persist the current database data if so configured via the path provided to the database constructor.

Link copied to clipboard
abstract fun connection(): SQLiteConnection

Obtain the underlying SQLite JDBC connection; this method is provided for host-side use only.

Link copied to clipboard
abstract fun deserialize(data: ByteArray, schema: String = MAIN_SCHEMA)

De-serialize an array of raw bytes previously emitted via serialize, loading the resulting data into the current SQLite database; the schema parameter is used to specify the schema to de-serialize (if none is provided, the main schema is used).

Link copied to clipboard
abstract fun dispose()

TBD.

Link copied to clipboard
abstract fun exec(statement: SQLiteStatement, vararg args: Any?)

Execute the provided statement, preparing it if necessary, with the provided args (if any), against the current SQLite database.

abstract fun exec(@Language(value = "sql") statement: String, vararg args: Any?)

Parse, prepare, and then execute an SQL query statement with the provided args (if any), against the current SQLite database.

Link copied to clipboard
abstract fun getMember(key: String): Any
Link copied to clipboard
open override fun getMemberKeys(): Array<String>
Link copied to clipboard
open override fun hasMember(key: String?): Boolean
Link copied to clipboard
abstract fun loadExtension(extension: String)

Load a native extension into the SQLite engine context managed by this database proxy; the extension is loaded via the native interface for SQLite.

Link copied to clipboard
abstract fun prepare(@Language(value = "sql") statement: String, vararg args: Any?): SQLiteStatement

Parse and prepare a query statement for execution; the resulting statement can be used multiple times, with intelligent caching for re-use.

Link copied to clipboard
open override fun putMember(key: String?, value: Value?)
Link copied to clipboard
abstract fun query(statement: SQLiteStatement, vararg args: Any?): SQLiteStatement
abstract fun query(@Language(value = "sql") statement: String, vararg args: Any?): SQLiteStatement

Parse a query statement for execution; the resulting statement can be used multiple times, with intelligent caching for re-use.

Link copied to clipboard
open override fun removeMember(key: String?): Boolean
Link copied to clipboard
abstract fun serialize(schema: String = MAIN_SCHEMA): ByteArray

Serialize the current database into a raw array of bytes; the resulting bytes can be persisted to disk, or sent across a network connection, and de-serialized into an equivalently behaving database object at a later time or on another peer.

Link copied to clipboard
abstract fun <R> transaction(runnable: SQLiteTransactor<R>): SQLiteTransaction<R>

Execute the provided runnable transaction function against the current SQLite database, applying recovery and exclusion logic where applicable; the return value R yielded by the transaction function is returned to the caller, via the resolution of the provided SQLiteTransaction.

Link copied to clipboard
abstract fun unwrap(): DB

Unwrap the underlying SQLite database; this method is provided for host-side use only.