Skip to content

Commit 3e07c0b

Browse files
authored
Also return for a clojure if the last statement returns a row count update (#5745)
1 parent b7a3b76 commit 3e07c0b

File tree

6 files changed

+158
-105
lines changed

6 files changed

+158
-105
lines changed

sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/ExecuteQueryGenerator.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ open class ExecuteQueryGenerator(
3838
},
3939
)
4040
.apply {
41-
if (mutatorReturns) {
42-
val type = if (generateAsync) LONG else QUERY_RESULT_TYPE.parameterizedBy(LONG)
43-
returns(type, CodeBlock.of("The number of rows updated."))
44-
}
41+
val type = if (generateAsync) LONG else QUERY_RESULT_TYPE.parameterizedBy(LONG)
42+
returns(type, CodeBlock.of("The number of rows updated."))
4543
}
4644
}
4745

sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/QueryGenerator.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,24 @@ abstract class QueryGenerator(
7575

7676
val notifyBlock = notifyQueriesBlock()
7777
if (query.statement is SqlDelightStmtClojureStmtList) {
78-
if (query is NamedQuery) {
79-
result
80-
.apply { if (generateAsync) beginControlFlow("return %T", ASYNC_RESULT_TYPE) }
81-
.beginControlFlow(if (generateAsync) "transactionWithResult" else "return transactionWithResult")
82-
} else {
83-
result.beginControlFlow("transaction")
84-
}
78+
result
79+
.apply { if (generateAsync) beginControlFlow("return %T", ASYNC_RESULT_TYPE) }
80+
.beginControlFlow(if (generateAsync) "transactionWithResult" else "return transactionWithResult")
8581
val handledArrayArgs = mutableSetOf<BindableQuery.Argument>()
8682
query.statement.findChildrenOfType<SqlStmt>().forEachIndexed { index, statement ->
8783
val (block, additionalArrayArgs) = executeBlock(statement, handledArrayArgs, query.idForIndex(index))
8884
handledArrayArgs.addAll(additionalArrayArgs)
8985
result.add(block)
9086
}
91-
if (query is NamedQuery && notifyBlock.isNotEmpty()) {
87+
if (notifyBlock.isNotEmpty()) {
9288
result.nextControlFlow(".also")
9389
result.add(notifyBlock)
9490
result.endControlFlow()
9591
} else {
9692
result.endControlFlow()
9793
result.add(notifyBlock)
9894
}
99-
if (generateAsync && query is NamedQuery) {
95+
if (generateAsync) {
10096
result.endControlFlow()
10197
}
10298
} else {

sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/MutatorQueryFunctionTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app.cash.sqldelight.core.queries
22

33
import app.cash.sqldelight.core.TestDialect
4+
import app.cash.sqldelight.core.compiler.ExecuteQueryGenerator
45
import app.cash.sqldelight.core.compiler.MutatorQueryGenerator
56
import app.cash.sqldelight.core.dialects.binderCheck
67
import app.cash.sqldelight.core.dialects.textType
@@ -59,6 +60,48 @@ class MutatorQueryFunctionTest {
5960
)
6061
}
6162

63+
@Test
64+
fun `mutator lambda generates proper method signature`(dialect: TestDialect) {
65+
val file = FixtureCompiler.parseSql(
66+
"""
67+
|CREATE TABLE data (
68+
| value ${dialect.textType}
69+
|);
70+
|
71+
|insertData {
72+
| INSERT INTO data
73+
| VALUES (:customTextValue);
74+
|}
75+
""".trimMargin(),
76+
tempFolder,
77+
dialect = dialect.dialect,
78+
)
79+
80+
val insert = file.namedExecutes.first()
81+
val generator = ExecuteQueryGenerator(insert)
82+
83+
assertThat(generator.function().toString()).isEqualTo(
84+
"""
85+
|/**
86+
| * @return The number of rows updated.
87+
| */
88+
|public fun insertData(customTextValue: kotlin.String?): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
89+
| driver.execute(${insert.idForIndex(0).withUnderscores}, ""${'"'}
90+
| |INSERT INTO data
91+
| | VALUES (?)
92+
| ""${'"'}.trimMargin(), 1) {
93+
| ${dialect.binderCheck}bindString(0, customTextValue)
94+
| }
95+
|} .also {
96+
| notifyQueries(${insert.id.withUnderscores}) { emit ->
97+
| emit("data")
98+
| }
99+
|}
100+
|
101+
""".trimMargin(),
102+
)
103+
}
104+
62105
@Test fun `mutator method generates proper private value`() {
63106
val file = FixtureCompiler.parseSql(
64107
"""

sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/MutatorQueryTypeTest.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -984,20 +984,22 @@ class MutatorQueryTypeTest {
984984

985985
assertThat(generator.function().toString()).isEqualTo(
986986
"""
987-
|public fun delete(ids: kotlin.collections.Collection<Int>) {
988-
| transaction {
989-
| val idsIndexes = createArguments(count = ids.size)
990-
| driver.execute(null, ""${'"'}DELETE FROM data WHERE id IN ${'$'}idsIndexes""${'"'}, ids.size) {
991-
| ids.forEachIndexed { index, ids_ ->
992-
| bindLong(index, data_Adapter.idAdapter.encode(ids_))
993-
| }
987+
|/**
988+
| * @return The number of rows updated.
989+
| */
990+
|public fun delete(ids: kotlin.collections.Collection<Int>): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
991+
| val idsIndexes = createArguments(count = ids.size)
992+
| driver.execute(null, ""${'"'}DELETE FROM data WHERE id IN ${'$'}idsIndexes""${'"'}, ids.size) {
993+
| ids.forEachIndexed { index, ids_ ->
994+
| bindLong(index, data_Adapter.idAdapter.encode(ids_))
994995
| }
995-
| driver.execute(null, ""${'"'}DELETE FROM data WHERE other IN ${'$'}idsIndexes""${'"'}, ids.size) {
996-
| ids.forEachIndexed { index, ids_ ->
997-
| bindLong(index, data_Adapter.idAdapter.encode(ids_))
998-
| }
996+
| }
997+
| driver.execute(null, ""${'"'}DELETE FROM data WHERE other IN ${'$'}idsIndexes""${'"'}, ids.size) {
998+
| ids.forEachIndexed { index, ids_ ->
999+
| bindLong(index, data_Adapter.idAdapter.encode(ids_))
9991000
| }
1000-
| }
1001+
| }
1002+
|} .also {
10011003
| notifyQueries(${mutator.id.withUnderscores}) { emit ->
10021004
| emit("data")
10031005
| }

sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/SelectQueryTypeTest.kt

Lines changed: 85 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,21 +1460,23 @@ class SelectQueryTypeTest {
14601460

14611461
assertThat(generator.function().toString()).isEqualTo(
14621462
"""
1463-
|public fun insertTwice(`value`: kotlin.Long) {
1464-
| transaction {
1465-
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1466-
| |INSERT INTO data (value)
1467-
| | VALUES (?)
1468-
| ""${'"'}.trimMargin(), 1) {
1469-
| bindLong(0, value)
1470-
| }
1471-
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1472-
| |INSERT INTO data (value)
1473-
| | VALUES (?)
1474-
| ""${'"'}.trimMargin(), 1) {
1475-
| bindLong(0, value)
1476-
| }
1477-
| }
1463+
|/**
1464+
| * @return The number of rows updated.
1465+
| */
1466+
|public fun insertTwice(`value`: kotlin.Long): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
1467+
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1468+
| |INSERT INTO data (value)
1469+
| | VALUES (?)
1470+
| ""${'"'}.trimMargin(), 1) {
1471+
| bindLong(0, value)
1472+
| }
1473+
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1474+
| |INSERT INTO data (value)
1475+
| | VALUES (?)
1476+
| ""${'"'}.trimMargin(), 1) {
1477+
| bindLong(0, value)
1478+
| }
1479+
|} .also {
14781480
| notifyQueries(-609_468_782) { emit ->
14791481
| emit("data")
14801482
| }
@@ -1511,21 +1513,23 @@ class SelectQueryTypeTest {
15111513

15121514
assertThat(generator.function().toString()).isEqualTo(
15131515
"""
1514-
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long) {
1515-
| transaction {
1516-
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1517-
| |INSERT INTO data (value)
1518-
| | VALUES (?)
1519-
| ""${'"'}.trimMargin(), 1) {
1520-
| bindLong(0, value_)
1521-
| }
1522-
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1523-
| |INSERT INTO data (value)
1524-
| | VALUES (?)
1525-
| ""${'"'}.trimMargin(), 1) {
1526-
| bindLong(0, value__)
1527-
| }
1528-
| }
1516+
|/**
1517+
| * @return The number of rows updated.
1518+
| */
1519+
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
1520+
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1521+
| |INSERT INTO data (value)
1522+
| | VALUES (?)
1523+
| ""${'"'}.trimMargin(), 1) {
1524+
| bindLong(0, value_)
1525+
| }
1526+
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1527+
| |INSERT INTO data (value)
1528+
| | VALUES (?)
1529+
| ""${'"'}.trimMargin(), 1) {
1530+
| bindLong(0, value__)
1531+
| }
1532+
|} .also {
15291533
| notifyQueries(-609_468_782) { emit ->
15301534
| emit("data")
15311535
| }
@@ -1562,21 +1566,23 @@ class SelectQueryTypeTest {
15621566

15631567
assertThat(generator.function().toString()).isEqualTo(
15641568
"""
1565-
|public fun insertTwice(value_: kotlin.Long) {
1566-
| transaction {
1567-
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1568-
| |INSERT INTO data (value)
1569-
| | VALUES (?)
1570-
| ""${'"'}.trimMargin(), 1) {
1571-
| bindLong(0, value_)
1572-
| }
1573-
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1574-
| |INSERT INTO data (value)
1575-
| | VALUES (?)
1576-
| ""${'"'}.trimMargin(), 1) {
1577-
| bindLong(0, value_)
1578-
| }
1579-
| }
1569+
|/**
1570+
| * @return The number of rows updated.
1571+
| */
1572+
|public fun insertTwice(value_: kotlin.Long): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
1573+
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1574+
| |INSERT INTO data (value)
1575+
| | VALUES (?)
1576+
| ""${'"'}.trimMargin(), 1) {
1577+
| bindLong(0, value_)
1578+
| }
1579+
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1580+
| |INSERT INTO data (value)
1581+
| | VALUES (?)
1582+
| ""${'"'}.trimMargin(), 1) {
1583+
| bindLong(0, value_)
1584+
| }
1585+
|} .also {
15801586
| notifyQueries(-609_468_782) { emit ->
15811587
| emit("data")
15821588
| }
@@ -1613,21 +1619,23 @@ class SelectQueryTypeTest {
16131619

16141620
assertThat(generator.function().toString()).isEqualTo(
16151621
"""
1616-
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long) {
1617-
| transaction {
1618-
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1619-
| |INSERT INTO data (value)
1620-
| | VALUES (?)
1621-
| ""${'"'}.trimMargin(), 1) {
1622-
| bindLong(0, value_)
1623-
| }
1624-
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1625-
| |INSERT INTO data (value)
1626-
| | VALUES (?)
1627-
| ""${'"'}.trimMargin(), 1) {
1628-
| bindLong(0, value__)
1629-
| }
1630-
| }
1622+
|/**
1623+
| * @return The number of rows updated.
1624+
| */
1625+
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
1626+
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1627+
| |INSERT INTO data (value)
1628+
| | VALUES (?)
1629+
| ""${'"'}.trimMargin(), 1) {
1630+
| bindLong(0, value_)
1631+
| }
1632+
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1633+
| |INSERT INTO data (value)
1634+
| | VALUES (?)
1635+
| ""${'"'}.trimMargin(), 1) {
1636+
| bindLong(0, value__)
1637+
| }
1638+
|} .also {
16311639
| notifyQueries(-609_468_782) { emit ->
16321640
| emit("data")
16331641
| }
@@ -1668,21 +1676,23 @@ class SelectQueryTypeTest {
16681676

16691677
assertThat(generator.function().toString()).isEqualTo(
16701678
"""
1671-
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long) {
1672-
| transaction {
1673-
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1674-
| |INSERT INTO data (value)
1675-
| | VALUES (?)
1676-
| ""${'"'}.trimMargin(), 1) {
1677-
| bindLong(0, value_)
1678-
| }
1679-
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1680-
| |INSERT INTO data (value)
1681-
| | VALUES (?)
1682-
| ""${'"'}.trimMargin(), 1) {
1683-
| bindLong(0, value__)
1684-
| }
1685-
| }
1679+
|/**
1680+
| * @return The number of rows updated.
1681+
| */
1682+
|public fun insertTwice(value_: kotlin.Long, value__: kotlin.Long): app.cash.sqldelight.db.QueryResult<kotlin.Long> = transactionWithResult {
1683+
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
1684+
| |INSERT INTO data (value)
1685+
| | VALUES (?)
1686+
| ""${'"'}.trimMargin(), 1) {
1687+
| bindLong(0, value_)
1688+
| }
1689+
| driver.execute(${query.idForIndex(1).withUnderscores}, ""${'"'}
1690+
| |INSERT INTO data (value)
1691+
| | VALUES (?)
1692+
| ""${'"'}.trimMargin(), 1) {
1693+
| bindLong(0, value__)
1694+
| }
1695+
|} .also {
16861696
| notifyQueries(${query.id.withUnderscores}) { emit ->
16871697
| emit("data")
16881698
| }

sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/async/AsyncSelectQueryTypeTest.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,11 @@ class AsyncSelectQueryTypeTest {
183183

184184
assertThat(generator.function().toString()).isEqualTo(
185185
"""
186-
|public suspend fun insertTwice(`value`: kotlin.Long) {
187-
| transaction {
186+
|/**
187+
| * @return The number of rows updated.
188+
| */
189+
|public suspend fun insertTwice(`value`: kotlin.Long): kotlin.Long = app.cash.sqldelight.db.QueryResult.AsyncValue {
190+
| transactionWithResult {
188191
| driver.execute(${query.idForIndex(0).withUnderscores}, ""${'"'}
189192
| |INSERT INTO data (value)
190193
| | VALUES (?)
@@ -197,9 +200,10 @@ class AsyncSelectQueryTypeTest {
197200
| ""${'"'}.trimMargin(), 1) {
198201
| bindLong(0, value)
199202
| }.await()
200-
| }
201-
| notifyQueries(-609_468_782) { emit ->
202-
| emit("data")
203+
| } .also {
204+
| notifyQueries(-609_468_782) { emit ->
205+
| emit("data")
206+
| }
203207
| }
204208
|}
205209
|

0 commit comments

Comments
 (0)