Extract Reified method of connection to Extension

Add update method
This commit is contained in:
2021-07-17 00:24:31 +02:00
parent 69f85b5bf5
commit 95e7a32747
8 changed files with 143 additions and 66 deletions

View File

@@ -49,6 +49,9 @@ class Connection(
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
/**
* Select One [EntityI] with [List] of parameters
*/
override fun <R : EntityI> selectOne(
sql: String,
typeReference: TypeReference<R>,
@@ -66,13 +69,9 @@ class Connection(
}
}
inline fun <reified R : EntityI> selectOne(
sql: String,
values: List<Any?> = emptyList(),
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, object : TypeReference<R>() {}, values, block)
/**
* Select One [EntityI] with named parameters
*/
override fun <R : EntityI> selectOne(
sql: String,
typeReference: TypeReference<R>,
@@ -84,13 +83,11 @@ class Connection(
}
}
inline fun <reified R : EntityI> selectOne(
sql: String,
values: Map<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, object : TypeReference<R>() {}, values, block)
/* Select Multiples */
/**
* Select multiple [EntityI] with [List] of parameters
*/
override fun <R : EntityI> select(
sql: String,
typeReference: TypeReference<List<R>>,
@@ -108,13 +105,25 @@ class Connection(
}
}
inline fun <reified R : EntityI> select(
/**
* Select multiple [EntityI] with [Map] of parameters
*/
override fun <R : EntityI> select(
sql: String,
values: List<Any?> = emptyList(),
noinline block: SelectCallback<R> = {}
): List<R> =
select(sql, object : TypeReference<List<R>>() {}, values, block)
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
return replaceArgs(sql, values) {
select(this.sql, typeReference, this.parameters, block)
}
}
/* Select Paginated */
/**
* Select Multiple [EntityI] with pagination
*/
override fun <R : EntityI> select(
sql: String,
page: Int,
@@ -150,33 +159,6 @@ class Connection(
}
}
inline fun <reified R : EntityI> select(
sql: String,
page: Int,
limit: Int,
values: Map<String, Any?> = emptyMap(),
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(sql, page, limit, object : TypeReference<List<R>>() {}, values, block)
override fun <R : EntityI> select(
sql: String,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
return replaceArgs(sql, values) {
select(this.sql, typeReference, this.parameters, block)
}
}
inline fun <reified R : EntityI> select(
sql: String,
values: Map<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(sql, object : TypeReference<List<R>>() {}, values, block)
override fun exec(sql: String, values: List<Any?>): QueryResult {
val compiledValues = compileArgs(values)
return stopwatchQuery(sql, compiledValues) {

View File

@@ -6,7 +6,7 @@ import fr.postgresjson.entity.EntityI
interface Executable {
/* Select One */
/* Update */
/**
* Update [EntityI] with one entity as argument
@@ -19,6 +19,8 @@ interface Executable {
): R? =
selectOne(sql, typeReference, listOf(value), block)
/* Select One */
/**
* Select One [EntityI] with [List] of parameters
*/

View File

@@ -0,0 +1,106 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import fr.postgresjson.entity.EntityI
/* Update */
/**
* Update [EntityI] with one entity as argument
*/
inline fun <reified R : EntityI> Executable.update(
sql: String,
value: R,
noinline block: SelectOneCallback<R> = {}
): R? =
update(sql, object : TypeReference<R>() {}, value, block)
/* Select One */
/**
* Select One [EntityI] with [List] of parameters
*/
inline fun <reified R : EntityI> Executable.selectOne(
sql: String,
values: List<Any?> = emptyList(),
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, object : TypeReference<R>() {}, values, block)
/**
* Select One [EntityI] with [Map] of parameters
*/
inline fun <reified R : EntityI> Executable.selectOne(
sql: String,
values: Map<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, object : TypeReference<R>() {}, values, block)
/**
* Select One [EntityI] with multiple [Pair] of parameters
*/
inline fun <reified R : EntityI> Executable.selectOne(
sql: String,
vararg values: Pair<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, object : TypeReference<R>() {}, values = values, block)
/* Select Multiples */
/**
* Select Multiple [EntityI] with [List] of parameters
*/
inline fun <reified R : EntityI> Executable.select(
sql: String,
values: List<Any?> = emptyList(),
noinline block: SelectCallback<R> = {}
): List<R> =
select(sql, object : TypeReference<List<R>>() {}, values, block)
/**
* Select Multiple [EntityI] with [Map] of parameters
*/
inline fun <reified R : EntityI> Executable.select(
sql: String,
values: Map<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(sql, object : TypeReference<List<R>>() {}, values, block)
/**
* Select Multiple [EntityI] with multiple [Pair] of parameters
*/
inline fun <reified R : EntityI> Executable.select(
sql: String,
vararg values: Pair<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(sql, object : TypeReference<List<R>>() {}, values = values, block)
/* Select Paginated */
/**
* Select Paginated [EntityI] with [Map] of parameters
*/
inline fun <reified R : EntityI> Executable.select(
sql: String,
page: Int,
limit: Int,
values: Map<String, Any?> = emptyMap(),
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(sql, page, limit, object : TypeReference<List<R>>() {}, values, block)
/**
* Select Paginated [EntityI] with multiple [Pair] of parameters
*/
inline fun <reified R : EntityI> Executable.select(
sql: String,
page: Int,
limit: Int,
vararg values: Pair<String, Any?>,
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(sql, page, limit, object : TypeReference<List<R>>() {}, values = values, block)

View File

@@ -12,16 +12,6 @@ class Function(val definition: Function, override val connection: Connection) :
override val name: String = definition.name
/**
* Update [EntityI]
*/
override fun <R : EntityI> update(
typeReference: TypeReference<R>,
value: R,
block: (QueryResult, R?) -> Unit
): R? =
connection.update(compileSql(value), typeReference, value, block)
/* Select One */
/**

View File

@@ -9,16 +9,6 @@ class Query(override val name: String, private val sql: String, override val con
return sql
}
/**
* Update [EntityI]
*/
override fun <R : EntityI> update(
typeReference: TypeReference<R>,
value: R,
block: (QueryResult, R?) -> Unit
): R? =
connection.update(sql, typeReference, value, block)
/* Select One */
/**
@@ -53,6 +43,9 @@ class Query(override val name: String, private val sql: String, override val con
): List<R> =
connection.select(sql, typeReference, values, block)
/**
* Select multiple [EntityI] with [Map] of parameters
*/
override fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,

View File

@@ -2,6 +2,7 @@ package fr.postgresjson.migration
import com.github.jasync.sql.db.postgresql.exceptions.GenericDatabaseException
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.selectOne
import fr.postgresjson.migration.Migration.Action
import fr.postgresjson.migration.Migration.Status
import java.util.Date

View File

@@ -1,6 +1,7 @@
package fr.postgresjson.migration
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.selectOne
import fr.postgresjson.entity.Entity
import fr.postgresjson.migration.Migration.Action
import java.util.Date