diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 70cd4c5..f04118d 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -49,6 +49,9 @@ class Connection( fun inTransaction(f: (Connection) -> CompletableFuture) = connect().inTransaction(f) + /** + * Select One [EntityI] with [List] of parameters + */ override fun selectOne( sql: String, typeReference: TypeReference, @@ -66,13 +69,9 @@ class Connection( } } - inline fun selectOne( - sql: String, - values: List = emptyList(), - noinline block: SelectOneCallback = {} - ): R? = - selectOne(sql, object : TypeReference() {}, values, block) - + /** + * Select One [EntityI] with named parameters + */ override fun selectOne( sql: String, typeReference: TypeReference, @@ -84,13 +83,11 @@ class Connection( } } - inline fun selectOne( - sql: String, - values: Map, - noinline block: SelectOneCallback = {} - ): R? = - selectOne(sql, object : TypeReference() {}, values, block) + /* Select Multiples */ + /** + * Select multiple [EntityI] with [List] of parameters + */ override fun select( sql: String, typeReference: TypeReference>, @@ -108,13 +105,25 @@ class Connection( } } - inline fun select( + /** + * Select multiple [EntityI] with [Map] of parameters + */ + override fun select( sql: String, - values: List = emptyList(), - noinline block: SelectCallback = {} - ): List = - select(sql, object : TypeReference>() {}, values, block) + typeReference: TypeReference>, + values: Map, + block: (QueryResult, List) -> Unit + ): List { + return replaceArgs(sql, values) { + select(this.sql, typeReference, this.parameters, block) + } + } + /* Select Paginated */ + + /** + * Select Multiple [EntityI] with pagination + */ override fun select( sql: String, page: Int, @@ -150,33 +159,6 @@ class Connection( } } - inline fun select( - sql: String, - page: Int, - limit: Int, - values: Map = emptyMap(), - noinline block: SelectPaginatedCallback = {} - ): Paginated = - select(sql, page, limit, object : TypeReference>() {}, values, block) - - override fun select( - sql: String, - typeReference: TypeReference>, - values: Map, - block: (QueryResult, List) -> Unit - ): List { - return replaceArgs(sql, values) { - select(this.sql, typeReference, this.parameters, block) - } - } - - inline fun select( - sql: String, - values: Map, - noinline block: SelectCallback = {} - ): List = - select(sql, object : TypeReference>() {}, values, block) - override fun exec(sql: String, values: List): QueryResult { val compiledValues = compileArgs(values) return stopwatchQuery(sql, compiledValues) { diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt index 2a7fe5a..ba4bf69 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -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 */ diff --git a/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt b/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt new file mode 100644 index 0000000..25686e1 --- /dev/null +++ b/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt @@ -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 Executable.update( + sql: String, + value: R, + noinline block: SelectOneCallback = {} +): R? = + update(sql, object : TypeReference() {}, value, block) + +/* Select One */ + +/** + * Select One [EntityI] with [List] of parameters + */ +inline fun Executable.selectOne( + sql: String, + values: List = emptyList(), + noinline block: SelectOneCallback = {} +): R? = + selectOne(sql, object : TypeReference() {}, values, block) + +/** + * Select One [EntityI] with [Map] of parameters + */ +inline fun Executable.selectOne( + sql: String, + values: Map, + noinline block: SelectOneCallback = {} +): R? = + selectOne(sql, object : TypeReference() {}, values, block) + +/** + * Select One [EntityI] with multiple [Pair] of parameters + */ +inline fun Executable.selectOne( + sql: String, + vararg values: Pair, + noinline block: SelectOneCallback = {} +): R? = + selectOne(sql, object : TypeReference() {}, values = values, block) + +/* Select Multiples */ + +/** + * Select Multiple [EntityI] with [List] of parameters + */ +inline fun Executable.select( + sql: String, + values: List = emptyList(), + noinline block: SelectCallback = {} +): List = + select(sql, object : TypeReference>() {}, values, block) + +/** + * Select Multiple [EntityI] with [Map] of parameters + */ +inline fun Executable.select( + sql: String, + values: Map, + noinline block: SelectCallback = {} +): List = + select(sql, object : TypeReference>() {}, values, block) + +/** + * Select Multiple [EntityI] with multiple [Pair] of parameters + */ +inline fun Executable.select( + sql: String, + vararg values: Pair, + noinline block: SelectCallback = {} +): List = + select(sql, object : TypeReference>() {}, values = values, block) + +/* Select Paginated */ + +/** + * Select Paginated [EntityI] with [Map] of parameters + */ +inline fun Executable.select( + sql: String, + page: Int, + limit: Int, + values: Map = emptyMap(), + noinline block: SelectPaginatedCallback = {} +): Paginated = + select(sql, page, limit, object : TypeReference>() {}, values, block) + +/** + * Select Paginated [EntityI] with multiple [Pair] of parameters + */ +inline fun Executable.select( + sql: String, + page: Int, + limit: Int, + vararg values: Pair, + noinline block: SelectPaginatedCallback = {} +): Paginated = + select(sql, page, limit, object : TypeReference>() {}, values = values, block) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Function.kt b/src/main/kotlin/fr/postgresjson/connexion/Function.kt index 613d3bf..867091e 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Function.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Function.kt @@ -12,16 +12,6 @@ class Function(val definition: Function, override val connection: Connection) : override val name: String = definition.name - /** - * Update [EntityI] - */ - override fun update( - typeReference: TypeReference, - value: R, - block: (QueryResult, R?) -> Unit - ): R? = - connection.update(compileSql(value), typeReference, value, block) - /* Select One */ /** diff --git a/src/main/kotlin/fr/postgresjson/connexion/Query.kt b/src/main/kotlin/fr/postgresjson/connexion/Query.kt index 4567cfd..fc3e1ae 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Query.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Query.kt @@ -9,16 +9,6 @@ class Query(override val name: String, private val sql: String, override val con return sql } - /** - * Update [EntityI] - */ - override fun update( - typeReference: TypeReference, - 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 = connection.select(sql, typeReference, values, block) + /** + * Select multiple [EntityI] with [Map] of parameters + */ override fun select( typeReference: TypeReference>, values: Map, diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index 7395f54..b874fb3 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -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 diff --git a/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt b/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt index fccd0dd..67cbc99 100644 --- a/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt +++ b/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt @@ -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 diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index a0645ac..8aa0c71 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -1,6 +1,8 @@ package fr.postgresjson import fr.postgresjson.connexion.Paginated +import fr.postgresjson.connexion.select +import fr.postgresjson.connexion.selectOne import fr.postgresjson.entity.Parameter import fr.postgresjson.entity.UuidEntity import org.junit.Assert.assertEquals