Skip to content

Commit

Permalink
Combine all Expressions and QueryComponents in respective files
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 2, 2024
1 parent df01eb9 commit 9498884
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 278 deletions.
2 changes: 0 additions & 2 deletions src/main/kotlin/ch/icken/processor/ColumnType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

@file:Suppress("unused")

package ch.icken.processor

import kotlin.annotation.AnnotationRetention.SOURCE
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/ch/icken/processor/ProcessorCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package ch.icken.processor

import ch.icken.query.*
import ch.icken.query.Column
import ch.icken.query.Expression
import ch.icken.query.PanacheSingleResult
import ch.icken.query.QueryComponent
import ch.icken.query.QueryComponent.LogicalQueryComponent.AndQueryComponent
import ch.icken.query.QueryComponent.LogicalQueryComponent.OrQueryComponent
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.asClassName
import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanionBase
Expand Down Expand Up @@ -51,14 +55,14 @@ abstract class ProcessorCommon(options: Map<String, String>) {

companion object {
//region Class Names
val AndQueryComponentClassName = LogicalQueryComponent.AndQueryComponent::class.asClassName()
val AndQueryComponentClassName = AndQueryComponent::class.asClassName()
val ColumnClassName = Column::class.asClassName()
val ExpressionClassName = Expression::class.asClassName()
val GeneratedClassName = Generated::class.asClassName()
val JvmNameClassName = JvmName::class.asClassName()
val ListClassName = List::class.asClassName()
val LongClassName = Long::class.asClassName()
val OrQueryComponentClassName = LogicalQueryComponent.OrQueryComponent::class.asClassName()
val OrQueryComponentClassName = OrQueryComponent::class.asClassName()
val PanacheQueryClassName = PanacheQuery::class.asClassName()
val PanacheSingleResultClassName = PanacheSingleResult::class.asClassName()
val QueryComponentClassName = QueryComponent::class.asClassName()
Expand Down
106 changes: 0 additions & 106 deletions src/main/kotlin/ch/icken/query/BooleanExpression.kt

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/kotlin/ch/icken/query/Column.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

package ch.icken.query

import ch.icken.query.BooleanExpression.BetweenExpression.Between
import ch.icken.query.BooleanExpression.BetweenExpression.NotBetween
import ch.icken.query.BooleanExpression.BooleanValueExpression.*
import ch.icken.query.BooleanExpression.IsExpression.IsNotNull
import ch.icken.query.BooleanExpression.IsExpression.IsNull
import ch.icken.query.Expression.BooleanExpression.BetweenExpression.Between
import ch.icken.query.Expression.BooleanExpression.BetweenExpression.NotBetween
import ch.icken.query.Expression.BooleanExpression.BooleanValueExpression.*
import ch.icken.query.Expression.BooleanExpression.IsExpression.IsNotNull
import ch.icken.query.Expression.BooleanExpression.IsExpression.IsNull

class Column<Columns, T : Any?>(internal val name: String)

Expand Down
124 changes: 120 additions & 4 deletions src/main/kotlin/ch/icken/query/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,134 @@

package ch.icken.query

import kotlin.random.Random

sealed class Expression<Columns> {
//region Chaining operations
fun and(expression: Expression<Columns>) = LogicalExpression.AndExpression(this, expression)
fun or(expression: Expression<Columns>) = LogicalExpression.OrExpression(this, expression)
//endregion

//region compile
internal fun compile() = when (this) {
is BooleanExpression -> compileExpression()
is LogicalExpression -> {
val compiledExpression = compileExpression()
Compiled("(${compiledExpression.expression})", compiledExpression.parameters)
}
}

internal abstract fun compileExpression(): Compiled
protected abstract fun compileExpression(): Compiled
data class Compiled internal constructor(val expression: String, val parameters: Map<String, Any>)
//endregion

fun and(expression: Expression<Columns>) = LogicalExpression.AndExpression(this, expression)
fun or(expression: Expression<Columns>) = LogicalExpression.OrExpression(this, expression)
sealed class BooleanExpression<Columns> private constructor(
protected val key: String,
protected val operator: String
) : Expression<Columns>() {
companion object {
private const val CHARS = //"0123456789" +
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

protected fun generateParameterName() = (0 ..< 8)
.map { CHARS[Random.nextInt(CHARS.length)] }
.toCharArray().concatToString()
}

sealed class BooleanValueExpression<Columns> private constructor(
key: String,
operator: String,
private val value: Any
) : BooleanExpression<Columns>(key, operator) {
private val parameterName: String = generateParameterName()

override fun compileExpression() = Compiled(
expression = "$key $operator :$parameterName",
parameters = mapOf(parameterName to value)
)

class EqualTo<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, "=", value)
class NotEqualTo<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, "!=", value)
class LessThan<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, "<", value)
class GreaterThan<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, ">", value)
class LessThanOrEqualTo<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, "<=", value)
class GreaterThanOrEqualTo<Columns> internal constructor(key: String, value: Any) :
BooleanValueExpression<Columns>(key, ">=", value)

class In<Columns> internal constructor(key: String, values: Collection<Any>) :
BooleanValueExpression<Columns>(key, "IN", values)
class NotIn<Columns> internal constructor(key: String, values: Collection<Any>) :
BooleanValueExpression<Columns>(key, "NOT IN", values)

class Like<Columns> internal constructor(key: String, expression: String) :
BooleanValueExpression<Columns>(key, "LIKE", expression)
class NotLike<Columns> internal constructor(key: String, expression: String) :
BooleanValueExpression<Columns>(key, "NOT LIKE", expression)

}

sealed class BetweenExpression<Columns> private constructor(
key: String,
operator: String,
private val min: Any,
private val maxIncl: Any
) : BooleanExpression<Columns>(key, operator) {
private val minParameterName: String = generateParameterName()
private val maxInclParameterName: String = generateParameterName()

override fun compileExpression() = Compiled(
expression = "$key $operator :$minParameterName AND :$maxInclParameterName",
parameters = mapOf(minParameterName to min, maxInclParameterName to maxIncl)
)

class Between<Columns> internal constructor(key: String, min: Any, maxIncl: Any) :
BetweenExpression<Columns>(key, "BETWEEN", min, maxIncl)
class NotBetween<Columns> internal constructor(key: String, min: Any, maxIncl: Any) :
BetweenExpression<Columns>(key, "NOT BETWEEN", min, maxIncl)
}

sealed class IsExpression<Columns> private constructor(
key: String,
operator: String
) : BooleanExpression<Columns>(key, operator) {
override fun compileExpression() = Compiled(
expression = "$key $operator NULL",
parameters = emptyMap()
)

class IsNull<Columns> internal constructor(key: String) :
IsExpression<Columns>(key, "IS")
class IsNotNull<Columns> internal constructor(key: String) :
IsExpression<Columns>(key, "IS NOT")
}
}

sealed class LogicalExpression<Columns> private constructor(
private val previous: Expression<Columns>,
private val operator: String,
private val expression: Expression<Columns>
) : Expression<Columns>() {
override fun compileExpression(): Compiled {
val compiledPrevious = previous.compileExpression()
val compiledExpression = expression.compileExpression()
return Compiled(
expression = "${compiledPrevious.expression} $operator ${compiledExpression.expression}",
parameters = compiledPrevious.parameters + compiledExpression.parameters
)
}

class AndExpression<Columns> internal constructor(
previous: Expression<Columns>,
expression: Expression<Columns>
) : LogicalExpression<Columns>(previous, "AND", expression)
class OrExpression<Columns> internal constructor(
previous: Expression<Columns>,
expression: Expression<Columns>
) : LogicalExpression<Columns>(previous, "OR", expression)
}
}
29 changes: 0 additions & 29 deletions src/main/kotlin/ch/icken/query/InitialQueryComponent.kt

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/kotlin/ch/icken/query/LogicalExpression.kt

This file was deleted.

Loading

0 comments on commit 9498884

Please sign in to comment.