A Multiplatform Delight (KotlinConf 2018)

A Multiplatform Delight (KotlinConf 2018)

SQL Delight, a type-safe database API, recently completed migration from being a Java-generating, Android-specific tool to a Kotlin-generating, multiplatform one. Migrating an API from Java to Kotlin has obvious benefits, but adding multiplatform support for iOS introduces a dynamic which complicates the API, code generation, and runtime.

This talk will cover the challenges of platform-agnostic API design, type-safe multiplatform Kotlin code generation, and the integration of platform-specific runtimes such that the library not only runs efficiently on each platform but also integrates well with the other languages each might be using.

Presented with Alec Strong.

Video: https://youtu.be/WkIry790PHI

E68309f117985270285ade8082f4877d?s=128

Jake Wharton

October 04, 2018
Tweet

Transcript

  1. 4.

    @Entity data class Player(A val name: String, val ranking: Int,

    val country: String )A @Entity data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  2. 5.

    Source Code Annotation
 Processor Reflection @Entity data class Player(A val

    name: String, val ranking: Int, val country: String )A @Entity data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  3. 6.

    Source Code Annotation
 Processor Reflection SQLite @Entity data class Player(A

    val name: String, val ranking: Int, val country: String )A @Entity data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  4. 7.

    CREATE TABLE player (A name TEXT NOT NULL, country TEXT

    NOT NULL, ranking INTEGER NOT NULL ); CREATE TABLE match (A player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player );
  5. 8.

    CREATE TABLE player (A id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); CREATE TABLE match (A player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player );B
  6. 9.

    CREATE TABLE player (A id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); CREATE TABLE match (A player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player, PRIMARY KEY (player1, player2) ) WITHOUT ROWID;B
  7. 10.

    SQLite Source Code Annotation
 Processor Reflection @Entity data class Player(

    val name: String, val ranking: Int, val country: String ) @Entity data class Match( val player1: Player, val player2: Player, val winner: Player ) CREATE TABLE player (A id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); CREATE TABLE match (A player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player, PRIMARY KEY (player1, player2) ) WITHOUT ROWID;
  8. 11.

    @Entity data class Player(A val name: String, val ranking: Int,

    val country: String )A @Entity data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  9. 12.

    @Entity data class Player(A @PrimaryKey @Autoincrement val id: Int, val

    name: String, val ranking: Int, val country: String )A @Entity data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  10. 13.

    @Entity data class Player(A @PrimaryKey @Autoincrement val id: Int, val

    name: String, val ranking: Int, val country: String )A @Entity(primaryKeys = ['player1', ‘player2']) data class Match(A val player1: Player, val player2: Player, val winner: Player )A
  11. 14.

    @Entity data class Player(A @PrimaryKey @Autoincrement val id: Int, val

    name: String, val ranking: Int, val country: String )A @Entity(primaryKeys = ['player1', ‘player2']) data class Match(A val player1: Player, val player2: Player, val winner: Player )A A @PrimaryKey @Autoincrement val id: Int, @Entity(primaryKeys = ['player1', ‘player2'])
  12. 15.

    ALTER TABLE player ADD COLUMN id INTEGER NOT NULL PRIMARY

    KEY AUTOINCREMENT; CREATE TABLE new_match ( player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player, PRIMARY KEY (player1, player2) ) WITHOUT ROWID; -- migrate match to new_match
  13. 17.
  14. 22.

    CREATE TABLE player (A id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL );
  15. 23.

    CREATE TABLE player (A id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?;
  16. 24.

    SQLite Compiler Generated Code CREATE TABLE player (A id INTEGER

    NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?;
  17. 25.

    interface PlayerModel {A long id(); @NonNull String name(); @NonNull String

    country(); long ranking(); final class Factory<T extends PlayerModel> {A public Factory(Creator<T> creator); public SqlDelightQuery withRanking(long ranking); public RowMapper<T> withRankingMapper(); }A }A
  18. 26.

    interface PlayerModel {A … final class Factory<T extends PlayerModel> {A

    public Factory(Creator<T> creator); … }B }A @AutoValue public abstract class MyPlayerModel implements PlayerModel {A public static Factory<MyPlayerModel> FACTORY = new Factory(AutoValue_MyPlayerModel::new) }A
  19. 27.

    SqlDelightStatement query = MyPlayerModel.FACTORY.withRanking(10); Observable<List<MyPlayerModel>> rank10 = db.createQuery(query.tables, query.statement, query.args)

    .mapToList(MyPlayerModel.FACTORY::withRankingMapper); interface PlayerModel {A … final class Factory<T extends PlayerModel> {A public Factory(Creator<T> creator); public SqlDelightQuery withRanking(long ranking); public RowMapper<T> withRankingMapper(); }B }A
  20. 29.
  21. 32.

    SQLite Compiler Generated Code CREATE TABLE player ( id INTEGER

    NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; insertPlayer: INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  22. 33.

    CREATE TABLE player ( id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; insertPlayer: INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  23. 34.

    SQLite Compiler Generated Code CREATE TABLE player ( id INTEGER

    NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; insertPlayer: INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  24. 35.

    interface Player {A val id: Long val name: String val

    country: String val ranking: Long data class Impl(A override val id: Long, override val name: String, override val country: String, override val ranking: Long ) : Player }A
  25. 36.

    class PlayerQueries : Transacter {A fun <T : Any> withRanking(

    ranking: Long, mapper: ( id: Long, name: String, country: String, ranking: Long ) -> T ): Query<T> fun withRanking(ranking: Long): Query<Player> fun insertPlayer( name: String, country: String, ranking: Long ): Long }A
  26. 37.

    headToHead: SELECT player1.name AS player1Name, player2.name AS player2Name, count(nullif(match.winner =

    player1.id, 0)) AS player1Wins, count(nullif(match.winner = player2.id, 0)) AS player2Wins FROM match JOIN player AS player1 ON (player1.name = :firstPlayerName AND (player1.id = match.player1 OR player1.id = match.player2) ) JOIN player AS player2 ON (player2.name = :secondPlayerName AND (player2.id = match.player1 OR player2.id = match.player2) ) ;
  27. 38.

    class PlayerQueries : Transacter {A fun <T : Any> headToHead(

    firstPlayerName: String, secondPlayerName: String, mapper: ( player1Name: String, player2Name: String, player1Wins: Long, player2Wins: Long ) -> T ): Query<T> fun headToHead( firstPlayerName: String, secondPlayerName: String ): Query<com.sample.tennis.db.HeadToHead> }A
  28. 39.

    class PlayerQueries : Transacter {A fun <T : Any> withRanking(

    ranking: Long, mapper: ( id: Long, name: String, country: String, ranking: Long ) -> T ): Query<T> fun withRanking(ranking: Long): Query<Player> fun insertPlayer( name: String, country: String, ranking: Long ): Long }A
  29. 40.

    class PlayerQueries : Transacter {A fun withRanking(ranking: Long): Query<Player> }A

    val player: Query<Player> = playerQueries.withRanking(10)
  30. 41.

    Query< > class PlayerQueries : Transacter {A fun withRanking(ranking: Long):

    Query<Player> }A val player: Player = playerQueries.withRanking(10).executeAsOne()
  31. 42.

    class PlayerQueries : Transacter {A fun withRanking(ranking: Long): Query<Player> }A

    val player: Observable<Query<Player>> = playerQueries.withRanking(10) .asObservable() .ex
  32. 43.

    class PlayerQueries : Transacter {A fun withRanking(ranking: Long): Query<Player> }A

    val player: Observable<List<Player>> = playerQueries.withRanking(10) .asObservable() .mapToList()
  33. 44.

    class PlayerQueries : Transacter {A fun <T : Any> withRanking(ranking:

    Long, mapper: ( id: Long, name: String, country: String, ranking: Long ) -> T): Query<T> }A val player = playerQueries.withRanking(10, ::MyPlayer) P l a y e r
  34. 45.

    class PlayerQueries : Transacter {A }A val id = playerQueries.insertPlayer(

    name = "Roger Federer", country = "Switzerland", ranking = 2 ) fun insertPlayer( name: String, country: String, ranking: Long ): Long
  35. 46.

    Generated Code SQLite Compiler CREATE TABLE player (A id INTEGER

    NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; class PlayerQueries : Transacter {A fun insertPlayer( name: String, country: String, ranking: Long ): Long }A
  36. 47.

    CREATE TABLE player ( id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country NOT NULL, ranking INTEGER NOT NULL ); TEXT
  37. 48.

    CREATE TABLE player ( id INTEGER NOT NULL PRIMARY KEY

    AUTOINCREMENT, name TEXT NOT NULL, country NOT NULL, ranking INTEGER NOT NULL ); import com.tennis.db.CountryCode; TEXT AS CountryCode
  38. 49.

    import com.tennis.db.CountryCode; CREATE TABLE player ( id INTEGER NOT NULL

    PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT AS CountryCode NOT NULL, ranking INTEGER NOT NULL );
  39. 50.

    SQLite Compiler Generated Code import com.tennis.db.CountryCode; CREATE TABLE player (

    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT AS CountryCode NOT NULL, ranking INTEGER NOT NULL );
  40. 52.

    class QueryWrapper( database: SqlDatabase, internal val playerAdapter: Player.Adapter ) {A

    val playerQueries: PlayerQueries }A interface Player {A class Adapter( val countryAdapter: ColumnAdapter<CountryCode, String> ) }A
  41. 53.

    class QueryWrapper( database: SqlDatabase, internal val playerAdapter: Player.Adapter ) {A

    val playerQueries: PlayerQueries }A val queryWrapper = QueryWrapper( database = database, playerAdapter = Player.Adapter( countryAdapter = EnumColumnAdapter() ), )
  42. 54.

    class QueryWrapper( database: SqlDatabase, internal val playerAdapter: Player.Adapter ) {A

    val playerQueries: PlayerQueries }A val queryWrapper = QueryWrapper( database = database, playerAdapter = Player.Adapter( countryAdapter = EnumColumnAdapter() ), ) val playerQueries: PlayerQueries = queryWrapper.playerQueries val playerQueries: PlayerQueries = queryWrapper.playerQueries val playerQueries: PlayerQueries
  43. 55.

    class QueryWrapper {A object Schema : SqlDatabase.Schema {A override val

    version: Int get() = 2 override fun create(db: SqlDatabaseConnection) override fun migrate( db: SqlDatabaseConnection, oldVersion: Int, newVersion: Int ) }A }A
  44. 56.

    SQLite Generated Code Compiler CREATE TABLE player (A id INTEGER

    NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; class QueryWrapper {A object Schema : SqlDatabase.Schema {A override val version: Int get() = 2 override fun create(db: SqlDatabaseConnection) override fun migrate( db: SqlDatabaseConnection, oldVersion: Int, newVersion: Int ) }A }A
  45. 58.

    ALTER TABLE player ADD COLUMN id INTEGER NOT NULL PRIMARY

    KEY AUTOINCREMENT; CREATE TABLE new_match ( player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player, PRIMARY KEY (player1, player2) ) WITHOUT ROWID; -- migrate match to new_match
  46. 59.

    SQLite Migrations SQLite Generated Code Compiler ALTER TABLE player ADD

    COLUMN id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT; CREATE TABLE new_match ( player1 INTEGER NOT NULL REFERENCES player, player2 INTEGER NOT NULL REFERENCES player, winner INTEGER NOT NULL REFERENCES player, PRIMARY KEY (player1, player2) ) WITHOUT ROWID; -- migrate match to new_match
  47. 63.
  48. 65.

    db

  49. 66.

    SQLite Generated Code SQL Delight CREATE TABLE player (A id

    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, country TEXT NOT NULL, ranking INTEGER NOT NULL ); withRanking: SELECT * FROM player WHERE ranking = ?; db
  50. 81.

    android.database.* android.database.sqlite.* sqlite3.h Node? WASM? Vanilla JS? Generated Code Android

    Native JavaScript Database Abstraction java.sql.* org.sqlite.* (xerial) JVM
  51. 90.

    android.database.* android.database.sqlite.* sqlite3.h Node? WASM? Vanilla JS? Generated Code Android

    JVM Native JavaScript Database Abstraction java.sql.* org.sqlite.* (xerial) S Sql Sql Tran T S Q
  52. 91.

    android.database.* android.database.sqlite.* sqlite3.h Node? WASM? Vanilla JS? Generated Code Android

    Native JavaScript Database Abstraction java.sql.* org.sqlite.* (xerial) Android Driver knarch.db Driver JDBC Driver JS Driver JVM
  53. 93.

    android.database.* android.database.sqlite.* sqlite3.h Android Native java.sql.* org.sqlite.* (xerial) Android Driver

    knarch.db Driver JDBC Driver JS JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl
  54. 94.

    android.database.* android.database.sqlite.* sqlite3.h Android Native java.sql.* org.sqlite.* (xerial) Android Driver

    knarch.db Driver JDBC Driver JS JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl Android android.database.* net.sqlcipher.database.* AndroidX SQLCipher Impl
  55. 95.

    android. android. sqlite3.h Node? WASM? Vanilla JS? ed Native JavaScript

    Database Abstraction java.sql.* org.sqlite.* (xerial) Android Driver knarch.db Driver JDBC Driver JS Driver JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl android. net.sqlc AndroidX SQLCipher Impl
  56. 96.

    android. android. sqlite3.h Node? WASM? Vanilla JS? ed Native JavaScript

    Database Abstraction java.sql.* org.sqlite.* (xerial) Android Driver knarch.db Driver JDBC Driver JS Driver JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl android. net.sqlc AndroidX SQLCipher Impl AAA BBB
  57. 97.

    sqlite3.h Node? WASM? Vanilla JS? ed Native JavaScript Database Abstraction

    java.sql.* org.sqlite.* (xerial) Driver knarch.db Driver JDBC Driver JS Driver JVM Abstraction android. net.sqlc AndroidX SQLCipher Impl AAA BBB
  58. 98.

    sqlite3.h ed Native Node? WASM? Vanilla JS? JavaScript Database Abstraction

    java.sql.* org.sqlite.* (xerial) Driver knarch.db Driver JDBC Driver JVM Abstraction android. net.sqlc AndroidX SQLCipher Impl
  59. 99.

    android.database.* android.database.sqlite.* sqlite3.h Generated Code Android Native Node? WASM? Vanilla

    JS? JavaScript Database Abstraction java.sql.* org.sqlite.* (xerial) Android Driver knarch.db Driver JDBC Driver JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl Android android.database.* net.sqlcipher.database.* AndroidX SQLCipher Impl
  60. 100.

    android.database.* android.database.sqlite.* sqlite3.h Generated Code Android Native Node? WASM? Vanilla

    JS? JavaScript Database Abstraction java.sql.* org.sqlite.* (xerial) Android Driver knarch.db Driver JDBC Driver JVM AndroidX Sqlite Abstraction AndroidX Sqlite Framework Impl Android android.database.* net.sqlcipher.database.* AndroidX SQLCipher Impl
  61. 103.

    package com.squareup.sqldelight.db expect interface Closeable { fun close() } expect

    inline fun <T : Closeable?, R> T.use(body: (T) -> R): R
  62. 104.

    package com.squareup.sqldelight.db expect interface Closeable { fun close() } expect

    inline fun <T : Closeable?, R> T.use(body: (T) -> R): R package com.squareup.sqldelight.db import kotlin.io.use as kotlinIoUse actual typealias Closeable = java.io.Closeable actual inline fun <T : Closeable?, R> T.use(body: (T) -> R): R = kotlinIoUse(body)
  63. 105.

    package com.squareup.sqldelight.db expect interface Closeable { fun close() } expect

    inline fun <T : Closeable?, R> T.use(body: (T) -> R): R package com.squareup.sqldelight.db actual interface Closeable { actual fun close() } actual inline fun <T : Closeable?, R> T.use(body: (T) -> R): R { // Copy/paste JVM implementation… }
  64. 106.

    apply plugin: 'org.jetbrains.kotlin.platform.common' dependencies { compile 'com.squareup.sqldelight:runtime-common:1.0.0' } apply plugin:

    'org.jetbrains.kotlin.platform.jvm' // Or .android dependencies { compile 'com.squareup.sqldelight:runtime-jdk:1.0.0' } apply plugin: 'org.jetbrains.kotlin.platform.js' dependencies { compile 'com.squareup.sqldelight:runtime-js:1.0.0' }
  65. 107.

    apply plugin: 'org.jetbrains.kotlin.platform.common' dependencies { compile 'com.squareup.sqldelight:runtime-common:1.0.0' } apply plugin:

    'org.jetbrains.kotlin.platform.jvm' // Or .android dependencies { compile 'com.squareup.sqldelight:runtime-jdk:1.0.0' } apply plugin: 'org.jetbrains.kotlin.platform.js' dependencies { compile 'com.squareup.sqldelight:runtime-js:1.0.0' } apply plugin: 'org.jetbrains.kotlin.platform.native' // ???
  66. 108.

    apply plugin: 'org.jetbrains.kotlin.platform.common' dependencies { compile 'com.squareup.sqldelight:runtime-common:1.0.0' } apply plugin:

    'org.jetbrains.kotlin.platform.jvm' // Or .android dependencies { compile 'com.squareup.sqldelight:runtime-jdk:1.0.0' } apply plugin: 'org.jetbrains.kotlin.platform.js' dependencies { compile 'com.squareup.sqldelight:runtime-js:1.0.0' } apply plugin: 'org.jetbrains.kotlin.platform.native' // nope!
  67. 113.

    runtime-1.0.0.module { "variants": [ { "name": "iosArm64-api", "available-at": { "url":

    "../../runtime-iosarm64/runtime-iosarm64-1.0.0.module" } }, ... ]
 }
  68. 119.

    com.squareup.sqldelight - android-driver android-driver-1.0.0.aar android-driver-1.0.0-javador.jar android-driver-1.0.0-sources.jar android-driver-1.0.0-pom.xml + runtime +

    runtime-common + runtime-iosarm64 + runtime-iosx64 + runtime-jvm + runtime-js android-driver-1.0.0-pom.xml + runtime-jvm
  69. 120.

    com.squareup.sqldelight - android-driver android-driver-1.0.0.aar android-driver-1.0.0-javador.jar android-driver-1.0.0-sources.jar android-driver-1.0.0-pom.xml + runtime +

    runtime-common + runtime-iosarm64 + runtime-iosx64 + runtime-jvm + runtime-js android-driver-1.0.0-pom.xml + runtime
  70. 121.

    com.squareup.sqldelight - android-driver android-driver-1.0.0.aar android-driver-1.0.0-javador.jar android-driver-1.0.0-sources.jar android-driver-1.0.0-pom.xml + runtime +

    runtime-common + runtime-iosarm64 + runtime-iosx64 + runtime-jvm + runtime-js android-driver-1.0.0-pom.xml + runtime-jdk
  71. 122.

    apply plugin: ‘com.android.application’ apply plugin: 'org.jetbrains.kotlin.multiplatform' dependencies {A implementation 'com.squareup.sqldelight:android-driver:1.0.0'

    }A kotlin {A sourceSets {A commonMain {A dependencies {A implementation project(':sqldelight-sample:sample-common') }A }A }A }A
  72. 123.

    apply plugin: ‘com.android.application’ apply plugin: 'org.jetbrains.kotlin.multiplatform' dependencies {A implementation 'com.squareup.sqldelight:android-driver:1.0.0'

    }A kotlin {A sourceSets {A commonMain {A dependencies {A implementation project(':sqldelight-sample:sample-common') }A }A }A }A A implementation 'com.squareup.sqldelight:android-driver:1.0.0' A A implementation project(':sqldelight-sample:sample-common')
  73. 124.

    apply plugin: ‘com.android.application’ apply plugin: 'org.jetbrains.kotlin.multiplatform' dependencies {A implementation (“com.squareup.sqldelight:android-driver:1.0.0)

    {A exclude group: 'com.squareup.sqldelight', module: 'runtime-jdk' }A }A kotlin {A sourceSets {A commonMain {A dependencies {A implementation project(':sqldelight-sample:sample-common') }A }A }A }A implementation ('com.squareup.sqldelight:android-driver:1.0.0') {A exclude group: 'com.squareup.sqldelight', module: 'runtime-jdk' }A implementation project(':sqldelight-sample:sample-common')
  74. 128.

    abstract class Query<out RowType : Any> { fun execute(): SqlCursor

    fun executeAsList(): List<RowType> fun executeAsOne(): RowType fun executeAsOneOrNull(): RowType? }X
  75. 129.

    abstract class Query<out RowType : Any> { fun execute(): SqlCursor

    fun executeAsList(): List<RowType> fun executeAsOne(): RowType fun executeAsOneOrNull(): RowType? fun addListener(listener: Listener) fun removeListener(listener: Listener) interface Listener { fun queryResultsChanged() }Y }X
  76. 130.

    abstract class Query<out RowType : Any> { fun execute(): SqlCursor

    fun executeAsList(): List<RowType> fun executeAsOne(): RowType fun executeAsOneOrNull(): RowType? fun addListener(listener: Listener) fun removeListener(listener: Listener) interface Listener { fun queryResultsChanged() }Y fun notifyDataChanged() }X
  77. 132.

    val usersQuery: Query<User> = // … usersQuery.addListener(object : Listener {

    override fun queryResultsChanged() { val users = usersQuery.executeAsList() // Show users somewhere… }G }) usersQuery.notifyDataChanged()
  78. 133.

    val usersQuery: Query<User> = // … val executor = //

    … usersQuery.addListener(object : Listener { override fun queryResultsChanged() { executor.execute { val users = usersQuery.executeAsList() // Show users somewhere… }G }G }) usersQuery.notifyDataChanged()
  79. 134.

    val usersQuery: Query<User> = // … val executor = //

    … val mainThread = Handler(Looper.getMainLooper()) usersQuery.addListener(object : Listener { override fun queryResultsChanged() { executor.execute { val users = usersQuery.executeAsList() mainThread.post { // Show users somewhere… } }G }G }) usersQuery.notifyDataChanged()
  80. 139.

    val usersQuery: Query<User> = // … val usersObservable: Observable<List<User>> =

    usersQuery.asObservable() .map { it.executeAsList() } Query
  81. 143.

    val usersQuery: Query<User> = // … val usersChannel: ReceiveChannel<List<User>> =

    usersQuery.asChannel() .map { it.executeAsList() } Query
  82. 145.

    suspend fun users() { val usersQuery: Query<User> = // …

    val usersChannel: ReceiveChannel<List<User>> = usersQuery.asChannel() .mapToList() return usersChannel.receive() }
  83. 146.

    suspend fun users() { val usersQuery: Query<User> = // …

    val usersChannel: ReceiveChannel<List<User>> = usersQuery.asChannel() .mapToList() return usersChannel.receive() }
  84. 147.

    suspend fun users() { val usersQuery: Query<User> = // …

    val usersChannel: ReceiveChannel<List<User>> = usersQuery.asChannel() .mapToList() return usersChannel.receive() }
  85. 151.

    val usersQuery: Query<User> = // … val executor = //

    … val usersLiveData: LiveData<List<User>> = usersQuery.asLiveDataList(executor)B
  86. 152.

    val pagedQuery = QueryDataSourceFactory( queryProvider = { limit, offset ->

    queries.users(limit, offset) }, count = queries.usersCount())
  87. 156.

    apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.jvm' // automatically adds… dependencies

    { api 'com.squareup.sqldelight:runtime-jdk:1.0.0' }X tasks.create('generateSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileKotlin').dependsOn('generateSqlDelight')
  88. 158.

    apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.js' // automatically adds… dependencies

    { api 'com.squareup.sqldelight:runtime-js:1.0.0' }X tasks.create('generateSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileKotlin').dependsOn('generateSqlDelight')
  89. 160.

    apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.multiplatform' // automatically adds… dependencies

    { api 'com.squareup.sqldelight:runtime:1.0.0' }X tasks.create('generateSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileKotlin').dependsOn('generateSqlDelight')
  90. 162.

    apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.android' // automatically adds… dependencies

    { api 'com.squareup.sqldelight:runtime-jdk:1.0.0' }X tasks.create('generateSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileKotlin').dependsOn('generateSqlDelight')
  91. 163.

    m u l t i p l a t f

    o r m apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.android' // automatically adds… dependencies { api 'com.squareup.sqldelight:runtime-jdk:1.0.0' }X tasks.create('generateDebugSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileDebugKotlin').dependsOn('generateDebugSqlDelight') tasks.create('generateReleaseSqlDelight', SqlDelightTask) { // … }X tasks.getByName('compileReleaseKotlin').dependsOn('generateReleaseSqlDelight')
  92. 164.

    tasks.getByName('compileDebugKotlin').dependsOn('generateDebugSqlDelight') tasks.getByName('compileReleaseKotlin').dependsOn('generateReleaseSqlDelight') m u l t i p l a

    t f o r m apply plugin: 'com.squareup.sqldelight' apply plugin: 'org.jetbrains.kotlin.android' // automatically adds… dependencies { api 'com.squareup.sqldelight:runtime-jdk:1.0.0' }X tasks.create('generateDebugSqlDelight', SqlDelightTask) { /* … */ }X tasks.create('generateReleaseSqlDelight', SqlDelightTask) { /* … */ }X tasks.create('verifySqlDelightMigration', VerifyMigrationTask) { // … }X
  93. 175.
  94. 176.

    sqlite3.h ed Native Node? WASM? Vanilla JS? JavaScript Database Abstraction

    java.sql.* org.sqlite.* (xerial) Driver knarch.db Driver JDBC Driver JS Driver JVM Abstraction android. net.sqlc AndroidX SQLCipher Impl
  95. 177.

    sqlite3.h ed Native Node? WASM? Vanilla JS? JavaScript Database Abstraction

    java.sql.* org.sqlite.* (xerial) Driver knarch.db Driver JDBC Driver JVM Abstraction android. net.sqlc AndroidX SQLCipher Impl
  96. 184.

    iOS

  97. 186.

    struct Player {A var id: NSNumber var name: String var

    country: String var ranking: NSNumber }A let firstPlace = playerQueries.withRanking( ranking: 1 ).executeAsOne()
  98. 187.

    struct Player {A var id: NSNumber var name: String var

    country: String var ranking: NSNumber }A func createPlayer(id: NSNumber, name: String, country: String, ranking: NSNumber) -> Player {A return Player(id: id, name: name, country: country, ranking: ranking) }A let firstPlace = playerQueries.withRanking( ranking: 1 ).executeAsOne()
  99. 188.

    struct Player {A var id: NSNumber var name: String var

    country: String var ranking: NSNumber }A func createPlayer(id: NSNumber, name: String, country: String, ranking: NSNumber) -> Player {A return Player(id: id, name: name, country: country, ranking: ranking) }A let firstPlace = playerQueries.withRanking( ranking: 1, mapper: createPlayer ).executeAsOne() as! Player
  100. 191.
  101. 192.
  102. 193.
  103. 194.
  104. 195.
  105. 196.
  106. 197.
  107. 198.
  108. 199.
  109. 200.

    withRanking: SELECT name FROM player WHERE ranking = ?; insert:

    INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  110. 201.

    withRanking: SELECT name WHERE ranking = ?; insert: (name, country,

    ranking) VALUES (?, ?, ?); FROM player INSERT INTO player
  111. 202.

    withRanking: SELECT name FROM player WHERE ranking = ?; insert:

    INSERT INTO player (name, country, ranking) VALUES (?, ?, ?); private inner class Insert { fun execute(): Long { val result = statement.execute() notifyQueries(queryWrapper.playerQueries.withRanking) return result } }
  112. 203.

    withRanking: SELECT name FROM player WHERE ranking = ?; insert:

    INSERT INTO player (name, country, ranking) VALUES (?, ?, ?); private inner class Insert { fun execute(): Long { val result = statement.execute() notifyQueries(queryWrapper.playerQueries.withRanking) return result } } private inner class Insert { fun execute(): Long { val result = statement.execute() notifyQueries(queryWrapper.playerQueries.withRanking) return result } } withRanking: SELECT name FROM player WHERE ranking = ?; insert: INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  113. 204.

    withRanking: SELECT name FROM player WHERE ranking = ?; insert:

    INSERT INTO player (name, country, ranking) VALUES (?, ?, ?); private inner class Insert { fun execute(): Long { val result = statement.execute() notifyQueries(queryWrapper.playerQueries.withRanking) return result } } withRanking: SELECT name FROM player WHERE ranking = ?; insert: INSERT INTO player (name, country, ranking) VALUES (?, ?, ?);
  114. 206.
  115. 207.