Skip to content

Commit

Permalink
Initial update DSL setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 5, 2024
1 parent 40fe1c3 commit 825abf9
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 176 deletions.
61 changes: 34 additions & 27 deletions src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ class PanacheCompanionBaseProcessor(
.plusParameter(idClassName)
.plusParameter(columnsObjectClassName)

//region where
//region update, where
val update = FunSpec.builder(FUNCTION_NAME_UPDATE)
.addModifiers(KModifier.INLINE)
.receiver(companionClassName)
//TODO setter providers parameter
.returns(UpdateComponentClassName.plusParameter(className)
.plusParameter(idClassName).plusParameter(columnsObjectClassName))
.addStatement("return %M(%T)",
MemberName(UpdateComponentClassName.packageName, FUNCTION_NAME_UPDATE), columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_UPDATE$classSimpleName"))

val where = FunSpec.builder(FUNCTION_NAME_WHERE)
.addModifiers(KModifier.INLINE)
.receiver(companionClassName)
Expand All @@ -86,37 +96,18 @@ class PanacheCompanionBaseProcessor(
.addModifiers(KModifier.INLINE)
.receiver(queryComponentType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(AndQueryComponentClassName.plusParameter(className)
.plusParameter(idClassName).plusParameter(columnsObjectClassName))
.returns(queryComponentType)
.addStatement("return $FUNCTION_NAME_AND($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_AND$classSimpleName"))
val or = FunSpec.builder(FUNCTION_NAME_OR)
.addModifiers(KModifier.INLINE)
.receiver(queryComponentType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(OrQueryComponentClassName.plusParameter(className)
.plusParameter(idClassName).plusParameter(columnsObjectClassName))
.returns(queryComponentType)
.addStatement("return $FUNCTION_NAME_OR($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_OR$classSimpleName"))
//endregion

//region andExpression, orExpression
val andExpression = FunSpec.builder(FUNCTION_NAME_AND)
.addModifiers(KModifier.INLINE)
.receiver(expressionType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(expressionType)
.addStatement("return $FUNCTION_NAME_AND($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_AND_EXPRESSION$classSimpleName"))
val orExpression = FunSpec.builder(FUNCTION_NAME_OR)
.addModifiers(KModifier.INLINE)
.receiver(expressionType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(expressionType)
.addStatement("return $FUNCTION_NAME_OR($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_OR_EXPRESSION$classSimpleName"))
//endregion

//region count, delete, find, stream
val count = FunSpec.builder(FUNCTION_NAME_COUNT)
.addModifiers(KModifier.INLINE)
Expand Down Expand Up @@ -169,7 +160,7 @@ class PanacheCompanionBaseProcessor(
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_STREAM_SORTED$classSimpleName"))
//endregion

//region single
//region single, multiple
val single = FunSpec.builder(FUNCTION_NAME_SINGLE)
.addModifiers(KModifier.INLINE)
.receiver(companionClassName)
Expand All @@ -184,9 +175,7 @@ class PanacheCompanionBaseProcessor(
.returns(PanacheSingleResultClassName.plusParameter(WildcardTypeName.producerOf(className)))
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).getSingleSafe()")
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_SINGLE_SAFE$classSimpleName"))
//endregion

//region multiple
val multipleReturns = ListClassName.plusParameter(className)
val multiple = FunSpec.builder(FUNCTION_NAME_MULTIPLE)
.addModifiers(KModifier.INLINE)
Expand All @@ -205,9 +194,27 @@ class PanacheCompanionBaseProcessor(
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_MULTIPLE_SORTED$classSimpleName"))
//endregion

val functions = listOf(where, and, or, andExpression, orExpression,
//region andExpression, orExpression
val andExpression = FunSpec.builder(FUNCTION_NAME_AND)
.addModifiers(KModifier.INLINE)
.receiver(expressionType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(expressionType)
.addStatement("return $FUNCTION_NAME_AND($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_AND_EXPRESSION$classSimpleName"))
val orExpression = FunSpec.builder(FUNCTION_NAME_OR)
.addModifiers(KModifier.INLINE)
.receiver(expressionType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(expressionType)
.addStatement("return $FUNCTION_NAME_OR($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_OR_EXPRESSION$classSimpleName"))
//endregion

val functions = listOf(update, where, and, or,
count, delete, find, findSorted, stream, streamSorted,
single, singleSafe, multiple, multipleSorted)
single, singleSafe, multiple, multipleSorted,
andExpression, orExpression)

FileSpec.builder(packageName, extensionFileName)
.apply {
Expand Down
60 changes: 27 additions & 33 deletions src/main/kotlin/ch/icken/processor/ProcessorCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package ch.icken.processor

import ch.icken.query.Column
import ch.icken.query.Component.QueryComponent
import ch.icken.query.Component.UpdateComponent
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 All @@ -35,9 +34,10 @@ import java.util.stream.Stream
abstract class ProcessorCommon(options: Map<String, String>) {

//region Options
val addGeneratedAnnotation = options[OPTION_ADD_GENERATED_ANNOTATION].toBoolean()
protected val addGeneratedAnnotation = options[OPTION_ADD_GENERATED_ANNOTATION].toBoolean()
//endregion

//region Annotations
protected val suppressFileAnnotation = AnnotationSpec.builder(SuppressClassName)
.addMember("%S", "RedundantVisibilityModifier")
.addMember("%S", "unused")
Expand All @@ -52,61 +52,55 @@ abstract class ProcessorCommon(options: Map<String, String>) {
protected fun jvmNameAnnotation(name: String) = AnnotationSpec.builder(JvmNameClassName)
.addMember("%S", name)
.build()
//endregion

companion object {
//region Class Names
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 = OrQueryComponent::class.asClassName()
val PanacheQueryClassName = PanacheQuery::class.asClassName()
val PanacheSingleResultClassName = PanacheSingleResult::class.asClassName()
val QueryComponentClassName = QueryComponent::class.asClassName()
val SetterClassName = UpdateComponent.Setter::class.asClassName()
val SortClassName = Sort::class.asClassName()
val StreamClassName = Stream::class.asClassName()
val StringClassName = String::class.asClassName()
val SuppressClassName = Suppress::class.asClassName()
val UpdateComponentClassName = UpdateComponent::class.asClassName()
//endregion
//region Constants
//Common
const val SUFFIX_PACKAGE_GENERATED = ".generated"
const val PARAM_NAME_MAPPED_BY = "mappedBy"
const val PARAM_NAME_TYPE = "type"

//PanacheEntityBaseProcessor
const val SUFFIX_OBJECT_COLUMNS = "Columns"
const val SUFFIX_CLASS_COLUMNS_BASE = "Base"
const val TYPE_VARIABLE_NAME_COLUMNS = "Columns"
const val PARAM_NAME_COLUMNS_BASE_CLASS = "parent"

//PanacheCompanionBaseProcessor
const val SUFFIX_FILE_EXTENSIONS = "Extensions"
const val CLASS_NAME_COMPANION = "Companion"
const val PARAM_NAME_EXPRESSION = "expression"
const val PARAM_NAME_SORT = "sort"

const val FUNCTION_NAME_WHERE = "where"
private const val EXPRESSION = "Expression"
const val FUNCTION_NAME_AND = "and"
const val FUNCTION_NAME_AND_EXPRESSION = "$FUNCTION_NAME_AND$EXPRESSION"
const val FUNCTION_NAME_OR = "or"
const val FUNCTION_NAME_OR_EXPRESSION = "$FUNCTION_NAME_OR$EXPRESSION"

const val FUNCTION_NAME_AND_EXPRESSION = "andExpression"
const val FUNCTION_NAME_COUNT = "count"
const val FUNCTION_NAME_DELETE = "delete"
const val FUNCTION_NAME_FIND = "find"
const val FUNCTION_NAME_FIND_SORTED = "findSorted"
const val FUNCTION_NAME_STREAM = "stream"
const val FUNCTION_NAME_STREAM_SORTED = "streamSorted"

const val FUNCTION_NAME_SINGLE = "single"
const val FUNCTION_NAME_SINGLE_SAFE = "singleSafe"
const val FUNCTION_NAME_MULTIPLE = "multiple"
const val FUNCTION_NAME_MULTIPLE_SORTED = "multipleSorted"
const val FUNCTION_NAME_OR = "or"
const val FUNCTION_NAME_OR_EXPRESSION = "orExpression"
const val FUNCTION_NAME_SINGLE = "single"
const val FUNCTION_NAME_SINGLE_SAFE = "singleSafe"
const val FUNCTION_NAME_STREAM = "stream"
const val FUNCTION_NAME_STREAM_SORTED = "streamSorted"
const val FUNCTION_NAME_UPDATE = "update"
const val FUNCTION_NAME_WHERE = "where"
const val PARAM_NAME_COLUMNS_BASE_CLASS = "parent"
const val PARAM_NAME_EXPRESSION = "expression"
const val PARAM_NAME_MAPPED_BY = "mappedBy"
const val PARAM_NAME_SETTERS = "setters"
const val PARAM_NAME_SORT = "sort"
const val PARAM_NAME_TYPE = "type"
const val SUFFIX_CLASS_COLUMNS_BASE = "Base"
const val SUFFIX_FILE_EXTENSIONS = "Extensions"
const val SUFFIX_OBJECT_COLUMNS = "Columns"
const val SUFFIX_PACKAGE_GENERATED = ".generated"
const val TYPE_VARIABLE_NAME_COLUMNS = "Columns"
//endregion
//region Names
val HibernatePanacheCompanionBase: String = PanacheCompanionBase::class.java.name
Expand Down
36 changes: 18 additions & 18 deletions src/main/kotlin/ch/icken/query/Column.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,85 +27,85 @@ import ch.icken.query.Expression.BooleanExpression.IsExpression.IsNull
class Column<Columns, T : Any?>(internal val name: String)

//region eq
private fun <Columns> eq(name: String, value: Any?) =
if (value == null) IsNull(name) else EqualTo<Columns>(name, value)
private fun <Columns> eq(name: String, value: Any?): Expression<Columns> =
if (value == null) IsNull(name) else EqualTo(name, value)
@JvmName("eq")
infix fun <Columns, T : Any> Column<Columns, T>.eq(value: T) = eq<Columns>(name, value)
@JvmName("eqNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.eq(value: T?) = eq<Columns>(name, value)
//endregion
//region neq
private fun <Columns> neq(name: String, value: Any?) =
if (value == null) IsNotNull(name) else NotEqualTo<Columns>(name, value)
private fun <Columns> neq(name: String, value: Any?): Expression<Columns> =
if (value == null) IsNotNull(name) else NotEqualTo(name, value)
@JvmName("neq")
infix fun <Columns, T : Any> Column<Columns, T>.neq(value: T) = neq<Columns>(name, value)
@JvmName("neqNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.neq(value: T?) = neq<Columns>(name, value)
//endregion
//region lt
private fun <Columns> lt(name: String, value: Any) = LessThan<Columns>(name, value)
private fun <Columns> lt(name: String, value: Any): Expression<Columns> = LessThan(name, value)
@JvmName("lt")
infix fun <Columns, T : Any> Column<Columns, T>.lt(value: T) = lt<Columns>(name, value)
@JvmName("ltNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.lt(value: T) = lt<Columns>(name, value)
//endregion
//region gt
private fun <Columns> gt(name: String, value: Any) = GreaterThan<Columns>(name, value)
private fun <Columns> gt(name: String, value: Any): Expression<Columns> = GreaterThan(name, value)
@JvmName("gt")
infix fun <Columns, T : Any> Column<Columns, T>.gt(value: T) = gt<Columns>(name, value)
@JvmName("gtNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.gt(value: T) = gt<Columns>(name, value)
//endregion
//region lte
private fun <Columns> lte(name: String, value: Any) = LessThanOrEqualTo<Columns>(name, value)
private fun <Columns> lte(name: String, value: Any): Expression<Columns> = LessThanOrEqualTo(name, value)
@JvmName("lte")
infix fun <Columns, T : Any> Column<Columns, T>.lte(value: T) = lte<Columns>(name, value)
@JvmName("lteNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.lte(value: T) = lte<Columns>(name, value)
//endregion
//region gte
private fun <Columns> gte(name: String, value: Any) = GreaterThanOrEqualTo<Columns>(name, value)
private fun <Columns> gte(name: String, value: Any): Expression<Columns> = GreaterThanOrEqualTo(name, value)
@JvmName("gte")
infix fun <Columns, T : Any> Column<Columns, T>.gte(value: T) = gte<Columns>(name, value)
@JvmName("gteNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.gte(value: T) = gte<Columns>(name, value)
//endregion

//region in
private fun <Columns> `in`(name: String, values: Collection<Any>) = In<Columns>(name, values)
private fun <Columns> `in`(name: String, values: Collection<Any>): Expression<Columns> = In(name, values)
@JvmName("in")
infix fun <Columns, T : Any> Column<Columns, T>.`in`(values: Collection<T>) = `in`<Columns>(name, values)
@JvmName("inNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.`in`(values: Collection<T>) = `in`<Columns>(name, values)
//endregion
//region notIn
private fun <Columns> notIn(name: String, values: Collection<Any>) = NotIn<Columns>(name, values)
private fun <Columns> notIn(name: String, values: Collection<Any>): Expression<Columns> = NotIn(name, values)
@JvmName("notIn")
infix fun <Columns, T : Any> Column<Columns, T>.notIn(values: Collection<T>) = notIn<Columns>(name, values)
@JvmName("notInNullable")
infix fun <Columns, T : Any> Column<Columns, T?>.notIn(values: Collection<T>) = notIn<Columns>(name, values)
//endregion

//region like
private fun <Columns> like(name: String, expression: String?) =
if (expression == null) IsNull(name) else Like<Columns>(name, expression)
private fun <Columns> like(name: String, expression: String?): Expression<Columns> =
if (expression == null) IsNull(name) else Like(name, expression)
@JvmName("like")
infix fun <Columns> Column<Columns, String>.like(expression: String) = like<Columns>(name, expression)
@JvmName("likeNullable")
infix fun <Columns> Column<Columns, String?>.like(expression: String?) = like<Columns>(name, expression)
//endregion
//region notLike
private fun <Columns> notLike(name: String, expression: String?) =
if (expression == null) IsNotNull(name) else NotLike<Columns>(name, expression)
private fun <Columns> notLike(name: String, expression: String?): Expression<Columns> =
if (expression == null) IsNotNull(name) else NotLike(name, expression)
@JvmName("notLike")
infix fun <Columns> Column<Columns, String>.notLike(expression: String) = notLike<Columns>(name, expression)
@JvmName("notLikeNullable")
infix fun <Columns> Column<Columns, String?>.notLike(expression: String?) = notLike<Columns>(name, expression)
//endregion

//region between
private fun <Columns> between(name: String, min: Any?, maxIncl: Any?) = when {
min != null && maxIncl != null -> Between<Columns>(name, min, maxIncl)
private fun <Columns> between(name: String, min: Any?, maxIncl: Any?): Expression<Columns> = when {
min != null && maxIncl != null -> Between(name, min, maxIncl)
min != null && maxIncl == null -> GreaterThanOrEqualTo(name, min)
min == null && maxIncl != null -> LessThanOrEqualTo(name, maxIncl)
else -> IsNull(name)
Expand All @@ -116,8 +116,8 @@ fun <Columns, T : Any> Column<Columns, T>.between(min: T, maxIncl: T) = between<
fun <Columns, T : Any> Column<Columns, T?>.between(min: T?, maxIncl: T?) = between<Columns>(name, min, maxIncl)
//endregion
//region notBetween
private fun <Columns> notBetween(name: String, min: Any?, maxIncl: Any?) = when {
min != null && maxIncl != null -> NotBetween<Columns>(name, min, maxIncl)
private fun <Columns> notBetween(name: String, min: Any?, maxIncl: Any?): Expression<Columns> = when {
min != null && maxIncl != null -> NotBetween(name, min, maxIncl)
min != null && maxIncl == null -> LessThan(name, min)
min == null && maxIncl != null -> GreaterThan(name, maxIncl)
else -> IsNotNull(name)
Expand Down
Loading

0 comments on commit 825abf9

Please sign in to comment.