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. SQLDelight natively supports Long, Double, String, ByteArray, Int, Short, Float, and Booleans.
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 some_int INTEGER AS Int, -- Stored as INTEGER in db, retrieved as Int some_short INTEGER AS Short, -- Stored as INTEGER in db, retrieved as Short some_float REAL AS Float -- Stored as REAL in db, retrieved as Float );
Boolean columns are stored in the db as INTEGER
, and so they can be given INTEGER
column constraints. Use DEFAULT 0
to default to false, for example.
CREATE TABLE hockey_player ( injured INTEGER AS Boolean DEFAULT 0 )
Custom Column Types
If you'd like to retrieve columns as custom types you can specify a Kotlin type:
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) = 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() ) )