Skip to content

Commit

Permalink
Finalize update DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 9, 2024
1 parent 5546cca commit cebd1f1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
Empty file modified examples/gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/ch/icken/Examples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.time.LocalDate

//WHERE FIRST_NAME = 'John'
//Using type-safe sealed result wrapper
fun findJohn() = Employee.where { firstName eq "John" }.getSingleSafe()
fun findJohn() = Employee.where { firstName eq "John" }.singleSafe()

//SELECT COUNT(*) FROM EMPLOYEE WHERE GENDER != 'M'
fun countNotMen() = Employee.count { gender neq Employee.Gender.M }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ class PanacheCompanionBaseProcessor(
.receiver(companionClassName)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(className)
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).getSingle()")
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).single()")
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_SINGLE$classSimpleName"))
val singleSafe = FunSpec.builder(FUNCTION_NAME_SINGLE_SAFE)
.addModifiers(KModifier.INLINE)
.receiver(companionClassName)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(PanacheSingleResultClassName.plusParameter(WildcardTypeName.producerOf(className)))
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).getSingleSafe()")
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).singleSafe()")
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_SINGLE_SAFE$classSimpleName"))

val multipleReturns = ListClassName.plusParameter(className)
Expand All @@ -183,19 +183,19 @@ class PanacheCompanionBaseProcessor(
.receiver(companionClassName)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(multipleReturns)
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).getMultiple()")
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).multiple()")
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_MULTIPLE$classSimpleName"))
val multipleSorted = FunSpec.builder(FUNCTION_NAME_MULTIPLE)
.addModifiers(KModifier.INLINE)
.receiver(companionClassName)
.addParameter(PARAM_NAME_SORT, SortClassName)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(multipleReturns)
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).getMultiple($PARAM_NAME_SORT)")
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION).multiple($PARAM_NAME_SORT)")
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_MULTIPLE_SORTED$classSimpleName"))
//endregion

//region update, where
//region update, whereUpdate, andUpdate, orUpdate
val updateExtensionFunction = MemberName(InitialUpdateComponentClassName.packageName, FUNCTION_NAME_UPDATE)
val update = FunSpec.builder(FUNCTION_NAME_UPDATE)
.receiver(companionClassName)
Expand All @@ -218,8 +218,20 @@ class PanacheCompanionBaseProcessor(
.addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_WHERE_UPDATE$classSimpleName"))

//TODO andUpdate
//TODO orUpdate
val andUpdate = FunSpec.builder(FUNCTION_NAME_AND)
.addModifiers(KModifier.INLINE)
.receiver(logicalUpdateComponentType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(logicalUpdateComponentType)
.addStatement("return $FUNCTION_NAME_AND($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_AND_UPDATE$classSimpleName"))
val orUpdate = FunSpec.builder(FUNCTION_NAME_OR)
.addModifiers(KModifier.INLINE)
.receiver(logicalUpdateComponentType)
.addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType)
.returns(logicalUpdateComponentType)
.addStatement("return $FUNCTION_NAME_OR($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName)
.addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_OR_UPDATE$classSimpleName"))
//endregion

//region andExpression, orExpression
Expand All @@ -242,7 +254,7 @@ class PanacheCompanionBaseProcessor(
val functions = listOf(where, and, or,
count, delete, find, findSorted, stream, streamSorted,
single, singleSafe, multiple, multipleSorted,
update, updateMultiple, whereUpdate,
update, updateMultiple, whereUpdate, andUpdate, orUpdate,
andExpression, orExpression)

FileSpec.builder(packageName, extensionFileName)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/ch/icken/processor/ProcessorCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ abstract class ProcessorCommon(options: Map<String, String>) {
internal const val CLASS_NAME_COMPANION = "Companion"
internal const val FUNCTION_NAME_AND = "and"
internal const val FUNCTION_NAME_AND_EXPRESSION = "andExpression"
internal const val FUNCTION_NAME_AND_UPDATE = "andUpdate"
internal const val FUNCTION_NAME_COUNT = "count"
internal const val FUNCTION_NAME_DELETE = "delete"
internal const val FUNCTION_NAME_FIND = "find"
Expand All @@ -85,6 +86,7 @@ abstract class ProcessorCommon(options: Map<String, String>) {
internal const val FUNCTION_NAME_MULTIPLE_SORTED = "multipleSorted"
internal const val FUNCTION_NAME_OR = "or"
internal const val FUNCTION_NAME_OR_EXPRESSION = "orExpression"
internal const val FUNCTION_NAME_OR_UPDATE = "orUpdate"
internal const val FUNCTION_NAME_SINGLE = "single"
internal const val FUNCTION_NAME_SINGLE_SAFE = "singleSafe"
internal const val FUNCTION_NAME_STREAM = "stream"
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/ch/icken/query/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co
fun stream() = with(compile()) { companion.stream(component, parameters) }
fun stream(sort: Sort) = with(compile()) { companion.stream(component, sort, parameters) }

fun getSingle() = find().singleResult()
fun getSingleSafe() = find().singleResultSafe()
fun getMultiple() = find().list()
fun getMultiple(sort: Sort) = find(sort).list()
fun single() = find().singleResult()
fun singleSafe() = find().singleResultSafe()
fun multiple() = find().list()
fun multiple(sort: Sort) = find(sort).list()
//endregion

class InitialQueryComponent<Entity : PanacheEntityBase, Id : Any, Columns> internal constructor(
Expand Down Expand Up @@ -108,7 +108,7 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co
//endregion

//region Terminal operations
//TODO execute on all rows
fun executeWithoutWhere() = with(compile()) { companion.update(component, parameters) }
//endregion

override fun compile(): Compiled {
Expand Down Expand Up @@ -137,14 +137,14 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co
private val expression: Expression<Columns>
) : UpdateComponent<Entity, Id, Columns>(companion) {
//region Chaining operations
fun and(expression: Expression<Columns>): UpdateComponent<Entity, Id, Columns> =
fun and(expression: Expression<Columns>): LogicalUpdateComponent<Entity, Id, Columns> =
AndUpdateComponent(companion, this, expression)
fun or(expression: Expression<Columns>): UpdateComponent<Entity, Id, Columns> =
fun or(expression: Expression<Columns>): LogicalUpdateComponent<Entity, Id, Columns> =
OrUpdateComponent(companion, this, expression)
//endregion

//region Terminal operations
//TODO execute
fun execute() = with(compile()) { companion.update(component, parameters) }
//endregion

override fun compile(): Compiled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class PanacheCompanionBaseProcessorCompileTests : ProcessorCompileTestCommon() {
compilation.assertHasFile("EmployeeExtensions.kt")

val employeeExtensions = result.loadClass("EmployeeExtensionsKt")
employeeExtensions.assertNumberOfDeclaredMethods(18)
employeeExtensions.assertNumberOfDeclaredMethods(20)
employeeExtensions.assertHasDeclaredMethodWithName("andEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("andExpressionEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("andUpdateEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("countEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("deleteEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("findEmployee")
Expand All @@ -58,6 +59,7 @@ class PanacheCompanionBaseProcessorCompileTests : ProcessorCompileTestCommon() {
employeeExtensions.assertHasDeclaredMethodWithName("multipleSortedEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("orEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("orExpressionEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("orUpdateEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("singleEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("singleSafeEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("streamEmployee")
Expand Down

0 comments on commit cebd1f1

Please sign in to comment.