Skip to content

Commit

Permalink
BeeRepository now contains selectById method that is also generated
Browse files Browse the repository at this point in the history
  • Loading branch information
kurbaniec committed Jan 17, 2024
1 parent fd03c97 commit 180aa3e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ class SelectWhereATest(
}
}

@Test
fun `where inside selectById`() {
transaction.executeWithoutResult {
val id1 = UUID.randomUUID()
val id2 = UUID.randomUUID()
circularRepo.persist(Circular(id1, null, null))
circularRepo.persist(Circular(id2, id1, null))
circularRepo.cbf.update(em, Circular::class.java)
.set("cId", id2)
.where("id").eq(id1)
.executeUpdate()

val selection = CircularDSL.select { this.circular { this.circular { } } }
val circle = circularRepo.selectById(id2, selection)

assertNotNull(circle)
assertEquals(id2, circle.id)
}
}

@Test
fun whereAnd() {
transaction.executeWithoutResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class BeePersistentRepoCodegen(
.buildPersist()
.buildUpdate()
.buildSelect()
.buildSelectById()
.buildSelectionInfo()
.build()
)
Expand Down Expand Up @@ -392,6 +393,27 @@ class BeePersistentRepoCodegen(
addStatement("${padding}return entity")
}

private fun TypeSpec.Builder.buildSelectById(): TypeSpec.Builder = apply {
val entityClassName = entity.declaration.toClassName()
val idClassname = entity.id.type.toTypeName()
val dslClassname = ClassName(config.dslPackageName, "${entity.simpleName}DSL")
val idName = entity.id.simpleName

val selectFN = FunSpec.builder("selectById")
.addModifiers(KModifier.OVERRIDE)
.addParameter("id", idClassname)
.addParameter("selection", poetMap.classMapping(BEE_SELECTION))
.returns(entityClassName.copy(nullable = true))
selectFN.apply {
addNamedStmt("return select(selection) {")
val eqStmt = if (!entity.id.isValueClass) "eq" else "eqInline"
addStatement(" where(%T.$idName.$eqStmt(id))", dslClassname)
addNamedStmt("}.firstOrNull()")
}

addFunction(selectFN.build())
}

private fun TypeSpec.Builder.buildSelect(): TypeSpec.Builder = apply {
val entityClassName = entity.declaration.toClassName()
val listOfEntity = ClassName("kotlin.collections", "List")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,29 @@ interface BeeBlazeRepository<T : Any, ID: Any> {
val cbf: CriteriaBuilderFactory
val evm: EntityViewManager

/**
* Inserts new entity into db.
* Use [update] to update an already persisted entity, not [persist].
*/
fun <E: T> persist(entity: E): E

/**
* Updates all non-relation dependent fields of an entity in db.
* Fields marked with [jakarta.persistence.OneToOne] and so on will NOT be updated.
*/
fun <E: T> update(entity: E): E

/**
* Queries entities with [dsl] and specified relations from db.
*/
fun select(
selection: BeeSelection = BeeSelection.empty(),
dsl: SelectQuery<T>.() -> Selection<T> = { this }
): List<T>

/**
* Queries entity with specified relations from db.
* If [id] does not exist, null will be returned.
*/
fun selectById(id: ID, selection: BeeSelection = BeeSelection.empty()): T?
}

0 comments on commit 180aa3e

Please sign in to comment.