Skip to content

Commit

Permalink
💩 ...
Browse files Browse the repository at this point in the history
  • Loading branch information
이용홍 committed Mar 3, 2023
1 parent 67d4fb7 commit 8affd97
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 91 deletions.
31 changes: 31 additions & 0 deletions src/main/java/herbaccara/jooq/JooqRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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<R extends Record, T extends TableImpl<R>, ID> {

final protected DSLContext dsl;
final protected T table;
final protected TableField<R, ID> idField;

public JooqRepository(final DSLContext dsl, final T table, final TableField<R, ID> idField) {
this.dsl = dsl;
this.table = table;
this.idField = idField;
}

public @NotNull Result<R> findAll() {
return findAll(query -> {});
}

public @NotNull Result<R> findAll(Consumer<SelectWhereStep<R>> consumer) {
final SelectWhereStep<R> query = dsl.selectFrom(table);
consumer.accept(query);
return query.fetch();
}
}
183 changes: 92 additions & 91 deletions src/main/kotlin/herbaccara/jooq/JooqRepository.kt
Original file line number Diff line number Diff line change
@@ -1,91 +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

abstract class JooqRepository<R : Record, T : TableImpl<R>, ID>(
protected val dsl: DSLContext,
protected val table: T,
protected val idField: TableField<R, ID>
) {
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: (query: SelectWhereStep<R>) -> Unit = {}): Result<R> {
return dsl
.selectFrom(table)
.also(block)
.fetch()
}

@JvmOverloads
open fun page(pageable: Pageable, block: (query: SelectConditionStep<R>) -> Unit = {}): Page<R> {
val query = dsl
.selectFrom(table)
.where(
DSL.noCondition()
)
.also(block)

return Pagination.ofPage(dsl, query, pageable) { it }
}

@JvmOverloads
open fun slice(pageable: Pageable, block: (query: SelectConditionStep<R>) -> Unit = {}): Slice<R> {
val query = dsl
.selectFrom(table)
.where(
DSL.noCondition()
)
.also(block)

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<R : Record, T : TableImpl<R>, ID>(
// protected val dsl: DSLContext,
// protected val table: T,
// protected val idField: TableField<R, ID>
//) {
// 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<SelectWhereStep<R>> = Consumer { }): Result<R> {
// return dsl
// .selectFrom(table)
// .also(block::accept)
// .fetch()
// }
//
// @JvmOverloads
// open fun page(pageable: Pageable, block: Consumer<SelectConditionStep<R>> = Consumer { }): Page<R> {
// 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<SelectConditionStep<R>> = Consumer { }): Slice<R> {
// val query = dsl
// .selectFrom(table)
// .where(
// DSL.noCondition()
// )
// .also(block::accept)
//
// return Pagination.ofSlice(query, pageable) { it }
// }
//}

0 comments on commit 8affd97

Please sign in to comment.