Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
be-hase committed Jun 8, 2024
1 parent 398def4 commit 23878cb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,66 @@ interface KueryBlockingClient {
fun sql(block: SqlDsl.() -> Unit): FetchSpec

interface FetchSpec {
/**
* Receives the results as a map.
*/
fun singleMap(): Map<String, Any?>

fun <T : Any> single(returnType: KClass<T>): T

/**
* Receives the results as a map.
*/
fun singleMapOrNull(): Map<String, Any?>?

/**
* Receives the results converted to the specified type.
*/
fun <T : Any> single(returnType: KClass<T>): T

/**
* Receives the results converted to the specified type.
*/
fun <T : Any> singleOrNull(returnType: KClass<T>): T?

/**
* Receives the results of multiple rows as a map.
*/
fun listMap(): List<Map<String, Any?>>

/**
* Receives the results of multiple rows converted to the specified type.
*/
fun <T : Any> list(returnType: KClass<T>): List<T>

/**
* Contract for fetching the number of affected rows
*/
fun rowsUpdated(): Long

/**
* Receives the values generated on the database side.
* For example, an auto increment value.
*/
fun generatedValues(vararg columns: String): Map<String, Any>
}
}

/**
* Receives the results converted to the specified type.
*/
inline fun <reified T : Any> FetchSpec.single(): T {
return single(T::class)
}

/**
* Receives the results converted to the specified type.
*/
inline fun <reified T : Any> FetchSpec.singleOrNull(): T? {
return singleOrNull(T::class)
}

/**
* Receives the results of multiple rows converted to the specified type.
*/
inline fun <reified T : Any> FetchSpec.list(): List<T> {
return list(T::class)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,88 @@ import kotlin.reflect.KClass

interface KueryClient {
/**
*
* Receives a DSL for constructing SQL and returns a FetchSpec that receives the execution results.
*/
fun sql(block: SqlDsl.() -> Unit): FetchSpec

interface FetchSpec {
/**
* Receives the results as a map.
*/
suspend fun singleMap(): Map<String, Any?>

suspend fun <T : Any> single(returnType: KClass<T>): T

/**
* Receives the results as a map.
*/
suspend fun singleMapOrNull(): Map<String, Any?>?

/**
* Receives the results converted to the specified type.
*/
suspend fun <T : Any> single(returnType: KClass<T>): T

/**
* Receives the results converted to the specified type.
*/
suspend fun <T : Any> singleOrNull(returnType: KClass<T>): T?

/**
* Receives the results of multiple rows as a map.
*/
suspend fun listMap(): List<Map<String, Any?>>

/**
* Receives the results of multiple rows converted to the specified type.
*/
suspend fun <T : Any> list(returnType: KClass<T>): List<T>

/**
* Receives the results of multiple rows as a map.
*/
fun flowMap(): Flow<Map<String, Any?>>

/**
* Receives the results of multiple rows converted to the specified type.
*/
fun <T : Any> flow(returnType: KClass<T>): Flow<T>

/**
* Contract for fetching the number of affected rows
*/
suspend fun rowsUpdated(): Long

/**
* Receives the values generated on the database side.
* For example, an auto increment value.
*/
suspend fun generatedValues(vararg columns: String): Map<String, Any>
}
}

/**
* Receives the results converted to the specified type.
*/
suspend inline fun <reified T : Any> FetchSpec.single(): T {
return single(T::class)
}

/**
* Receives the results converted to the specified type.
*/
suspend inline fun <reified T : Any> FetchSpec.singleOrNull(): T? {
return singleOrNull(T::class)
}

/**
* Receives the results of multiple rows converted to the specified type.
*/
suspend inline fun <reified T : Any> FetchSpec.list(): List<T> {
return list(T::class)
}

/**
* Receives the results of multiple rows converted to the specified type.
*/
inline fun <reified T : Any> FetchSpec.flow(): Flow<T> {
return flow(T::class)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CodeEnumConversionTest : MysqlTestContainersBase() {
}

enum class SampleStringCodeEnum(override val code: String) : StringCodeEnum {
HOGE("hoge"),
BAR("hoge"),
}

data class Record(
Expand Down Expand Up @@ -111,14 +111,14 @@ class CodeEnumConversionTest : MysqlTestContainersBase() {
fun test() = runTest {
kueryClient.sql {
+"INSERT INTO code_enum (int_enum, string_enum)"
+"VALUES (${bind(SampleIntCodeEnum.HOGE)}, ${bind(SampleStringCodeEnum.HOGE)})"
+"VALUES (${bind(SampleIntCodeEnum.HOGE)}, ${bind(SampleStringCodeEnum.BAR)})"
}.rowsUpdated()

val record: Record = kueryClient.sql {
+"SELECT * FROM code_enum"
}.single()

assertThat(record.intEnum).isEqualTo(SampleIntCodeEnum.HOGE)
assertThat(record.stringEnum).isEqualTo(SampleStringCodeEnum.HOGE)
assertThat(record.stringEnum).isEqualTo(SampleStringCodeEnum.BAR)
}
}

0 comments on commit 23878cb

Please sign in to comment.