From 5f1015e5493e8183258dcd025c2a3d83632a5a11 Mon Sep 17 00:00:00 2001 From: frevib Date: Fri, 14 Jun 2024 22:28:45 +0200 Subject: [PATCH 1/4] Fix issue 5298: allow keywords to be used as table names --- .../sqldelight/core/lang/util/TreeUtil.kt | 2 +- .../cash/sqldelight/core/QueriesTypeTest.kt | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/util/TreeUtil.kt b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/util/TreeUtil.kt index 8fc5b7a6d1e..1b6edd31570 100644 --- a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/util/TreeUtil.kt +++ b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/util/TreeUtil.kt @@ -183,7 +183,7 @@ private fun PsiElement.rangesToReplace(): List> { first = parent!!.tableName.range, second = parent!!.columns.joinToString( separator = ", ", - prefix = "${parent!!.tableName.text} (", + prefix = "${parent!!.tableName.node.text} (", postfix = ")", ) { it.name }, ), diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt index 4a6fef64bc6..44c24251544 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt @@ -915,4 +915,44 @@ class QueriesTypeTest { """.trimMargin(), ) } + + @Test fun `issue 5298 INSERT OTHER`() { + val result = FixtureCompiler.compileSql( + """ + |CREATE TABLE "order" ( + | data_id INTEGER NOT NULL + |); + |selectForId: + |INSERT INTO "order" VALUES ?; + """.trimMargin(), + temporaryFolder, +// overrideDialect = PostgreSqlDialect() + ) + + val database = File(result.outputDirectory, "com/example/TestQueries.kt") + assertThat(result.compilerOutput).containsKey(database) + println(result.compilerOutput[database].toString()) + assertThat(result.compilerOutput[database].toString()).isEqualTo( + """ + package com.example + + import app.cash.sqldelight.TransacterImpl + import app.cash.sqldelight.db.SqlDriver + + public class TestQueries( + driver: SqlDriver, + ) : TransacterImpl(driver) { + public fun selectForId(order: Order) { + driver.execute(-304_025_397, ""${'"'}INSERT INTO "order" (data_id) VALUES (?)""${'"'}, 1) { + bindLong(0, order.data_id) + } + notifyQueries(-304_025_397) { emit -> + emit("order") + } + } + } + + """.trimIndent()) + + } } From c9d025fc4adc34413cf03fb0c89de4bc80bfb120 Mon Sep 17 00:00:00 2001 From: frevib Date: Sat, 15 Jun 2024 17:06:29 +0200 Subject: [PATCH 2/4] Refactor test Cleanup --- .../cash/sqldelight/core/QueriesTypeTest.kt | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt index 44c24251544..67c6969dc9d 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt @@ -10,9 +10,11 @@ import org.junit.Test import org.junit.rules.TemporaryFolder class QueriesTypeTest { - @get:Rule val temporaryFolder = TemporaryFolder() + @get:Rule + val temporaryFolder = TemporaryFolder() - @Test fun `queries file is generated properly via compilation`() { + @Test + fun `queries file is generated properly via compilation`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE data ( @@ -217,7 +219,8 @@ class QueriesTypeTest { ) } - @Test fun `queries file is generated properly with adapter`() { + @Test + fun `queries file is generated properly with adapter`() { val result = FixtureCompiler.compileSql( """ |import foo.S; @@ -326,7 +329,8 @@ class QueriesTypeTest { ) } - @Test fun `unused adapters are not passed to the database constructor`() { + @Test + fun `unused adapters are not passed to the database constructor`() { val result = FixtureCompiler.compileSql( """ |import kotlin.Int; @@ -393,7 +397,8 @@ class QueriesTypeTest { ) } - @Test fun `queries file is generated properly via compilation1a`() { + @Test + fun `queries file is generated properly via compilation1a`() { val result = FixtureCompiler.compileSql( """ |CREATE VIRTUAL TABLE data USING fts5( @@ -552,7 +557,8 @@ class QueriesTypeTest { ) } - @Test fun `queries file is generated properly via compilation with offsets`() { + @Test + fun `queries file is generated properly via compilation with offsets`() { val result = FixtureCompiler.compileSql( """ |CREATE VIRTUAL TABLE search USING fts3( @@ -708,7 +714,8 @@ class QueriesTypeTest { ) } - @Test fun `adapter through view resolves correctly`() { + @Test + fun `adapter through view resolves correctly`() { FixtureCompiler.writeSql( """ |import com.chicken.SoupBase.Broth; @@ -852,7 +859,8 @@ class QueriesTypeTest { ) } - @Test fun `grouped statement with return and no arguments gets a query type`() { + @Test + fun `grouped statement with return and no arguments gets a query type`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE data ( @@ -916,7 +924,8 @@ class QueriesTypeTest { ) } - @Test fun `issue 5298 INSERT OTHER`() { + @Test + fun `SQL keywords can be used as table names when escaped`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE "order" ( @@ -926,13 +935,11 @@ class QueriesTypeTest { |INSERT INTO "order" VALUES ?; """.trimMargin(), temporaryFolder, -// overrideDialect = PostgreSqlDialect() ) - val database = File(result.outputDirectory, "com/example/TestQueries.kt") - assertThat(result.compilerOutput).containsKey(database) - println(result.compilerOutput[database].toString()) - assertThat(result.compilerOutput[database].toString()).isEqualTo( + val dataQueries = File(result.outputDirectory, "com/example/TestQueries.kt") + assertThat(result.compilerOutput).containsKey(dataQueries) + assertThat(result.compilerOutput[dataQueries].toString()).isEqualTo( """ package com.example @@ -952,7 +959,7 @@ class QueriesTypeTest { } } - """.trimIndent()) - + """.trimIndent(), + ) } } From 5af92ea2cb90d9beb4b10810652ad5b0d702d3c3 Mon Sep 17 00:00:00 2001 From: frevib Date: Tue, 18 Jun 2024 08:36:10 +0200 Subject: [PATCH 3/4] Fix formatting --- .../cash/sqldelight/core/QueriesTypeTest.kt | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt index 67c6969dc9d..98c5471e7ac 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt @@ -13,8 +13,7 @@ class QueriesTypeTest { @get:Rule val temporaryFolder = TemporaryFolder() - @Test - fun `queries file is generated properly via compilation`() { + @Test fun `queries file is generated properly via compilation`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE data ( @@ -219,8 +218,7 @@ class QueriesTypeTest { ) } - @Test - fun `queries file is generated properly with adapter`() { + @Test fun `queries file is generated properly with adapter`() { val result = FixtureCompiler.compileSql( """ |import foo.S; @@ -329,8 +327,7 @@ class QueriesTypeTest { ) } - @Test - fun `unused adapters are not passed to the database constructor`() { + @Test fun `unused adapters are not passed to the database constructor`() { val result = FixtureCompiler.compileSql( """ |import kotlin.Int; @@ -397,8 +394,7 @@ class QueriesTypeTest { ) } - @Test - fun `queries file is generated properly via compilation1a`() { + @Test fun `queries file is generated properly via compilation1a`() { val result = FixtureCompiler.compileSql( """ |CREATE VIRTUAL TABLE data USING fts5( @@ -557,8 +553,7 @@ class QueriesTypeTest { ) } - @Test - fun `queries file is generated properly via compilation with offsets`() { + @Test fun `queries file is generated properly via compilation with offsets`() { val result = FixtureCompiler.compileSql( """ |CREATE VIRTUAL TABLE search USING fts3( @@ -714,8 +709,7 @@ class QueriesTypeTest { ) } - @Test - fun `adapter through view resolves correctly`() { + @Test fun `adapter through view resolves correctly`() { FixtureCompiler.writeSql( """ |import com.chicken.SoupBase.Broth; @@ -859,8 +853,7 @@ class QueriesTypeTest { ) } - @Test - fun `grouped statement with return and no arguments gets a query type`() { + @Test fun `grouped statement with return and no arguments gets a query type`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE data ( @@ -924,8 +917,7 @@ class QueriesTypeTest { ) } - @Test - fun `SQL keywords can be used as table names when escaped`() { + @Test fun `SQL keywords can be used as table names when escaped`() { val result = FixtureCompiler.compileSql( """ |CREATE TABLE "order" ( From 37d9fc8bb8cf6040d2573c9cc39f2a41e12d91c0 Mon Sep 17 00:00:00 2001 From: frevib Date: Tue, 18 Jun 2024 09:54:15 +0200 Subject: [PATCH 4/4] Fix formatting --- .../test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt index 98c5471e7ac..d3c9f813ad4 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/QueriesTypeTest.kt @@ -10,8 +10,7 @@ import org.junit.Test import org.junit.rules.TemporaryFolder class QueriesTypeTest { - @get:Rule - val temporaryFolder = TemporaryFolder() + @get:Rule val temporaryFolder = TemporaryFolder() @Test fun `queries file is generated properly via compilation`() { val result = FixtureCompiler.compileSql(