-
Notifications
You must be signed in to change notification settings - Fork 540
Fix codegen for RETURNING clause #3872
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
Conversation
@@ -92,13 +95,34 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes | |||
} | |||
|
|||
override fun queryWithResults(sqlStmt: SqlStmt): QueryWithResults? { | |||
fun List<QueryElement.QueryColumn>.flattenCompounded(): List<QueryElement.QueryColumn> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fun
is copied from SelectQueryable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, all new code was actually copied? What about publishing the code instead and reuse it instead copying it twice (and having it 3 times now)?
@@ -81,8 +81,8 @@ class PgInsertReturningTest { | |||
|
|||
assertThat(generator.defaultResultTypeFunction().toString()).isEqualTo( | |||
""" | |||
|public fun insertReturn(data_: com.example.Data_): app.cash.sqldelight.ExecutableQuery<com.example.Data_> = insertReturn(data_) { data__, id -> | |||
| com.example.Data_( | |||
|public fun insertReturn(data_: com.example.Data_): app.cash.sqldelight.ExecutableQuery<com.example.InsertReturn> = insertReturn(data_) { data__, id -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test verified that the code got generated, but the generated code didn't compile. I'd recommend actually deleting the test, given the integration tests have, in my opinion, better coverage, but either way, the test passes now.
insertAndReturnMany: | ||
INSERT INTO dog | ||
VALUES (?, ?, DEFAULT) | ||
RETURNING name, breed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the problem scenario. 1
and All
variants are present for coverage.
@@ -92,13 +95,34 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes | |||
} | |||
|
|||
override fun queryWithResults(sqlStmt: SqlStmt): QueryWithResults? { | |||
fun List<QueryElement.QueryColumn>.flattenCompounded(): List<QueryElement.QueryColumn> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, all new code was actually copied? What about publishing the code instead and reuse it instead copying it twice (and having it 3 times now)?
Thank you. |
The code generation is 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 uses the table type, rather than generating a new interface for the projection.
…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.
4a4c262
to
d9089c4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes:
- Extracted the common logic into
ReturningQueryable
andFlattenCompounded
. Happy to change names as needed. - Added tests for
UPDATE
andDELETE
RETURNING
scenarios. These did indeed exhibit the bug, and benefit from the same change, further reusing the common logic.
@@ -21,16 +20,6 @@ class SelectQueryable( | |||
* which points to that table (Pure meaning it has exactly the same columns in the same order). | |||
*/ | |||
override val pureTable: NamedElement? by lazy { | |||
fun List<QueryColumn>.flattenCompounded(): List<QueryColumn> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to FlattenCompounded
with internal
visibility, for reuse in new class ReturningQueryable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Names are okay.
* @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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used by SQLite and Postgres dialects at the moment, but reasonable to think additional dialects would want this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, thanks.
This fixes a bug in the codegen of RETURNING clauses for Postgres and SQLite INSERT statements.
Previously, the generated code did not compile if multiple, but not all, columns of the table were returned.
The same bug likely exists for UPDATE and DELETE, but I didn't check or try to fix those.