refactoring: symplify generics
This commit is contained in:
@@ -12,10 +12,10 @@ import java.util.concurrent.CompletableFuture
|
|||||||
|
|
||||||
|
|
||||||
interface Executable {
|
interface Executable {
|
||||||
fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
fun <R : EntityI<*>> 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 <R : EntityI<*>> 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 <R : List<EntityI<*>>> 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 : 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: List<Any?> = emptyList()): CompletableFuture<QueryResult>
|
||||||
fun exec(sql: String, values: Map<String, Any?>): 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)
|
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 future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
return if (json === null) {
|
return if (json === null) {
|
||||||
@@ -50,44 +50,30 @@ class Connection(
|
|||||||
serializer.deserialize(json, typeReference)
|
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? {
|
override fun <R : EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||||
val args = compileArgs(values)
|
val replacedQuery = replaceArgs(sql, values)
|
||||||
val replacedQuery = replaceArgs(sql, args)
|
return select(replacedQuery.sql, typeReference, replacedQuery.parameters)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
inline fun <reified R : EntityI<*>> selectOne(sql: String, values: Map<String, Any?>): R? = select(sql, object: TypeReference<R>() {}, values)
|
||||||
inline fun <T, reified R : EntityI<T?>?> 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 future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
return if (json === null) {
|
return if (json === null) {
|
||||||
listOf<EntityI<T?>?>() as R
|
listOf<EntityI<*>>() as R
|
||||||
} else {
|
} else {
|
||||||
serializer.deserializeList(json, typeReference)
|
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 {
|
override fun <R : List<EntityI<*>>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R {
|
||||||
val args = compileArgs(values)
|
val replacedQuery = replaceArgs(sql, values)
|
||||||
val replacedQuery = replaceArgs(sql, args)
|
return select(replacedQuery.sql, typeReference, replacedQuery.parameters)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
inline fun <reified R : List<EntityI<*>>> select(sql: String, values: Map<String, Any?>): R = select(sql, object : TypeReference<R>() {}, values)
|
||||||
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: Map<String, Any?>): R = select(sql, object : TypeReference<R>() {}, values)
|
|
||||||
|
|
||||||
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
|
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
|
||||||
return connect().sendPreparedStatement(sql, compileArgs(values))
|
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 {
|
private fun replaceArgs(sql: String, values: Map<String, Any?>): ParametersQuery {
|
||||||
val paramRegex = "(?<!:):([a-zA-Z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
|
val paramRegex = "(?<!:):([a-zA-Z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
|
||||||
val newArgs = paramRegex.findAll(sql).map { match ->
|
val newArgs = paramRegex.findAll(sql).map { match ->
|
||||||
|
|||||||
@@ -79,25 +79,25 @@ class Requester (
|
|||||||
return sql
|
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)
|
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)
|
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)
|
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)
|
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> {
|
override fun exec(values: List<Any?>): CompletableFuture<QueryResult> {
|
||||||
return connection.exec(sql, values)
|
return connection.exec(sql, values)
|
||||||
@@ -116,46 +116,46 @@ class Requester (
|
|||||||
/**
|
/**
|
||||||
* Select One entity with list of parameters
|
* 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 args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.select(sql, typeReference, values)
|
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
|
* 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 args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.select(sql, typeReference, values)
|
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
|
* 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 args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.select(sql, typeReference, values)
|
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
|
* 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 args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.select(sql, typeReference, values)
|
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> {
|
override fun exec(values: List<Any?>): CompletableFuture<QueryResult> {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
@@ -203,11 +203,11 @@ class Requester (
|
|||||||
val connection : Connection
|
val connection : Connection
|
||||||
override fun toString(): String
|
override fun toString(): String
|
||||||
|
|
||||||
fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
fun <R : EntityI<*>> 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: Map<String, Any?>): R?
|
||||||
|
|
||||||
fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
fun <R : List<EntityI<*>>> 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: Map<String, Any?>): R
|
||||||
|
|
||||||
fun exec(values: List<Any?> = emptyList()): CompletableFuture<QueryResult>
|
fun exec(values: List<Any?> = emptyList()): CompletableFuture<QueryResult>
|
||||||
fun exec(values: Map<String, Any?>): CompletableFuture<QueryResult>
|
fun exec(values: Map<String, Any?>): CompletableFuture<QueryResult>
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ data class Function(
|
|||||||
connection.exec(up.script)
|
connection.exec(up.script)
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/insertFunction.sql").toURI()).let {
|
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
|
executedAt = function.executedAt
|
||||||
doExecute = Action.OK
|
doExecute = Action.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,14 +59,14 @@ data class Migrations private constructor(
|
|||||||
*/
|
*/
|
||||||
private fun getMigrationFromDB() {
|
private fun getMigrationFromDB() {
|
||||||
File(this::class.java.getResource("/sql/migration/findAllFunction.sql").toURI()).let {
|
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 ->
|
.filterNotNull().map { function ->
|
||||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/findAllHistory.sql").toURI()).let {
|
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 ->
|
.filterNotNull().map { query ->
|
||||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ data class Query(
|
|||||||
connection.exec(up).join()
|
connection.exec(up).join()
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/insertHistory.sql").toURI()).let {
|
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
|
executedAt = query.executedAt
|
||||||
doExecute = Action.OK
|
doExecute = Action.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
return mapper.writeValueAsString(source)
|
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)
|
return this.mapper.readValue(json, valueTypeRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
return deserializeList(json, object: TypeReference<E>() {})
|
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)
|
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getObject() {
|
fun getObject() {
|
||||||
val obj: ObjTest? = connection.selectOne<Int?, ObjTest>("select to_json(a) from test a limit 1")
|
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||||
assertTrue(obj is ObjTest)
|
assertTrue(obj is ObjTest)
|
||||||
assertTrue(obj!!.id == 1)
|
assertTrue(obj!!.id == 1)
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
"third" to 123,
|
"third" to 123,
|
||||||
"seconde" to "sec"
|
"seconde" to "sec"
|
||||||
)
|
)
|
||||||
val result: List<ObjTest3?> = connection.select(
|
val result: List<ObjTest3> = connection.select(
|
||||||
"""
|
"""
|
||||||
SELECT json_build_array(
|
SELECT json_build_array(
|
||||||
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int),
|
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int),
|
||||||
|
|||||||
Reference in New Issue
Block a user