diff --git a/library/src/main/kotlin/ch/icken/processor/ClassNames.kt b/library/src/main/kotlin/ch/icken/processor/ClassNames.kt index 7425790..536daea 100644 --- a/library/src/main/kotlin/ch/icken/processor/ClassNames.kt +++ b/library/src/main/kotlin/ch/icken/processor/ClassNames.kt @@ -16,11 +16,11 @@ 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() @@ -28,8 +28,14 @@ internal object ClassNames { 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() } diff --git a/library/src/main/kotlin/ch/icken/processor/GenerationValues.kt b/library/src/main/kotlin/ch/icken/processor/GenerationValues.kt index c43eb96..0318773 100644 --- a/library/src/main/kotlin/ch/icken/processor/GenerationValues.kt +++ b/library/src/main/kotlin/ch/icken/processor/GenerationValues.kt @@ -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" @@ -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") diff --git a/library/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt b/library/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt index 87f26ee..31454fa 100644 --- a/library/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt +++ b/library/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/library/src/main/kotlin/ch/icken/query/QueryComponent.kt b/library/src/main/kotlin/ch/icken/query/QueryComponent.kt index 9dda9e8..5301a73 100644 --- a/library/src/main/kotlin/ch/icken/query/QueryComponent.kt +++ b/library/src/main/kotlin/ch/icken/query/QueryComponent.kt @@ -32,11 +32,11 @@ abstract class QueryComponent 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.() -> QueryComponent) = AndGroupQueryComponent(companion, this, expression, groupComponent) + + fun or(expression: BooleanExpression) = OrQueryComponent(companion, this, expression) fun orGroup(expression: BooleanExpression, groupComponent: QueryComponent.() -> QueryComponent) = OrGroupQueryComponent(companion, this, expression, groupComponent) @@ -54,10 +54,5 @@ abstract class QueryComponent 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 }