3 Commits
2.1.1 ... jdbc

Author SHA1 Message Date
d58f367843 clean code 2019-08-04 22:30:20 +02:00
81aebd815d use sendQuery if no return is expected 2019-08-04 20:00:17 +02:00
aeabe3f8c1 change jasync to jdbc 2019-08-04 18:07:25 +02:00
21 changed files with 210 additions and 122 deletions

View File

@@ -18,8 +18,8 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.31")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin: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.postgresql:postgresql:42.2.6")
testImplementation("ch.qos.logback:logback-classic:1.2.3")
testImplementation("ch.qos.logback:logback-core:1.2.3")
@@ -32,7 +32,7 @@ publishing {
publications {
create<MavenPublication>("maven") {
groupId = "fr.postgresjson"
artifactId = "postgresjson"
artifactId = "postgresjson-jdbc"
version = "0.1"
from(components["java"])

View File

@@ -1,20 +1,17 @@
package fr.postgresjson.connexion
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.serializer.Serializer
import fr.postgresjson.utils.LoggerDelegate
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 SelectCallback<T> = QueryResult.(List<T>) -> Unit
typealias SelectPaginatedCallback<T> = QueryResult.(Paginated<T>) -> Unit
typealias SelectOneCallback<T> = ResultSet.(T?) -> Unit
typealias SelectCallback<T> = ResultSet.(List<T>) -> Unit
typealias SelectPaginatedCallback<T> = ResultSet.(Paginated<T>) -> Unit
class Connection(
private val database: String,
@@ -23,32 +20,34 @@ class Connection(
private val host: String = "localhost",
private val port: Int = 5432
): Executable {
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
private lateinit var connection: JDBCConnection
private val serializer = Serializer()
private val logger: Logger? by LoggerDelegate()
internal fun connect(): ConnectionPool<PostgreSQLConnection> {
if (!::connection.isInitialized || !connection.isConnected()) {
connection = PostgreSQLConnectionBuilder.createConnectionPool(
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
)
internal fun connect(): JDBCConnection {
if (!::connection.isInitialized || connection.isClosed) {
connection = DriverManager.getConnection("jdbc:postgresql://$host:$port/$database", username, password)
}
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(
sql: String,
typeReference: TypeReference<R>,
values: List<Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
val primaryObject = values.firstOrNull {
it is EntityI<*> && typeReference.type.typeName == it::class.java.name
} as R?
val result = exec(sql, compileArgs(values))
val json = result.rows[0].getString(0)
val json = result.getString(1)
return if (json === null) {
null
} else {
@@ -73,7 +72,7 @@ class Connection(
sql: String,
typeReference: TypeReference<R>,
values: Map<String, Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
return replaceArgs(sql, values) {
select(this.sql, typeReference, this.parameters, block)
@@ -91,10 +90,10 @@ class Connection(
sql: String,
typeReference: TypeReference<List<R>>,
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
val result = exec(sql, compileArgs(values))
val json = result.rows[0].getString(0)
val json = result.getString(1)
return if (json === null) {
listOf<EntityI<*>>() as List<R>
} else {
@@ -117,7 +116,7 @@ class Connection(
limit: Int,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, Paginated<R>) -> Unit
block: (ResultSet, Paginated<R>) -> Unit
): Paginated<R> {
val offset = (page - 1) * limit
val newValues = values
@@ -125,11 +124,11 @@ class Connection(
.plus("limit" to limit)
val line = replaceArgs(sql, newValues) {
exec(this.sql, compileArgs(this.parameters))
exec(this.sql, this.parameters)
}
return line.run {
val json = rows[0].getString(0)
val json = getString(1)
val entities = if (json === null) {
listOf<EntityI<*>>() as List<R>
} else {
@@ -139,7 +138,7 @@ class Connection(
entities,
offset,
limit,
rows[0].getInt("total") ?: error("The query not return total")
getInt("total")
)
}.also {
block(line, it)
@@ -159,7 +158,7 @@ class Connection(
sql: String,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
return replaceArgs(sql, values) {
select(this.sql, typeReference, this.parameters, block)
@@ -173,21 +172,43 @@ class Connection(
): List<R> =
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) {
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) {
exec(this.sql, this.parameters)
}
}
override fun sendQuery(sql: String): QueryResult {
return stopwatchQuery(sql) {
connect().sendQuery(sql).join()
override fun sendQuery(sql: String, values: List<Any?>): Int {
return stopwatchQuery(sql, values) {
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)
}
}

View File

@@ -1,8 +1,8 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
import fr.postgresjson.entity.EntityI
import java.sql.ResultSet
interface EmbedExecutable {
val connection: Connection
@@ -47,6 +47,8 @@ interface EmbedExecutable {
block: SelectPaginatedCallback<R> = {}
): Paginated<R>
fun exec(values: List<Any?> = emptyList()): QueryResult
fun exec(values: Map<String, Any?>): QueryResult
fun exec(values: List<Any?> = emptyList()): ResultSet
fun exec(values: Map<String, Any?>): ResultSet
fun sendQuery(values: List<Any?> = emptyList()): Int
fun sendQuery(values: Map<String, Any?>): Int
}

View File

@@ -1,8 +1,8 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
import fr.postgresjson.entity.EntityI
import java.sql.ResultSet
interface Executable {
/* Select One */
@@ -48,7 +48,8 @@ interface Executable {
block: SelectPaginatedCallback<R> = {}
): Paginated<R>
fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult
fun exec(sql: String, values: Map<String, Any?>): QueryResult
fun sendQuery(sql: String): QueryResult
fun exec(sql: String, values: List<Any?> = emptyList()): ResultSet
fun exec(sql: String, values: Map<String, Any?>): ResultSet
fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
fun sendQuery(sql: String, values: Map<String, Any?>): Int
}

View File

@@ -1,9 +1,9 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
import fr.postgresjson.definition.Function
import fr.postgresjson.entity.EntityI
import java.sql.ResultSet
class Function(val definition: Function, override val connection: Connection): EmbedExecutable {
override fun toString(): String {
@@ -20,7 +20,7 @@ class Function(val definition: Function, override val connection: Connection): E
override fun <R: EntityI<*>> select(
typeReference: TypeReference<R>,
values: List<Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
val args = compileArgs(values)
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(
typeReference: TypeReference<R>,
values: Map<String, Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
val args = compileArgs(values)
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(
typeReference: TypeReference<List<R>>,
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
val args = compileArgs(values)
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(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
@@ -124,7 +124,7 @@ class Function(val definition: Function, override val connection: Connection): E
limit: Int,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, Paginated<R>) -> Unit
block: (ResultSet, Paginated<R>) -> Unit
): Paginated<R> {
val offset = (page - 1) * limit
val newValues = values
@@ -155,26 +155,36 @@ class Function(val definition: Function, override val connection: Connection): E
/* Execute function without traitements */
override fun exec(values: List<Any?>): QueryResult {
override fun exec(values: List<Any?>): ResultSet {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
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 sql = "SELECT * FROM ${definition.name} ($args)"
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 {
val placeholders = values
.filterIndexed { index, any ->
definition.parameters[index].default === null || any !== null
.filterIndexed { index, value ->
definition.parameters[index].default === null || value != null
}
.mapIndexed { index, any ->
.mapIndexed { index, _ ->
"?::" + definition.parameters[index].type
}

View File

@@ -1,6 +1,5 @@
package fr.postgresjson.connexion
import com.github.jasync.sql.db.util.length
import fr.postgresjson.entity.EntityI
data class Paginated<T: EntityI<*>>(
@@ -10,7 +9,7 @@ data class Paginated<T: EntityI<*>>(
val total: Int
) {
val currentPage: Int = (offset / limit) + 1
val count: Int = result.length
val count: Int = result.size
init {
if (offset < 0) error("offset must be greather or equal than 0")

View File

@@ -1,8 +1,8 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.QueryResult
import fr.postgresjson.entity.EntityI
import java.sql.ResultSet
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(
typeReference: TypeReference<R>,
values: List<Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
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(
typeReference: TypeReference<R>,
values: Map<String, Any?>,
block: (QueryResult, R?) -> Unit
block: (ResultSet, R?) -> Unit
): R? {
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(
typeReference: TypeReference<List<R>>,
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
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(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
block: (ResultSet, List<R>) -> Unit
): List<R> {
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,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, Paginated<R>) -> Unit
block: (ResultSet, Paginated<R>) -> Unit
): Paginated<R> {
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 */
override fun exec(values: List<Any?>): QueryResult {
override fun exec(values: List<Any?>): ResultSet {
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)
}
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)
}
}

View File

@@ -17,7 +17,7 @@ data class Function(
init {
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)
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
connection.exec(it, listOf(down))
connection.sendQuery(it, listOf(down))
}
return Status.OK
}
@@ -59,7 +59,7 @@ data class Function(
up()
down()
it.sendQuery("ROLLBACK")
}.join()
}
return Status.OK // TODO
}
@@ -69,7 +69,7 @@ data class Function(
up()
down()
it.sendQuery("ROLLBACK")
}.join()
}
return Status.OK // TODO
}

View File

@@ -1,7 +1,6 @@
package fr.postgresjson.migration
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.util.size
import fr.postgresjson.connexion.Connection
import fr.postgresjson.definition.Function.FunctionNotFound
import fr.postgresjson.entity.Entity
@@ -97,11 +96,11 @@ data class Migrations private constructor(
it.isFile
}.forEach { file ->
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 {
val down = File("$it.down.sql").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)
} catch (e: FileNotFoundException) {
throw DownMigrationNotDefined("$it.down.sql", e)

View File

@@ -31,7 +31,7 @@ data class Query(
connection.sendQuery(down)
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
@@ -42,7 +42,7 @@ data class Query(
up()
down()
it.sendQuery("ROLLBACK")
}.join()
}
return Migration.Status.OK // TODO
}
@@ -52,7 +52,7 @@ data class Query(
up()
down()
it.sendQuery("ROLLBACK")
}.join()
}
return Migration.Status.OK // TODO
}

View File

@@ -1,11 +1,9 @@
package fr.postgresjson
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.Paginated
import fr.postgresjson.entity.IdEntity
import org.junit.Assert.*
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
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 ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
private lateinit var connection: Connection
@BeforeEach
fun before() {
connection = getConnextion()
}
@Test
fun getObject() {
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
@@ -72,7 +63,7 @@ class ConnectionTest(): TestAbstract() {
fun callExec() {
val o = ObjTest("myName")
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
@@ -166,7 +157,7 @@ class ConnectionTest(): TestAbstract() {
params
) {
assertEquals("ff", it!!.first)
assertEquals("plop", rows[0].getString("other"))
assertEquals("plop", getString("other"))
}
assertNotNull(result)
assertEquals("ff", result!!.first)

View File

@@ -17,7 +17,7 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run up query`() {
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
val m = Migrations(resources, getConnextion())
val m = Migrations(resources, connection)
m.up().apply {
this `should contain` Pair("1", Migration.Status.OK)
size `should be equal to` 1
@@ -30,14 +30,14 @@ class MigrationTest(): TestAbstract() {
fun `migration up Query should throw error if no down`() {
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
invoking {
Migrations(resources, getConnextion())
Migrations(resources, connection)
} shouldThrow Migrations.DownMigrationNotDefined::class
}
@Test
fun `run forced down query`() {
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
val m = Migrations(resources, getConnextion())
val m = Migrations(resources, connection)
repeat(3) {
m.down(true).apply {
this `should contain` Pair("1", Migration.Status.OK)
@@ -49,10 +49,10 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run dry migrations`() {
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
}
Migrations(resources, getConnextion()).apply {
Migrations(resources, connection).apply {
runDry().size `should be equal to` 2
}
}
@@ -60,7 +60,7 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run dry migrations launch twice`() {
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
}
@@ -69,7 +69,7 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run migrations`() {
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
Migrations(resources, getConnextion()).apply {
Migrations(resources, connection).apply {
run().apply {
size `should be equal to` 1
}
@@ -79,14 +79,15 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run migrations force down`() {
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 {
size `should be equal to` 1
size `should be equal to` 6
}
}
Migrations(resources, getConnextion()).apply {
Migrations(listOf(resources, resourcesFunctions), connection).apply {
forceAllDown().apply {
size `should be equal to` 1
size `should be equal to` 6
}
}
}
@@ -94,11 +95,11 @@ class MigrationTest(): TestAbstract() {
@Test
fun `run functions migrations`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
Migrations(resources, getConnextion()).apply {
run().size `should be equal to` 4
Migrations(resources, connection).apply {
run().size `should be equal to` 5
}
val objTest: RequesterTest.ObjTest? = Requester(getConnextion())
val objTest: RequesterTest.ObjTest? = Requester(connection)
.addFunction(resources)
.getFunction("test_function")
.selectOne(listOf("test", "plip"))

View File

@@ -5,6 +5,7 @@ import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.IdEntity
import org.junit.Assert
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import java.io.File
@@ -14,7 +15,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `get query from file`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val objTest: ObjTest? = Requester(getConnextion())
val objTest: ObjTest? = Requester(connection)
.addQuery(resources)
.getQuery("Test/selectOne")
.selectOne()
@@ -26,7 +27,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `get function from file`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val objTest: ObjTest? = Requester(getConnextion())
val objTest: ObjTest? = Requester(connection)
.addFunction(resources)
.getFunction("test_function")
.selectOne(listOf("test", "plip"))
@@ -38,29 +39,51 @@ class RequesterTest: TestAbstract() {
@Test
fun `call exec on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val result = Requester(getConnextion())
val result = Requester(connection)
.addQuery(resources)
.getQuery("Test/selectOne")
.exec()
assertEquals(1, result.rowsAffected)
assertNotNull(result.getString(1))
}
@Test
fun `call exec on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val result = Requester(getConnextion())
val result = Requester(connection)
.addFunction(resources)
.getFunction("test_function")
.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
fun `call selectOne on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: ObjTest = Requester(getConnextion())
val obj: ObjTest = Requester(connection)
.addFunction(resources)
.getFunction("test_function")
.selectOne(mapOf("name" to "myName"))!!
@@ -72,7 +95,7 @@ class RequesterTest: TestAbstract() {
fun `call selectOne on function with object`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj2 = ObjTest("original")
val obj: ObjTest = Requester(getConnextion())
val obj: ObjTest = Requester(connection)
.addFunction(resources)
.getFunction("test_function_object")
.selectOne("resource" to obj2)!!
@@ -84,7 +107,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `call selectOne on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val obj: ObjTest = Requester(getConnextion())
val obj: ObjTest = Requester(connection)
.addQuery(resources)
.getQuery("Test/selectOneWithParameters")
.selectOne(mapOf("name" to "myName"))!!
@@ -95,7 +118,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `call select (multiple) on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: List<ObjTest>? = Requester(getConnextion())
val obj: List<ObjTest>? = Requester(connection)
.addFunction(resources)
.getFunction("test_function_multiple")
.select(mapOf("name" to "myName"))
@@ -106,7 +129,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `call select paginated on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val result: Paginated<ObjTest> = Requester(getConnextion())
val result: Paginated<ObjTest> = Requester(connection)
.addQuery(resources)
.getQuery("Test/selectPaginated")
.select(1, 2, mapOf("name" to "ff"))
@@ -120,7 +143,7 @@ class RequesterTest: TestAbstract() {
@Test
fun `call select paginated on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val result: Paginated<ObjTest> = Requester(getConnextion())
val result: Paginated<ObjTest> = Requester(connection)
.addFunction(resources)
.getFunction("test_function_paginated")
.select(1, 2, mapOf("name" to "ff"))
@@ -134,12 +157,12 @@ class RequesterTest: TestAbstract() {
@Test
fun `call selectOne on query with extra parameter`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val obj: ObjTest = Requester(getConnextion())
val obj: ObjTest = Requester(connection)
.addQuery(resources)
.getQuery("Test/selectOneWithParameters")
.selectOne(mapOf("name" to "myName")) {
assertEquals("myName", it!!.name)
Assert.assertEquals("plop", rows[0].getString("other"))
Assert.assertEquals("plop", getString("other"))
}!!
assertEquals("myName", obj.name)

View File

@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@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 ObjTestDate(var val1: DateTime) : IdEntity(2)

View File

@@ -9,20 +9,23 @@ import java.io.File
@TestInstance(PER_CLASS)
abstract class TestAbstract {
protected fun getConnextion(): Connection {
return Connection(database = "test", username = "test", password = "test")
}
protected val connection = Connection(database = "test", username = "test", password = "test")
@BeforeEach
fun beforeAll() {
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
val promise = getConnextion().connect().sendQuery(initSQL.readText())
promise.join()
connection
.connect()
.createStatement()
.executeUpdate(initSQL.readText())
}
@AfterEach
fun afterAll() {
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()
}
}

View File

@@ -73,3 +73,12 @@ BEGIN
resource = json_build_object('id', 1, 'name', 'changedName');
END;
$$;
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
LANGUAGE plpgsql
AS
$$
BEGIN
PERFORM 1;
END;
$$

View File

@@ -0,0 +1,8 @@
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
LANGUAGE plpgsql
AS
$$
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;
$$

View File

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

View File

@@ -0,0 +1 @@
delete FROM test where 2038538 = 2;