use sendQuery if no return is expected

This commit is contained in:
2019-08-04 21:09:24 +02:00
parent cbb86dacc5
commit 24349fc71f
12 changed files with 101 additions and 16 deletions

View File

@@ -185,9 +185,17 @@ class Connection(
} }
} }
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() replaceArgsIntoSql(sql, compileArgs(values)) {
connect().sendQuery(it).join().rowsAffected.toInt()
}
}
}
override fun sendQuery(sql: String, values: Map<String, Any?>): Int {
return replaceArgs(sql, values) {
sendQuery(this.sql, this.parameters)
} }
} }
@@ -219,6 +227,19 @@ class Connection(
return block(ParametersQuery(newSql, newArgs)) return block(ParametersQuery(newSql, newArgs))
} }
private fun <T> replaceArgsIntoSql(sql: String, values: List<Any?>, block: (String) -> T): T {
val paramRegex = "(?<!\\?)(\\?)".toRegex(RegexOption.IGNORE_CASE)
var i = 0
val newSql = paramRegex.replace(sql) { _ ->
values[i] ?: error("Parameter $i missing")
val valToReplace = values[i].toString()
++i
"'$valToReplace'"
}
return block(newSql)
}
data class ParametersQuery(val sql: String, val parameters: List<Any?>) data class ParametersQuery(val sql: String, val parameters: List<Any?>)
private fun <T> stopwatchQuery(sql: String, values: List<Any?> = emptyList(), callback: () -> T): T { private fun <T> stopwatchQuery(sql: String, values: List<Any?> = emptyList(), callback: () -> T): T {

View File

@@ -49,4 +49,6 @@ interface EmbedExecutable {
fun exec(values: List<Any?> = emptyList()): QueryResult fun exec(values: List<Any?> = emptyList()): QueryResult
fun exec(values: Map<String, Any?>): QueryResult fun exec(values: Map<String, Any?>): QueryResult
fun sendQuery(values: List<Any?> = emptyList()): Int
fun sendQuery(values: Map<String, Any?>): Int
} }

View File

@@ -50,5 +50,6 @@ interface Executable {
fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult
fun exec(sql: String, values: Map<String, Any?>): QueryResult fun exec(sql: String, values: Map<String, Any?>): QueryResult
fun sendQuery(sql: String): QueryResult fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
fun sendQuery(sql: String, values: Map<String, Any?>): Int
} }

View File

@@ -169,12 +169,22 @@ class Function(val definition: Function, override val connection: Connection): E
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
} }

View File

@@ -99,4 +99,12 @@ class Query(override val name: String, private val sql: String, override val con
override fun exec(values: Map<String, Any?>): QueryResult { override fun exec(values: Map<String, Any?>): QueryResult {
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)
}
} }

View File

@@ -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.name))
} }
return Status.OK return Status.OK
} }

View File

@@ -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), getConnextion()).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), getConnextion()).apply {
forceAllDown().apply { forceAllDown().apply {
size `should be equal to` 1 size `should be equal to` 6
} }
} }
} }
@@ -95,7 +96,7 @@ class MigrationTest(): TestAbstract() {
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, getConnextion()).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(getConnextion())

View File

@@ -57,6 +57,28 @@ class RequesterTest: TestAbstract() {
assertEquals(1, result.rowsAffected) assertEquals(1, result.rowsAffected)
} }
@Test
fun `call sendQuery on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val result = Requester(getConnextion())
.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(getConnextion())
.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())

View File

@@ -9,8 +9,9 @@ import java.io.File
@TestInstance(PER_CLASS) @TestInstance(PER_CLASS)
abstract class TestAbstract { abstract class TestAbstract {
private val con = Connection(database = "test", username = "test", password = "test")
protected fun getConnextion(): Connection { protected fun getConnextion(): Connection {
return Connection(database = "test", username = "test", password = "test") return con
} }
@BeforeEach @BeforeEach
@@ -24,5 +25,6 @@ abstract class TestAbstract {
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().sendQuery(downSQL.readText()).join()
getConnextion().connect().disconnect()
} }
} }

View File

@@ -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;
$$

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

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