-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
이용홍
committed
Mar 3, 2023
1 parent
67d4fb7
commit 8affd97
Showing
2 changed files
with
123 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
// } | ||
//} |