Skip to content

Commit

Permalink
Add "where" extension to update DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 8, 2024
1 parent 9570403 commit 5546cca
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 21 deletions.
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/ch/icken/model/Assignment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/ch/icken/model/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/ch/icken/model/Department.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/kotlin/ch/icken/model/Employee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/ch/icken/processor/ProcessorCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ abstract class ProcessorCommon(options: Map<String, String>) {
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()
Expand All @@ -70,7 +72,6 @@ abstract class ProcessorCommon(options: Map<String, String>) {
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"
Expand All @@ -91,6 +92,7 @@ abstract class ProcessorCommon(options: Map<String, String>) {
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"
Expand Down
13 changes: 6 additions & 7 deletions src/main/kotlin/ch/icken/query/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +29,7 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co
) {
//region compile
internal abstract fun compile(): Compiled
data class Compiled internal constructor(val component: String, val parameters: Map<String, Any>)
class Compiled internal constructor(val component: String, val parameters: Map<String, Any>)
//endregion

sealed class QueryComponent<Entity : PanacheEntityBase, Id : Any, Columns> private constructor(
Expand Down Expand Up @@ -104,7 +103,7 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co
private val setters: Array<out Columns.() -> Setter>
) : UpdateComponent<Entity, Id, Columns>(companion) {
//region Chaining operations
fun where(expression: Expression<Columns>): UpdateComponent<Entity, Id, Columns> =
fun where(expression: Expression<Columns>): LogicalUpdateComponent<Entity, Id, Columns> =
LogicalUpdateComponent.WhereUpdateComponent(companion, this, expression)
//endregion

Expand All @@ -120,14 +119,14 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> 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<String, Any>?)
class Compiled internal constructor(val assignment: String, val parameter: Pair<String, Any>?)
}
}

Expand Down Expand Up @@ -178,10 +177,10 @@ sealed class Component<Entity : PanacheEntityBase, Id : Any, Columns> private co

fun <Entity : PanacheEntityBase, Id : Any, Columns>
PanacheCompanionBase<Entity, Id>.update(columns: Columns, setter: Columns.() -> Setter):
UpdateComponent<Entity, Id, Columns> = InitialUpdateComponent(this, columns, arrayOf(setter))
InitialUpdateComponent<Entity, Id, Columns> = InitialUpdateComponent(this, columns, arrayOf(setter))
fun <Entity : PanacheEntityBase, Id : Any, Columns>
PanacheCompanionBase<Entity, Id>.update(columns: Columns, setters: Array<out Columns.() -> Setter>):
UpdateComponent<Entity, Id, Columns> = InitialUpdateComponent(this, columns, setters)
InitialUpdateComponent<Entity, Id, Columns> = InitialUpdateComponent(this, columns, setters)

fun <Entity : PanacheEntityBase, Id : Any, Columns>
PanacheCompanionBase<Entity, Id>.where(expression: Expression<Columns>):
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ch/icken/query/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sealed class Expression<Columns> {
else -> compileExpression()
}
protected abstract fun compileExpression(): Compiled
data class Compiled internal constructor(val expression: String, val parameters: Map<String, Any>)
class Compiled internal constructor(val expression: String, val parameters: Map<String, Any>)
//endregion

sealed class BooleanExpression<Columns> private constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -65,5 +65,6 @@ class PanacheCompanionBaseProcessorCompileTests : ProcessorCompileTestCommon() {
employeeExtensions.assertHasDeclaredMethodWithName("updateEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("updateMultipleEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("whereEmployee")
employeeExtensions.assertHasDeclaredMethodWithName("whereUpdateEmployee")
}
}

0 comments on commit 5546cca

Please sign in to comment.