diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
index 85e6648..4a20c62 100644
--- a/.idea/codeStyles/Project.xml
+++ b/.idea/codeStyles/Project.xml
@@ -14,8 +14,18 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml
index 7b7cef8..322c147 100644
--- a/.idea/dataSources.xml
+++ b/.idea/dataSources.xml
@@ -1,11 +1,11 @@
-
+
postgresql
true
org.postgresql.Driver
- jdbc:postgresql://localhost:5555/json_test
+ jdbc:postgresql://localhost:35555/json_test
$ProjectFileDir$
diff --git a/build.gradle.kts b/build.gradle.kts
index 9d0c400..bacd380 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,6 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val containerAlwaysOn: String by project
val disableLint: String by project
+val projectName: String by project
plugins {
jacoco
@@ -75,6 +76,8 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.7.20")
testImplementation("org.amshove.kluent:kluent:1.68")
+ testImplementation("io.kotest:kotest-runner-junit5:5.5.5")
+ testImplementation("io.kotest:kotest-property:5.5.5")
}
val sourcesJar by tasks.creating(Jar::class) {
@@ -84,7 +87,7 @@ val sourcesJar by tasks.creating(Jar::class) {
apply(plugin = "docker-compose")
dockerCompose {
- setProjectName("postgres-json")
+ setProjectName(projectName.toString())
setProperty("useComposeFiles", listOf("docker-compose.yml"))
setProperty("stopContainers", !containerAlwaysOn.toBoolean())
isRequiredBy(project.tasks.test)
@@ -93,7 +96,7 @@ dockerCompose {
publishing {
repositories {
maven {
- name = "postgres-json"
+ name = projectName.toString()
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
credentials {
username = System.getenv("GITHUB_USERNAME")
@@ -103,7 +106,7 @@ publishing {
}
publications {
- create("postgres-json") {
+ create(projectName.toString()) {
from(components["java"])
artifact(sourcesJar)
}
diff --git a/docker-compose.yml b/docker-compose.yml
index e36c83c..31ff62c 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -8,7 +8,7 @@ services:
context: docker/postgresql
restart: always
ports:
- - "5555:5432"
+ - "35555:5432"
environment:
POSTGRES_DB: json_test
POSTGRES_USER: test
diff --git a/docker/postgresql/Dockerfile b/docker/postgresql/Dockerfile
index feacb49..a0282a0 100644
--- a/docker/postgresql/Dockerfile
+++ b/docker/postgresql/Dockerfile
@@ -1,4 +1,4 @@
-FROM postgres:13
+FROM postgres:15
COPY postgresql.conf /tmp/postgresql.conf
COPY extension.sh /docker-entrypoint-initdb.d/000-extension.sh
diff --git a/docs/usage/multi-level.md b/docs/usage/multi-level.md
index 197d867..1360cb8 100644
--- a/docs/usage/multi-level.md
+++ b/docs/usage/multi-level.md
@@ -62,7 +62,7 @@ import fr.postgresjson.connexion.Requester
val requester: Requester = TODO()
val result: Parent = requester
.getFunction("find_parent_by_id")
- .selectOne("id" to "379e0687-9e4a-4781-b0e9-d94a62e4261f")
+ .execute("id" to "379e0687-9e4a-4781-b0e9-d94a62e4261f")
```
The requester create dynamically this request
diff --git a/docs/usage/paginated.md b/docs/usage/paginated.md
deleted file mode 100644
index f2df3c2..0000000
--- a/docs/usage/paginated.md
+++ /dev/null
@@ -1,19 +0,0 @@
-Paginated request
-=================
-
-```kotlin
-import fr.postgresjson.connexion.Paginated
-import fr.postgresjson.connexion.Requester
-import java.util.UUID
-
-class Article(val id: UUID, val name: String)
-
-val request: Requester = TODO()
-val article: Paginated = requester
- .getFunction("find_articles")
- .select(
- page = 1,
- limit = 10,
- "id" to "4a04820e-f880-4d80-b1c9-aeacccb24977"
- )
-```
\ No newline at end of file
diff --git a/docs/usage/raw-request.md b/docs/usage/raw-request.md
index 4c04946..5c1b2f4 100644
--- a/docs/usage/raw-request.md
+++ b/docs/usage/raw-request.md
@@ -26,10 +26,10 @@ data class Inventor(
val id: UUID = UUID.randomUUID(),
val name: String,
val roles: List = listOf(),
-): Serializable
+)
// Select one entity
-val result: Inventor? = connection.selectOne(
+val result: Inventor = connection.execute(
"""
SELECT json_build_object(
'id', '9e65de49-712e-47ce-8bf2-dfffae53a82e',
@@ -40,7 +40,7 @@ val result: Inventor? = connection.selectOne(
)
// Select multiple entities
-val result = connection.select>(
+val result = connection.execute>(
"""
SELECT json_build_array(
json_build_object(
@@ -60,7 +60,7 @@ val result = connection.select>(
)
// Select multiple with real query
-val result: List = connection.select(
+val result: List = connection.execute(
"""
select json_agg(i)
from inventor i
@@ -71,7 +71,7 @@ val result: List = connection.select(
// Select multiple with only some rows
-val result: List = connection.select(
+val result: List = connection.execute(
"""
select json_agg(i)
from (
diff --git a/docs/usage/stored-procedure.md b/docs/usage/stored-procedure.md
index 02a567e..f73d7e8 100644
--- a/docs/usage/stored-procedure.md
+++ b/docs/usage/stored-procedure.md
@@ -22,7 +22,6 @@ val requester = Requester(
```kotlin
import java.util.UUID
import org.joda.time.DateTime
-import fr.postgresjson.entity.Serializable
enum class Roles { ROLE_USER, ROLE_ADMIN }
@@ -31,15 +30,16 @@ class User(
override var username: String,
var blockedAt: DateTime? = null,
var roles: List = emptyList()
-): Serializable
+)
+@SqlSerializable
class UserForCreate(
id: UUID = UUID.randomUUID(),
username: String,
val password: String,
blockedAt: DateTime? = null,
roles: List = emptyList()
-): Serializable
+)
```
3. and, define Repositories
[See SQL function](../migrations/migrations.md)
@@ -53,19 +53,14 @@ class UserRepository(override var requester: Requester): RepositoryI {
fun findById(id: UUID): User {
return requester
.getFunction("find_user_by_id") // Use the name of the function
- .selectOne(
- "id" to id // You can pass parameters by their names. The underscore prefix on parameters is not required to be mapped.
- ) ?: throw UserNotFound(id) // Throw exception if user not found
+ .execute("id" to id) // You can pass parameters by their names. The underscore prefix on parameters is not required to be mapped.
+ // Throw exception if user not found
}
fun insert(user: UserForCreate): User {
return requester
.getFunction("insert_user")
- .selectOne("resource" to user)
- }
-
- class UserNotFound(override val message: String?, override val cause: Throwable?): Throwable(message, cause) {
- constructor(id: UUID): this("No User with ID $id", null)
+ .execute("resource" to user)
}
}
```
diff --git a/docs/usage/usage.md b/docs/usage/usage.md
index 8064140..06753a8 100644
--- a/docs/usage/usage.md
+++ b/docs/usage/usage.md
@@ -3,5 +3,4 @@
1. [Init connection](./init-connection.md)
2. [Raw request](./raw-request.md)
3. [Stored Procedure](./stored-procedure.md)
-4. [Paginated request](./paginated.md)
-5. [Multi level request](./multi-level.md)
\ No newline at end of file
+4. [Multi level request](./multi-level.md)
\ No newline at end of file
diff --git a/gradle.properties b/gradle.properties
index c9ef82b..2701bb5 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -7,4 +7,5 @@ systemProp.sonar.java.coveragePlugin=jacoco
systemProp.sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
org.gradle.jvmargs=-Xmx4096M
containerAlwaysOn=false
-disableLint=false
\ No newline at end of file
+disableLint=false
+projectName=postgres-json
\ No newline at end of file
diff --git a/src/.editorconfig b/src/.editorconfig
new file mode 100644
index 0000000..66fbfb1
--- /dev/null
+++ b/src/.editorconfig
@@ -0,0 +1,9 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_size = 4
+indent_style = space
+insert_final_newline = true
+tab_width = 4
diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt
index 1ffdfea..c14e893 100644
--- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt
+++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt
@@ -3,21 +3,16 @@ package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
import com.github.jasync.sql.db.ResultSet
-import com.github.jasync.sql.db.general.ArrayRowData
import com.github.jasync.sql.db.pool.ConnectionPool
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
import com.github.jasync.sql.db.util.length
-import fr.postgresjson.entity.EntityI
-import fr.postgresjson.entity.Serializable
import fr.postgresjson.serializer.Serializer
import fr.postgresjson.utils.LoggerDelegate
import org.slf4j.Logger
+import kotlin.jvm.Throws
import kotlin.random.Random
-
-typealias SelectOneCallback = QueryResult.(T?) -> Unit
-typealias SelectCallback = QueryResult.(List) -> Unit
-typealias SelectPaginatedCallback = QueryResult.(Paginated) -> Unit
+import kotlin.reflect.full.hasAnnotation
class Connection(
private val database: String,
@@ -25,18 +20,18 @@ class Connection(
private val password: String,
private val host: String = "localhost",
private val port: Int = 5432
-) : Executable {
- private var connection: ConnectionPool? = null
+) : ExecutableRaw {
+ private var connectionPool: ConnectionPool? = null
private val serializer = Serializer()
private val logger: Logger? by LoggerDelegate()
internal fun connect(): ConnectionPool {
- return connection.let { connectionPool ->
+ return connectionPool.let { connectionPool ->
if (connectionPool == null || !connectionPool.isConnected()) {
PostgreSQLConnectionBuilder.createConnectionPool(
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
).also {
- connection = it
+ this.connectionPool = it
}
} else {
connectionPool
@@ -45,7 +40,7 @@ class Connection(
}
fun disconnect() {
- connection?.disconnect()
+ connectionPool?.disconnect()
}
fun inTransaction(block: Connection.() -> A?): A? = connect().run {
@@ -59,16 +54,19 @@ class Connection(
}
/**
- * Select One [EntityI] with [List] of parameters
+ * Select with unnamed parameters
*/
- override fun selectOne(
+ @Throws(DataNotFoundException::class)
+ override fun execute(
sql: String,
typeReference: TypeReference,
values: List,
- block: (QueryResult, R?) -> Unit
+ block: SelectCallback
): R? {
- val result = exec(sql, compileArgs(values))
- val json = result.rows.firstOrNull()?.getString(0)
+ val result: QueryResult = exec(sql, compileArgs(values))
+ if (result.rows.size == 0) throw DataNotFoundException(sql)
+
+ val json: String? = result.rows.firstOrNull()?.getString(0)
return if (json === null) {
null
} else {
@@ -79,99 +77,16 @@ class Connection(
}
/**
- * Select One [EntityI] with named parameters
+ * Select with named parameters
*/
- override fun selectOne(
+ override fun execute(
sql: String,
typeReference: TypeReference,
values: Map,
- block: (QueryResult, R?) -> Unit
+ block: SelectCallback
): R? {
return replaceArgs(sql, values) {
- selectOne(this.sql, typeReference, parameters, block)
- }
- }
-
- /* Select Multiples */
-
- /**
- * Select multiple [EntityI] with [List] of parameters
- */
- override fun select(
- sql: String,
- typeReference: TypeReference>,
- values: List,
- block: QueryResult.(List) -> Unit
- ): List {
- val result = exec(sql, values)
- val json = result.rows[0].getString(0)
- return if (json === null) {
- emptyList()
- } else {
- serializer.deserializeList(json, typeReference)
- }.also {
- block(result, it)
- }
- }
-
- /**
- * Select multiple [EntityI] with [Map] of parameters
- */
- 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)
- }
- }
-
- /* Select Paginated */
-
- /**
- * Select Multiple [EntityI] with pagination
- */
- override fun select(
- sql: String,
- page: Int,
- limit: Int,
- typeReference: TypeReference>,
- values: Map,
- block: QueryResult.(Paginated) -> Unit
- ): Paginated {
- val offset = (page - 1) * limit
- val newValues = values
- .plus("offset" to offset)
- .plus("limit" to limit)
-
- val line = replaceArgs(sql, newValues) {
- exec(this.sql, this.parameters)
- }
-
- return line.run {
- val firstLine = rows.firstOrNull() ?: queryError("The query has no return", sql, newValues)
- if (!(firstLine as ArrayRowData).mapping.keys.contains("total")) queryError("""The query not return the "total" column""", sql, newValues, rows)
- val total = try {
- firstLine.getInt("total") ?: queryError("The query return \"total\" must not be null", sql, newValues, rows)
- } catch (e: ClassCastException) {
- queryError("""Column "total" must be an integer""", sql, newValues, rows)
- }
- val json = firstLine.getString(0)
- val entities = if (json == null) {
- emptyList()
- } else {
- serializer.deserializeList(json, typeReference)
- }
- Paginated(
- entities,
- offset,
- limit,
- total
- )
- }.also {
- block(line, it)
+ execute(this.sql, typeReference, parameters, block)
}
}
@@ -211,10 +126,12 @@ class Connection(
private fun compileArgs(values: List): List {
return values.map {
- if (it is Serializable || (it is List<*> && it.firstOrNull() is Serializable)) {
- serializer.serialize(it)
- } else {
- it
+ when {
+ it == null -> it
+ it is List<*> && it.isEmpty() -> it
+ it is List<*> && it.first()!!::class.hasAnnotation() -> serializer.serialize(it)
+ it::class.hasAnnotation() -> serializer.serialize(it)
+ else -> it
}
}
}
@@ -307,10 +224,11 @@ class Connection(
} catch (e: Throwable) {
logger?.info(
"""
- Query Error:
- ${sql.prependIndent()},
- ${values.joinToString(", ").prependIndent()}
- """.trimIndent(),
+ |Query Error:
+ |$sql,
+ |Arguments (${values.length}):
+ |${values.joinToString(", ").ifBlank { "No arguments" }.prependIndent()}
+ """.trimMargin().prependIndent(" > "),
e
)
throw e
diff --git a/src/main/kotlin/fr/postgresjson/connexion/DataNotFoundException.kt b/src/main/kotlin/fr/postgresjson/connexion/DataNotFoundException.kt
new file mode 100644
index 0000000..afd4847
--- /dev/null
+++ b/src/main/kotlin/fr/postgresjson/connexion/DataNotFoundException.kt
@@ -0,0 +1,6 @@
+package fr.postgresjson.connexion
+
+class DataNotFoundException(val queryExecuted: String) : Exception() {
+ override val message: String
+ get() = "No data return for the query"
+}
diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt
index d77677c..89e92c4 100644
--- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt
+++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt
@@ -2,107 +2,43 @@ package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
-import fr.postgresjson.entity.EntityI
+import kotlin.jvm.Throws
-sealed interface EmbedExecutable {
+sealed interface EmbedExecutable : Executable {
val connection: Connection
override fun toString(): String
val name: String
- /* Select One */
-
/**
- * Update [EntityI] with one entity as argument
+ * Select with unnamed parameters
*/
- fun update(
- typeReference: TypeReference,
- value: R,
- block: SelectOneCallback = {}
- ): R? =
- selectOne(typeReference, listOf(value), block)
-
- /**
- * Select One [EntityI] with [List] of parameters
- */
- fun selectOne(
+ @Throws(DataNotFoundException::class)
+ fun execute(
typeReference: TypeReference,
values: List,
- block: SelectOneCallback = {}
+ block: SelectCallback = {}
): R?
/**
- * Select One [EntityI] with [Map] of parameters
+ * Select with named parameters
*/
- fun selectOne(
+ @Throws(DataNotFoundException::class)
+ fun execute(
typeReference: TypeReference,
values: Map,
- block: SelectOneCallback = {}
+ block: SelectCallback = {}
): R?
/**
- * Select One [EntityI] with multiple [Pair] of parameters
+ * Select with named parameters
*/
- fun selectOne(
+ @Throws(DataNotFoundException::class)
+ fun execute(
typeReference: TypeReference,
vararg values: Pair,
- block: SelectOneCallback = {}
+ block: SelectCallback = {}
): R? =
- selectOne(typeReference, values.toMap(), block)
-
- /* Select Multiples */
-
- /**
- * Select Multiple [EntityI] with [List] of parameters
- */
- fun select(
- typeReference: TypeReference>,
- values: List,
- block: SelectCallback = {}
- ): List
-
- /**
- * Select Multiple [EntityI] with [Map] of parameters
- */
- fun select(
- typeReference: TypeReference>,
- values: Map,
- block: SelectCallback = {}
- ): List
-
- /**
- * Select Multiple [EntityI] with multiple [Pair] of parameters
- */
- fun select(
- typeReference: TypeReference>,
- vararg values: Pair,
- block: SelectCallback = {}
- ): List =
- select(typeReference, values.toMap(), block)
-
- /* Select Paginated */
-
- /**
- * Select Paginated [EntityI] with [Map] of parameters
- */
- fun select(
- page: Int,
- limit: Int,
- typeReference: TypeReference>,
- values: Map,
- block: SelectPaginatedCallback = {}
- ): Paginated
-
- /**
- * Select Paginated [EntityI] with multiple [Pair] of parameters
- */
- fun select(
- page: Int,
- limit: Int,
- typeReference: TypeReference>,
- vararg values: Pair,
- block: SelectPaginatedCallback = {}
- ): Paginated =
- select(page, limit, typeReference, values.toMap(), block)
+ execute(typeReference, values.toMap(), block)
fun exec(values: List): QueryResult
fun exec(values: Map): QueryResult
diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt
index 73e4df4..4df0e8b 100644
--- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt
+++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt
@@ -1,68 +1,25 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
-import fr.postgresjson.entity.EntityI
+import kotlin.jvm.Throws
-/* Select One */
-
-inline fun EmbedExecutable.update(
- value: R,
- noinline block: SelectOneCallback = {}
-): R? =
- update(object : TypeReference() {}, value, block)
-
-inline fun EmbedExecutable.selectOne(
- values: List,
- noinline block: SelectOneCallback = {}
-): R? =
- selectOne(object : TypeReference() {}, values, block)
-
-inline fun EmbedExecutable.selectOne(
- values: Map,
- noinline block: SelectOneCallback = {}
-): R? =
- selectOne(object : TypeReference() {}, values, block)
-
-inline fun EmbedExecutable.selectOne(
- vararg values: Pair,
- noinline block: SelectOneCallback = {}
-): R? =
- selectOne(object : TypeReference() {}, values = values, block)
-
-/* Select Multiples */
-
-inline fun EmbedExecutable.select(
+@Throws(DataNotFoundException::class)
+inline fun EmbedExecutable.execute(
values: List,
noinline block: SelectCallback = {}
-): List =
- select(object : TypeReference>() {}, values, block)
+): R? =
+ execute(object : TypeReference() {}, values, block)
-inline fun EmbedExecutable.select(
+@Throws(DataNotFoundException::class)
+inline fun EmbedExecutable.execute(
values: Map,
noinline block: SelectCallback = {}
-): List =
- select(object : TypeReference>() {}, values, block)
+): R? =
+ execute(object : TypeReference() {}, values, block)
-inline fun EmbedExecutable.select(
+@Throws(DataNotFoundException::class)
+inline fun EmbedExecutable.execute(
vararg values: Pair,
noinline block: SelectCallback = {}
-): List =
- select(object : TypeReference>() {}, values = values, block)
-
-/* Select Paginated */
-
-inline fun EmbedExecutable.select(
- page: Int,
- limit: Int,
- values: Map = emptyMap(),
- noinline block: SelectPaginatedCallback = {}
-): Paginated =
- select(page, limit, object : TypeReference>() {}, values, block)
-
-inline fun EmbedExecutable.select(
- page: Int,
- limit: Int,
- vararg values: Pair,
- noinline block: SelectPaginatedCallback = {}
-): Paginated =
- select(page, limit, object : TypeReference>() {}, values = values, block)
+): R? =
+ execute(object : TypeReference() {}, values = values, block)
diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt
index d4be1a5..80e0d81 100644
--- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt
+++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt
@@ -1,136 +1,3 @@
package fr.postgresjson.connexion
-import com.fasterxml.jackson.core.type.TypeReference
-import com.github.jasync.sql.db.QueryResult
-import fr.postgresjson.entity.EntityI
-
-interface Executable {
-
- /* Update */
-
- /**
- * Update [EntityI] with one entity as argument
- */
- fun update(
- sql: String,
- typeReference: TypeReference,
- value: R,
- block: SelectOneCallback = {}
- ): R? =
- selectOne(sql, typeReference, listOf(value), block)
-
- /* Select One */
-
- /**
- * Select One [EntityI] with [List] of parameters
- */
- fun selectOne(
- sql: String,
- typeReference: TypeReference,
- values: List,
- block: SelectOneCallback = {}
- ): R?
-
- /**
- * Select One [EntityI] with [Map] of parameters
- */
- fun selectOne(
- sql: String,
- typeReference: TypeReference,
- values: Map,
- block: SelectOneCallback = {}
- ): R?
-
- /**
- * Select One [EntityI] with multiple [Pair] of parameters
- */
- fun selectOne(
- sql: String,
- typeReference: TypeReference,
- vararg values: Pair,
- block: SelectOneCallback = {}
- ): R? =
- selectOne(sql, typeReference, values.toMap(), block)
-
- /* Select Multiples */
-
- /**
- * Select Multiple [EntityI] with [List] of parameters
- */
- fun select(
- sql: String,
- typeReference: TypeReference>,
- values: List = emptyList(),
- block: SelectCallback = {}
- ): List
-
- /**
- * Select Multiple [EntityI] with [Map] of parameters
- */
- fun select(
- sql: String,
- typeReference: TypeReference>,
- values: Map,
- block: SelectCallback = {}
- ): List
-
- /**
- * Select Multiple [EntityI] with multiple [Pair] of parameters
- */
- fun select(
- sql: String,
- typeReference: TypeReference>,
- vararg values: Pair,
- block: SelectCallback = {}
- ): List =
- select(sql, typeReference, values.toMap(), block)
-
- /* Select Paginated */
-
- /**
- * Select Paginated [EntityI] with [Map] of parameters
- */
- fun select(
- sql: String,
- page: Int,
- limit: Int,
- typeReference: TypeReference>,
- values: Map,
- block: SelectPaginatedCallback = {}
- ): Paginated
-
- /**
- * Select Paginated [EntityI] with multiple [Pair] of parameters
- */
- fun select(
- sql: String,
- page: Int,
- limit: Int,
- typeReference: TypeReference>,
- vararg values: Pair,
- block: SelectPaginatedCallback = {}
- ): Paginated =
- select(sql, page, limit, typeReference, values.toMap(), block)
-
- fun exec(sql: String, value: R): QueryResult = exec(sql, listOf(value))
- fun exec(sql: String, values: List): QueryResult
- fun exec(sql: String, values: Map): QueryResult
- fun exec(sql: String, vararg values: Pair): QueryResult = exec(sql, values.toMap())
-
- /**
- * Warning: this method not use prepared statement
- */
- fun sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
- /**
- * Warning: this method not use prepared statement
- */
- fun sendQuery(sql: String, values: List): QueryResult
- /**
- * Warning: this method not use prepared statement
- */
- fun sendQuery(sql: String, values: Map): QueryResult
- /**
- * Warning: this method not use prepared statement
- */
- fun sendQuery(sql: String, vararg values: Pair): QueryResult = sendQuery(sql, values.toMap())
-}
+sealed interface Executable
diff --git a/src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt b/src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
new file mode 100644
index 0000000..59249a1
--- /dev/null
+++ b/src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
@@ -0,0 +1,64 @@
+package fr.postgresjson.connexion
+
+import com.fasterxml.jackson.core.type.TypeReference
+import com.github.jasync.sql.db.QueryResult
+import kotlin.jvm.Throws
+
+typealias SelectCallback = QueryResult.(R?) -> Unit
+
+sealed interface ExecutableRaw : Executable {
+ /**
+ * Select with [List] of parameters
+ */
+ @Throws(DataNotFoundException::class)
+ fun execute(
+ sql: String,
+ typeReference: TypeReference,
+ values: List = emptyList(),
+ block: SelectCallback = {}
+ ): R?
+
+ /**
+ * Select with [Map] of parameters
+ */
+ @Throws(DataNotFoundException::class)
+ fun execute(
+ sql: String,
+ typeReference: TypeReference,
+ values: Map,
+ block: SelectCallback = {}
+ ): R?
+
+ /**
+ * Select with multiple [Pair] of parameters
+ */
+ @Throws(DataNotFoundException::class)
+ fun execute(
+ sql: String,
+ typeReference: TypeReference,
+ vararg values: Pair,
+ block: SelectCallback = {}
+ ): R? = execute(sql, typeReference, values.toMap(), block)
+
+ fun exec(sql: String, value: R): QueryResult = exec(sql, listOf(value))
+ fun exec(sql: String, values: List): QueryResult
+ fun exec(sql: String, values: Map): QueryResult
+ fun exec(sql: String, vararg values: Pair): QueryResult = exec(sql, values.toMap())
+
+ /**
+ * Warning: this method not use prepared statement
+ */
+ fun sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
+ /**
+ * Warning: this method not use prepared statement
+ */
+ fun sendQuery(sql: String, values: List): QueryResult
+ /**
+ * Warning: this method not use prepared statement
+ */
+ fun sendQuery(sql: String, values: Map): QueryResult
+ /**
+ * Warning: this method not use prepared statement
+ */
+ fun sendQuery(sql: String, vararg values: Pair): QueryResult = sendQuery(sql, values.toMap())
+}
diff --git a/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt b/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt
index 25686e1..0f5cb45 100644
--- a/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt
+++ b/src/main/kotlin/fr/postgresjson/connexion/ExecutableReified.kt
@@ -1,106 +1,37 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
-import fr.postgresjson.entity.EntityI
-
-/* Update */
+import kotlin.jvm.Throws
/**
- * Update [EntityI] with one entity as argument
+ * Select with unnamed parameters
*/
-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(
+@Throws(DataNotFoundException::class)
+inline fun ExecutableRaw.execute(
sql: String,
values: List = emptyList(),
noinline block: SelectCallback = {}
-): List =
- select(sql, object : TypeReference>() {}, values, block)
+): R? =
+ execute(sql, object : TypeReference() {}, values, block)
/**
- * Select Multiple [EntityI] with [Map] of parameters
+ * Select with named parameters
*/
-inline fun Executable.select(
+@Throws(DataNotFoundException::class)
+inline fun ExecutableRaw.execute(
sql: String,
values: Map,
noinline block: SelectCallback = {}
-): List =
- select(sql, object : TypeReference>() {}, values, block)
+): R? =
+ execute(sql, object : TypeReference() {}, values, block)
/**
- * Select Multiple [EntityI] with multiple [Pair] of parameters
+ * Select with named parameters
*/
-inline fun Executable.select(
+@Throws(DataNotFoundException::class)
+inline fun ExecutableRaw.execute(
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