WIP: Compiled SQL function #33
@@ -1,6 +1,7 @@
|
|||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
val containerAlwaysOn: String by project
|
val containerAlwaysOn: String by project
|
||||||
val disableLint: String by project
|
val disableLint: String by project
|
||||||
|
val projectName = "postgres-json"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
jacoco
|
jacoco
|
||||||
@@ -86,7 +87,7 @@ val sourcesJar by tasks.creating(Jar::class) {
|
|||||||
|
|
||||||
apply(plugin = "docker-compose")
|
apply(plugin = "docker-compose")
|
||||||
dockerCompose {
|
dockerCompose {
|
||||||
setProjectName("postgres-json")
|
setProjectName(projectName)
|
||||||
setProperty("useComposeFiles", listOf("docker-compose.yml"))
|
setProperty("useComposeFiles", listOf("docker-compose.yml"))
|
||||||
setProperty("stopContainers", !containerAlwaysOn.toBoolean())
|
setProperty("stopContainers", !containerAlwaysOn.toBoolean())
|
||||||
isRequiredBy(project.tasks.test)
|
isRequiredBy(project.tasks.test)
|
||||||
@@ -95,7 +96,7 @@ dockerCompose {
|
|||||||
publishing {
|
publishing {
|
||||||
repositories {
|
repositories {
|
||||||
maven {
|
maven {
|
||||||
name = "postgres-json"
|
name = projectName
|
||||||
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
|
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
|
||||||
credentials {
|
credentials {
|
||||||
username = System.getenv("GITHUB_USERNAME")
|
username = System.getenv("GITHUB_USERNAME")
|
||||||
@@ -105,7 +106,7 @@ publishing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
publications {
|
publications {
|
||||||
create<MavenPublication>("postgres-json") {
|
create<MavenPublication>(projectName) {
|
||||||
from(components["java"])
|
from(components["java"])
|
||||||
artifact(sourcesJar)
|
artifact(sourcesJar)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,6 @@ import kotlin.jvm.Throws
|
|||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.reflect.full.hasAnnotation
|
import kotlin.reflect.full.hasAnnotation
|
||||||
|
|
||||||
// TODO move execute function outside
|
|
||||||
// TODO create function executeNullable
|
|
||||||
|
|
||||||
class Connection(
|
class Connection(
|
||||||
private val database: String,
|
private val database: String,
|
||||||
private val username: String,
|
private val username: String,
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ internal fun ScriptPart.removeParentheses(): ScriptPart {
|
|||||||
this
|
this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get next part of script.
|
* Get next part of script.
|
||||||
* You can define a list of characters that end the part of script. Like `(` or space.
|
* You can define a list of characters that end the part of script. Like `(` or space.
|
||||||
@@ -36,35 +37,26 @@ internal fun ScriptPart.removeParentheses(): ScriptPart {
|
|||||||
internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }): NextScript<String> {
|
internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }): NextScript<String> {
|
||||||
val status = Status()
|
val status = Status()
|
||||||
|
|
||||||
fun String.unescape(): String {
|
for ((index, c) in restOfScript.withIndex()) {
|
||||||
val first = take(1)
|
val prevChar = restOfScript.getOrNull(index - 1)
|
||||||
val last = takeLast(1)
|
val nextChar = restOfScript.getOrNull(index + 1)
|
||||||
return if (first == last && first in listOf("\"", "'")) {
|
val nestedChars = listOf(prevChar, nextChar)
|
||||||
drop(1).dropLast(1).replace("$first$first", first)
|
|
||||||
} else {
|
if (c == '"' && nestedChars.none { c == it }) {
|
||||||
this
|
status.doubleQuoted = !status.doubleQuoted
|
||||||
}
|
} else if (c == '\'' && nestedChars.none { c == it }) {
|
||||||
|
status.simpleQuoted = !status.simpleQuoted
|
||||||
}
|
}
|
||||||
|
|
||||||
for ((index, c) in restOfScript.withIndex()) {
|
if (status.isNotQuoted()) {
|
||||||
val nextChar = restOfScript.getOrNull(index + 1)
|
when (c) {
|
||||||
val prevChar = restOfScript.getOrNull(index - 1)
|
'(' -> status.parentheses++
|
||||||
if (c == '"' && (nextChar != '"' && prevChar != '"')) {
|
')' -> status.parentheses--
|
||||||
status.doubleQuoted = !status.doubleQuoted
|
'[' -> status.brackets++
|
||||||
} else if (c == '\'' && (nextChar != '\'' && prevChar != '\'')) {
|
']' -> status.brackets--
|
||||||
status.simpleQuoted = !status.simpleQuoted
|
'{' -> status.braces++
|
||||||
} else if (c == '(' && status.isNotQuoted()) {
|
'}' -> status.braces--
|
||||||
status.parentheses++
|
}
|
||||||
} else if (c == ')' && status.isNotQuoted()) {
|
|
||||||
status.parentheses--
|
|
||||||
} else if (c == '[' && status.isNotQuoted()) {
|
|
||||||
status.brackets++
|
|
||||||
} else if (c == ']' && status.isNotQuoted()) {
|
|
||||||
status.brackets--
|
|
||||||
} else if (c == '{' && status.isNotQuoted()) {
|
|
||||||
status.braces++
|
|
||||||
} else if (c == '}' && status.isNotQuoted()) {
|
|
||||||
status.braces--
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isEnd(Context(index, c, status.copy(), restOfScript))) {
|
if (isEnd(Context(index, c, status.copy(), restOfScript))) {
|
||||||
@@ -72,11 +64,21 @@ internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (status.isNotEscaped()) {
|
if (status.isNotEscaped()) {
|
||||||
return NextScript(restOfScript.unescape().trim(), "").trimSpace()
|
return NextScript(restOfScript.trim(), "").trimSpace()
|
||||||
}
|
}
|
||||||
throw ParseError()
|
throw ParseError()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun String.unescape(): String {
|
||||||
|
val first = take(1)
|
||||||
|
val last = takeLast(1)
|
||||||
|
return if (first == last && first in listOf("\"", "'")) {
|
||||||
|
drop(1).dropLast(1).replace("$first$first", first)
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal fun <T> NextScript<T>.trimSpace(): NextScript<T> {
|
internal fun <T> NextScript<T>.trimSpace(): NextScript<T> {
|
||||||
val spaces = charArrayOf(' ', '\n', '\t')
|
val spaces = charArrayOf(' ', '\n', '\t')
|
||||||
return trim(chars = spaces)
|
return trim(chars = spaces)
|
||||||
|
|||||||
@@ -117,8 +117,8 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
|||||||
|
|
||||||
val functionDecl = if (generics.isNotEmpty()) "inline fun <${generics.joinToString(", ")}>" else "fun"
|
val functionDecl = if (generics.isNotEmpty()) "inline fun <${generics.joinToString(", ")}>" else "fun"
|
||||||
|
|
||||||
if (hasReturn) {
|
return if (hasReturn) {
|
||||||
return """
|
"""
|
||||||
|package fr.postgresjson.functionGenerator.generated
|
|package fr.postgresjson.functionGenerator.generated
|
||||||
|
|
|
|
||||||
|import com.fasterxml.jackson.core.type.TypeReference
|
|import com.fasterxml.jackson.core.type.TypeReference
|
||||||
@@ -130,7 +130,7 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
|||||||
|}
|
|}
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
} else {
|
} else {
|
||||||
return """
|
"""
|
||||||
|package fr.postgresjson.functionGenerator.generated
|
|package fr.postgresjson.functionGenerator.generated
|
||||||
|
|
|
|
||||||
|import fr.postgresjson.connexion.Requester
|
|import fr.postgresjson.connexion.Requester
|
||||||
|
|||||||
Reference in New Issue
Block a user