Skip to content

Commit

Permalink
Fix result expression for grouped statements (#4378)
Browse files Browse the repository at this point in the history
* Fix result expression for grouped statements

* Extract the value in async transactions only

* Add async transaction test

---------

Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
  • Loading branch information
dellisd and hfhbd authored Jul 17, 2023
1 parent 8511959 commit 41ac02d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ abstract class QueryGenerator(
}
}

// Extract value from the result of a grouped statement in async,
// because the transaction is put in an QueryResult.AsyncValue block.
if (generateAsync && isNamedQuery && query.statement is SqlDelightStmtClojureStmtList) {
binder += "%L"
arguments.add(".await()")
}

val statementId = if (needsFreshStatement) CodeBlock.of("null") else CodeBlock.of("%L", id)

if (isNamedQuery) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class AsyncSelectQueryTypeTest {
| |SELECT value
| | FROM data
| | WHERE id = last_insert_rowid()
| ""${'"'}.trimMargin(), mapper, 0)
| ""${'"'}.trimMargin(), mapper, 0).await()
| }
| }
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ insertDog:
INSERT INTO dog
VALUES (?, ?, DEFAULT, ?);

insertDogAndReturnAll {
INSERT INTO dog
VALUES (?, ?, DEFAULT, ?);

SELECT * FROM dog;
}

selectDogs:
SELECT *
FROM dog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class MySqlTest {
)
}

@Test
fun insertAndReturn() = runTest { database ->
val all = database.dogQueries.insertDogAndReturnAll("Tilda", "Pomeranian", null)
assertThat(all.awaitAsList())
.containsExactly(
Dog(
name = "Tilda",
breed = "Pomeranian",
is_good = true,
age = null,
),
)
}

@Test
fun simpleSelectWithIn() = runTest { database ->
with(database) {
Expand Down

0 comments on commit 41ac02d

Please sign in to comment.