refractor query params
This commit is contained in:
parent
43279e56bf
commit
419c855463
@ -1,5 +1,10 @@
|
||||
package com.restapi.controllers
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.databind.DeserializationContext
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
|
||||
import com.restapi.domain.*
|
||||
import com.restapi.domain.Session.currentUser
|
||||
import com.restapi.domain.Session.database
|
||||
@ -22,25 +27,57 @@ enum class QueryParamType {
|
||||
STRING, NUMBER, DATETIME, DATE
|
||||
}
|
||||
|
||||
data class QueryParam(val type: QueryParamType, val value: String) {
|
||||
fun getValue(): Any {
|
||||
return when (type) {
|
||||
QueryParamType.STRING -> value
|
||||
QueryParamType.NUMBER -> if (value.matches(Regex("\\d+"))) value.toLong() else value.toDouble()
|
||||
QueryParamType.DATETIME -> LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
|
||||
QueryParamType.DATE -> LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class RawQuery(
|
||||
val sql: String,
|
||||
val params: Map<String, QueryParam>
|
||||
)
|
||||
|
||||
data class QueryById(
|
||||
val params: List<QueryParam>
|
||||
)
|
||||
|
||||
@JsonDeserialize(using = QueryByIdParamsDeSerializer::class)
|
||||
sealed class QueryParam {
|
||||
data class Simple(val simple: String) : QueryParam()
|
||||
data class Complex(val type: QueryParamType, val value: String) : QueryParam() {
|
||||
fun getValueComplex(): Any {
|
||||
return when (type) {
|
||||
QueryParamType.STRING -> value
|
||||
QueryParamType.NUMBER -> if (value.matches(Regex("\\d+"))) value.toLong() else value.toDouble()
|
||||
QueryParamType.DATETIME -> LocalDateTime.parse(
|
||||
value,
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
)
|
||||
|
||||
QueryParamType.DATE -> LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getValue(): Any {
|
||||
return when (this) {
|
||||
is Complex -> getValueComplex()
|
||||
is Simple -> simple
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class QueryByIdParamsDeSerializer : JsonDeserializer<QueryParam>() {
|
||||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext?): QueryParam {
|
||||
val node = p.readValueAsTree<JsonNode>()
|
||||
return if (node.isTextual) {
|
||||
QueryParam.Simple(node.asText())
|
||||
} else {
|
||||
QueryParam.Complex(
|
||||
QueryParamType.valueOf(node.get("type").textValue()),
|
||||
node.get("value").textValue(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum class ResultType {
|
||||
INTEGER, DECIMAL, STRING, DATETIME, ARRAY, OBJECT
|
||||
}
|
||||
@ -182,7 +219,7 @@ object Entities {
|
||||
.setRawSql(RawSqlBuilder.parse(query.sql).create())
|
||||
.apply {
|
||||
sql.params.forEachIndexed { index, entry ->
|
||||
setParameter(index+1, entry.getValue())
|
||||
setParameter(index + 1, entry.getValue())
|
||||
}
|
||||
}
|
||||
.findList()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user