I'm really struggling to set up SQL Delight in a C...
# compose-desktop
j
I'm really struggling to set up SQL Delight in a Compose Desktop project. This the reference I'm using but I can't quite follow: https://cashapp.github.io/sqldelight/jvm_sqlite/ , does anyone have another template? Please look in the thread to see what I have attempted.
settings.gradle.kts
file:
Copy code
pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
        maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
    }

    plugins {
        kotlin("multiplatform").version(extra["kotlin.version"] as String)
        id("org.jetbrains.compose").version(extra["compose.version"] as String)
        id("com.squareup.sqldelight").version("1.5.3")
    }
}
build.gradle.kts
file:
Copy code
plugins {
    kotlin("multiplatform")
    id("org.jetbrains.compose")
    id("com.squareup.sqldelight")
}

sqldelight {
    database("MyDatabase") { // This will be the name of the generated database class.
        packageName = "delight-desktop"
    }
}

...

kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = "11"
        }
        withJava()
    }
    sourceSets {
        val jvmMain by getting {
            dependencies {
                implementation(compose.desktop.currentOs)
                implementation("com.squareup.sqldelight:sqlite-driver:1.5.3")
            }
        }
        val jvmTest by getting
    }
}
Is it indeed in
jvmMain
I'm supposed to create the
sqldelight
directory? Am I supposed to use in the driver the same database name as in the build.gradle.kts? (I tried that but it doesn't recognize the reference).
Copy code
class DriverFactory {
    fun createDriver(): SqlDriver {
        val driver: SqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
        MyDatabase.Schema.create(driver)
        return driver
    }
}
h
Yes, you can use the folder
sqldelight
in jvmMain. What do you mean with the driver?
j
Well, if you look at the reference, it say
Copy code
val driver: SqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
Database.Schema.create(driver)
Sorry, there was a mistake in my question: in the driver, should I use the same database name as in the build.gradle.kts?
h
The database name declared in the sqldelight gradle plugin is only for code generation, it does not matter during using a "real" sql database.
j
So what name am I supposed to use for the driver? Database.Schema.create(driver)
h
Ahh, okay I got your question. Yes
I thought you mean somethink like this:
Copy code
val driver: SqlDriver = JdbcSqliteDriver("<jdbc://sqlite>:localhost/fooDB")
MyDatabase.Schema.create(driver)
j
Yes, I tried
MyDatabase.Schema.create(driver)
but it says it doesn't know what "MyDatabase" is.
h
Thats strange. Is your project uploaded?
j
Do you mean on Github?
h
yeah
j
example of it in following if that helps (at least part of kmp project anyway) https://github.com/joreilly/PeopleInSpace/blob/main/common/src/jvmMain/kotlin/com/surrus/common/repository/actual.kt
j
Btw, not sure if the problem is related, but
Copy code
sqldelight {
  Database { // This will be the name of the generated database class.
    packageName = "com.example"
  }
}
doesn't work, I have to use
Copy code
database("MyDatabase") { // This will be the name of the generated database class.
        packageName = "delight-desktop"
    }
h
Yes the first one is groovy, which some magic (missing function name as db name hacks) while the last one is kotlin with a typed syntax 🙂
j
@John O'Reilly where do you define your tables? I can't find them in "surrus".
j
Curiously, it works with a multiplatform project, but with a Desktop only project it won't detect the DB directory (in @John O'Reilly’s case
com.surrus.peopleinspace.db
)
o
j
@oianmol Thank you, I got it to work on multiplatform, now I'm trying to do it with only Dekstop
983 Views