Add more tests

remove "sendQuery" on function
sendQuery now return QueryResult
remove null on queryError message
This commit is contained in:
2021-07-18 01:19:27 +02:00
parent 35d43abc4b
commit a4a4ef5f6f
10 changed files with 116 additions and 78 deletions

View File

@@ -185,11 +185,11 @@ class Connection(
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(sql: String, values: List<Any?>): Int {
override fun sendQuery(sql: String, values: List<Any?>): QueryResult {
val compiledValues = compileArgs(values)
return stopwatchQuery(sql, compiledValues) {
replaceArgsIntoSql(sql, compiledValues) {
connect().sendQuery(it).join().rowsAffected.toInt()
connect().sendQuery(it).join()
}
}
}
@@ -197,7 +197,7 @@ class Connection(
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(sql: String, values: Map<String, Any?>): Int {
override fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult {
return replaceArgs(sql, values) {
sendQuery(this.sql, this.parameters)
}
@@ -217,7 +217,7 @@ class Connection(
val paramRegex = "(?<!:):([a-zA-Z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
val newArgs = paramRegex.findAll(sql).map { match ->
val name = match.groups[1]!!.value
values[name] ?: values[name.trimStart('_')] ?: queryError("Parameter $name missing", sql, values)
values[name] ?: values[name.trimStart('_')] ?: queryError("""Parameter "$name" missing""", sql, values)
}.toList()
var newSql = sql
@@ -296,11 +296,11 @@ class Connection(
"""
|$msg
|
|${parameters.joinToString(", ") { it.toString() }.prependIndent(" > ")}
|${parameters.joinToString(", ") { it.toString() }.prependIndent(" > ") ?: ""}
|${sql.prependIndent(" > ")}
|${result?.let { "-----" }?.prependIndent(" > ")}
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ")}
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ")}
|${result?.let { "-----" }?.prependIndent(" > ") ?: ""}
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ") ?: ""}
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ") ?: ""}
""".trimMargin().trim(' ', '\n')
)
@@ -313,11 +313,11 @@ class Connection(
"""
|$msg
|
|${parameters.map { it.key + ": " + it.value }.joinToString(", ").prependIndent(" > ")}
|${parameters.map { ":" + it.key + " = " + it.value }.joinToString(", ").prependIndent(" > ") ?: ""}
|${sql.prependIndent(" > ")}
|${result?.let { "-----" }?.prependIndent(" > ")}
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ")}
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ")}
""".trimMargin().trim(' ')
|${result?.let { "-----" }?.prependIndent(" > ") ?: ""}
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ") ?: ""}
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ") ?: ""}
""".trimMargin().trim(' ', '\n')
)
}

View File

@@ -111,17 +111,4 @@ sealed interface EmbedExecutable {
fun perform(values: List<Any?>) { exec(values) }
fun perform(values: Map<String, Any?>) { exec(values) }
fun perform(vararg values: Pair<String, Any?>) = perform(values.toMap())
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(values: List<Any?>): Int
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(values: Map<String, Any?>): Int
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(vararg values: Pair<String, Any?>): Int = sendQuery(values.toMap())
}

View File

@@ -125,17 +125,17 @@ interface Executable {
/**
* Warning: this method not use prepared statement
*/
fun <R : EntityI> sendQuery(sql: String, value: R): Int = sendQuery(sql, listOf(value))
fun <R : EntityI> sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
fun sendQuery(sql: String, values: List<Any?>): QueryResult
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(sql: String, values: Map<String, Any?>): Int
fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): Int = sendQuery(sql, values.toMap())
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): QueryResult = sendQuery(sql, values.toMap())
}

View File

@@ -82,22 +82,6 @@ class Function(val definition: Function, override val connection: Connection) :
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(compileSql(values), values)
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(values: List<Any?>): Int {
exec(values)
return 0
}
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(values: Map<String, Any?>): Int {
exec(values)
return 0
}
private fun <R : EntityI> compileArgs(value: R): String = compileArgs(listOf(value))
private fun compileArgs(values: List<Any?>): String {

View File

@@ -76,10 +76,14 @@ class Query(override val name: String, private val sql: String, override val con
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(values: List<Any?>): Int = connection.sendQuery(sql, values)
fun sendQuery(values: List<Any?>): QueryResult = connection.sendQuery(sql, values)
/**
* Warning: this method not use prepared statement
*/
override fun sendQuery(values: Map<String, Any?>): Int = connection.sendQuery(sql, values)
fun sendQuery(values: Map<String, Any?>): QueryResult = connection.sendQuery(sql, values)
/**
* Warning: this method not use prepared statement
*/
fun sendQuery(vararg values: Pair<String, Any?>): QueryResult = sendQuery(values.toMap())
}

View File

@@ -47,21 +47,25 @@ data class Function(
}
}
this::class.java.classLoader.getResource("sql/migration/insertFunction.sql")!!.readText().let {
connection.selectOne<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script))?.let { function ->
this::class.java.classLoader
.getResource("sql/migration/insertFunction.sql")!!.readText()
.let { connection.selectOne<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script)) }
?.let { function ->
executedAt = function.executedAt
doExecute = Action.OK
}
}
return Status.OK
}
override fun down(): Status {
connection.sendQuery(down.script)
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
connection.sendQuery(it, listOf(down.name))
}
this::class.java.classLoader
.getResource("sql/migration/deleteFunction.sql")!!
.readText()
.let { connection.sendQuery(it, listOf(down.name)) }
return Status.OK
}
@@ -75,13 +79,9 @@ data class Function(
return Status.OK
}
fun copy(): Function {
return this.copy(up = up, down = down, connection = connection, executedAt = executedAt).also {
it.doExecute = this.doExecute
}
}
fun copy(): Function = this
.copy(up = up, down = down, connection = connection, executedAt = executedAt)
.also { it.doExecute = this.doExecute }
infix fun `is different from`(other: DefinitionFunction): Boolean {
return other.script != this.up.script
}
infix fun `is different from`(other: DefinitionFunction): Boolean = other.script != this.up.script
}