SQLDatabase

@ThreadSafe
class SQLDatabase(val path: String, sqlite: SQLite, configuration: DatabaseConfiguration, key: DatabaseKey?, random: IRandom = CommonThreadLocalRandom) : IDatabase, SharedCloseable(source)

The use of ThreadLocal underpins SQLDatabase's thread-safety.

A thread can acquire at most one connection session per database instance. Sessions hold at most one connection at any given time, and exclusively. Sessions control all access to a database's connections and each session is thread-bound. This prevents two threads from ever sharing a single connection.

Parameters marked TrustedSql become part of SQL syntax. They must contain only application-controlled SQL; pass external values through the corresponding bind-argument parameters.

This is the same strategy Google employs in the Android SDK.

See also

Constructors

Link copied to clipboard
constructor(path: String, sqlite: SQLite, configuration: DatabaseConfiguration, key: DatabaseKey?, random: IRandom = CommonThreadLocalRandom)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val inTransaction: Boolean
Link copied to clipboard

Returns true if any database connection managed by this database has been interrupted and has not yet completed. This method is safe to call from any thread.

Link copied to clipboard
Link copied to clipboard
open override var version: Int

Functions

Link copied to clipboard
fun batch(@TrustedSql sql: String, bindArgs: Stream<Array<out Any?>>): Int
fun batch(@TrustedSql sql: String, bindArgs: Iterable<Array<out Any?>>): Int
fun batch(@TrustedSql sql: String, bindArgs: List<Array<out Any?>>): Int
fun batch(@TrustedSql sql: String, bindArgs: Sequence<Array<out Any?>>): Int
fun batch(@TrustedSql sql: String, bindArgs: Array<out Array<out Any?>>, fromIndex: Int = 0, toIndex: Int = bindArgs.size): Int
Link copied to clipboard
Link copied to clipboard
open override fun beginDeferredTransaction()
Link copied to clipboard
open override fun beginExclusiveTransaction()
Link copied to clipboard
open override fun beginImmediateTransaction()
Link copied to clipboard
fun close()
Link copied to clipboard
open override fun compileStatement(@TrustedSql sql: String, bindArgs: Array<out Any?>? = null): ISQLStatement
Link copied to clipboard
fun databaseConfig(op: Int, value: Int)
Link copied to clipboard
open override fun delete(table: String, @TrustedSql whereClause: String, whereArgs: Array<out Any?>): Int
Link copied to clipboard
open override fun endTransaction()
Link copied to clipboard
open override fun exec(@TrustedSql sql: String, bindArgs: Array<out Any?>? = null)
Link copied to clipboard
fun <T> execute(readOnly: Boolean, block: SQLDatabase.() -> T): T
Link copied to clipboard
Link copied to clipboard
open override fun insert(table: String, values: IContentValues, conflictAlgorithm: IConflictAlgorithm): Long
Link copied to clipboard

Inserts rowid-table rows and writes their BLOB values incrementally with bounded memory.

Cancellable counterpart of insertBlobs.

Link copied to clipboard
fun interrupt()

Interrupts all database connections managed by this database. This causes any pending database operations on those connections to abort and return at the earliest opportunity. This method is safe to call from any thread.

Link copied to clipboard
Link copied to clipboard
fun openSession(beforePrimaryConnectionAccess: () -> Unit, onTransactionStateChanged: (Boolean) -> Unit): SQLDatabaseSession

Opens an explicitly owned session with callbacks for coordinating its lifecycle with an external owner. The returned session is not thread-safe and must be closed when no longer needed.

Link copied to clipboard
fun pragma(pragma: SQLitePragma): String
fun pragma(key: String): String
fun pragma(pragma: SQLitePragma, value: Any): String?
fun pragma(key: String, value: Any): String?
Link copied to clipboard
Link copied to clipboard
open override fun query(query: ISQLQuery): ICursor
open override fun query(@TrustedSql sql: String, selectionArgs: Array<out Any?>): ICursor
fun query(@TrustedSql sql: String, bindArgs: ParameterRow, cancellationSignal: CancellationSignal): ICursor
open override fun query(distinct: Boolean, @TrustedSql table: String, @TrustedSql columns: Array<out String>, @TrustedSql selection: String, selectionArgs: Array<out Any?>, @TrustedSql groupBy: String?, @TrustedSql having: String?, @TrustedSql orderBy: String?, limit: Int?): ICursor

fun query(query: ISQLQuery, cancellationSignal: CancellationSignal): ICursor
fun query(@TrustedSql sql: String, selectionArgs: Array<out Any?>, cancellationSignal: CancellationSignal): ICursor
fun query(distinct: Boolean, @TrustedSql table: String, @TrustedSql columns: Array<out String>, @TrustedSql selection: String, selectionArgs: Array<out Any?>, @TrustedSql groupBy: String?, @TrustedSql having: String?, @TrustedSql orderBy: String?, limit: Int?, cancellationSignal: CancellationSignal): ICursor

Executes a cancellable query. If the cancellationSignal is cancelled from another thread, the query will be aborted at the earliest opportunity and an OperationCancelledException will be thrown.

Link copied to clipboard
fun queryForwardOnly(@TrustedSql sql: String, selectionArgs: Array<out Any?>): ICursor

Returns a forward-only streaming ICursor backed by a ForwardCursor. The caller must close the returned cursor to release the underlying prepared statement back to the connection's cache and to release the session's connection back to the pool.

fun queryForwardOnly(@TrustedSql sql: String, bindArgs: ParameterRow, cancellationSignal: CancellationSignal): ICursor

fun queryForwardOnly(@TrustedSql sql: String, selectionArgs: Array<out Any?>, cancellationSignal: CancellationSignal): ICursor

Cancellable counterpart of queryForwardOnly.

Link copied to clipboard
fun queryUpTo(@TrustedSql sql: String, bindArgs: ParameterRow, maximumRows: Int, cancellationSignal: CancellationSignal): ICursor

fun queryUpTo(@TrustedSql sql: String, selectionArgs: Array<out Any?>, maximumRows: Int, cancellationSignal: CancellationSignal): ICursor

Executes a cancellable query while stepping no more than maximumRows.

Link copied to clipboard
fun readFromBlob(table: String, column: String, row: Long, offset: Int, limit: Int, stream: OutputStream)
fun readFromBlob(name: String, table: String, column: String, row: Long, offset: Int, limit: Int, stream: OutputStream)
Link copied to clipboard
fun release()
Link copied to clipboard
fun releaseMemory(priority: Priority)
Link copied to clipboard
Link copied to clipboard
fun retain()
Link copied to clipboard
Link copied to clipboard
fun setProgressHandler(instructionCount: Int, handler: SQLProgressHandler?)

Registers a progress handler that is invoked periodically during long-running SQL statements on all database connections managed by this database. If the handler returns non-zero, the operation is interrupted.

Link copied to clipboard
fun setSavepoint(name: String? = null): String
Link copied to clipboard
open override fun setTransactionSuccessful()
Link copied to clipboard
fun sizeOfBlob(table: String, column: String, row: Long): Int
fun sizeOfBlob(name: String, table: String, column: String, row: Long): Int
Link copied to clipboard
fun <T> transact(transactionMode: SQLiteTransactionMode = SQLiteTransactionMode.EXCLUSIVE, block: SQLDatabase.() -> T): T
fun <D, T> transact(database: D, transactionMode: SQLiteTransactionMode, block: D.() -> T): T
fun <T> transact(listener: SQLTransactionListener, transactionMode: SQLiteTransactionMode = SQLiteTransactionMode.EXCLUSIVE, block: SQLDatabase.() -> T): T
Link copied to clipboard
Link copied to clipboard
open override fun update(table: String, values: IContentValues, @TrustedSql whereClause: String, whereArgs: Array<out Any?>, conflictAlgorithm: IConflictAlgorithm): Int
Link copied to clipboard
open override fun upsert(table: String, insertValues: IContentValues, columns: Array<out String>, updateValues: IContentValues): Long
Link copied to clipboard
fun <T> withCancellationSignal(cancellationSignal: CancellationSignal, primary: Boolean = false, block: SQLDatabase.() -> T): T
Link copied to clipboard
fun writeToBlob(table: String, column: String, row: Long, offset: Int, stream: InputStream)
fun writeToBlob(name: String, table: String, column: String, row: Long, offset: Int, stream: InputStream)
Link copied to clipboard
open override fun yieldTransaction(): Boolean
open override fun yieldTransaction(pauseMillis: Long): Boolean