com.dzikoysk.sqiffy.dsl.generator.dialects.GenericQueryGenerator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqiffy Show documentation
Show all versions of sqiffy Show documentation
Core implementation of Sqiffy specification
package com.dzikoysk.sqiffy.dsl.generator.dialects
import com.dzikoysk.sqiffy.dsl.*
import com.dzikoysk.sqiffy.dsl.generator.ArgumentType.VALUE
import com.dzikoysk.sqiffy.dsl.generator.Arguments
import com.dzikoysk.sqiffy.dsl.generator.ExpressionColumns
import com.dzikoysk.sqiffy.dsl.generator.ParameterAllocator
import com.dzikoysk.sqiffy.dsl.generator.SqlQueryGenerator
import com.dzikoysk.sqiffy.dsl.generator.SqlQueryGenerator.GeneratorResult
import com.dzikoysk.sqiffy.dsl.statements.Join
import com.dzikoysk.sqiffy.dsl.statements.JoinType.FULL
import com.dzikoysk.sqiffy.dsl.statements.JoinType.INNER
import com.dzikoysk.sqiffy.dsl.statements.JoinType.LEFT
import com.dzikoysk.sqiffy.dsl.statements.JoinType.RIGHT
import com.dzikoysk.sqiffy.dsl.statements.OrderBy
import com.dzikoysk.sqiffy.shared.multiline
abstract class GenericQueryGenerator : SqlQueryGenerator {
protected open fun Selectable.toIdentifier(): String =
when (this) {
is Column<*> -> "${table.getName().toQuoted()}.${name.toQuoted()}"
is Aggregation<*> -> "$aggregationFunction(${quotedIdentifier.toString(quoteType())})"
else -> throw IllegalArgumentException("Unknown selectable type: $javaClass")
}
protected fun ExpressionColumns.toUpdateValues(): String =
this
.map { (column, expression) -> "${column.name.toQuoted()} = $expression" }
.joinToString(separator = ", ")
override fun createSelectQuery(
tableName: String,
distinct: Boolean,
selected: List,
where: String?,
joins: List,
joinsExpressions: Map, String>,
groupBy: List>?,
having: String?,
orderBy: List?,
limit: Int?,
offset: Int?,
): GeneratorResult =
GeneratorResult(
query = multiline("""
SELECT ${if (distinct) "DISTINCT" else ""} ${
selected.joinToString(separator = ", ") {
it.toIdentifier() + " AS " + when (it) {
is Column<*> -> "${it.table.getName()}.${it.name}".toQuoted()
is Aggregation<*> -> ("${it.aggregationFunction}(${it.rawIdentifier})").toQuoted()
else -> throw IllegalArgumentException("Unknown selectable type: $javaClass")
}
}
}
FROM ${tableName.toQuoted()}
${
joins.joinToString(separator = " ") { join ->
val joinType = when (join.type) {
INNER -> "INNER JOIN"
LEFT -> "LEFT JOIN"
RIGHT -> "RIGHT JOIN"
FULL -> "FULL JOIN"
}
"$joinType ${join.table.getName().toQuoted()} ON (${join.conditions.joinToString(separator = " AND ") {
"${it.on.quotedIdentifier.toString(quoteType())} = ${joinsExpressions[it.toExpression]}"
}})"
}
}
${where?.let { "WHERE $it" } ?: ""}
${groupBy?.let { "GROUP BY ${groupBy.joinToString(separator = ", ") { it.quotedIdentifier.toString(quoteType()) }}" } ?: ""}
${having?.let { "HAVING $it" } ?: ""}
${orderBy?.let { "ORDER BY ${orderBy.joinToString(separator = ", ") { "${it.selectable.toIdentifier()} ${it.order}" }}" } ?: ""}
${limit?.let { "LIMIT $it" } ?: ""}
${offset?.let { "OFFSET $it" } ?: ""}
""")
)
override fun createDeleteQuery(tableName: String, where: String?): GeneratorResult =
GeneratorResult(
query = multiline("""
DELETE FROM ${tableName.toQuoted()}
${where?.let { "WHERE $it" } ?: ""}
""")
)
override fun
© 2015 - 2024 Weber Informatics LLC | Privacy Policy