Compare commits
3 Commits
compiled_s
...
jdbc
| Author | SHA1 | Date | |
|---|---|---|---|
| d58f367843 | |||
| 81aebd815d | |||
| aeabe3f8c1 |
@@ -18,8 +18,8 @@ dependencies {
|
|||||||
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.31")
|
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.31")
|
||||||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9")
|
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9")
|
||||||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.9")
|
implementation("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.9")
|
||||||
implementation("com.github.jasync-sql:jasync-postgresql:0.9.53")
|
|
||||||
implementation("org.slf4j:slf4j-api:1.7.26")
|
implementation("org.slf4j:slf4j-api:1.7.26")
|
||||||
|
implementation("org.postgresql:postgresql:42.2.6")
|
||||||
|
|
||||||
testImplementation("ch.qos.logback:logback-classic:1.2.3")
|
testImplementation("ch.qos.logback:logback-classic:1.2.3")
|
||||||
testImplementation("ch.qos.logback:logback-core:1.2.3")
|
testImplementation("ch.qos.logback:logback-core:1.2.3")
|
||||||
@@ -32,7 +32,7 @@ publishing {
|
|||||||
publications {
|
publications {
|
||||||
create<MavenPublication>("maven") {
|
create<MavenPublication>("maven") {
|
||||||
groupId = "fr.postgresjson"
|
groupId = "fr.postgresjson"
|
||||||
artifactId = "postgresjson"
|
artifactId = "postgresjson-jdbc"
|
||||||
version = "0.1"
|
version = "0.1"
|
||||||
|
|
||||||
from(components["java"])
|
from(components["java"])
|
||||||
|
|||||||
@@ -1,20 +1,17 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.Connection
|
|
||||||
import com.github.jasync.sql.db.QueryResult
|
|
||||||
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 fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
import fr.postgresjson.utils.LoggerDelegate
|
import fr.postgresjson.utils.LoggerDelegate
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import java.util.concurrent.CompletableFuture
|
import java.sql.DriverManager
|
||||||
|
import java.sql.ResultSet
|
||||||
|
import java.sql.Connection as JDBCConnection
|
||||||
|
|
||||||
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
typealias SelectOneCallback<T> = ResultSet.(T?) -> Unit
|
||||||
typealias SelectCallback<T> = QueryResult.(List<T>) -> Unit
|
typealias SelectCallback<T> = ResultSet.(List<T>) -> Unit
|
||||||
typealias SelectPaginatedCallback<T> = QueryResult.(Paginated<T>) -> Unit
|
typealias SelectPaginatedCallback<T> = ResultSet.(Paginated<T>) -> Unit
|
||||||
|
|
||||||
class Connection(
|
class Connection(
|
||||||
private val database: String,
|
private val database: String,
|
||||||
@@ -23,32 +20,34 @@ class Connection(
|
|||||||
private val host: String = "localhost",
|
private val host: String = "localhost",
|
||||||
private val port: Int = 5432
|
private val port: Int = 5432
|
||||||
): Executable {
|
): Executable {
|
||||||
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
private lateinit var connection: JDBCConnection
|
||||||
private val serializer = Serializer()
|
private val serializer = Serializer()
|
||||||
private val logger: Logger? by LoggerDelegate()
|
private val logger: Logger? by LoggerDelegate()
|
||||||
|
|
||||||
internal fun connect(): ConnectionPool<PostgreSQLConnection> {
|
internal fun connect(): JDBCConnection {
|
||||||
if (!::connection.isInitialized || !connection.isConnected()) {
|
if (!::connection.isInitialized || connection.isClosed) {
|
||||||
connection = PostgreSQLConnectionBuilder.createConnectionPool(
|
connection = DriverManager.getConnection("jdbc:postgresql://$host:$port/$database", username, password)
|
||||||
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return connection
|
return connection
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
fun <T> inTransaction(f: (Connection) -> T) {
|
||||||
|
sendQuery("BEGIN")
|
||||||
|
f(this)
|
||||||
|
sendQuery("COMMIT")
|
||||||
|
}
|
||||||
|
|
||||||
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: (ResultSet, 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.getString(1)
|
||||||
return if (json === null) {
|
return if (json === null) {
|
||||||
null
|
null
|
||||||
} else {
|
} else {
|
||||||
@@ -73,7 +72,7 @@ class Connection(
|
|||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<R>,
|
typeReference: TypeReference<R>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, R?) -> Unit
|
block: (ResultSet, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
return replaceArgs(sql, values) {
|
return replaceArgs(sql, values) {
|
||||||
select(this.sql, typeReference, this.parameters, block)
|
select(this.sql, typeReference, this.parameters, block)
|
||||||
@@ -91,10 +90,10 @@ class Connection(
|
|||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: List<Any?>,
|
values: List<Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
val result = exec(sql, compileArgs(values))
|
val result = exec(sql, compileArgs(values))
|
||||||
val json = result.rows[0].getString(0)
|
val json = result.getString(1)
|
||||||
return if (json === null) {
|
return if (json === null) {
|
||||||
listOf<EntityI<*>>() as List<R>
|
listOf<EntityI<*>>() as List<R>
|
||||||
} else {
|
} else {
|
||||||
@@ -117,7 +116,7 @@ class Connection(
|
|||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, Paginated<R>) -> Unit
|
block: (ResultSet, Paginated<R>) -> Unit
|
||||||
): Paginated<R> {
|
): Paginated<R> {
|
||||||
val offset = (page - 1) * limit
|
val offset = (page - 1) * limit
|
||||||
val newValues = values
|
val newValues = values
|
||||||
@@ -125,11 +124,11 @@ class Connection(
|
|||||||
.plus("limit" to limit)
|
.plus("limit" to limit)
|
||||||
|
|
||||||
val line = replaceArgs(sql, newValues) {
|
val line = replaceArgs(sql, newValues) {
|
||||||
exec(this.sql, compileArgs(this.parameters))
|
exec(this.sql, this.parameters)
|
||||||
}
|
}
|
||||||
|
|
||||||
return line.run {
|
return line.run {
|
||||||
val json = rows[0].getString(0)
|
val json = getString(1)
|
||||||
val entities = if (json === null) {
|
val entities = if (json === null) {
|
||||||
listOf<EntityI<*>>() as List<R>
|
listOf<EntityI<*>>() as List<R>
|
||||||
} else {
|
} else {
|
||||||
@@ -139,7 +138,7 @@ class Connection(
|
|||||||
entities,
|
entities,
|
||||||
offset,
|
offset,
|
||||||
limit,
|
limit,
|
||||||
rows[0].getInt("total") ?: error("The query not return total")
|
getInt("total")
|
||||||
)
|
)
|
||||||
}.also {
|
}.also {
|
||||||
block(line, it)
|
block(line, it)
|
||||||
@@ -159,7 +158,7 @@ class Connection(
|
|||||||
sql: String,
|
sql: String,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, List<R>) -> Unit
|
block: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
return replaceArgs(sql, values) {
|
return replaceArgs(sql, values) {
|
||||||
select(this.sql, typeReference, this.parameters, block)
|
select(this.sql, typeReference, this.parameters, block)
|
||||||
@@ -173,21 +172,43 @@ class Connection(
|
|||||||
): List<R> =
|
): List<R> =
|
||||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||||
|
|
||||||
override fun exec(sql: String, values: List<Any?>): QueryResult {
|
override fun exec(sql: String, values: List<Any?>): ResultSet {
|
||||||
return stopwatchQuery(sql, values) {
|
return stopwatchQuery(sql, values) {
|
||||||
connect().sendPreparedStatement(sql, compileArgs(values)).join()
|
connect().prepareStatement(sql).apply {
|
||||||
|
compileArgs(values).forEachIndexed { i, v ->
|
||||||
|
when (v) {
|
||||||
|
is String -> setString(i+1, v)
|
||||||
|
is Int -> setInt(i+1, v)
|
||||||
|
else -> setString(i+1, v.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.executeQuery().apply { next() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun exec(sql: String, values: Map<String, Any?>): QueryResult {
|
override fun exec(sql: String, values: Map<String, Any?>): ResultSet {
|
||||||
return replaceArgs(sql, values) {
|
return replaceArgs(sql, values) {
|
||||||
exec(this.sql, this.parameters)
|
exec(this.sql, this.parameters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sendQuery(sql: String): QueryResult {
|
override fun sendQuery(sql: String, values: List<Any?>): Int {
|
||||||
return stopwatchQuery(sql) {
|
return stopwatchQuery(sql, values) {
|
||||||
connect().sendQuery(sql).join()
|
connect().prepareStatement(sql).apply {
|
||||||
|
compileArgs(values).forEachIndexed { i, v ->
|
||||||
|
when (v) {
|
||||||
|
is String -> setString(i+1, v)
|
||||||
|
is Int -> setInt(i+1, v)
|
||||||
|
else -> setString(i+1, v.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.executeUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun sendQuery(sql: String, values: Map<String, Any?>): Int {
|
||||||
|
return replaceArgs(sql, values) {
|
||||||
|
sendQuery(this.sql, this.parameters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.QueryResult
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import java.sql.ResultSet
|
||||||
|
|
||||||
interface EmbedExecutable {
|
interface EmbedExecutable {
|
||||||
val connection: Connection
|
val connection: Connection
|
||||||
@@ -47,6 +47,8 @@ interface EmbedExecutable {
|
|||||||
block: SelectPaginatedCallback<R> = {}
|
block: SelectPaginatedCallback<R> = {}
|
||||||
): Paginated<R>
|
): Paginated<R>
|
||||||
|
|
||||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
fun exec(values: List<Any?> = emptyList()): ResultSet
|
||||||
fun exec(values: Map<String, Any?>): QueryResult
|
fun exec(values: Map<String, Any?>): ResultSet
|
||||||
|
fun sendQuery(values: List<Any?> = emptyList()): Int
|
||||||
|
fun sendQuery(values: Map<String, Any?>): Int
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.QueryResult
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import java.sql.ResultSet
|
||||||
|
|
||||||
interface Executable {
|
interface Executable {
|
||||||
/* Select One */
|
/* Select One */
|
||||||
@@ -48,7 +48,8 @@ interface Executable {
|
|||||||
block: SelectPaginatedCallback<R> = {}
|
block: SelectPaginatedCallback<R> = {}
|
||||||
): Paginated<R>
|
): Paginated<R>
|
||||||
|
|
||||||
fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult
|
fun exec(sql: String, values: List<Any?> = emptyList()): ResultSet
|
||||||
fun exec(sql: String, values: Map<String, Any?>): QueryResult
|
fun exec(sql: String, values: Map<String, Any?>): ResultSet
|
||||||
fun sendQuery(sql: String): QueryResult
|
fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
|
||||||
|
fun sendQuery(sql: String, values: Map<String, Any?>): Int
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.QueryResult
|
|
||||||
import fr.postgresjson.definition.Function
|
import fr.postgresjson.definition.Function
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import java.sql.ResultSet
|
||||||
|
|
||||||
class Function(val definition: Function, override val connection: Connection): EmbedExecutable {
|
class Function(val definition: Function, override val connection: Connection): EmbedExecutable {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
@@ -20,7 +20,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
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: (ResultSet, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
@@ -46,7 +46,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
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: (ResultSet, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
@@ -74,7 +74,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
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: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
@@ -94,7 +94,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
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: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
@@ -124,7 +124,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, Paginated<R>) -> Unit
|
block: (ResultSet, Paginated<R>) -> Unit
|
||||||
): Paginated<R> {
|
): Paginated<R> {
|
||||||
val offset = (page - 1) * limit
|
val offset = (page - 1) * limit
|
||||||
val newValues = values
|
val newValues = values
|
||||||
@@ -155,26 +155,36 @@ class Function(val definition: Function, override val connection: Connection): E
|
|||||||
|
|
||||||
/* Execute function without traitements */
|
/* Execute function without traitements */
|
||||||
|
|
||||||
override fun exec(values: List<Any?>): QueryResult {
|
override fun exec(values: List<Any?>): ResultSet {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.exec(sql, values)
|
return connection.exec(sql, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun exec(values: Map<String, Any?>): QueryResult {
|
override fun exec(values: Map<String, Any?>): ResultSet {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||||
|
|
||||||
return connection.exec(sql, values)
|
return connection.exec(sql, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun sendQuery(values: List<Any?>): Int {
|
||||||
|
exec(values)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun sendQuery(values: Map<String, Any?>): Int {
|
||||||
|
exec(values)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
private fun compileArgs(values: List<Any?>): String {
|
private fun compileArgs(values: List<Any?>): String {
|
||||||
val placeholders = values
|
val placeholders = values
|
||||||
.filterIndexed { index, any ->
|
.filterIndexed { index, value ->
|
||||||
definition.parameters[index].default === null || any !== null
|
definition.parameters[index].default === null || value != null
|
||||||
}
|
}
|
||||||
.mapIndexed { index, any ->
|
.mapIndexed { index, _ ->
|
||||||
"?::" + definition.parameters[index].type
|
"?::" + definition.parameters[index].type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.github.jasync.sql.db.util.length
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
|
||||||
data class Paginated<T: EntityI<*>>(
|
data class Paginated<T: EntityI<*>>(
|
||||||
@@ -10,7 +9,7 @@ data class Paginated<T: EntityI<*>>(
|
|||||||
val total: Int
|
val total: Int
|
||||||
) {
|
) {
|
||||||
val currentPage: Int = (offset / limit) + 1
|
val currentPage: Int = (offset / limit) + 1
|
||||||
val count: Int = result.length
|
val count: Int = result.size
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (offset < 0) error("offset must be greather or equal than 0")
|
if (offset < 0) error("offset must be greather or equal than 0")
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.QueryResult
|
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import java.sql.ResultSet
|
||||||
|
|
||||||
|
|
||||||
class Query(override val name: String, private val sql: String, override val connection: Connection): EmbedExecutable {
|
class Query(override val name: String, private val sql: String, override val connection: Connection): EmbedExecutable {
|
||||||
@@ -15,7 +15,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
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: (ResultSet, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
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: (ResultSet, R?) -> Unit
|
||||||
): R? {
|
): R? {
|
||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
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: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
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: (ResultSet, List<R>) -> Unit
|
||||||
): List<R> {
|
): List<R> {
|
||||||
return connection.select(this.toString(), typeReference, values, block)
|
return connection.select(this.toString(), typeReference, values, block)
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
limit: Int,
|
limit: Int,
|
||||||
typeReference: TypeReference<List<R>>,
|
typeReference: TypeReference<List<R>>,
|
||||||
values: Map<String, Any?>,
|
values: Map<String, Any?>,
|
||||||
block: (QueryResult, Paginated<R>) -> Unit
|
block: (ResultSet, Paginated<R>) -> Unit
|
||||||
): Paginated<R> {
|
): Paginated<R> {
|
||||||
return connection.select(this.toString(), page, limit, typeReference, values, block)
|
return connection.select(this.toString(), page, limit, typeReference, values, block)
|
||||||
}
|
}
|
||||||
@@ -92,11 +92,19 @@ class Query(override val name: String, private val sql: String, override val con
|
|||||||
|
|
||||||
/* Execute function without traitements */
|
/* Execute function without traitements */
|
||||||
|
|
||||||
override fun exec(values: List<Any?>): QueryResult {
|
override fun exec(values: List<Any?>): ResultSet {
|
||||||
return connection.exec(sql, values)
|
return connection.exec(sql, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun exec(values: Map<String, Any?>): QueryResult {
|
override fun exec(values: Map<String, Any?>): ResultSet {
|
||||||
return connection.exec(sql, values)
|
return connection.exec(sql, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun sendQuery(values: List<Any?>): Int {
|
||||||
|
return connection.sendQuery(sql, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun sendQuery(values: Map<String, Any?>): Int {
|
||||||
|
return connection.sendQuery(sql, values)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ data class Function(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
if (up.name != down.name) {
|
if (up.name != down.name) {
|
||||||
throw Exception("UP and DOWN migration must have the same name [${up.name} !== ${down.name}]")
|
throw Exception("UP and DOWN migration must have the same name [${up.name} != ${down.name}]")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ data class Function(
|
|||||||
connection.sendQuery(down.script)
|
connection.sendQuery(down.script)
|
||||||
|
|
||||||
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
|
||||||
connection.exec(it, listOf(down))
|
connection.sendQuery(it, listOf(down))
|
||||||
}
|
}
|
||||||
return Status.OK
|
return Status.OK
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ data class Function(
|
|||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
it.sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Status.OK // TODO
|
return Status.OK // TODO
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ data class Function(
|
|||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
it.sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Status.OK // TODO
|
return Status.OK // TODO
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package fr.postgresjson.migration
|
package fr.postgresjson.migration
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.util.size
|
|
||||||
import fr.postgresjson.connexion.Connection
|
import fr.postgresjson.connexion.Connection
|
||||||
import fr.postgresjson.definition.Function.FunctionNotFound
|
import fr.postgresjson.definition.Function.FunctionNotFound
|
||||||
import fr.postgresjson.entity.Entity
|
import fr.postgresjson.entity.Entity
|
||||||
@@ -97,11 +96,11 @@ data class Migrations private constructor(
|
|||||||
it.isFile
|
it.isFile
|
||||||
}.forEach { file ->
|
}.forEach { file ->
|
||||||
if (file.name.endsWith(".up.sql")) {
|
if (file.name.endsWith(".up.sql")) {
|
||||||
file.path.substring(0, file.path.size - 7).let {
|
file.path.substring(0, file.path.length - 7).let {
|
||||||
try {
|
try {
|
||||||
val down = File("$it.down.sql").readText()
|
val down = File("$it.down.sql").readText()
|
||||||
val up = file.readText()
|
val up = file.readText()
|
||||||
val name = file.name.substring(0, file.name.size - 7)
|
val name = file.name.substring(0, file.name.length - 7)
|
||||||
addQuery(name, up, down)
|
addQuery(name, up, down)
|
||||||
} catch (e: FileNotFoundException) {
|
} catch (e: FileNotFoundException) {
|
||||||
throw DownMigrationNotDefined("$it.down.sql", e)
|
throw DownMigrationNotDefined("$it.down.sql", e)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ data class Query(
|
|||||||
connection.sendQuery(down)
|
connection.sendQuery(down)
|
||||||
|
|
||||||
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
||||||
connection.exec(it, listOf(name))
|
connection.sendQuery(it, listOf(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Migration.Status.OK
|
return Migration.Status.OK
|
||||||
@@ -42,7 +42,7 @@ data class Query(
|
|||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
it.sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Migration.Status.OK // TODO
|
return Migration.Status.OK // TODO
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ data class Query(
|
|||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
it.sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Migration.Status.OK // TODO
|
return Migration.Status.OK // TODO
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package fr.postgresjson
|
package fr.postgresjson
|
||||||
|
|
||||||
import fr.postgresjson.connexion.Connection
|
|
||||||
import fr.postgresjson.connexion.Paginated
|
import fr.postgresjson.connexion.Paginated
|
||||||
import fr.postgresjson.entity.IdEntity
|
import fr.postgresjson.entity.IdEntity
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.*
|
||||||
import org.junit.jupiter.api.Assertions
|
import org.junit.jupiter.api.Assertions
|
||||||
import org.junit.jupiter.api.BeforeEach
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
|
||||||
@@ -15,13 +13,6 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
||||||
private class ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
|
private class ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
|
||||||
|
|
||||||
private lateinit var connection: Connection
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
fun before() {
|
|
||||||
connection = getConnextion()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getObject() {
|
fun getObject() {
|
||||||
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||||
@@ -72,7 +63,7 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
fun callExec() {
|
fun callExec() {
|
||||||
val o = ObjTest("myName")
|
val o = ObjTest("myName")
|
||||||
val result = connection.exec("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
|
val result = connection.exec("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
|
||||||
Assertions.assertEquals(1, result.rowsAffected)
|
Assertions.assertNotNull(result.getString(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -166,7 +157,7 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
params
|
params
|
||||||
) {
|
) {
|
||||||
assertEquals("ff", it!!.first)
|
assertEquals("ff", it!!.first)
|
||||||
assertEquals("plop", rows[0].getString("other"))
|
assertEquals("plop", getString("other"))
|
||||||
}
|
}
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result!!.first)
|
assertEquals("ff", result!!.first)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run up query`() {
|
fun `run up query`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||||
val m = Migrations(resources, getConnextion())
|
val m = Migrations(resources, connection)
|
||||||
m.up().apply {
|
m.up().apply {
|
||||||
this `should contain` Pair("1", Migration.Status.OK)
|
this `should contain` Pair("1", Migration.Status.OK)
|
||||||
size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
@@ -30,14 +30,14 @@ class MigrationTest(): TestAbstract() {
|
|||||||
fun `migration up Query should throw error if no down`() {
|
fun `migration up Query should throw error if no down`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
|
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
|
||||||
invoking {
|
invoking {
|
||||||
Migrations(resources, getConnextion())
|
Migrations(resources, connection)
|
||||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `run forced down query`() {
|
fun `run forced down query`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||||
val m = Migrations(resources, getConnextion())
|
val m = Migrations(resources, connection)
|
||||||
repeat(3) {
|
repeat(3) {
|
||||||
m.down(true).apply {
|
m.down(true).apply {
|
||||||
this `should contain` Pair("1", Migration.Status.OK)
|
this `should contain` Pair("1", Migration.Status.OK)
|
||||||
@@ -49,10 +49,10 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run dry migrations`() {
|
fun `run dry migrations`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, connection).apply {
|
||||||
runDry().size `should be equal to` 2
|
runDry().size `should be equal to` 2
|
||||||
}
|
}
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, connection).apply {
|
||||||
runDry().size `should be equal to` 2
|
runDry().size `should be equal to` 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run dry migrations launch twice`() {
|
fun `run dry migrations launch twice`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, connection).apply {
|
||||||
runDry().size `should be equal to` 2
|
runDry().size `should be equal to` 2
|
||||||
runDry().size `should be equal to` 2
|
runDry().size `should be equal to` 2
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run migrations`() {
|
fun `run migrations`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, connection).apply {
|
||||||
run().apply {
|
run().apply {
|
||||||
size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
}
|
}
|
||||||
@@ -79,14 +79,15 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run migrations force down`() {
|
fun `run migrations force down`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
Migrations(resources, getConnextion()).apply {
|
val resourcesFunctions = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
|
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||||
up().apply {
|
up().apply {
|
||||||
size `should be equal to` 1
|
size `should be equal to` 6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||||
forceAllDown().apply {
|
forceAllDown().apply {
|
||||||
size `should be equal to` 1
|
size `should be equal to` 6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,11 +95,11 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run functions migrations`() {
|
fun `run functions migrations`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, connection).apply {
|
||||||
run().size `should be equal to` 4
|
run().size `should be equal to` 5
|
||||||
}
|
}
|
||||||
|
|
||||||
val objTest: RequesterTest.ObjTest? = Requester(getConnextion())
|
val objTest: RequesterTest.ObjTest? = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(listOf("test", "plip"))
|
.selectOne(listOf("test", "plip"))
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import fr.postgresjson.connexion.Requester
|
|||||||
import fr.postgresjson.entity.IdEntity
|
import fr.postgresjson.entity.IdEntity
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `get query from file`() {
|
fun `get query from file`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
val objTest: ObjTest? = Requester(getConnextion())
|
val objTest: ObjTest? = Requester(connection)
|
||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectOne")
|
.getQuery("Test/selectOne")
|
||||||
.selectOne()
|
.selectOne()
|
||||||
@@ -26,7 +27,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `get function from file`() {
|
fun `get function from file`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val objTest: ObjTest? = Requester(getConnextion())
|
val objTest: ObjTest? = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(listOf("test", "plip"))
|
.selectOne(listOf("test", "plip"))
|
||||||
@@ -38,29 +39,51 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call exec on query`() {
|
fun `call exec on query`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
val result = Requester(getConnextion())
|
val result = Requester(connection)
|
||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectOne")
|
.getQuery("Test/selectOne")
|
||||||
.exec()
|
.exec()
|
||||||
|
|
||||||
assertEquals(1, result.rowsAffected)
|
assertNotNull(result.getString(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `call exec on function`() {
|
fun `call exec on function`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val result = Requester(getConnextion())
|
val result = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.exec(listOf("test", "plip"))
|
.exec(listOf("test", "plip"))
|
||||||
|
|
||||||
assertEquals(1, result.rowsAffected)
|
assertNotNull(result.getString(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `call sendQuery on query`() {
|
||||||
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
|
val result = Requester(connection)
|
||||||
|
.addQuery(resources)
|
||||||
|
.getQuery("Test/exec")
|
||||||
|
.sendQuery()
|
||||||
|
|
||||||
|
assertEquals(0, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `call sendQuery on function`() {
|
||||||
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
|
val result = Requester(connection)
|
||||||
|
.addFunction(resources)
|
||||||
|
.getFunction("function_void")
|
||||||
|
.sendQuery(listOf("test"))
|
||||||
|
|
||||||
|
assertEquals(0, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `call selectOne on function`() {
|
fun `call selectOne on function`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val obj: ObjTest = Requester(getConnextion())
|
val obj: ObjTest = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(mapOf("name" to "myName"))!!
|
.selectOne(mapOf("name" to "myName"))!!
|
||||||
@@ -72,7 +95,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
fun `call selectOne on function with object`() {
|
fun `call selectOne on function with object`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val obj2 = ObjTest("original")
|
val obj2 = ObjTest("original")
|
||||||
val obj: ObjTest = Requester(getConnextion())
|
val obj: ObjTest = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function_object")
|
.getFunction("test_function_object")
|
||||||
.selectOne("resource" to obj2)!!
|
.selectOne("resource" to obj2)!!
|
||||||
@@ -84,7 +107,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call selectOne on query`() {
|
fun `call selectOne on query`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
val obj: ObjTest = Requester(getConnextion())
|
val obj: ObjTest = Requester(connection)
|
||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectOneWithParameters")
|
.getQuery("Test/selectOneWithParameters")
|
||||||
.selectOne(mapOf("name" to "myName"))!!
|
.selectOne(mapOf("name" to "myName"))!!
|
||||||
@@ -95,7 +118,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call select (multiple) on function`() {
|
fun `call select (multiple) on function`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val obj: List<ObjTest>? = Requester(getConnextion())
|
val obj: List<ObjTest>? = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function_multiple")
|
.getFunction("test_function_multiple")
|
||||||
.select(mapOf("name" to "myName"))
|
.select(mapOf("name" to "myName"))
|
||||||
@@ -106,7 +129,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call select paginated on query`() {
|
fun `call select paginated on query`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
val result: Paginated<ObjTest> = Requester(getConnextion())
|
val result: Paginated<ObjTest> = Requester(connection)
|
||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectPaginated")
|
.getQuery("Test/selectPaginated")
|
||||||
.select(1, 2, mapOf("name" to "ff"))
|
.select(1, 2, mapOf("name" to "ff"))
|
||||||
@@ -120,7 +143,7 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call select paginated on function`() {
|
fun `call select paginated on function`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||||
val result: Paginated<ObjTest> = Requester(getConnextion())
|
val result: Paginated<ObjTest> = Requester(connection)
|
||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function_paginated")
|
.getFunction("test_function_paginated")
|
||||||
.select(1, 2, mapOf("name" to "ff"))
|
.select(1, 2, mapOf("name" to "ff"))
|
||||||
@@ -134,12 +157,12 @@ class RequesterTest: TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call selectOne on query with extra parameter`() {
|
fun `call selectOne on query with extra parameter`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
val obj: ObjTest = Requester(getConnextion())
|
val obj: ObjTest = Requester(connection)
|
||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectOneWithParameters")
|
.getQuery("Test/selectOneWithParameters")
|
||||||
.selectOne(mapOf("name" to "myName")) {
|
.selectOne(mapOf("name" to "myName")) {
|
||||||
assertEquals("myName", it!!.name)
|
assertEquals("myName", it!!.name)
|
||||||
Assert.assertEquals("plop", rows[0].getString("other"))
|
Assert.assertEquals("plop", getString("other"))
|
||||||
}!!
|
}!!
|
||||||
|
|
||||||
assertEquals("myName", obj.name)
|
assertEquals("myName", obj.name)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
internal class SerializerTest: TestAbstract() {
|
internal class SerializerTest {
|
||||||
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||||
private class ObjTestDate(var val1: DateTime) : IdEntity(2)
|
private class ObjTestDate(var val1: DateTime) : IdEntity(2)
|
||||||
|
|
||||||
|
|||||||
@@ -9,20 +9,23 @@ import java.io.File
|
|||||||
|
|
||||||
@TestInstance(PER_CLASS)
|
@TestInstance(PER_CLASS)
|
||||||
abstract class TestAbstract {
|
abstract class TestAbstract {
|
||||||
protected fun getConnextion(): Connection {
|
protected val connection = Connection(database = "test", username = "test", password = "test")
|
||||||
return Connection(database = "test", username = "test", password = "test")
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun beforeAll() {
|
fun beforeAll() {
|
||||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
|
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
|
||||||
val promise = getConnextion().connect().sendQuery(initSQL.readText())
|
connection
|
||||||
promise.join()
|
.connect()
|
||||||
|
.createStatement()
|
||||||
|
.executeUpdate(initSQL.readText())
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun afterAll() {
|
fun afterAll() {
|
||||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
|
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
|
||||||
getConnextion().connect().sendQuery(downSQL.readText()).join()
|
connection.connect().apply {
|
||||||
|
createStatement()
|
||||||
|
.executeUpdate(downSQL.readText())
|
||||||
|
}.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,3 +73,12 @@ BEGIN
|
|||||||
resource = json_build_object('id', 1, 'name', 'changedName');
|
resource = json_build_object('id', 1, 'name', 'changedName');
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS
|
||||||
|
$$
|
||||||
|
BEGIN
|
||||||
|
PERFORM 1;
|
||||||
|
END;
|
||||||
|
$$
|
||||||
8
src/test/resources/sql/function/Test/function_void.sql
Normal file
8
src/test/resources/sql/function/Test/function_void.sql
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS
|
||||||
|
$$
|
||||||
|
BEGIN
|
||||||
|
PERFORM 1;
|
||||||
|
END;
|
||||||
|
$$;
|
||||||
@@ -1 +1,5 @@
|
|||||||
SELECT 1;
|
do $$
|
||||||
|
begin
|
||||||
|
PERFORM 1;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
|
|||||||
@@ -1 +1,5 @@
|
|||||||
SELECT 1;
|
do $$
|
||||||
|
begin
|
||||||
|
PERFORM 1;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
@@ -1 +1,5 @@
|
|||||||
SELECT 1;
|
do $$
|
||||||
|
begin
|
||||||
|
PERFORM 1;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
1
src/test/resources/sql/query/Test/exec.sql
Normal file
1
src/test/resources/sql/query/Test/exec.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
delete FROM test where 2038538 = 2;
|
||||||
Reference in New Issue
Block a user