-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: RETURNING multiple values on INSERT, UPDATE, DELETE generates wr…
…ong code The code generation was correct for scenarios where a single column is returned, as well as when all columns are returned. However, if multiple, but not all, columns are returned, then the generated code still used the table type, rather than generating a new interface for the projection. With this change, the pureTable computation more closely resembles that of SelectQueryable, which contains more complex logic for purity identification.
- Loading branch information
1 parent
94a2a13
commit d9089c4
Showing
5 changed files
with
58 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 4 additions & 22 deletions
26
...qlite-3-35/src/main/kotlin/app/cash/sqldelight/dialects/sqlite_3_35/SqliteTypeResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ght-compiler/dialect/src/main/kotlin/app/cash/sqldelight/dialect/api/FlattenCompounded.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app.cash.sqldelight.dialect.api | ||
|
||
import com.alecstrong.sql.psi.core.psi.QueryElement | ||
|
||
internal fun List<QueryElement.QueryColumn>.flattenCompounded(): List<QueryElement.QueryColumn> { | ||
return map { column -> | ||
if (column.compounded.none { it.element != column.element || it.nullable != column.nullable }) { | ||
column.copy(compounded = emptyList()) | ||
} else { | ||
column | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ht-compiler/dialect/src/main/kotlin/app/cash/sqldelight/dialect/api/ReturningQueryable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package app.cash.sqldelight.dialect.api | ||
|
||
import com.alecstrong.sql.psi.core.psi.QueryElement | ||
import com.alecstrong.sql.psi.core.psi.Queryable | ||
import com.alecstrong.sql.psi.core.psi.SqlAnnotatedElement | ||
import com.alecstrong.sql.psi.core.psi.SqlTableName | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
/** | ||
* Query deriving from the `RETURNING` clause of an expression. | ||
* | ||
* Typical use cases include `INSERT`, `UPDATE`, and `DELETE`. This class is similar to [SelectQueryable] but differs in | ||
* the fact that only 1 table can be part of the query, and that table is guaranteed to be "real". | ||
* | ||
* @param statement Parent statement. Typically, this is the `INSERT`, `UPDATE`, or `DELETE` statement. | ||
* @param select The `RETURNING` clause of the statement. Represented as a query since it returns values to the caller. | ||
* @param tableName Name of the table the [statement] is operating on. | ||
*/ | ||
class ReturningQueryable( | ||
override var statement: SqlAnnotatedElement, | ||
override val select: QueryElement, | ||
private val tableName: SqlTableName?, | ||
) : QueryWithResults { | ||
|
||
override val pureTable by lazy { | ||
val pureColumns = select.queryExposed().singleOrNull()?.columns?.flattenCompounded() | ||
val resolvedTable = tableName?.reference?.resolve() | ||
val table = PsiTreeUtil.getParentOfType(resolvedTable, Queryable::class.java)?.tableExposed() | ||
?: return@lazy null | ||
val requestedColumnsAreIdenticalToTable = table.query.columns.flattenCompounded() == pureColumns | ||
if (requestedColumnsAreIdenticalToTable) { | ||
tableName | ||
} else { | ||
null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters