RoomDatabase

abstract class RoomDatabase
kotlin.Any
   ↳ androidx.room.RoomDatabase

Base class for all Room databases. All classes that are annotated with Database must extend this class.

RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes.

Summary

Nested classes

Journal modes for SQLite database.

open

Builder for RoomDatabase.

open

A container to hold migrations.

abstract

Callback for RoomDatabase.

Public constructors

Creates a RoomDatabase.

Public methods
open SupportSQLiteStatement!
compileStatement(@NonNull sql: String)

Wrapper for SupportSQLiteDatabase#compileStatement(String).

open Unit
init(@NonNull configuration: DatabaseConfiguration)

Called by Room when it is initialized.

open Cursor!
query(query: String!, @Nullable args: Array<Any!>?)

Convenience method to query the database with arguments.

open Cursor!

Wrapper for SupportSQLiteDatabase#query(SupportSQLiteQuery).

open Unit

Wrapper for SupportSQLiteDatabase#endTransaction().

open Unit

Wrapper for SupportSQLiteDatabase#beginTransaction().

open SupportSQLiteOpenHelper

Returns the SQLite open helper used by this database.

open InvalidationTracker

Returns the invalidation tracker for this database.

open Boolean

Returns true if database connection is open and initialized.

open Unit

Closes the database if it is already open.

open Unit

Wrapper for SupportSQLiteDatabase#setTransactionSuccessful().

open Boolean

Returns true if current thread is in a transaction.

open Executor

open Unit
runInTransaction(@NonNull body: Runnable)

Executes the specified Runnable in a database transaction.

open V
runInTransaction(@NonNull body: Callable<V>)

Executes the specified Callable in a database transaction.

abstract Unit

Deletes all rows from all the tables that are registered to this database as Database#entities().

Protected methods
open Unit

Called by the generated code when database is open.

abstract SupportSQLiteOpenHelper

Creates the open helper to access the database.

abstract InvalidationTracker

Called when the RoomDatabase is created.

Properties
SupportSQLiteDatabase!

Set by the generated open helper.

MutableList<RoomDatabase.Callback!>!

Public constructors

<init>

RoomDatabase()

Creates a RoomDatabase.

You cannot create an instance of a database, instead, you should acquire it via Room#databaseBuilder(Context, Class, String) or Room#inMemoryDatabaseBuilder(Context, Class).

Public methods

compileStatement

open fun compileStatement(@NonNull sql: String): SupportSQLiteStatement!

Wrapper for SupportSQLiteDatabase#compileStatement(String).

Parameters
sql String: The query to compile.
Return
SupportSQLiteStatement!: The compiled query.

init

@CallSuper open fun init(@NonNull configuration: DatabaseConfiguration): Unit

Called by Room when it is initialized.

Parameters
configuration DatabaseConfiguration: The database configuration.

query

open fun query(query: String!, @Nullable args: Array<Any!>?): Cursor!

Convenience method to query the database with arguments.

Parameters
query String!: The sql query
args String!: The bind arguments for the placeholders in the query
Return
Cursor!: A Cursor obtained by running the given query in the Room database.

query

open fun query(query: SupportSQLiteQuery!): Cursor!

Wrapper for SupportSQLiteDatabase#query(SupportSQLiteQuery).

Parameters
query SupportSQLiteQuery!: The Query which includes the SQL and a bind callback for bind arguments.
Return
Cursor!: Result of the query.

endTransaction

open fun endTransaction(): Unit

Wrapper for SupportSQLiteDatabase#endTransaction().

beginTransaction

open fun beginTransaction(): Unit

Wrapper for SupportSQLiteDatabase#beginTransaction().

getOpenHelper

@NonNull open fun getOpenHelper(): SupportSQLiteOpenHelper

Returns the SQLite open helper used by this database.

Return
SupportSQLiteOpenHelper: The SQLite open helper used by this database.

getInvalidationTracker

@NonNull open fun getInvalidationTracker(): InvalidationTracker

Returns the invalidation tracker for this database.

You can use the invalidation tracker to get notified when certain tables in the database are modified.

Return
InvalidationTracker: The invalidation tracker for the database.

isOpen

open fun isOpen(): Boolean

Returns true if database connection is open and initialized.

Return
Boolean: true if the database connection is open, false otherwise.

close

open fun close(): Unit

Closes the database if it is already open.

setTransactionSuccessful

open fun setTransactionSuccessful(): Unit

Wrapper for SupportSQLiteDatabase#setTransactionSuccessful().

inTransaction

open fun inTransaction(): Boolean

Returns true if current thread is in a transaction.

Return
Boolean: True if there is an active transaction in current thread, false otherwise.

getQueryExecutor

@NonNull open fun getQueryExecutor(): Executor
Return
Executor: The Executor in use by this database for async queries.

runInTransaction

open fun runInTransaction(@NonNull body: Runnable): Unit

Executes the specified Runnable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Runnable.

Parameters
body Runnable: The piece of code to execute.

runInTransaction

open fun <V : Any!> runInTransaction(@NonNull body: Callable<V>): V

Executes the specified Callable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Callable.

Parameters
body Callable<V>: The piece of code to execute.
<V> Callable<V>: The type of the return value.
Return
V: The value returned from the Callable.

clearAllTables

@WorkerThread abstract fun clearAllTables(): Unit

Deletes all rows from all the tables that are registered to this database as Database#entities().

This does NOT reset the auto-increment value generated by PrimaryKey#autoGenerate().

After deleting the rows, Room will set a WAL checkpoint and run VACUUM. This means that the data is completely erased. The space will be reclaimed by the system if the amount surpasses the threshold of database file size.

See Also

  • <a href="https://www.sqlite.org/fileformat.html">Database File Format</a>

Protected methods

internalInitInvalidationTracker

protected open fun internalInitInvalidationTracker(@NonNull db: SupportSQLiteDatabase): Unit

Called by the generated code when database is open.

You should never call this method manually.

Parameters
db SupportSQLiteDatabase: The database instance.

createOpenHelper

@NonNull protected abstract fun createOpenHelper(config: DatabaseConfiguration!): SupportSQLiteOpenHelper

Creates the open helper to access the database. Generated class already implements this method. Note that this method is called when the RoomDatabase is initialized.

Parameters
config DatabaseConfiguration!: The configuration of the Room database.
Return
SupportSQLiteOpenHelper: A new SupportSQLiteOpenHelper to be used while connecting to the database.

createInvalidationTracker

@NonNull protected abstract fun createInvalidationTracker(): InvalidationTracker

Called when the RoomDatabase is created.

This is already implemented by the generated code.

Return
InvalidationTracker: Creates a new InvalidationTracker.

Properties

mDatabase

protected var mDatabase: SupportSQLiteDatabase!

Set by the generated open helper.

mCallbacks

@Nullable protected var mCallbacks: MutableList<RoomDatabase.Callback!>!