Skip to content

Commit

Permalink
✨ DAOImpl extension 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
allbegray committed Mar 12, 2023
1 parent f0acc93 commit 51bab1a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/main/kotlin/herbaccara/jooq/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

package herbaccara.jooq

import org.jooq.UpdatableRecord
import org.jooq.*
import org.jooq.impl.DAOImpl
import org.jooq.impl.DSL
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import java.util.function.Consumer

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.page(pageable: Pageable): Page<P> {
val query = ctx().selectFrom(table).where(DSL.noCondition())
Expand All @@ -18,3 +19,36 @@ fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.slice(pageable: Pageable): S
val query = ctx().selectFrom(table).where(DSL.noCondition())
return Pagination.ofSlice(ctx(), query, pageable) { it.into(type) }
}

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.findOne(block: Consumer<SelectWhereStep<R>>): R? {
return ctx().selectFrom(table).also(block::accept).fetchOne()
}

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.find(block: Consumer<SelectWhereStep<R>>): Result<R> {
return ctx().selectFrom(table).also(block::accept).fetch()
}

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.count(block: Consumer<SelectJoinStep<Record1<Int>>>): Long {
return ctx().selectCount().from(table).also(block::accept).fetchSingle(0, Long::class.java)!!
}

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.update(id: T, block: Consumer<UpdateSetFirstStep<R>>): Int {
val pk = table.primaryKey?.fieldsArray ?: return 0
val query = ctx().update(table)
.also(block::accept)
.setNull(DSL.noField())
.where(
if (pk.size == 1) {
@Suppress("UNCHECKED_CAST")
(pk[0] as Field<Any>).equal(pk[0].dataType.convert(id))
} else {
DSL.row(*pk).equal(id as Record)
}
)
if (query.isExecutable.not()) return 0
return query.execute()
}

fun <R : UpdatableRecord<R>, P, T> DAOImpl<R, P, T>.delete(block: Consumer<DeleteUsingStep<R>>): Int {
return ctx().deleteFrom(table).also(block::accept).execute()
}
18 changes: 18 additions & 0 deletions src/test/kotlin/herbaccara/jooq/PaginationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ class PaginationTest {
title.contains("게시")
)

@Test
fun update() {
val query = dsl
.update(table)
.set(title, "testttttt")
.setNull(DSL.noField())
.where(
id.eq(1)
)

println("isExecutable : " + query.isExecutable)

val execute = query
.execute()

println("execute : " + execute)
}

@Test
fun seek() {
val query: () -> SelectConditionStep<Record3<Int, String, Int>> = {
Expand Down

0 comments on commit 51bab1a

Please sign in to comment.