refactoring: symplify generics
This commit is contained in:
@@ -12,10 +12,10 @@ import java.util.concurrent.CompletableFuture
|
||||
|
||||
|
||||
interface Executable {
|
||||
fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R
|
||||
fun <R : EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <R : EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
fun <R : List<EntityI<*>>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <R : List<EntityI<*>>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R
|
||||
fun exec(sql: String, values: List<Any?> = emptyList()): CompletableFuture<QueryResult>
|
||||
fun exec(sql: String, values: Map<String, Any?>): CompletableFuture<QueryResult>
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class Connection(
|
||||
|
||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
||||
|
||||
override fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R : EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
val future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||
val json = future.get().rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
@@ -50,44 +50,30 @@ class Connection(
|
||||
serializer.deserialize(json, typeReference)
|
||||
}
|
||||
}
|
||||
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? = select(sql, object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : EntityI<*>> selectOne(sql: String, values: List<Any?> = emptyList()): R? = select(sql, object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
val args = compileArgs(values)
|
||||
val replacedQuery = replaceArgs(sql, args)
|
||||
val future = connect().sendPreparedStatement(replacedQuery.sql, replacedQuery.parameters)
|
||||
val json = future.get().rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
null
|
||||
} else {
|
||||
serializer.deserialize(json, typeReference)
|
||||
}
|
||||
override fun <R : EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
val replacedQuery = replaceArgs(sql, values)
|
||||
return select(replacedQuery.sql, typeReference, replacedQuery.parameters)
|
||||
}
|
||||
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: Map<String, Any?>): R? = select(sql, object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : EntityI<*>> selectOne(sql: String, values: Map<String, Any?>): R? = select(sql, object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R {
|
||||
override fun <R : List<EntityI<*>>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R {
|
||||
val future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||
val json = future.get().rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
listOf<EntityI<T?>?>() as R
|
||||
listOf<EntityI<*>>() as R
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}
|
||||
}
|
||||
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: List<Any?> = emptyList()): R = select(sql, object : TypeReference<R>() {}, values)
|
||||
inline fun <reified R : List<EntityI<*>>> select(sql: String, values: List<Any?> = emptyList()): R = select(sql, object : TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
val args = compileArgs(values)
|
||||
val replacedQuery = replaceArgs(sql, args)
|
||||
val future = connect().sendPreparedStatement(replacedQuery.sql, replacedQuery.parameters)
|
||||
val json = future.get().rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
listOf<EntityI<T?>?>() as R
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}
|
||||
override fun <R : List<EntityI<*>>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
val replacedQuery = replaceArgs(sql, values)
|
||||
return select(replacedQuery.sql, typeReference, replacedQuery.parameters)
|
||||
}
|
||||
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: Map<String, Any?>): R = select(sql, object : TypeReference<R>() {}, values)
|
||||
inline fun <reified R : List<EntityI<*>>> select(sql: String, values: Map<String, Any?>): R = select(sql, object : TypeReference<R>() {}, values)
|
||||
|
||||
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
|
||||
return connect().sendPreparedStatement(sql, compileArgs(values))
|
||||
@@ -110,18 +96,6 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
private fun compileArgs(values: Map<String, Any?>): Map<String, Any?> {
|
||||
return values.map {(key, value) ->
|
||||
if (value is EntityI<*>) {
|
||||
val json = serializer.serialize(value)
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(value as EntityI<Any?>)
|
||||
key to json
|
||||
} else {
|
||||
key to value
|
||||
}
|
||||
}.toMap()
|
||||
}
|
||||
|
||||
private fun replaceArgs(sql: String, values: Map<String, Any?>): ParametersQuery {
|
||||
val paramRegex = "(?<!:):([a-zA-Z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
|
||||
val newArgs = paramRegex.findAll(sql).map { match ->
|
||||
|
||||
@@ -79,25 +79,25 @@ class Requester (
|
||||
return sql
|
||||
}
|
||||
|
||||
override fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R : EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R : EntityI<T?>?> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : EntityI<*>> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R: EntityI<T?>?> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R : EntityI<T?>?> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : EntityI<*>> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R : List<EntityI<*>>> select(typeReference: TypeReference<R>, values: List<Any?>): R {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R : List<EntityI<T?>?>> select(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : List<EntityI<*>>> select(values: List<Any?> = emptyList()): R = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun <T, R: List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
override fun <R: List<EntityI<*>>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R : List<EntityI<T?>?>> select(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R : List<EntityI<*>>> select(values: Map<String, Any?>): R = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun exec(values: List<Any?>): CompletableFuture<QueryResult> {
|
||||
return connection.exec(sql, values)
|
||||
@@ -116,46 +116,46 @@ class Requester (
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
override fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R : EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R: EntityI<T?>?> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
/**
|
||||
* Select One entity with named parameters
|
||||
*/
|
||||
override fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
override fun <R : EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R: EntityI<T?>?> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
/**
|
||||
* Select list of entities with list of parameters
|
||||
*/
|
||||
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R : List<EntityI<*>>> select(typeReference: TypeReference<R>, values: List<Any?>): R {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R: List<EntityI<T?>?>> select(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: List<EntityI<*>>> select(values: List<Any?> = emptyList()): R = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
/**
|
||||
* Select list of entities with named parameters
|
||||
*/
|
||||
override fun <T, R: List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
override fun <R: List<EntityI<*>>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values)
|
||||
}
|
||||
inline fun <T, reified R: List<EntityI<T?>?>> select(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: List<EntityI<*>>> select(values: Map<String, Any?>): R = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
override fun exec(values: List<Any?>): CompletableFuture<QueryResult> {
|
||||
val args = compileArgs(values)
|
||||
@@ -203,11 +203,11 @@ class Requester (
|
||||
val connection : Connection
|
||||
override fun toString(): String
|
||||
|
||||
fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
fun <R : EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <R : EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
|
||||
fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R
|
||||
fun <R : List<EntityI<*>>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R
|
||||
fun <R : List<EntityI<*>>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): CompletableFuture<QueryResult>
|
||||
fun exec(values: Map<String, Any?>): CompletableFuture<QueryResult>
|
||||
|
||||
@@ -38,7 +38,7 @@ data class Function(
|
||||
connection.exec(up.script)
|
||||
|
||||
File(this::class.java.getResource("/sql/migration/insertFunction.sql").toURI()).let {
|
||||
connection.selectOne<String, MigrationEntity?>(it.readText(), listOf(up))?.let { function ->
|
||||
connection.selectOne<MigrationEntity>(it.readText(), listOf(up))?.let { function ->
|
||||
executedAt = function.executedAt
|
||||
doExecute = Action.OK
|
||||
}
|
||||
|
||||
@@ -59,14 +59,14 @@ data class Migrations private constructor(
|
||||
*/
|
||||
private fun getMigrationFromDB() {
|
||||
File(this::class.java.getResource("/sql/migration/findAllFunction.sql").toURI()).let {
|
||||
connection.select<String, List<MigrationEntity?>>(it.readText())
|
||||
connection.select<List<MigrationEntity>>(it.readText())
|
||||
.filterNotNull().map { function ->
|
||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||
}
|
||||
}
|
||||
|
||||
File(this::class.java.getResource("/sql/migration/findAllHistory.sql").toURI()).let {
|
||||
connection.select<String, List<MigrationEntity?>>(it.readText())
|
||||
connection.select<List<MigrationEntity>>(it.readText())
|
||||
.filterNotNull().map { query ->
|
||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ data class Query(
|
||||
connection.exec(up).join()
|
||||
|
||||
File(this::class.java.getResource("/sql/migration/insertHistory.sql").toURI()).let {
|
||||
connection.selectOne<String, MigrationEntity?>(it.readText(), listOf(name, up, down))?.let { query ->
|
||||
connection.selectOne<MigrationEntity>(it.readText(), listOf(name, up, down))?.let { query ->
|
||||
executedAt = query.executedAt
|
||||
doExecute = Action.OK
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
return mapper.writeValueAsString(source)
|
||||
}
|
||||
|
||||
fun <T, E : EntityI<T?>?> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
fun <E : EntityI<*>> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
return this.mapper.readValue(json, valueTypeRef)
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
return deserializeList(json, object: TypeReference<E>() {})
|
||||
}
|
||||
|
||||
fun <T, E : EntityI<T?>> deserialize(json: String, target: E): E {
|
||||
fun <E : EntityI<*>> deserialize(json: String, target: E): E {
|
||||
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user