Add ShadowJar compatibility and Query file can be define name with comment
Add Migration and Query definition class Add Docker DB for tests
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
open class Function(
|
||||
override val script: String
|
||||
class Function(
|
||||
override val script: String,
|
||||
override var source: Path? = null
|
||||
) : Resource, ParametersInterface {
|
||||
val returns: String
|
||||
override val name: String
|
||||
override val parameters: List<Parameter>
|
||||
override var source: File? = null
|
||||
|
||||
init {
|
||||
val functionRegex =
|
||||
@@ -46,8 +47,7 @@ open class Function(
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null) : ParseException("Function not found in script", cause)
|
||||
class FunctionNotFound(cause: Throwable? = null) : Resource.ParseException("Function not found in script", cause)
|
||||
|
||||
fun getDefinition(): String {
|
||||
return parameters
|
||||
|
||||
35
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
35
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Migration(
|
||||
override val script: String,
|
||||
source: Path
|
||||
) : Resource {
|
||||
override val name: String
|
||||
val direction: Direction
|
||||
override var source: Path? = null
|
||||
|
||||
init {
|
||||
this.source = source
|
||||
this.direction = source.fileName.toString()
|
||||
.let {
|
||||
when {
|
||||
it.endsWith(".down.sql") -> Direction.DOWN
|
||||
it.endsWith(".up.sql") -> Direction.UP
|
||||
else -> throw MigrationNotFound()
|
||||
}
|
||||
}
|
||||
this.name = source.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.let {
|
||||
when (direction) {
|
||||
Direction.DOWN -> it.substringBefore(".down.sql")
|
||||
Direction.UP -> it.substringBefore(".up.sql")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MigrationNotFound(cause: Throwable? = null) : Resource.ParseException("Migration not found in script", cause)
|
||||
enum class Direction { UP, DOWN }
|
||||
}
|
||||
25
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
25
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Query(
|
||||
override val script: String,
|
||||
source: Path
|
||||
) : Resource {
|
||||
override var source: Path? = source
|
||||
override val name: String = getNameFromComment(script) ?: getNameFromFile(source)
|
||||
|
||||
/** Try to get name from comment in file */
|
||||
private fun getNameFromComment(script: String): String? =
|
||||
"""-- *name ?: ?(?<name>[^ \n]+)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
.find(script)?.let {
|
||||
it.groups["name"]?.value?.trim()
|
||||
}
|
||||
|
||||
/** Try to get name from the filename */
|
||||
private fun getNameFromFile(source: Path): String = source
|
||||
.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.substringBeforeLast(".sql")
|
||||
}
|
||||
@@ -1,11 +1,38 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
|
||||
interface Resource {
|
||||
val name: String
|
||||
val script: String
|
||||
var source: File?
|
||||
var 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 {
|
||||
Function(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Migration(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Query(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
throw ParseException("No SQL resource found")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ResourceCollection {
|
||||
|
||||
Reference in New Issue
Block a user