Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better version catalog support for dialects #3435

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -41,7 +41,7 @@ class SqlDelightDatabase(

fun dialect(dialect: Any) {
if (addedDialect) throw IllegalStateException("Can only set a single dialect.")
configuration.dependencies.add(project.dependencies.create(dialect))
project.dependencies.add(configuration.name, dialect)
addedDialect = true
}

Expand Down
27 changes: 27 additions & 0 deletions sqldelight-gradle-plugin/src/test/integration-catalog/build.gradle
@@ -0,0 +1,27 @@
buildscript {
apply from: "${projectDir.absolutePath}/../buildscript.gradle"
}

apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'app.cash.sqldelight'

sqldelight {
MyDatabase {
packageName = "app.cash.sqldelight.mysql.integration"
dialect(deps.sqldelight.mysql)
}
}

repositories {
maven {
url "file://${projectDir.absolutePath}/../../../../build/localMaven"
}
mavenCentral()
}

dependencies {
implementation deps.mysqlJdbc
implementation "org.testcontainers:mysql:1.16.2"
implementation "app.cash.sqldelight:jdbc-driver:${app.cash.sqldelight.VersionKt.VERSION}"
implementation deps.truth
}
@@ -0,0 +1,11 @@
apply from: "../settings.gradle"

rootProject.name = 'sqldelight-mysql-integration'

dependencyResolutionManagement {
versionCatalogs {
libs {
library('sqldelight-mysql', 'app.cash.sqldelight:mysql-dialect') version app.cash.sqldelight.VersionKt.VERSION
}
}
}
@@ -0,0 +1,13 @@
CREATE TABLE dates(
time TIME NOT NULL,
date DATE NOT NULL,
datetime DATETIME NOT NULL,
timestamp TIMESTAMP NOT NULL,
year YEAR NOT NULL
);

insertDate {
INSERT INTO dates VALUES (?, ?, ?, ?, ?);

SELECT * FROM dates LIMIT 1;
}
@@ -0,0 +1,19 @@
CREATE TABLE dog (
name VARCHAR(8) NOT NULL,
breed TEXT NOT NULL,
is_good BOOLEAN NOT NULL DEFAULT 1
);

insertDog:
INSERT INTO dog
VALUES (?, ?, ?);

selectDogs:
SELECT *
FROM dog;

selectDogsByBreedAndNames:
SELECT *
FROM dog
WHERE breed = ?
AND name IN ?;
@@ -0,0 +1,128 @@
package app.cash.sqldelight.mysql.integration

import app.cash.sqldelight.Query
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.JdbcDriver
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.sql.Connection
import java.sql.DriverManager
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

class MySqlTest {
lateinit var connection: Connection
lateinit var dogQueries: DogQueries
lateinit var datesQueries: DatesQueries
lateinit var driver: JdbcDriver

@Before
fun before() {
connection = DriverManager.getConnection("jdbc:tc:mysql:///myDb")
driver = object : JdbcDriver() {
override fun getConnection() = connection
override fun closeConnection(connection: Connection) = Unit
override fun addListener(listener: Query.Listener, queryKeys: Array<String>) = Unit
override fun removeListener(listener: Query.Listener, queryKeys: Array<String>) = Unit
override fun notifyListeners(queryKeys: Array<String>) = Unit
}
val database = MyDatabase(driver)

MyDatabase.Schema.create(driver)
dogQueries = database.dogQueries
datesQueries = database.datesQueries
}

@After
fun after() {
connection.close()
}

@Test fun simpleSelect() {
dogQueries.insertDog("Tilda", "Pomeranian", true)
assertThat(dogQueries.selectDogs().executeAsOne())
.isEqualTo(
Dog(
name = "Tilda",
breed = "Pomeranian",
is_good = true,
),
)
}

@Test
fun simpleSelectWithIn() {
dogQueries.insertDog("Tilda", "Pomeranian", true)
dogQueries.insertDog("Tucker", "Portuguese Water Dog", true)
dogQueries.insertDog("Cujo", "Pomeranian", false)
dogQueries.insertDog("Buddy", "Pomeranian", true)
assertThat(
dogQueries.selectDogsByBreedAndNames(
breed = "Pomeranian",
name = listOf("Tilda", "Buddy"),
).executeAsList(),
)
.containsExactly(
Dog(
name = "Tilda",
breed = "Pomeranian",
is_good = true,
),
Dog(
name = "Buddy",
breed = "Pomeranian",
is_good = true,
),
)
}

@Test
fun testDates() {
with(
datesQueries.insertDate(
date = LocalDate.of(2020, 1, 1),
time = LocalTime.of(21, 30, 59),
datetime = LocalDateTime.of(2020, 1, 1, 21, 30, 59),
timestamp = OffsetDateTime.of(1980, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)),
year = "2022",
).executeAsOne(),
) {
assertThat(date).isEqualTo(LocalDate.of(2020, 1, 1))
assertThat(time).isEqualTo(LocalTime.of(21, 30, 59))
assertThat(datetime).isEqualTo(LocalDateTime.of(2020, 1, 1, 21, 30, 59))

assertThat(timestamp.format(DateTimeFormatter.ISO_LOCAL_DATE))
.isEqualTo(OffsetDateTime.of(1980, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)).format(DateTimeFormatter.ISO_LOCAL_DATE))
assertThat(year).isEqualTo("2022-01-01")
}
}

@Test
fun transactionCrashRollsBack() {
val transacter = SqlDriverTransacter(driver)

try {
transacter.transaction {
driver.execute(null, "CREATE TABLE throw_test(some Text)", 0, null)
afterRollback { driver.execute(null, "DROP TABLE throw_test", 0, null) }
throw ExpectedException()
}
Assert.fail()
} catch (_: ExpectedException) {
transacter.transaction {
driver.execute(null, "CREATE TABLE throw_test(some Text)", 0, null)
}
}
}

private class ExpectedException : Exception()
private class SqlDriverTransacter(driver: SqlDriver) : TransacterImpl(driver)
}
Expand Up @@ -273,4 +273,13 @@ class IntegrationTest {
val result = runner.build()
assertThat(result.output).contains("BUILD SUCCESSFUL")
}

@Test fun `dialect accepts version catalog dependency`() {
val runner = GradleRunner.create()
.withCommonConfiguration(File("src/test/integration-catalog"))
.withArguments("clean", "check", "--stacktrace")

val result = runner.build()
assertThat(result.output).contains("BUILD SUCCESSFUL")
}
}