WIP: Compiled SQL function #33

Draft
flecomte wants to merge 37 commits from compiled_sql_function into master
4 changed files with 35 additions and 35 deletions
Showing only changes of commit 1def5ae095 - Show all commits

View File

@@ -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)
} }

View File

@@ -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,

View File

@@ -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 {
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
}
}
for ((index, c) in restOfScript.withIndex()) { for ((index, c) in restOfScript.withIndex()) {
val nextChar = restOfScript.getOrNull(index + 1)
val prevChar = restOfScript.getOrNull(index - 1) val prevChar = restOfScript.getOrNull(index - 1)
if (c == '"' && (nextChar != '"' && prevChar != '"')) { val nextChar = restOfScript.getOrNull(index + 1)
val nestedChars = listOf(prevChar, nextChar)
if (c == '"' && nestedChars.none { c == it }) {
status.doubleQuoted = !status.doubleQuoted status.doubleQuoted = !status.doubleQuoted
} else if (c == '\'' && (nextChar != '\'' && prevChar != '\'')) { } else if (c == '\'' && nestedChars.none { c == it }) {
status.simpleQuoted = !status.simpleQuoted status.simpleQuoted = !status.simpleQuoted
} else if (c == '(' && status.isNotQuoted()) { }
status.parentheses++
} else if (c == ')' && status.isNotQuoted()) { if (status.isNotQuoted()) {
status.parentheses-- when (c) {
} else if (c == '[' && status.isNotQuoted()) { '(' -> status.parentheses++
status.brackets++ ')' -> status.parentheses--
} else if (c == ']' && status.isNotQuoted()) { '[' -> status.brackets++
status.brackets-- ']' -> status.brackets--
} else if (c == '{' && status.isNotQuoted()) { '{' -> status.braces++
status.braces++ '}' -> 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)

View File

@@ -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