diff --git a/src/main/kotlin/herbaccara/jooq/extensions.kt b/src/main/kotlin/herbaccara/jooq/extensions.kt index b097425..424a5f4 100644 --- a/src/main/kotlin/herbaccara/jooq/extensions.kt +++ b/src/main/kotlin/herbaccara/jooq/extensions.kt @@ -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 , P, T> DAOImpl.page(pageable: Pageable): Page

{ val query = ctx().selectFrom(table).where(DSL.noCondition()) @@ -18,3 +19,36 @@ fun , P, T> DAOImpl.slice(pageable: Pageable): S val query = ctx().selectFrom(table).where(DSL.noCondition()) return Pagination.ofSlice(ctx(), query, pageable) { it.into(type) } } + +fun , P, T> DAOImpl.findOne(block: Consumer>): R? { + return ctx().selectFrom(table).also(block::accept).fetchOne() +} + +fun , P, T> DAOImpl.find(block: Consumer>): Result { + return ctx().selectFrom(table).also(block::accept).fetch() +} + +fun , P, T> DAOImpl.count(block: Consumer>>): Long { + return ctx().selectCount().from(table).also(block::accept).fetchSingle(0, Long::class.java)!! +} + +fun , P, T> DAOImpl.update(id: T, block: Consumer>): 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).equal(pk[0].dataType.convert(id)) + } else { + DSL.row(*pk).equal(id as Record) + } + ) + if (query.isExecutable.not()) return 0 + return query.execute() +} + +fun , P, T> DAOImpl.delete(block: Consumer>): Int { + return ctx().deleteFrom(table).also(block::accept).execute() +} diff --git a/src/test/kotlin/herbaccara/jooq/PaginationTest.kt b/src/test/kotlin/herbaccara/jooq/PaginationTest.kt index 04b7e96..4d47b9d 100644 --- a/src/test/kotlin/herbaccara/jooq/PaginationTest.kt +++ b/src/test/kotlin/herbaccara/jooq/PaginationTest.kt @@ -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> = {