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..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 @@ -915,4 +915,42 @@ class QueriesTypeTest { """.trimMargin(), ) } + + @Test fun `SQL keywords can be used as table names when escaped`() { + val result = FixtureCompiler.compileSql( + """ + |CREATE TABLE "order" ( + | data_id INTEGER NOT NULL + |); + |selectForId: + |INSERT INTO "order" VALUES ?; + """.trimMargin(), + temporaryFolder, + ) + + val dataQueries = File(result.outputDirectory, "com/example/TestQueries.kt") + assertThat(result.compilerOutput).containsKey(dataQueries) + assertThat(result.compilerOutput[dataQueries].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(), + ) + } }