remove id in EntityI interface & remove usless EntitiesCollections
This commit is contained in:
1
.idea/.gitignore
generated
vendored
1
.idea/.gitignore
generated
vendored
@@ -7,3 +7,4 @@
|
|||||||
/uiDesigner.xml
|
/uiDesigner.xml
|
||||||
/dataSources.xml
|
/dataSources.xml
|
||||||
/sonarlint/
|
/sonarlint/
|
||||||
|
/jarRepositories.xml
|
||||||
@@ -38,14 +38,14 @@ class Connection(
|
|||||||
|
|
||||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (QueryResult, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
val primaryObject = values.firstOrNull {
|
val primaryObject = values.firstOrNull {
|
||||||
it is EntityI<*> && typeReference.type.typeName == it::class.java.name
|
it is EntityI && typeReference.type.typeName == it::class.java.name
|
||||||
} as R?
|
} as R?
|
||||||
val result = exec(sql, compileArgs(values))
|
val result = exec(sql, compileArgs(values))
|
||||||
val json = result.rows[0].getString(0)
|
val json = result.rows[0].getString(0)
|
||||||
@@ -62,14 +62,14 @@ class Connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
sql: String,
|
sql: String,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
select(sql, object: TypeReference<R>() {}, values, block)
|
select(sql, object: TypeReference<R>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
@@ -80,14 +80,14 @@ class Connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
sql: String,
|
sql: String,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
select(sql, object: TypeReference<R>() {}, values, block)
|
select(sql, object: TypeReference<R>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
@@ -96,7 +96,7 @@ class Connection(
|
|||||||
val result = exec(sql, compileArgs(values))
|
val result = exec(sql, compileArgs(values))
|
||||||
val json = result.rows[0].getString(0)
|
val json = result.rows[0].getString(0)
|
||||||
return if (json === null) {
|
return if (json === null) {
|
||||||
listOf<EntityI<*>>() as List<R>
|
listOf<EntityI>() as List<R>
|
||||||
} else {
|
} else {
|
||||||
serializer.deserializeList(json, typeReference)
|
serializer.deserializeList(json, typeReference)
|
||||||
}.also {
|
}.also {
|
||||||
@@ -104,14 +104,14 @@ class Connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
@@ -131,7 +131,7 @@ class Connection(
|
|||||||
return line.run {
|
return line.run {
|
||||||
val json = rows[0].getString(0)
|
val json = rows[0].getString(0)
|
||||||
val entities = if (json === null) {
|
val entities = if (json === null) {
|
||||||
listOf<EntityI<*>>() as List<R>
|
listOf<EntityI>() as List<R>
|
||||||
} else {
|
} else {
|
||||||
serializer.deserializeList(json, typeReference)
|
serializer.deserializeList(json, typeReference)
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,7 @@ class Connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
@@ -155,7 +155,7 @@ class Connection(
|
|||||||
): Paginated<R> =
|
): Paginated<R> =
|
||||||
select(sql, page, limit, object: TypeReference<List<R>>() {}, values, block)
|
select(sql, page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
@@ -166,7 +166,7 @@ class Connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
@@ -201,10 +201,8 @@ class Connection(
|
|||||||
|
|
||||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||||
return values.map {
|
return values.map {
|
||||||
if (it is EntityI<*>) {
|
if (it is EntityI) {
|
||||||
serializer.serialize(it).apply {
|
serializer.serialize(it)
|
||||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
it
|
it
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,33 +13,33 @@ interface EmbedExecutable {
|
|||||||
/**
|
/**
|
||||||
* Select One entity with list of parameters
|
* Select One entity with list of parameters
|
||||||
*/
|
*/
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
block: SelectOneCallback<R> = {}
|
block: SelectOneCallback<R> = {}
|
||||||
): R?
|
): R?
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: SelectOneCallback<R> = {}
|
block: SelectOneCallback<R> = {}
|
||||||
): R?
|
): R?
|
||||||
|
|
||||||
/* Select Miltiples */
|
/* Select Miltiples */
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
block: SelectCallback<R> = {}
|
block: SelectCallback<R> = {}
|
||||||
): List<R>
|
): List<R>
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: SelectCallback<R> = {}
|
block: SelectCallback<R> = {}
|
||||||
): List<R>
|
): List<R>
|
||||||
|
|
||||||
/* Select Paginated */
|
/* Select Paginated */
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import fr.postgresjson.entity.EntityI
|
|||||||
interface Executable {
|
interface Executable {
|
||||||
/* Select One */
|
/* Select One */
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
block: SelectOneCallback<R> = {}
|
block: SelectOneCallback<R> = {}
|
||||||
): R?
|
): R?
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
@@ -23,14 +23,14 @@ interface Executable {
|
|||||||
|
|
||||||
/* Select Miltiples */
|
/* Select Miltiples */
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
block: SelectCallback<R> = {}
|
block: SelectCallback<R> = {}
|
||||||
): List<R>
|
): List<R>
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
@@ -39,7 +39,7 @@ interface Executable {
|
|||||||
|
|
||||||
/* Select Paginated */
|
/* Select Paginated */
|
||||||
|
|
||||||
fun <R: EntityI<*>> select(
|
fun <R: EntityI> select(
|
||||||
sql: String,
|
sql: String,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
/**
|
/**
|
||||||
* Select One entity with list of parameters
|
* Select One entity with list of parameters
|
||||||
*/
|
*/
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (QueryResult, R?) -> Unit
|
||||||
@@ -28,13 +28,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
return connection.select(sql, typeReference, values, block)
|
return connection.select(sql, typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
select(object: TypeReference<R>() {}, values, block)
|
select(object: TypeReference<R>() {}, values, block)
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
value: R,
|
value: R,
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
@@ -43,7 +43,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
/**
|
/**
|
||||||
* Select One entity with named parameters
|
* Select One entity with named parameters
|
||||||
*/
|
*/
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (QueryResult, R?) -> Unit
|
||||||
@@ -54,13 +54,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
return connection.select(sql, typeReference, values, block)
|
return connection.select(sql, typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
select(object: TypeReference<R>() {}, values, block)
|
select(object: TypeReference<R>() {}, values, block)
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
vararg values: Pair<String, Any?>,
|
vararg values: Pair<String, Any?>,
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
@@ -71,7 +71,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
/**
|
/**
|
||||||
* Select list of entities with list of parameters
|
* Select list of entities with list of parameters
|
||||||
*/
|
*/
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (QueryResult, List<R>) -> Unit
|
||||||
@@ -82,7 +82,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
return connection.select(sql, typeReference, values, block)
|
return connection.select(sql, typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
@@ -91,7 +91,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
/**
|
/**
|
||||||
* Select list of entities with named parameters
|
* Select list of entities with named parameters
|
||||||
*/
|
*/
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (QueryResult, List<R>) -> Unit
|
||||||
@@ -102,13 +102,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
return connection.select(sql, typeReference, values, block)
|
return connection.select(sql, typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
select(object: TypeReference<List<R>>() {}, values, block)
|
select(object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
vararg values: Pair<String, Any?>,
|
vararg values: Pair<String, Any?>,
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
@@ -119,7 +119,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
/**
|
/**
|
||||||
* Select Multiple with pagination
|
* Select Multiple with pagination
|
||||||
*/
|
*/
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
@@ -137,7 +137,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
return connection.select(sql, page, limit, typeReference, values, block)
|
return connection.select(sql, page, limit, typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
values: Map<String, Any?> = emptyMap(),
|
values: Map<String, Any?> = emptyMap(),
|
||||||
@@ -145,7 +145,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
): Paginated<R> =
|
): Paginated<R> =
|
||||||
select(page, limit, object: TypeReference<List<R>>() {}, values, block)
|
select(page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
vararg values: Pair<String, Any?>,
|
vararg values: Pair<String, Any?>,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package fr.postgresjson.connexion
|
|||||||
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
|
||||||
data class Paginated<T: EntityI<*>>(
|
data class Paginated<T: EntityI>(
|
||||||
val result: List<T>,
|
val result: List<T>,
|
||||||
val offset: Int,
|
val offset: Int,
|
||||||
val limit: Int,
|
val limit: Int,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
|
|
||||||
/* Select One */
|
/* Select One */
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (QueryResult, R?) -> Unit
|
||||||
@@ -20,13 +20,13 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
select(object: TypeReference<R>() {}, values, block)
|
select(object: TypeReference<R>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (QueryResult, R?) -> Unit
|
||||||
@@ -34,7 +34,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> selectOne(
|
inline fun <reified R: EntityI> selectOne(
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectOneCallback<R> = {}
|
noinline block: SelectOneCallback<R> = {}
|
||||||
): R? =
|
): R? =
|
||||||
@@ -42,7 +42,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
|
|
||||||
/* Select Multiples */
|
/* Select Multiples */
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (QueryResult, List<R>) -> Unit
|
||||||
@@ -50,13 +50,13 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
values: List<Any?> = emptyList(),
|
values: List<Any?> = emptyList(),
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
select(object: TypeReference<List<R>>() {}, values, block)
|
select(object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (QueryResult, List<R>) -> Unit
|
||||||
@@ -64,13 +64,13 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
noinline block: SelectCallback<R> = {}
|
noinline block: SelectCallback<R> = {}
|
||||||
): List<R> =
|
): List<R> =
|
||||||
select(object: TypeReference<List<R>>() {}, values, block)
|
select(object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
override fun <R: EntityI<*>> select(
|
override fun <R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
@@ -82,7 +82,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
|
|
||||||
/* Select Paginated */
|
/* Select Paginated */
|
||||||
|
|
||||||
inline fun <reified R: EntityI<*>> select(
|
inline fun <reified R: EntityI> select(
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int,
|
limit: Int,
|
||||||
values: Map<String, Any?> = emptyMap(),
|
values: Map<String, Any?> = emptyMap(),
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
package fr.postgresjson.entity
|
|
||||||
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
class EntitiesCollections {
|
|
||||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
|
||||||
|
|
||||||
fun <I, R: EntityI<I>> get(id: I, className: KClass<R>): R? {
|
|
||||||
val collection = collections[className]
|
|
||||||
val entity = collection?.get(id!!)
|
|
||||||
return entity as R?
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <I, reified R: EntityI<I>> get(id: I): R? {
|
|
||||||
return get(id, R::class)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <I, R: EntityI<out I?>> set(entity: R): EntitiesCollections {
|
|
||||||
if (collections[entity.className] == null) {
|
|
||||||
collections[entity.className] = EntityCollection()
|
|
||||||
}
|
|
||||||
|
|
||||||
collections[entity.className]!!.set(entity as EntityI<Any?>)
|
|
||||||
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
class EntityCollection<T, E: EntityI<T?>> {
|
|
||||||
private var collection: MutableMap<T, E> = mutableMapOf()
|
|
||||||
|
|
||||||
fun get(id: T): E? {
|
|
||||||
return collection[id]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun set(entity: E) {
|
|
||||||
val id = entity.id
|
|
||||||
if (id !== null) {
|
|
||||||
collection[id] = entity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,13 +6,12 @@ import java.util.*
|
|||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
/* ID */
|
/* ID */
|
||||||
interface EntityI<T> {
|
interface EntityI {
|
||||||
var id: T?
|
val className: KClass<EntityI>
|
||||||
val className: KClass<EntityI<T>>
|
@JsonIgnore() get() = this::class as KClass<EntityI>
|
||||||
@JsonIgnore() get() = this::class as KClass<EntityI<T>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Entity<T>(override var id: T? = null): EntityI<T>
|
abstract class Entity<T>(open var id: T? = null): EntityI
|
||||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
||||||
open class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
open class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
||||||
|
|
||||||
@@ -57,62 +56,62 @@ class EntityDeletedAtImp: EntityDeletedAt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Author */
|
/* Author */
|
||||||
interface EntityCreatedBy<T: EntityI<*>> {
|
interface EntityCreatedBy<T: EntityI> {
|
||||||
var createdBy: T?
|
var createdBy: T?
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EntityUpdatedBy<T: EntityI<*>> {
|
interface EntityUpdatedBy<T: EntityI> {
|
||||||
var updatedBy: T?
|
var updatedBy: T?
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EntityDeletedBy<T: EntityI<*>> {
|
interface EntityDeletedBy<T: EntityI> {
|
||||||
var deletedBy: T?
|
var deletedBy: T?
|
||||||
}
|
}
|
||||||
|
|
||||||
class EntityCreatedByImp<UserT: EntityI<*>>(
|
class EntityCreatedByImp<UserT: EntityI>(
|
||||||
override var createdBy: UserT?
|
override var createdBy: UserT?
|
||||||
): EntityCreatedBy<UserT>
|
): EntityCreatedBy<UserT>
|
||||||
|
|
||||||
class EntityUpdatedByImp<UserT: EntityI<*>>(
|
class EntityUpdatedByImp<UserT: EntityI>(
|
||||||
override var updatedBy: UserT?
|
override var updatedBy: UserT?
|
||||||
): EntityUpdatedBy<UserT>
|
): EntityUpdatedBy<UserT>
|
||||||
|
|
||||||
class EntityDeletedByImp<UserT: EntityI<*>>(
|
class EntityDeletedByImp<UserT: EntityI>(
|
||||||
override var deletedBy: UserT?
|
override var deletedBy: UserT?
|
||||||
): EntityDeletedBy<UserT>
|
): EntityDeletedBy<UserT>
|
||||||
|
|
||||||
/* Mixed */
|
/* Mixed */
|
||||||
class EntityDeletedImp<UserT: EntityI<*>>(
|
class EntityDeletedImp<UserT: EntityI>(
|
||||||
override var deletedBy: UserT? = null
|
override var deletedBy: UserT? = null
|
||||||
): EntityDeletedBy<UserT>,
|
): EntityDeletedBy<UserT>,
|
||||||
EntityDeletedAt by EntityDeletedAtImp()
|
EntityDeletedAt by EntityDeletedAtImp()
|
||||||
|
|
||||||
class EntityUpdatedImp<UserT: EntityI<*>>(
|
class EntityUpdatedImp<UserT: EntityI>(
|
||||||
override var updatedAt: DateTime? = null,
|
override var updatedAt: DateTime? = null,
|
||||||
override var updatedBy: UserT? = null
|
override var updatedBy: UserT? = null
|
||||||
): EntityUpdatedBy<UserT>,
|
): EntityUpdatedBy<UserT>,
|
||||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||||
|
|
||||||
class EntityCreatedImp<UserT: EntityI<*>>(
|
class EntityCreatedImp<UserT: EntityI>(
|
||||||
override var createdAt: DateTime? = null,
|
override var createdAt: DateTime? = null,
|
||||||
override var createdBy: UserT? = null
|
override var createdBy: UserT? = null
|
||||||
): EntityCreatedBy<UserT>,
|
): EntityCreatedBy<UserT>,
|
||||||
EntityCreatedAt by EntityCreatedAtImp()
|
EntityCreatedAt by EntityCreatedAtImp()
|
||||||
|
|
||||||
/* Published */
|
/* Published */
|
||||||
interface Published<UserT: EntityI<*>> {
|
interface Published<UserT: EntityI> {
|
||||||
var publishedAt: DateTime?
|
var publishedAt: DateTime?
|
||||||
var publishedBy: UserT?
|
var publishedBy: UserT?
|
||||||
}
|
}
|
||||||
|
|
||||||
class EntityPublishedImp<UserT: EntityI<*>>(
|
class EntityPublishedImp<UserT: EntityI>(
|
||||||
override var publishedBy: UserT?
|
override var publishedBy: UserT?
|
||||||
): Published<UserT> {
|
): Published<UserT> {
|
||||||
override var publishedAt: DateTime? = null
|
override var publishedAt: DateTime? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Implementation */
|
/* Implementation */
|
||||||
abstract class EntityImp<T, UserT: EntityI<*>>(
|
abstract class EntityImp<T, UserT: EntityI>(
|
||||||
updatedBy: UserT?
|
updatedBy: UserT?
|
||||||
): Entity<T>(),
|
): Entity<T>(),
|
||||||
EntityCreatedAt by EntityCreatedAtImp(),
|
EntityCreatedAt by EntityCreatedAtImp(),
|
||||||
@@ -122,7 +121,7 @@ abstract class EntityImp<T, UserT: EntityI<*>>(
|
|||||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||||
|
|
||||||
abstract class UuidEntityExtended<T, UserT: EntityI<*>>(
|
abstract class UuidEntityExtended<T, UserT: EntityI>(
|
||||||
updatedBy: UserT?,
|
updatedBy: UserT?,
|
||||||
publishedBy: UserT?
|
publishedBy: UserT?
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import fr.postgresjson.connexion.Requester
|
|||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
interface RepositoryI<E: EntityI<*>> {
|
interface RepositoryI<E: EntityI> {
|
||||||
val entityName: KClass<E>
|
val entityName: KClass<E>
|
||||||
var requester: Requester
|
var requester: Requester
|
||||||
fun getClassName(): String {
|
fun getClassName(): String {
|
||||||
|
|||||||
@@ -1,29 +1,19 @@
|
|||||||
package fr.postgresjson.serializer
|
package fr.postgresjson.serializer
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParser
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.fasterxml.jackson.databind.*
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategy
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||||
import com.fasterxml.jackson.datatype.joda.JodaModule
|
import com.fasterxml.jackson.datatype.joda.JodaModule
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.fasterxml.jackson.module.kotlin.readValue
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
import fr.postgresjson.entity.EntitiesCollections
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import fr.postgresjson.entity.IdEntity
|
|
||||||
import fr.postgresjson.entity.UuidEntity
|
|
||||||
import java.io.IOException
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||||
|
|
||||||
var collection: EntitiesCollections = EntitiesCollections()
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val module = SimpleModule()
|
val module = SimpleModule()
|
||||||
module.addDeserializer(UuidEntity::class.java, EntityUuidDeserializer(collection))
|
|
||||||
module.addDeserializer(IdEntity::class.java, EntityIdDeserializer(collection))
|
|
||||||
mapper.registerModule(module)
|
mapper.registerModule(module)
|
||||||
mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
|
mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
|
||||||
|
|
||||||
@@ -32,16 +22,16 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> serialize(source: EntityI<T>, pretty: Boolean = false): String {
|
fun serialize(source: EntityI, pretty: Boolean = false): String {
|
||||||
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
||||||
else mapper.writeValueAsString(source)
|
else mapper.writeValueAsString(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <E: EntityI<*>> 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified E: EntityI<*>> deserialize(json: String): E? {
|
inline fun <reified E: EntityI> deserialize(json: String): E? {
|
||||||
return this.mapper.readValue(json)
|
return this.mapper.readValue(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,50 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
return deserializeList(json, object: TypeReference<E>() {})
|
return deserializeList(json, object: TypeReference<E>() {})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <E: EntityI<*>> 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> EntityI<T>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||||
inline fun <reified E: EntityI<*>> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
inline fun <reified E: EntityI> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||||
inline fun <reified E: EntityI<*>> String.deserialize() = Serializer().deserialize<E>(this)
|
inline fun <reified E: EntityI> String.deserialize() = Serializer().deserialize<E>(this)
|
||||||
|
|
||||||
|
|
||||||
class EntityUuidDeserializer<T: UuidEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(vc) {
|
|
||||||
var collection: EntitiesCollections = EntitiesCollections()
|
|
||||||
|
|
||||||
constructor(collection: EntitiesCollections): this() {
|
|
||||||
this.collection = collection
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(IOException::class, JsonProcessingException::class)
|
|
||||||
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
|
||||||
val node = jp.codec.readTree<JsonNode>(jp)
|
|
||||||
val id = node.get("id").asText()
|
|
||||||
val entity = collection.get<UUID, UuidEntity>(UUID.fromString(id))
|
|
||||||
|
|
||||||
return (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as T
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class EntityIdDeserializer<T: IdEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(vc) {
|
|
||||||
var collection: EntitiesCollections = EntitiesCollections()
|
|
||||||
|
|
||||||
constructor(collection: EntitiesCollections): this() {
|
|
||||||
this.collection = collection
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(IOException::class, JsonProcessingException::class)
|
|
||||||
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
|
||||||
val node = jp.codec.readTree<JsonNode>(jp)
|
|
||||||
val id = node.get("id").asInt()
|
|
||||||
val entity = collection.get<Int, IdEntity>(id)
|
|
||||||
|
|
||||||
val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI<Int>
|
|
||||||
collection.set(obj)
|
|
||||||
|
|
||||||
return obj as T
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,7 @@ import org.junit.jupiter.api.TestInstance
|
|||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class EntityTest() {
|
class EntityTest() {
|
||||||
private class User(override var id: Int?): EntityI<Int?>
|
private class User(id: Int?): Entity<Int?>(id)
|
||||||
private class ObjTest(var name: String): UuidEntityExtended<Int?, User>(User(1), User(2))
|
private class ObjTest(var name: String): UuidEntityExtended<Int?, User>(User(1), User(2))
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -15,7 +15,7 @@ class EntityTest() {
|
|||||||
val obj: ObjTest? = ObjTest("plop")
|
val obj: ObjTest? = ObjTest("plop")
|
||||||
assertTrue(obj is ObjTest)
|
assertTrue(obj is ObjTest)
|
||||||
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
||||||
assertTrue(obj is EntityI<Int?>)
|
assertTrue(obj is EntityI)
|
||||||
assertTrue(obj is Entity<Int?>)
|
assertTrue(obj is Entity<Int?>)
|
||||||
assertTrue(obj is Published<User>)
|
assertTrue(obj is Published<User>)
|
||||||
assertTrue(obj is EntityCreatedBy<User>)
|
assertTrue(obj is EntityCreatedBy<User>)
|
||||||
|
|||||||
Reference in New Issue
Block a user