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

Fix issue 5298: allow keywords to be used as table names #5303

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private fun PsiElement.rangesToReplace(): List<Pair<IntRange, String>> {
first = parent!!.tableName.range,
second = parent!!.columns.joinToString(
separator = ", ",
prefix = "${parent!!.tableName.text} (",
prefix = "${parent!!.tableName.node.text} (",
postfix = ")",
) { it.name },
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
}