Skip to content

Commit

Permalink
Add extensions for single column queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Oct 14, 2023
1 parent 6231327 commit 0f662ff
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 16 deletions.
14 changes: 10 additions & 4 deletions library/src/main/kotlin/ch/icken/processor/ClassNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@

package ch.icken.processor

import ch.icken.query.BooleanExpression
import ch.icken.query.ColumnName
import ch.icken.query.LogicalQueryComponent
import ch.icken.query.QueryComponent
import ch.icken.query.*
import com.squareup.kotlinpoet.asClassName
import io.quarkus.hibernate.orm.panache.kotlin.PanacheQuery
import io.quarkus.panache.common.Sort
import java.util.stream.Stream

internal object ClassNames {
val AndQueryComponentClassName = LogicalQueryComponent.AndQueryComponent::class.asClassName()
val BooleanExpressionClassName = BooleanExpression::class.asClassName()
val ColumnNameClassName = ColumnName::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 PanacheQueryClassName = PanacheQuery::class.asClassName()
val PanacheSingleResultClassName = PanacheSingleResult::class.asClassName()
val QueryComponentClassName = QueryComponent::class.asClassName()
val SortClassName = Sort::class.asClassName()
val StreamClassName = Stream::class.asClassName()
val StringClassName = String::class.asClassName()
val SuppressClassName = Suppress::class.asClassName()
}
15 changes: 13 additions & 2 deletions library/src/main/kotlin/ch/icken/processor/GenerationValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ internal object GenerationValues {
const val EXTENSIONS_FILE = "QueryComponentExtensions"
const val EXPRESSION_PARAM_NAME = "expression"
const val GROUP_COMPONENT_PARAM_NAME = "groupComponent"

const val COMPANION_OBJECT = "Companion"
const val SORT_PARAM_NAME = "sort"

const val WHERE = "where"
const val AND = "and"
Expand All @@ -40,6 +39,18 @@ internal object GenerationValues {
const val AND_GROUP = "$AND$GROUP"
const val OR_GROUP = "$OR$GROUP"

const val COUNT = "count"
const val DELETE = "delete"
const val FIND = "find"
const val FIND_SORTED = "findSorted"
const val STREAM = "stream"
const val STREAM_SORTED = "streamSorted"

const val SINGLE = "single"
const val SINGLE_SAFE = "singleSafe"
const val MULTIPLE = "multiple"
const val MULTIPLE_SORTED = "multipleSorted"

val FileSuppress = AnnotationSpec.builder(SuppressClassName)
.addMember("%S", "RedundantVisibilityModifier")
.addMember("%S", "unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,37 @@ package ch.icken.processor
import ch.icken.processor.ClassNames.AndQueryComponentClassName
import ch.icken.processor.ClassNames.BooleanExpressionClassName
import ch.icken.processor.ClassNames.JvmNameClassName
import ch.icken.processor.ClassNames.ListClassName
import ch.icken.processor.ClassNames.LongClassName
import ch.icken.processor.ClassNames.OrQueryComponentClassName
import ch.icken.processor.ClassNames.PanacheQueryClassName
import ch.icken.processor.ClassNames.PanacheSingleResultClassName
import ch.icken.processor.ClassNames.QueryComponentClassName
import ch.icken.processor.ClassNames.SortClassName
import ch.icken.processor.ClassNames.StreamClassName
import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION
import ch.icken.processor.GenerationOptions.generatedAnnotation
import ch.icken.processor.GenerationValues.AND
import ch.icken.processor.GenerationValues.AND_GROUP
import ch.icken.processor.GenerationValues.COLUMN_NAME_OBJECT_SUFFIX
import ch.icken.processor.GenerationValues.COMPANION_OBJECT
import ch.icken.processor.GenerationValues.COUNT
import ch.icken.processor.GenerationValues.DELETE
import ch.icken.processor.GenerationValues.EXPRESSION_PARAM_NAME
import ch.icken.processor.GenerationValues.EXTENSIONS_FILE
import ch.icken.processor.GenerationValues.FIND
import ch.icken.processor.GenerationValues.FIND_SORTED
import ch.icken.processor.GenerationValues.FileSuppress
import ch.icken.processor.GenerationValues.GENERATED_PACKAGE_SUFFIX
import ch.icken.processor.GenerationValues.GROUP_COMPONENT_PARAM_NAME
import ch.icken.processor.GenerationValues.MULTIPLE
import ch.icken.processor.GenerationValues.MULTIPLE_SORTED
import ch.icken.processor.GenerationValues.OR
import ch.icken.processor.GenerationValues.OR_GROUP
import ch.icken.processor.GenerationValues.SINGLE
import ch.icken.processor.GenerationValues.SINGLE_SAFE
import ch.icken.processor.GenerationValues.SORT_PARAM_NAME
import ch.icken.processor.GenerationValues.STREAM
import ch.icken.processor.GenerationValues.STREAM_SORTED
import ch.icken.processor.GenerationValues.WHERE
import ch.icken.processor.GenerationValues.WHERE_GROUP
import ch.icken.processor.QualifiedNames.HibernatePanacheCompanionBase
Expand Down Expand Up @@ -105,7 +121,8 @@ class PanacheCompanionBaseProcessor(
returnType = queryComponentType
)

val whereReceiver = className.nestedClass(COMPANION_OBJECT)
val whereReceiver = className.nestedClass("Companion")
//region where
val where = FunSpec.builder(WHERE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
Expand All @@ -122,7 +139,9 @@ class PanacheCompanionBaseProcessor(
.addStatement("return %M($EXPRESSION_PARAM_NAME(%T), $GROUP_COMPONENT_PARAM_NAME)",
MemberName(QueryComponentClassName.packageName, WHERE_GROUP), objectClassName)
.addAnnotation(jvmNameAnnotation("$WHERE_GROUP$objectName"))
//endregion

//region and
val and = FunSpec.builder(AND)
.addModifiers(KModifier.INLINE)
.receiver(queryComponentType)
Expand All @@ -138,7 +157,9 @@ class PanacheCompanionBaseProcessor(
.addStatement("return $AND_GROUP($EXPRESSION_PARAM_NAME(%T), $GROUP_COMPONENT_PARAM_NAME)",
objectClassName)
.addAnnotation(jvmNameAnnotation("$AND_GROUP$objectName"))
//endregion

//region or
val or = FunSpec.builder(OR)
.addModifiers(KModifier.INLINE)
.receiver(queryComponentType)
Expand All @@ -154,8 +175,99 @@ class PanacheCompanionBaseProcessor(
.addStatement("return $OR_GROUP($EXPRESSION_PARAM_NAME(%T), $GROUP_COMPONENT_PARAM_NAME)",
objectClassName)
.addAnnotation(jvmNameAnnotation("$OR_GROUP$objectName"))
//endregion

listOf(where, whereGroup, and, andGroup, or, orGroup)
//region
val count = FunSpec.builder(COUNT)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(LongClassName)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$COUNT()")
.addAnnotation(jvmNameAnnotation("$COUNT$objectName"))

val delete = FunSpec.builder(DELETE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(LongClassName)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$DELETE()")
.addAnnotation(jvmNameAnnotation("$DELETE$objectName"))

val findReturns = PanacheQueryClassName.plusParameter(className)
val find = FunSpec.builder(FIND)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(findReturns)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$FIND()")
.addAnnotation(jvmNameAnnotation("$FIND$objectName"))
val findSorted = FunSpec.builder(FIND)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(SORT_PARAM_NAME, SortClassName)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(findReturns)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$FIND($SORT_PARAM_NAME)")
.addAnnotation(jvmNameAnnotation("$FIND_SORTED$objectName"))

val streamReturn = StreamClassName.plusParameter(className)
val stream = FunSpec.builder(STREAM)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(streamReturn)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$STREAM()")
.addAnnotation(jvmNameAnnotation("$STREAM$objectName"))
val streamSorted = FunSpec.builder(STREAM)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(SORT_PARAM_NAME, SortClassName)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(streamReturn)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).$STREAM($SORT_PARAM_NAME)")
.addAnnotation(jvmNameAnnotation("$STREAM_SORTED$objectName"))
//endregion

//region single
val single = FunSpec.builder(SINGLE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(className)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).getSingle()")
.addAnnotation(jvmNameAnnotation("$SINGLE$objectName"))
val singleSafe = FunSpec.builder(SINGLE_SAFE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(PanacheSingleResultClassName.plusParameter(WildcardTypeName.producerOf(className)))
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).getSingleSafe()")
.addAnnotation(jvmNameAnnotation("$SINGLE_SAFE$objectName"))
//endregion

//region multiple
val multipleReturns = ListClassName.plusParameter(className)
val multiple = FunSpec.builder(MULTIPLE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(multipleReturns)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).getMultiple()")
.addAnnotation(jvmNameAnnotation("$MULTIPLE$objectName"))
val multipleSorted = FunSpec.builder(MULTIPLE)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
.addParameter(SORT_PARAM_NAME, SortClassName)
.addParameter(EXPRESSION_PARAM_NAME, expressionParameterType)
.returns(multipleReturns)
.addStatement("return $WHERE($EXPRESSION_PARAM_NAME).getMultiple($SORT_PARAM_NAME)")
.addAnnotation(jvmNameAnnotation("$MULTIPLE_SORTED$objectName"))
//endregion

listOf(where, whereGroup, and, andGroup, or, orGroup,
count, delete, find, findSorted, stream, streamSorted,
single, singleSafe, multiple, multipleSorted)
}

FileSpec.builder(packageName, EXTENSIONS_FILE)
Expand Down
9 changes: 2 additions & 7 deletions library/src/main/kotlin/ch/icken/query/QueryComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ abstract class QueryComponent<Entity : PanacheEntityBase, Id : Any> internal con

//region Intermediate operations
fun and(expression: BooleanExpression) = AndQueryComponent(companion, this, expression)
fun or(expression: BooleanExpression) = OrQueryComponent(companion, this, expression)

fun andGroup(expression: BooleanExpression,
groupComponent: QueryComponent<Entity, Id>.() -> QueryComponent<Entity, Id>) =
AndGroupQueryComponent(companion, this, expression, groupComponent)

fun or(expression: BooleanExpression) = OrQueryComponent(companion, this, expression)
fun orGroup(expression: BooleanExpression,
groupComponent: QueryComponent<Entity, Id>.() -> QueryComponent<Entity, Id>) =
OrGroupQueryComponent(companion, this, expression, groupComponent)
Expand All @@ -54,10 +54,5 @@ abstract class QueryComponent<Entity : PanacheEntityBase, Id : Any> internal con
fun getSingleSafe() = find().singleResultSafe()
fun getMultiple() = find().list()
fun getMultiple(sort: Sort) = find(sort).list()

fun print() = with(compile()) {
println("[HQL Query] $query")
println("[Parameters] ${parameters.entries.joinToString()}")
}
//endregion
}

0 comments on commit 0f662ff

Please sign in to comment.