diff --git a/src/main/java/herbaccara/jooq/JooqRepository.java b/src/main/java/herbaccara/jooq/JooqRepository.java deleted file mode 100644 index 08cd24d..0000000 --- a/src/main/java/herbaccara/jooq/JooqRepository.java +++ /dev/null @@ -1,31 +0,0 @@ -package herbaccara.jooq; - -import org.jetbrains.annotations.NotNull; -import org.jooq.*; -import org.jooq.Record; -import org.jooq.impl.TableImpl; - -import java.util.function.Consumer; - -public abstract class JooqRepository, ID> { - - final protected DSLContext dsl; - final protected T table; - final protected TableField idField; - - public JooqRepository(final DSLContext dsl, final T table, final TableField idField) { - this.dsl = dsl; - this.table = table; - this.idField = idField; - } - - public @NotNull Result findAll() { - return findAll(query -> {}); - } - - public @NotNull Result findAll(Consumer> consumer) { - final SelectWhereStep query = dsl.selectFrom(table); - consumer.accept(query); - return query.fetch(); - } -} diff --git a/src/main/kotlin/herbaccara/jooq/JooqRepository.kt b/src/main/kotlin/herbaccara/jooq/JooqRepository.kt index a67215e..6c444bc 100644 --- a/src/main/kotlin/herbaccara/jooq/JooqRepository.kt +++ b/src/main/kotlin/herbaccara/jooq/JooqRepository.kt @@ -1,92 +1,92 @@ -//package herbaccara.jooq -// -//import org.jooq.DSLContext -//import org.jooq.Record -//import org.jooq.Result -//import org.jooq.SelectConditionStep -//import org.jooq.SelectWhereStep -//import org.jooq.TableField -//import org.jooq.impl.DSL -//import org.jooq.impl.TableImpl -//import org.springframework.data.domain.Page -//import org.springframework.data.domain.Pageable -//import org.springframework.data.domain.Slice -//import java.util.function.Consumer -// -//abstract class JooqRepository, ID>( -// protected val dsl: DSLContext, -// protected val table: T, -// protected val idField: TableField -//) { -// open fun deleteAllByIdInBatch(vararg id: ID) { -// id.map { -// dsl.deleteFrom(table) -// .where( -// idField.eq(it) -// ) -// }.apply { -// dsl.batch(this).execute() -// } -// } -// -// open fun deleteAllById(vararg id: ID) { -// dsl.deleteFrom(table) -// .where( -// idField.`in`(*id) -// ) -// .execute() -// } -// -// open fun deleteAll() { -// dsl.deleteFrom(table).execute() -// } -// -// open fun deleteById(id: ID) { -// deleteAllById(id) -// } -// -// open fun existsByIx(id: ID): Boolean { -// return findById(id) != null -// } -// -// open fun findById(id: ID): R? { -// return dsl -// .selectFrom(table) -// .where( -// idField.eq(id) -// ) -// .fetchOne() -// } -// -// @JvmOverloads -// open fun findAll(block: Consumer> = Consumer { }): Result { -// return dsl -// .selectFrom(table) -// .also(block::accept) -// .fetch() -// } -// -// @JvmOverloads -// open fun page(pageable: Pageable, block: Consumer> = Consumer { }): Page { -// val query = dsl -// .selectFrom(table) -// .where( -// DSL.noCondition() -// ) -// .also(block::accept) -// -// return Pagination.ofPage(dsl, query, pageable) { it } -// } -// -// @JvmOverloads -// open fun slice(pageable: Pageable, block: Consumer> = Consumer { }): Slice { -// val query = dsl -// .selectFrom(table) -// .where( -// DSL.noCondition() -// ) -// .also(block::accept) -// -// return Pagination.ofSlice(query, pageable) { it } -// } -//} +package herbaccara.jooq + +import org.jooq.DSLContext +import org.jooq.Record +import org.jooq.Result +import org.jooq.SelectConditionStep +import org.jooq.SelectWhereStep +import org.jooq.TableField +import org.jooq.impl.DSL +import org.jooq.impl.TableImpl +import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable +import org.springframework.data.domain.Slice +import java.util.function.Consumer + +abstract class JooqRepository, ID>( + protected val dsl: DSLContext, + protected val table: T, + protected val idField: TableField +) { + open fun deleteAllByIdInBatch(vararg id: ID) { + id.map { + dsl.deleteFrom(table) + .where( + idField.eq(it) + ) + }.apply { + dsl.batch(this).execute() + } + } + + open fun deleteAllById(vararg id: ID) { + dsl.deleteFrom(table) + .where( + idField.`in`(*id) + ) + .execute() + } + + open fun deleteAll() { + dsl.deleteFrom(table).execute() + } + + open fun deleteById(id: ID) { + deleteAllById(id) + } + + open fun existsByIx(id: ID): Boolean { + return findById(id) != null + } + + open fun findById(id: ID): R? { + return dsl + .selectFrom(table) + .where( + idField.eq(id) + ) + .fetchOne() + } + + @JvmOverloads + open fun findAll(block: Consumer> = Consumer { }): Result { + return dsl + .selectFrom(table) + .also(block::accept) + .fetch() + } + + @JvmOverloads + open fun page(pageable: Pageable, block: Consumer> = Consumer { }): Page { + val query = dsl + .selectFrom(table) + .where( + DSL.noCondition() + ) + .also(block::accept) + + return Pagination.ofPage(dsl, query, pageable) { it } + } + + @JvmOverloads + open fun slice(pageable: Pageable, block: Consumer> = Consumer { }): Slice { + val query = dsl + .selectFrom(table) + .where( + DSL.noCondition() + ) + .also(block::accept) + + return Pagination.ofSlice(query, pageable) { it } + } +}