Skip to content

Commit a1cf25d

Browse files
committed
Refactor: rename query as queryAsFlow to avoid confusion when using the API.
1 parent b87bd96 commit a1cf25d

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

coroutines/src/integrationTest/kotlin/net/samyn/kapper/coroutines/DatasourceWithConnectionDbTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class DatasourceWithConnectionDbTest {
2424
@Test
2525
fun `slow query on IO dispatcher`() {
2626
// this is a bit of a naughty test as it includes a sleep,
27-
// but wanted to validate this against a real JDBC driver and a real slow query.
27+
// but wanted to validate this against a real JDBC driver and a real slow queryAsFlow.
2828
runBlocking {
2929
val dataSource = createDataSource(postgresql)
3030
var slept = false
3131
val queryJob =
3232
launch {
3333
val int =
3434
dataSource.withConnection { connection ->
35-
println("Executing slow query - $connection")
35+
println("Executing slow queryAsFlow - $connection")
3636
connection.querySingle<Int>(
3737
"SELECT 1, pg_sleep(1)",
3838
{ rs, _ -> rs.getInt(1) },
@@ -51,7 +51,7 @@ class DatasourceWithConnectionDbTest {
5151
@Test
5252
fun `execute tx on IO dispatcher`() {
5353
// this is a bit of a naughty test as it includes a sleep,
54-
// but wanted to validate this against a real JDBC driver and a real slow query.
54+
// but wanted to validate this against a real JDBC driver and a real slow queryAsFlow.
5555
runBlocking {
5656
val dataSource = createDataSource(postgresql)
5757
val insertJob =
@@ -82,7 +82,7 @@ class DatasourceWithConnectionDbTest {
8282
@Test
8383
fun `execute failed tx on IO dispatcher`() {
8484
// this is a bit of a naughty test as it includes a sleep,
85-
// but wanted to validate this against a real JDBC driver and a real slow query.
85+
// but wanted to validate this against a real JDBC driver and a real slow queryAsFlow.
8686
runBlocking {
8787
val dataSource = createDataSource(postgresql)
8888
val queryJob =

coroutines/src/integrationTest/kotlin/net/samyn/kapper/coroutines/FlowTest.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class FlowTest {
7272
createDataSource(postgresql).withConnection { connection ->
7373
val one =
7474
async {
75-
printGreen("Executing query - $connection, filtering even ages")
75+
printGreen("Executing queryAsFlow - $connection, filtering even ages")
7676
connection
77-
.query<SuperHero>(
77+
.queryAsFlow<SuperHero>(
7878
"SELECT * FROM super_heroes",
7979
)
8080
.filter {
@@ -86,9 +86,9 @@ class FlowTest {
8686
}
8787
val two =
8888
async {
89-
printRed("Executing query - $connection, filtering odd ages")
89+
printRed("Executing queryAsFlow - $connection, filtering odd ages")
9090
connection
91-
.query<SuperHero>(
91+
.queryAsFlow<SuperHero>(
9292
"SELECT * FROM super_heroes",
9393
)
9494
.filter {
@@ -131,9 +131,9 @@ class FlowTest {
131131
createDataSource(postgresql).withConnection { connection ->
132132
val one =
133133
async {
134-
printGreen("Executing query - $connection, filtering even ages")
134+
printGreen("Executing queryAsFlow - $connection, filtering even ages")
135135
connection
136-
.query<SuperHero>(
136+
.queryAsFlow<SuperHero>(
137137
"SELECT * FROM super_heroes",
138138
)
139139
.filter {
@@ -153,9 +153,9 @@ class FlowTest {
153153
}
154154
val two =
155155
async {
156-
printRed("Executing query - $connection, filtering odd ages")
156+
printRed("Executing queryAsFlow - $connection, filtering odd ages")
157157
connection
158-
.query<SuperHero>(
158+
.queryAsFlow<SuperHero>(
159159
"SELECT * FROM super_heroes",
160160
)
161161
.filter {
@@ -196,9 +196,9 @@ class FlowTest {
196196
createDataSource(postgresql).withConnection { connection ->
197197
val job =
198198
async {
199-
println("Executing query - $connection, exit when age 20 is exceeded")
199+
println("Executing queryAsFlow - $connection, exit when age 20 is exceeded")
200200
connection
201-
.query<SuperHero>(
201+
.queryAsFlow<SuperHero>(
202202
"SELECT * FROM super_heroes",
203203
)
204204
.map {
@@ -209,7 +209,7 @@ class FlowTest {
209209
printGreen("[${Thread.currentThread().name}] acc=$a")
210210
a.also {
211211
if (a > 20) {
212-
println("Cancelling query as cumulative age is $a")
212+
println("Cancelling queryAsFlow as cumulative age is $a")
213213
cancel("Cumulative age exceeded 20")
214214
}
215215
}

coroutines/src/main/kotlin/net/samyn/kapper/coroutines/DatasourceWithConnection.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import javax.sql.DataSource
77
import kotlin.coroutines.CoroutineContext
88

99
/**
10-
* create a DB connection and execute query/aueries using the `Dispatchers.IO` CoroutineScope.
10+
* create a DB connection and execute queryAsFlow/aueries using the `Dispatchers.IO` CoroutineScope.
1111
* The connection is closed after the block is executed.
1212
* @param block The block of code to execute.
1313
*/
1414
suspend inline fun <T> DataSource.withConnection(crossinline block: suspend (Connection) -> T): T = withConnection(Dispatchers.IO, block)
1515

1616
/**
17-
* create a DB connection and execute query/aueries using the given CoroutineScope.
17+
* create a DB connection and execute queryAsFlow/aueries using the given CoroutineScope.
1818
* The connection is closed after the block is executed.
1919
* @param context The CoroutineContext to use.
2020
* @param block The block of code to execute.

coroutines/src/main/kotlin/net/samyn/kapper/coroutines/KapperKotlinFlowQueryFun.kt

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.sql.ResultSet
1515
import java.sql.SQLException
1616

1717
/**
18-
* Execute a SQL query and map the results to a Flow of instances of the specified class.
18+
* Execute a SQL queryAsFlow and map the results to a Flow of instances of the specified class.
1919
*
2020
* This function uses reflection to automatically map the result set columns to the properties of the specified class.
2121
* For advanced mappings, use the overloaded version with a custom `mapper` function.
@@ -26,7 +26,7 @@ import java.sql.SQLException
2626
* data class User(val id: Int, val name: String)
2727
*
2828
* // Fetch all users where "active" is true
29-
* val users: Flow<User> = connection.query(
29+
* val users: Flow<User> = connection.queryAsFlow(
3030
* sql = "SELECT id, name FROM users WHERE active = :active",
3131
* "active" to true
3232
* )
@@ -35,27 +35,27 @@ import java.sql.SQLException
3535
* users.collect { println(it) }
3636
* ```
3737
*
38-
* @param sql The SQL query to execute.
39-
* @param args Optional key-value pairs representing named parameters to substitute into the query.
40-
* @return The query result as a [Flow] of [T] instances.
38+
* @param sql The SQL queryAsFlow to execute.
39+
* @param args Optional key-value pairs representing named parameters to substitute into the queryAsFlow.
40+
* @return The queryAsFlow result as a [Flow] of [T] instances.
4141
* @throws java.sql.SQLException If there's a database error.
4242
*/
43-
inline fun <reified T : Any> Connection.query(
43+
inline fun <reified T : Any> Connection.queryAsFlow(
4444
sql: String,
4545
vararg args: Pair<String, Any?>,
4646
): Flow<T> =
47-
query(
47+
queryAsFlow(
4848
sql,
4949
createMapper(T::class.java)::createInstance,
5050
*args,
5151
)
5252

5353
/**
54-
* Execute a SQL query and map the results to a Flow of instances of the specified class with a custom mapper.
54+
* Execute a SQL queryAsFlow and map the results to a Flow of instances of the specified class with a custom mapper.
5555
*
5656
* **Example**:
5757
* ```kotlin
58-
* val users: Flow<User> = connection.query(
58+
* val users: Flow<User> = connection.queryAsFlow(
5959
* sql = "SELECT id, name FROM users",
6060
* mapper = { resultSet, _ ->
6161
* User(
@@ -67,18 +67,18 @@ inline fun <reified T : Any> Connection.query(
6767
* users.collect { println(it) }
6868
* ```
6969
*
70-
* @param sql The SQL query to execute.
70+
* @param sql The SQL queryAsFlow to execute.
7171
* @param mapper Custom mapping function to transform the [ResultSet] into the target class.
72-
* @param args Optional parameters to be substituted in the SQL query during execution.
73-
* @return The query result as a [Flow] of [T] instances.
72+
* @param args Optional parameters to be substituted in the SQL queryAsFlow during execution.
73+
* @return The queryAsFlow result as a [Flow] of [T] instances.
7474
* @throws KapperQueryException If there's a database error.
7575
*/
76-
inline fun <reified T : Any> Connection.query(
76+
inline fun <reified T : Any> Connection.queryAsFlow(
7777
sql: String,
7878
noinline mapper: (ResultSet, Map<String, Field>) -> T,
7979
vararg args: Pair<String, Any?>,
8080
): Flow<T> {
81-
require(sql.isNotBlank()) { "SQL query cannot be empty or blank" }
81+
require(sql.isNotBlank()) { "SQL queryAsFlow cannot be empty or blank" }
8282
this.executeQuery(Query(sql), args.toMap()).let { rs ->
8383
return queryFlow(rs, mapper, sql)
8484
}
@@ -102,7 +102,7 @@ fun <T : Any> queryFlow(
102102
logger.info("Query results processing cancelled: ${e.message}")
103103
throw e
104104
} catch (e: SQLException) {
105-
"Error executing query: $sql".also {
105+
"Error executing queryAsFlow: $sql".also {
106106
logger.warn(it, e)
107107
throw KapperQueryException(it, e)
108108
}

coroutines/src/test/kotlin/net/samyn/kapper/coroutines/FlowQueryTest.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FlowQueryTest {
5151
@Test
5252
fun `when query emit each row as a flow item`() {
5353
runBlocking {
54-
connection.query<Hero>(
54+
connection.queryAsFlow<Hero>(
5555
queryTemplate,
5656
mapper,
5757
"id" to 1,
@@ -64,7 +64,7 @@ class FlowQueryTest {
6464
runBlocking {
6565
mockkStatic("net.samyn.kapper.MapperFactoryKt") {
6666
every { createMapper(Hero::class.java).createInstance(any(), any()) } returns result
67-
connection.query<Hero>(
67+
connection.queryAsFlow<Hero>(
6868
queryTemplate,
6969
"id" to 1,
7070
).toList() shouldBe listOf(result)
@@ -75,7 +75,7 @@ class FlowQueryTest {
7575
@Test
7676
fun `when query emit close after collection`() {
7777
runBlocking {
78-
connection.query<Hero>(
78+
connection.queryAsFlow<Hero>(
7979
queryTemplate,
8080
mapper,
8181
"id" to 1,
@@ -89,7 +89,7 @@ class FlowQueryTest {
8989
val ex = Exception("test")
9090
runBlocking {
9191
try {
92-
connection.query<Hero>(
92+
connection.queryAsFlow<Hero>(
9393
queryTemplate,
9494
mapper,
9595
"id" to 1,
@@ -105,13 +105,13 @@ class FlowQueryTest {
105105

106106
@Test
107107
fun `when cancel close`() {
108-
// never finish the query
108+
// never finish the queryAsFlow
109109
every { resultSet.next() } returns true
110110
runBlocking {
111111
var count = 0
112112
val job =
113113
async {
114-
connection.query<Hero>(
114+
connection.queryAsFlow<Hero>(
115115
queryTemplate,
116116
mapper,
117117
"id" to 1,
@@ -134,7 +134,7 @@ class FlowQueryTest {
134134
every { resultSet.next() } throws ex
135135
runBlocking {
136136
shouldThrow<KapperQueryException> {
137-
connection.query<Hero>(
137+
connection.queryAsFlow<Hero>(
138138
queryTemplate,
139139
mapper,
140140
"id" to 1,
@@ -147,7 +147,7 @@ class FlowQueryTest {
147147
fun `when sql blank throw`() {
148148
runBlocking {
149149
shouldThrow<IllegalArgumentException> {
150-
connection.query<Hero>(
150+
connection.queryAsFlow<Hero>(
151151
"",
152152
mapper,
153153
"id" to 1,

0 commit comments

Comments
 (0)