use sendQuery if no return is expected
This commit is contained in:
@@ -185,9 +185,17 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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))
|
||||
}
|
||||
|
||||
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?>)
|
||||
|
||||
private fun <T> stopwatchQuery(sql: String, values: List<Any?> = emptyList(), callback: () -> T): T {
|
||||
|
||||
@@ -49,4 +49,6 @@ interface EmbedExecutable {
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
fun sendQuery(values: List<Any?> = emptyList()): Int
|
||||
fun sendQuery(values: Map<String, Any?>): Int
|
||||
}
|
||||
@@ -50,5 +50,6 @@ interface Executable {
|
||||
|
||||
fun exec(sql: String, values: List<Any?> = emptyList()): 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
|
||||
}
|
||||
@@ -169,12 +169,22 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
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.name))
|
||||
}
|
||||
return Status.OK
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user