11package com .stubbornjava .common .db ;
22
3+ import static org .jooq .impl .DSL .using ;
4+
35import java .util .ArrayList ;
46import java .util .Arrays ;
57import java .util .Collection ;
68import java .util .Collections ;
79import java .util .List ;
810import java .util .function .Function ;
9- import java .util .function .Supplier ;
1011
1112import org .jooq .Condition ;
12- import org .jooq .DSLContext ;
13+ import org .jooq .Configuration ;
1314import org .jooq .Field ;
1415import org .jooq .Record ;
1516import org .jooq .RecordMapper ;
@@ -22,16 +23,16 @@ public class Dao<Rec extends UpdatableRecord<Rec>, T> {
2223 private final TableImpl <Rec > table ;
2324 private final RecordMapper <Rec , T > mapper ;
2425 private final RecordUnmapper <T , Rec > unmapper ;
25- private final Supplier < DSLContext > configSupplier ;
26+ private final Configuration configuration ;
2627 public Dao (TableImpl <Rec > table ,
2728 RecordMapper <Rec , T > mapper ,
2829 RecordUnmapper <T , Rec > unmapper ,
29- Supplier < DSLContext > configSupplier ) {
30+ Configuration configuration ) {
3031 super ();
3132 this .table = table ;
3233 this .mapper = mapper ;
3334 this .unmapper = unmapper ;
34- this .configSupplier = configSupplier ;
35+ this .configuration = configuration ;
3536 }
3637
3738 public T insertReturning (T obj ) {
@@ -52,7 +53,7 @@ public void insert(T... objects) {
5253 public void insert (Collection <T > objects ) {
5354 // Execute a batch INSERT
5455 if (objects .size () > 1 ) {
55- configSupplier . get ( ).batchInsert (records (objects , false )).execute ();
56+ using ( configuration ).batchInsert (records (objects , false )).execute ();
5657 }
5758
5859 // Execute a regular INSERT
@@ -73,7 +74,7 @@ public void update(T... objects) {
7374 public void update (Collection <T > objects ) {
7475 // Execute a batch UPDATE
7576 if (objects .size () > 1 ) {
76- configSupplier . get ( ).batchUpdate (records (objects , false )).execute ();
77+ using ( configuration ).batchUpdate (records (objects , false )).execute ();
7778 }
7879
7980 // Execute a regular UPDATE
@@ -94,7 +95,7 @@ public void delete(T... objects) {
9495 public void delete (Collection <T > objects ) {
9596 // Execute a batch DELETE
9697 if (objects .size () > 1 ) {
97- configSupplier . get ( ).batchDelete (records (objects , false )).execute ();
98+ using ( configuration ).batchDelete (records (objects , false )).execute ();
9899 }
99100
100101 // Execute a regular DELETE
@@ -104,15 +105,19 @@ else if (objects.size() == 1) {
104105 }
105106
106107 public T fetchOne (Function <TableImpl <Rec >, Condition > func ) {
107- return mapper .map (configSupplier . get ( ).fetchOne (table , func .apply (table )));
108+ return mapper .map (using ( configuration ).fetchOne (table , func .apply (table )));
108109 }
109110
110111 public List <T > fetch (Function <TableImpl <Rec >, Condition > func ) {
111- return configSupplier .get ().fetch (table , func .apply (table )).map (mapper );
112+ return using (configuration ).fetch (table , func .apply (table )).map (mapper );
113+ }
114+
115+ public List <T > fetchAll () {
116+ return using (configuration ).fetch (table ).map (mapper );
112117 }
113118
114119 public int deleteWhere (Function <TableImpl <Rec >, Condition > func ) {
115- return configSupplier . get ( ).deleteFrom (table ).where (func .apply (table )).execute ();
120+ return using ( configuration ).deleteFrom (table ).where (func .apply (table )).execute ();
116121 }
117122
118123 // Copy pasted from jOOQ's DAOImpl.java
@@ -128,7 +133,7 @@ public int deleteWhere(Function<TableImpl<Rec>, Condition> func) {
128133
129134 for (T object : objects ) {
130135 Rec record = unmapper .unmap (object );
131- record .attach (configSupplier . get ( ).configuration ());
136+ record .attach (using ( configuration ).configuration ());
132137
133138 if (forUpdate && pk != null )
134139 for (Field <?> field : pk )
0 commit comments