Files
postgres-json/src/main/kotlin/fr/postgresjson/definition/Resource.kt

38 lines
1.1 KiB
Kotlin

package fr.postgresjson.definition
import fr.postgresjson.definition.parse.parseFunction
import java.io.File
import java.net.URL
import java.nio.file.Path
sealed interface Resource {
val name: String
val script: String
val source: Path?
open class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
companion object {
fun build(file: File): Resource =
build(file.readText(), Path.of(file.toURI()))
fun build(url: URL): Resource =
build(url.readText(), Path.of(url.toURI()))
fun build(resource: String, path: Path): Resource =
try {
Migration(resource, path)
} catch (e: ParseException) {
try {
parseFunction(resource, path)
} catch (e: ParseException) {
try {
Query(resource, path)
} catch (e: ParseException) {
throw ParseException("No SQL resource found")
}
}
}
}
}