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

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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`() {
frevib marked this conversation as resolved.
Show resolved Hide resolved
val result = FixtureCompiler.compileSql(
"""
|CREATE TABLE data (
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -915,4 +923,43 @@ 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(),
)
}
}