refactoring: cleanup and reformat
This commit is contained in:
@@ -3,20 +3,22 @@ package fr.postgresjson.definition
|
||||
import java.io.File
|
||||
|
||||
|
||||
open class Function (
|
||||
open class Function(
|
||||
override val script: String
|
||||
) : Resource, ParametersInterface {
|
||||
): Resource, ParametersInterface {
|
||||
val returns: String?
|
||||
override val name: String
|
||||
override val parameters: List<Parameter>
|
||||
override var source: File? = null
|
||||
|
||||
init {
|
||||
val functionRegex = """create (or replace )?(procedure|function) *(?<name>[^(\s]+)\s*\((?<params>(\s*((IN|OUT|INOUT|VARIADIC)?\s+)?([^\s,)]+\s+)?([^\s,)]+)(\s+(?:default\s|=)\s*[^\s,)]+)?\s*(,|(?=\))))*)\) *(?<return>RETURNS *[^ ]+)?"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
val functionRegex =
|
||||
"""create (or replace )?(procedure|function) *(?<name>[^(\s]+)\s*\((?<params>(\s*((IN|OUT|INOUT|VARIADIC)?\s+)?([^\s,)]+\s+)?([^\s,)]+)(\s+(?:default\s|=)\s*[^\s,)]+)?\s*(,|(?=\))))*)\) *(?<return>RETURNS *[^ ]+)?"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
|
||||
val paramsRegex = """\s*(?<param>((?<direction>IN|OUT|INOUT|VARIADIC)?\s+)?("?(?<name>[^\s,")]+)"?\s+)?(?<type>[^\s,)]+)(\s+(?<default>default\s|=)\s*[^\s,)]+)?)\s*(,|$)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
val paramsRegex =
|
||||
"""\s*(?<param>((?<direction>IN|OUT|INOUT|VARIADIC)?\s+)?("?(?<name>[^\s,")]+)"?\s+)?(?<type>[^\s,)]+)(\s+(?<default>default\s|=)\s*[^\s,)]+)?)\s*(,|$)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
|
||||
val queryMatch = functionRegex.find(script)
|
||||
if (queryMatch !== null) {
|
||||
@@ -32,7 +34,8 @@ open class Function (
|
||||
paramsMatch.groups["name"]!!.value.trim(),
|
||||
paramsMatch.groups["type"]!!.value.trim(),
|
||||
paramsMatch.groups["direction"]?.value?.trim(),
|
||||
paramsMatch.groups["default"]?.value?.trim())
|
||||
paramsMatch.groups["default"]?.value?.trim()
|
||||
)
|
||||
}.toList()
|
||||
} else {
|
||||
listOf()
|
||||
@@ -43,10 +46,11 @@ open class Function (
|
||||
throw FunctionNotFound()
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ParseException(message: String, cause: Throwable? = null): Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null): ParseException("Function not found in script", cause)
|
||||
|
||||
fun getDefinition (): String {
|
||||
fun getDefinition(): String {
|
||||
return "$name (" + parameters.joinToString(", ") + ") $returns"
|
||||
}
|
||||
|
||||
@@ -56,21 +60,28 @@ open class Function (
|
||||
}.toMap()
|
||||
}
|
||||
|
||||
infix fun `has same definition` (other: Function): Boolean {
|
||||
infix fun `has same definition`(other: Function): Boolean {
|
||||
return other.getDefinition() == this.getDefinition()
|
||||
}
|
||||
|
||||
infix fun `is same` (other: Function): Boolean {
|
||||
infix fun `is same`(other: Function): Boolean {
|
||||
return other.script == this.script
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun build(source: File): List<Function> {
|
||||
return source.readText()
|
||||
.split("CREATE +(OR REPLACE +)?(PROCEDURE|FUNCTION)".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)))
|
||||
.split(
|
||||
"CREATE +(OR REPLACE +)?(PROCEDURE|FUNCTION)".toRegex(
|
||||
setOf(
|
||||
RegexOption.IGNORE_CASE,
|
||||
RegexOption.MULTILINE
|
||||
)
|
||||
)
|
||||
)
|
||||
.map {
|
||||
Function("CREATE OR REPLACE FUNCTION $it")
|
||||
}
|
||||
Function("CREATE OR REPLACE FUNCTION $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user