diff --git a/examples/src/main/kotlin/ch/icken/model/Assignment.kt b/examples/src/main/kotlin/ch/icken/model/Assignment.kt index 93042d7..ceb506a 100644 --- a/examples/src/main/kotlin/ch/icken/model/Assignment.kt +++ b/examples/src/main/kotlin/ch/icken/model/Assignment.kt @@ -21,9 +21,10 @@ import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity import jakarta.persistence.* +@Suppress("unused") @Entity @Table(name = "ASSIGNMENT") -data class Assignment( +class Assignment( @OneToOne(optional = false) @JoinColumn(name = "EMPLOYEE_ID", unique = true, nullable = false) diff --git a/examples/src/main/kotlin/ch/icken/model/Client.kt b/examples/src/main/kotlin/ch/icken/model/Client.kt index ffefea5..a79e392 100644 --- a/examples/src/main/kotlin/ch/icken/model/Client.kt +++ b/examples/src/main/kotlin/ch/icken/model/Client.kt @@ -20,9 +20,10 @@ import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity import jakarta.persistence.* +@Suppress("unused") @Entity @Table(name = "CLIENT") -data class Client( +class Client( @Column(name = "NAME", unique = true, nullable = false) var name: String, diff --git a/examples/src/main/kotlin/ch/icken/model/Department.kt b/examples/src/main/kotlin/ch/icken/model/Department.kt index 2a016f7..15f09eb 100644 --- a/examples/src/main/kotlin/ch/icken/model/Department.kt +++ b/examples/src/main/kotlin/ch/icken/model/Department.kt @@ -20,9 +20,10 @@ import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity import jakarta.persistence.* +@Suppress("unused") @Entity @Table(name = "DEPARTMENT") -data class Department( +class Department( @Column(name = "NAME", unique = true, nullable = false) var name: String, diff --git a/examples/src/main/kotlin/ch/icken/model/Employee.kt b/examples/src/main/kotlin/ch/icken/model/Employee.kt index 771fe1e..fdfd01c 100644 --- a/examples/src/main/kotlin/ch/icken/model/Employee.kt +++ b/examples/src/main/kotlin/ch/icken/model/Employee.kt @@ -21,9 +21,10 @@ import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity import jakarta.persistence.* import java.time.LocalDate +@Suppress("unused") @Entity @Table(name = "EMPLOYEE") -data class Employee( +class Employee( @Column(name = "EMPLOYEE_NO", unique = true, nullable = false, updatable = false) val employeeNumber: Int, diff --git a/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt b/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt index a6a5b2c..8e6829e 100644 --- a/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt +++ b/src/main/kotlin/ch/icken/processor/PanacheCompanionBaseProcessor.kt @@ -69,11 +69,16 @@ class PanacheCompanionBaseProcessor( .plusParameter(className) .plusParameter(idClassName) .plusParameter(columnsObjectClassName) + val setterExpressionParameterLambdaType = LambdaTypeName.get( receiver = columnsObjectClassName, returnType = SetterClassName ) - val updateComponentType = UpdateComponentClassName + val initialUpdateComponentType = InitialUpdateComponentClassName + .plusParameter(className) + .plusParameter(idClassName) + .plusParameter(columnsObjectClassName) + val logicalUpdateComponentType = LogicalUpdateComponentClassName .plusParameter(className) .plusParameter(idClassName) .plusParameter(columnsObjectClassName) @@ -190,22 +195,28 @@ class PanacheCompanionBaseProcessor( .addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_MULTIPLE_SORTED$classSimpleName")) //endregion - //region update - val updateExtensionFunction = MemberName(UpdateComponentClassName.packageName, FUNCTION_NAME_UPDATE) + //region update, where + val updateExtensionFunction = MemberName(InitialUpdateComponentClassName.packageName, FUNCTION_NAME_UPDATE) val update = FunSpec.builder(FUNCTION_NAME_UPDATE) .receiver(companionClassName) .addParameter(PARAM_NAME_SETTER, setterExpressionParameterLambdaType) - .returns(updateComponentType) + .returns(initialUpdateComponentType) .addStatement("return %M(%T, $PARAM_NAME_SETTER)", updateExtensionFunction, columnsObjectClassName) .addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_UPDATE$classSimpleName")) val updateMultiple = FunSpec.builder(FUNCTION_NAME_UPDATE) .receiver(companionClassName) .addParameter(PARAM_NAME_SETTERS, setterExpressionParameterLambdaType, KModifier.VARARG) - .returns(updateComponentType) + .returns(initialUpdateComponentType) .addStatement("return %M(%T, $PARAM_NAME_SETTERS)", updateExtensionFunction, columnsObjectClassName) .addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_UPDATE_MULTIPLE$classSimpleName")) - //TODO whereUpdate + val whereUpdate = FunSpec.builder(FUNCTION_NAME_WHERE) + .addModifiers(KModifier.INLINE) + .receiver(initialUpdateComponentType) + .addParameter(PARAM_NAME_EXPRESSION, expressionParameterLambdaType) + .returns(logicalUpdateComponentType) + .addStatement("return $FUNCTION_NAME_WHERE($PARAM_NAME_EXPRESSION(%T))", columnsObjectClassName) + .addAnnotation(jvmNameAnnotation("$FUNCTION_NAME_WHERE_UPDATE$classSimpleName")) //TODO andUpdate //TODO orUpdate @@ -231,7 +242,7 @@ class PanacheCompanionBaseProcessor( val functions = listOf(where, and, or, count, delete, find, findSorted, stream, streamSorted, single, singleSafe, multiple, multipleSorted, - update, updateMultiple, + update, updateMultiple, whereUpdate, andExpression, orExpression) FileSpec.builder(packageName, extensionFileName) diff --git a/src/main/kotlin/ch/icken/processor/ProcessorCommon.kt b/src/main/kotlin/ch/icken/processor/ProcessorCommon.kt index 8716c8a..686b03d 100644 --- a/src/main/kotlin/ch/icken/processor/ProcessorCommon.kt +++ b/src/main/kotlin/ch/icken/processor/ProcessorCommon.kt @@ -59,8 +59,10 @@ abstract class ProcessorCommon(options: Map) { internal val ColumnClassName = Column::class.asClassName() internal val ExpressionClassName = Expression::class.asClassName() internal val GeneratedClassName = Generated::class.asClassName() + internal val InitialUpdateComponentClassName = UpdateComponent.InitialUpdateComponent::class.asClassName() internal val JvmNameClassName = JvmName::class.asClassName() internal val ListClassName = List::class.asClassName() + internal val LogicalUpdateComponentClassName = UpdateComponent.LogicalUpdateComponent::class.asClassName() internal val LongClassName = Long::class.asClassName() internal val PanacheQueryClassName = PanacheQuery::class.asClassName() internal val PanacheSingleResultClassName = PanacheSingleResult::class.asClassName() @@ -70,7 +72,6 @@ abstract class ProcessorCommon(options: Map) { internal val StreamClassName = Stream::class.asClassName() internal val StringClassName = String::class.asClassName() internal val SuppressClassName = Suppress::class.asClassName() - internal val UpdateComponentClassName = UpdateComponent::class.asClassName() //endregion //region Constants internal const val CLASS_NAME_COMPANION = "Companion" @@ -91,6 +92,7 @@ abstract class ProcessorCommon(options: Map) { internal const val FUNCTION_NAME_UPDATE = "update" internal const val FUNCTION_NAME_UPDATE_MULTIPLE = "updateMultiple" internal const val FUNCTION_NAME_WHERE = "where" + internal const val FUNCTION_NAME_WHERE_UPDATE = "whereUpdate" internal const val PARAM_NAME_COLUMNS_BASE_CLASS = "parent" internal const val PARAM_NAME_EXPRESSION = "expression" internal const val PARAM_NAME_MAPPED_BY = "mappedBy" diff --git a/src/main/kotlin/ch/icken/query/Component.kt b/src/main/kotlin/ch/icken/query/Component.kt index 03492f1..a89fccf 100644 --- a/src/main/kotlin/ch/icken/query/Component.kt +++ b/src/main/kotlin/ch/icken/query/Component.kt @@ -18,7 +18,6 @@ package ch.icken.query import ch.icken.query.Component.QueryComponent import ch.icken.query.Component.QueryComponent.InitialQueryComponent -import ch.icken.query.Component.UpdateComponent import ch.icken.query.Component.UpdateComponent.InitialUpdateComponent import ch.icken.query.Component.UpdateComponent.InitialUpdateComponent.Setter import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanionBase @@ -30,7 +29,7 @@ sealed class Component private co ) { //region compile internal abstract fun compile(): Compiled - data class Compiled internal constructor(val component: String, val parameters: Map) + class Compiled internal constructor(val component: String, val parameters: Map) //endregion sealed class QueryComponent private constructor( @@ -104,7 +103,7 @@ sealed class Component private co private val setters: Array Setter> ) : UpdateComponent(companion) { //region Chaining operations - fun where(expression: Expression): UpdateComponent = + fun where(expression: Expression): LogicalUpdateComponent = LogicalUpdateComponent.WhereUpdateComponent(companion, this, expression) //endregion @@ -120,14 +119,14 @@ sealed class Component private co ) } - data class Setter internal constructor(val columnName: String, val value: Any?) { + class Setter internal constructor(private val columnName: String, private val value: Any?) { private val parameterName: String = generateParameterName() internal fun compile(): Compiled = when (value) { null -> Compiled("$columnName = null", null) else -> Compiled("$columnName = $parameterName", parameterName to value) } - data class Compiled internal constructor(val assignment: String, val parameter: Pair?) + class Compiled internal constructor(val assignment: String, val parameter: Pair?) } } @@ -178,10 +177,10 @@ sealed class Component private co fun PanacheCompanionBase.update(columns: Columns, setter: Columns.() -> Setter): - UpdateComponent = InitialUpdateComponent(this, columns, arrayOf(setter)) + InitialUpdateComponent = InitialUpdateComponent(this, columns, arrayOf(setter)) fun PanacheCompanionBase.update(columns: Columns, setters: Array Setter>): - UpdateComponent = InitialUpdateComponent(this, columns, setters) + InitialUpdateComponent = InitialUpdateComponent(this, columns, setters) fun PanacheCompanionBase.where(expression: Expression): diff --git a/src/main/kotlin/ch/icken/query/Expression.kt b/src/main/kotlin/ch/icken/query/Expression.kt index bbc7645..7975c27 100644 --- a/src/main/kotlin/ch/icken/query/Expression.kt +++ b/src/main/kotlin/ch/icken/query/Expression.kt @@ -31,7 +31,7 @@ sealed class Expression { else -> compileExpression() } protected abstract fun compileExpression(): Compiled - data class Compiled internal constructor(val expression: String, val parameters: Map) + class Compiled internal constructor(val expression: String, val parameters: Map) //endregion sealed class BooleanExpression private constructor( diff --git a/src/test/kotlin/ch/icken/processor/PanacheCompanionBaseProcessorCompileTests.kt b/src/test/kotlin/ch/icken/processor/PanacheCompanionBaseProcessorCompileTests.kt index 04f95c7..9816eaf 100644 --- a/src/test/kotlin/ch/icken/processor/PanacheCompanionBaseProcessorCompileTests.kt +++ b/src/test/kotlin/ch/icken/processor/PanacheCompanionBaseProcessorCompileTests.kt @@ -47,7 +47,7 @@ class PanacheCompanionBaseProcessorCompileTests : ProcessorCompileTestCommon() { compilation.assertHasFile("EmployeeExtensions.kt") val employeeExtensions = result.loadClass("EmployeeExtensionsKt") - employeeExtensions.assertNumberOfDeclaredMethods(17) + employeeExtensions.assertNumberOfDeclaredMethods(18) employeeExtensions.assertHasDeclaredMethodWithName("andEmployee") employeeExtensions.assertHasDeclaredMethodWithName("andExpressionEmployee") employeeExtensions.assertHasDeclaredMethodWithName("countEmployee") @@ -65,5 +65,6 @@ class PanacheCompanionBaseProcessorCompileTests : ProcessorCompileTestCommon() { employeeExtensions.assertHasDeclaredMethodWithName("updateEmployee") employeeExtensions.assertHasDeclaredMethodWithName("updateMultipleEmployee") employeeExtensions.assertHasDeclaredMethodWithName("whereEmployee") + employeeExtensions.assertHasDeclaredMethodWithName("whereUpdateEmployee") } }