Types
SQLite Types¶
SQLDelight column definitions are identical to regular SQLite column definitions but support an extra column constraint which specifies the Kotlin type of the column in the generated interface.
CREATE TABLE some_types (
some_long INTEGER, -- Stored as INTEGER in db, retrieved as Long
some_double REAL, -- Stored as REAL in db, retrieved as Double
some_string TEXT, -- Stored as TEXT in db, retrieved as String
some_blob BLOB -- Stored as BLOB in db, retrieved as ByteArray
);
Primitives¶
A sibling module that adapts primitives for your convenience.
dependencies {
implementation("app.cash.sqldelight:primitive-adapters:2.0.2")
}
dependencies {
implementation "app.cash.sqldelight:primitive-adapters:2.0.2"
}
The following adapters exist:
FloatColumnAdapter
— Retrieveskotlin.Float
for an SQL type implicitly stored askotlin.Double
IntColumnAdapter
— Retrieveskotlin.Int
for an SQL type implicitly stored askotlin.Long
ShortColumnAdapter
— Retrieveskotlin.Short
for an SQL type implicitly stored askotlin.Long
Custom Column Types¶
If you'd like to retrieve columns as custom types you can specify a Kotlin type:
import kotlin.String;
import kotlin.collections.List;
CREATE TABLE hockeyPlayer (
cup_wins TEXT AS List<String> NOT NULL
);
However, creating the Database
will require you to provide a ColumnAdapter
which knows how to map between the database type and your custom type:
val listOfStringsAdapter = object : ColumnAdapter<List<String>, String> {
override fun decode(databaseValue: String) =
if (databaseValue.isEmpty()) {
listOf()
} else {
databaseValue.split(",")
}
override fun encode(value: List<String>) = value.joinToString(separator = ",")
}
val queryWrapper: Database = Database(
driver = driver,
hockeyPlayerAdapter = hockeyPlayer.Adapter(
cup_winsAdapter = listOfStringsAdapter
)
)
Enums¶
As a convenience the SQLDelight runtime includes a ColumnAdapter
for storing an enum as String data.
import com.example.hockey.HockeyPlayer;
CREATE TABLE hockeyPlayer (
position TEXT AS HockeyPlayer.Position
)
val queryWrapper: Database = Database(
driver = driver,
hockeyPlayerAdapter = HockeyPlayer.Adapter(
positionAdapter = EnumColumnAdapter()
)
)
Value types¶
SQLDelight can generate a value type for a column which wraps the underlying database type if requested:
CREATE TABLE hockeyPlayer (
id INT AS VALUE
);