Skip to content

Commit

Permalink
Correctly infer argument types in compound selects
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova committed Apr 16, 2022
1 parent c13aa6d commit 4acfcaf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ private fun SqlSelectStmt.argumentType(result: SqlResultColumn): IntermediateTyp
else -> {
// Check if this is part of an inner expression of a resulit column.
val parentResult = PsiTreeUtil.getParentOfType(parentRule, SqlResultColumn::class.java)
?: return null
(parentResult.parent as SqlSelectStmt).argumentType(parentResult)

if (parentResult == null) NamedQuery("temp", SelectQueryable(compoundSelect, compoundSelect))
.resultColumns[resultColumnList.indexOf(result)]
else (parentResult.parent as SqlSelectStmt).argumentType(parentResult)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1718,4 +1718,27 @@ class SelectQueryFunctionTest {
|""".trimMargin()
)
}

@Test fun `parameter correctly generated for union`() {
val file = FixtureCompiler.parseSql(
"""
|query:
|SELECT 'foo'
|UNION
|SELECT ?;
|""".trimMargin(),
tempFolder
)

val query = file.namedQueries.first()
val generator = SelectQueryGenerator(query)

assertThat(generator.customResultTypeFunction().toString()).isEqualTo(
"""
|public fun query(expr: kotlin.String): app.cash.sqldelight.Query<kotlin.String> = QueryQuery(expr) { cursor ->
| cursor.getString(0)!!
|}
|""".trimMargin()
)
}
}

0 comments on commit 4acfcaf

Please sign in to comment.