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

Common tables do not generate data classes so dont return them #3097

Merged
merged 1 commit into from
Apr 16, 2022
Merged
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 @@ -6,6 +6,7 @@ import com.alecstrong.sql.psi.core.psi.QueryElement.QueryColumn
import com.alecstrong.sql.psi.core.psi.Queryable
import com.alecstrong.sql.psi.core.psi.SqlAnnotatedElement
import com.alecstrong.sql.psi.core.psi.SqlCompoundSelectStmt
import com.alecstrong.sql.psi.core.psi.SqlCteTableName
import com.intellij.psi.util.PsiTreeUtil

class SelectQueryable(
Expand Down Expand Up @@ -44,7 +45,8 @@ class SelectQueryable(
}

return@lazy select.tablesAvailable(select).firstOrNull {
it.query.columns.flattenCompounded() == pureColumns
(it.tableName.parent !is SqlCteTableName) &&
it.query.columns.flattenCompounded() == pureColumns
}?.tableName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,121 @@ class InterfaceGeneration {
)
}

@Test fun `value types correctly generated`() {
val result = FixtureCompiler.compileSql(
"""
|CREATE TABLE item (
| id INTEGER PRIMARY KEY AUTOINCREMENT,
| parent_id INTEGER,
| children INTEGER NOT NULL
|);
|
|recursiveQuery:
|WITH RECURSIVE
|descendants AS (
| SELECT id, parent_id
| FROM item
| WHERE item.id = :id
| UNION ALL
| SELECT item.id, item.parent_id
| FROM item, descendants
| WHERE item.id = descendants.parent_id
|)
|SELECT descendants.id, descendants.parent_id
|FROM descendants;
|""".trimMargin(),
temporaryFolder, fileName = "Recursive.sq"
)

val query = result.compiledFile.namedQueries[0]

assertThat(result.errors).isEmpty()
val generatedInterface = result.compilerOutput.get(File(result.outputDirectory, "com/example/RecursiveQuery.kt"))
assertThat(generatedInterface).isNotNull()
assertThat(generatedInterface.toString()).isEqualTo(
"""
|package com.example
|
|import kotlin.Long
|
|public data class RecursiveQuery(
| public val id: Long,
| public val parent_id: Long?,
|)
|""".trimMargin()
)

val generatedQueries = result.compilerOutput.get(File(result.outputDirectory, "com/example/RecursiveQueries.kt"))
assertThat(generatedQueries).isNotNull()
assertThat(generatedQueries.toString()).isEqualTo(
"""
|package com.example
|
|import app.cash.sqldelight.Query
|import app.cash.sqldelight.TransacterImpl
|import app.cash.sqldelight.db.SqlCursor
|import app.cash.sqldelight.db.SqlDriver
|import kotlin.Any
|import kotlin.Long
|import kotlin.String
|import kotlin.Unit
|
|public class RecursiveQueries(
| private val driver: SqlDriver,
|) : TransacterImpl(driver) {
| public fun <T : Any> recursiveQuery(id: Long, mapper: (id: Long, parent_id: Long?) -> T): Query<T>
| = RecursiveQueryQuery(id) { cursor ->
| mapper(
| cursor.getLong(0)!!,
| cursor.getLong(1)
| )
| }
|
| public fun recursiveQuery(id: Long): Query<RecursiveQuery> = recursiveQuery(id) { id_,
| parent_id ->
| RecursiveQuery(
| id_,
| parent_id
| )
| }
|
| private inner class RecursiveQueryQuery<out T : Any>(
| public val id: Long,
| mapper: (SqlCursor) -> T,
| ) : Query<T>(mapper) {
| public override fun addListener(listener: Query.Listener): Unit {
| driver.addListener(listener, arrayOf("item"))
| }
|
| public override fun removeListener(listener: Query.Listener): Unit {
| driver.removeListener(listener, arrayOf("item"))
| }
|
| public override fun <R> execute(mapper: (SqlCursor) -> R): R = driver.executeQuery(${query.id},
| ""${'"'}
| |WITH RECURSIVE
| |descendants AS (
| | SELECT id, parent_id
| | FROM item
| | WHERE item.id = ?
| | UNION ALL
| | SELECT item.id, item.parent_id
| | FROM item, descendants
| | WHERE item.id = descendants.parent_id
| |)
| |SELECT descendants.id, descendants.parent_id
| |FROM descendants
| ""${'"'}.trimMargin(), mapper, 1) {
| bindLong(1, id)
| }
|
| public override fun toString(): String = "Recursive.sq:recursiveQuery"
| }
|}
|""".trimMargin()
)
}

private fun checkFixtureCompiles(fixtureRoot: String) {
val result = FixtureCompiler.compileFixture(
fixtureRoot = "src/test/query-interface-fixtures/$fixtureRoot",
Expand Down