clean Requester
This commit is contained in:
@@ -4,13 +4,13 @@ import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
fun DefinitionFunction.toConnection(connection: Connection): Function = Function(this, connection)
|
||||
fun DefinitionFunction.toRunnable(connection: Connection): Function = Function(this, connection)
|
||||
|
||||
fun Sequence<DefinitionFunction>.toConnection(connection: Connection): Sequence<Function> = map { it.toConnection(connection) }
|
||||
fun Sequence<DefinitionFunction>.toRunnable(connection: Connection): Sequence<Function> = map { it.toRunnable(connection) }
|
||||
|
||||
fun Sequence<Function>.toMutableMap(): MutableMap<String, Function> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
internal fun URI.toFunction(connection: Connection): MutableMap<String, Function> = searchSqlFiles()
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.toConnection(connection)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
|
||||
@@ -4,13 +4,13 @@ import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
fun QueryDefinition.toConnection(connection: Connection): Query = Query(name, script, connection)
|
||||
fun QueryDefinition.toRunnable(connection: Connection): Query = Query(name, script, connection)
|
||||
|
||||
fun Sequence<QueryDefinition>.toConnection(connection: Connection): Sequence<Query> = map { it.toConnection(connection) }
|
||||
fun Sequence<QueryDefinition>.toRunnable(connection: Connection): Sequence<Query> = map { it.toRunnable(connection) }
|
||||
|
||||
fun Sequence<Query>.toMutableMap(): MutableMap<String, Query> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
internal fun URI.toQuery(connection: Connection): MutableMap<String, Query> = searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.toConnection(connection)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
|
||||
@@ -22,67 +22,46 @@ class Requester(
|
||||
functions = functionsDirectory?.toFunction(connection) ?: mutableMapOf(),
|
||||
)
|
||||
|
||||
fun addQuery(query: Query): Requester {
|
||||
fun addQuery(query: Query) {
|
||||
queries[query.name] = query
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(query: QueryDefinition): Requester = addQuery(query.name, query.script)
|
||||
fun addQuery(query: QueryDefinition) = addQuery(query.toRunnable(connection))
|
||||
|
||||
fun addQuery(name: String, sql: String): Requester {
|
||||
fun addQuery(name: String, sql: String) {
|
||||
addQuery(Query(name, sql, connection))
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(queriesDirectory: URI): Requester {
|
||||
queriesDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
if (it is QueryDefinition) {
|
||||
addQuery(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
fun addQuery(queriesDirectory: URI) {
|
||||
queriesDirectory
|
||||
.searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.forEach(this::addQuery)
|
||||
}
|
||||
|
||||
fun getQueries(): List<Query> {
|
||||
return queries.map { it.value }
|
||||
fun getQueries(): List<Query> = queries.map { it.value }
|
||||
|
||||
fun addFunction(definition: DefinitionFunction) {
|
||||
definition
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(definition: DefinitionFunction): Requester {
|
||||
functions[definition.name] = Function(definition, connection)
|
||||
return this
|
||||
fun addFunction(sql: String) {
|
||||
DefinitionFunction(sql)
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(sql: String): Requester {
|
||||
DefinitionFunction(sql).let {
|
||||
functions[it.name] = Function(it, connection)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun addFunction(functionsDirectory: URI): Requester {
|
||||
fun addFunctions(functionsDirectory: URI) {
|
||||
functionsDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
if (it is DefinitionFunction) {
|
||||
addFunction(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.forEach(this::addFunction)
|
||||
}
|
||||
|
||||
fun getFunction(name: String): Function {
|
||||
if (functions[name] === null) {
|
||||
throw NoFunctionDefined(name)
|
||||
}
|
||||
return functions[name]!!
|
||||
}
|
||||
fun getFunction(name: String): Function = functions[name] ?: throw NoFunctionDefined(name)
|
||||
|
||||
fun getQuery(path: String): Query {
|
||||
if (queries[path] === null) {
|
||||
throw NoQueryDefined(path)
|
||||
}
|
||||
return queries[path]!!
|
||||
}
|
||||
fun getQuery(path: String): Query = queries[path] ?: throw NoQueryDefined(path)
|
||||
|
||||
class NoFunctionDefined(name: String) : Exception("No function defined for $name")
|
||||
class NoQueryDefined(path: String) : Exception("No query defined in $path")
|
||||
|
||||
Reference in New Issue
Block a user