remove RequesterFactory
This commit is contained in:
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
interface EmbedExecutable {
|
||||
sealed interface EmbedExecutable {
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
val name: String
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
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 Sequence<DefinitionFunction>.toConnection(connection: Connection): Sequence<Function> = map { it.toConnection(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)
|
||||
.toMutableMap()
|
||||
16
src/main/kotlin/fr/postgresjson/connexion/QueryConverter.kt
Normal file
16
src/main/kotlin/fr/postgresjson/connexion/QueryConverter.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
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 Sequence<QueryDefinition>.toConnection(connection: Connection): Sequence<Query> = map { it.toConnection(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)
|
||||
.toMutableMap()
|
||||
@@ -10,6 +10,18 @@ class Requester(
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
) {
|
||||
constructor(connection: Connection) : this(connection, mutableMapOf(), mutableMapOf())
|
||||
|
||||
constructor(
|
||||
connection: Connection,
|
||||
queriesDirectory: URI? = null,
|
||||
functionsDirectory: URI? = null
|
||||
) : this(
|
||||
connection = connection,
|
||||
queries = queriesDirectory?.toQuery(connection) ?: mutableMapOf(),
|
||||
functions = functionsDirectory?.toFunction(connection) ?: mutableMapOf(),
|
||||
)
|
||||
|
||||
fun addQuery(query: Query): Requester {
|
||||
queries[query.name] = query
|
||||
return this
|
||||
@@ -72,42 +84,6 @@ class Requester(
|
||||
return queries[path]!!
|
||||
}
|
||||
|
||||
class RequesterFactory(
|
||||
private val connection: Connection,
|
||||
private val queriesDirectory: URI? = null,
|
||||
private val functionsDirectory: URI? = null
|
||||
) {
|
||||
constructor(
|
||||
host: String = "localhost",
|
||||
port: Int = 5432,
|
||||
database: String,
|
||||
username: String,
|
||||
password: String,
|
||||
queriesDirectory: URI? = null,
|
||||
functionsDirectory: URI? = null
|
||||
) : this(
|
||||
Connection(host = host, port = port, database = database, username = username, password = password),
|
||||
queriesDirectory,
|
||||
functionsDirectory
|
||||
)
|
||||
|
||||
fun createRequester(): Requester {
|
||||
return initRequester(Requester(connection))
|
||||
}
|
||||
|
||||
private fun initRequester(req: Requester): Requester {
|
||||
if (queriesDirectory !== null) {
|
||||
req.addQuery(queriesDirectory)
|
||||
}
|
||||
|
||||
if (functionsDirectory !== null) {
|
||||
req.addFunction(functionsDirectory)
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
}
|
||||
|
||||
class NoFunctionDefined(name: String) : Exception("No function defined for $name")
|
||||
class NoQueryDefined(path: String) : Exception("No query defined in $path")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
interface ParameterI {
|
||||
val name: String
|
||||
val type: String
|
||||
@@ -21,7 +23,7 @@ class Parameter(val name: String, val type: String, direction: Direction? = Dire
|
||||
constructor(name: String, type: String, direction: String? = "IN", default: Any? = null) : this(
|
||||
name = name,
|
||||
type = type,
|
||||
direction = direction?.let { Direction.valueOf(direction.toUpperCase()) },
|
||||
direction = direction?.let { Direction.valueOf(direction.uppercase(Locale.getDefault())) },
|
||||
default = default
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.io.File
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
|
||||
interface Resource {
|
||||
sealed interface Resource {
|
||||
val name: String
|
||||
val script: String
|
||||
var source: Path?
|
||||
@@ -34,7 +34,3 @@ interface Resource {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ResourceCollection {
|
||||
val parameters: List<Parameter>
|
||||
}
|
||||
|
||||
@@ -24,6 +24,27 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `requester constructor function directory`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val name: String = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `requester constructor query directory`() {
|
||||
val resources = this::class.java.getResource("/sql/query/Test").toURI()
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.name
|
||||
|
||||
assertEquals("DeleteTest", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add function as string`() {
|
||||
val sql = """
|
||||
|
||||
Reference in New Issue
Block a user