change jasync to jdbc

This commit is contained in:
2019-08-04 18:05:31 +02:00
parent cbb86dacc5
commit aeabe3f8c1
16 changed files with 107 additions and 79 deletions

View File

@@ -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"])

View File

@@ -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,37 @@ 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()
} }
} }

View File

@@ -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,6 @@ 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
} }

View File

@@ -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,7 @@ 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
} }

View File

@@ -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,14 +155,14 @@ 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)"

View File

@@ -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")

View File

@@ -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,11 @@ 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)
} }
} }

View File

@@ -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
} }

View File

@@ -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)

View File

@@ -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
} }

View File

@@ -72,7 +72,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 +166,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)

View File

@@ -4,6 +4,7 @@ import fr.postgresjson.connexion.Paginated
import fr.postgresjson.connexion.Requester 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
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.io.File import java.io.File
@@ -43,7 +44,7 @@ class RequesterTest: TestAbstract() {
.getQuery("Test/selectOne") .getQuery("Test/selectOne")
.exec() .exec()
assertEquals(1, result.rowsAffected) Assertions.assertNotNull(result.getString(1))
} }
@Test @Test
@@ -54,7 +55,7 @@ class RequesterTest: TestAbstract() {
.getFunction("test_function") .getFunction("test_function")
.exec(listOf("test", "plip")) .exec(listOf("test", "plip"))
assertEquals(1, result.rowsAffected) Assertions.assertNotNull(result.getString(1))
} }
@Test @Test
@@ -139,7 +140,7 @@ class RequesterTest: TestAbstract() {
.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)

View File

@@ -9,20 +9,22 @@ import java.io.File
@TestInstance(PER_CLASS) @TestInstance(PER_CLASS)
abstract class TestAbstract { abstract class TestAbstract {
private var connection = Connection(database = "test", username = "test", password = "test")
protected fun getConnextion(): Connection { protected fun getConnextion(): Connection {
return Connection(database = "test", username = "test", password = "test") return connection
} }
@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()) getConnextion().connect().createStatement().executeUpdate(initSQL.readText())
promise.join()
} }
@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() getConnextion().connect().createStatement().executeUpdate(downSQL.readText())
getConnextion().connect().close()
} }
} }

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$