|
| 1 | +package com.stubbornjava.common.db; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.function.Supplier; |
| 10 | + |
| 11 | +import org.jooq.Condition; |
| 12 | +import org.jooq.DSLContext; |
| 13 | +import org.jooq.Field; |
| 14 | +import org.jooq.Record; |
| 15 | +import org.jooq.RecordMapper; |
| 16 | +import org.jooq.RecordUnmapper; |
| 17 | +import org.jooq.UniqueKey; |
| 18 | +import org.jooq.UpdatableRecord; |
| 19 | +import org.jooq.impl.TableImpl; |
| 20 | + |
| 21 | +public class TableCrud<Rec extends UpdatableRecord<Rec>, T> { |
| 22 | + private final TableImpl<Rec> table; |
| 23 | + private final RecordMapper<Record, T> mapper; |
| 24 | + private final RecordUnmapper<T, Rec> unmapper; |
| 25 | + private final Supplier<DSLContext> configSupplier; |
| 26 | + public TableCrud(TableImpl<Rec> table, |
| 27 | + RecordMapper<Rec, T> mapper, |
| 28 | + RecordUnmapper<T, Rec> unmapper, |
| 29 | + Supplier<DSLContext> configSupplier) { |
| 30 | + super(); |
| 31 | + this.table = table; |
| 32 | + this.mapper = (RecordMapper<Record, T>) mapper; |
| 33 | + this.unmapper = unmapper; |
| 34 | + this.configSupplier = configSupplier; |
| 35 | + } |
| 36 | + |
| 37 | + public T insertReturning(T obj) { |
| 38 | + Rec rec = records(Collections.singletonList(obj), false).get(0); |
| 39 | + rec.insert(); |
| 40 | + return rec.map(mapper); |
| 41 | + } |
| 42 | + |
| 43 | + public void insert(T obj) { |
| 44 | + insert(Collections.singletonList(obj)); |
| 45 | + } |
| 46 | + |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + public void insert(T... objects) { |
| 49 | + insert(Arrays.asList(objects)); |
| 50 | + } |
| 51 | + |
| 52 | + public void insert(Collection<T> objects) { |
| 53 | + // Execute a batch INSERT |
| 54 | + if (objects.size() > 1) { |
| 55 | + configSupplier.get().batchInsert(records(objects, false)).execute(); |
| 56 | + } |
| 57 | + |
| 58 | + // Execute a regular INSERT |
| 59 | + else if (objects.size() == 1) { |
| 60 | + records(objects, false).get(0).insert(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void update(T obj) { |
| 65 | + update(Collections.singletonList(obj)); |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + public void update(T... objects) { |
| 70 | + update(Arrays.asList(objects)); |
| 71 | + } |
| 72 | + |
| 73 | + public void update(Collection<T> objects) { |
| 74 | + // Execute a batch UPDATE |
| 75 | + if (objects.size() > 1) { |
| 76 | + configSupplier.get().batchUpdate(records(objects, false)).execute(); |
| 77 | + } |
| 78 | + |
| 79 | + // Execute a regular UPDATE |
| 80 | + else if (objects.size() == 1) { |
| 81 | + records(objects, false).get(0).update(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void delete(T obj) { |
| 86 | + delete(Collections.singletonList(obj)); |
| 87 | + } |
| 88 | + |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + public void delete(T... objects) { |
| 91 | + delete(Arrays.asList(objects)); |
| 92 | + } |
| 93 | + |
| 94 | + public void delete(Collection<T> objects) { |
| 95 | + // Execute a batch DELETE |
| 96 | + if (objects.size() > 1) { |
| 97 | + configSupplier.get().batchDelete(records(objects, false)).execute(); |
| 98 | + } |
| 99 | + |
| 100 | + // Execute a regular DELETE |
| 101 | + else if (objects.size() == 1) { |
| 102 | + records(objects, false).get(0).delete(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public T findOne(Function<TableImpl<Rec>, Condition> func) { |
| 107 | + return configSupplier.get().fetchOne(table, func.apply(table)).map(mapper); |
| 108 | + } |
| 109 | + |
| 110 | + public List<T> find(Function<TableImpl<Rec>, Condition> func) { |
| 111 | + return configSupplier.get().fetch(table, func.apply(table)).map(mapper); |
| 112 | + } |
| 113 | + |
| 114 | + public int deleteWhere(Function<TableImpl<Rec>, Condition> func) { |
| 115 | + return configSupplier.get().deleteFrom(table).where(func.apply(table)).execute(); |
| 116 | + } |
| 117 | + |
| 118 | + // Copy pasted from jOOQ's DAOImpl.java |
| 119 | + private /* non-final */ Field<?>[] pk() { |
| 120 | + UniqueKey<?> key = table.getPrimaryKey(); |
| 121 | + return key == null ? null : key.getFieldsArray(); |
| 122 | + } |
| 123 | + |
| 124 | + // Copy pasted from jOOQ's DAOImpl.java |
| 125 | + private /* non-final */ List<Rec> records(Collection<T> objects, boolean forUpdate) { |
| 126 | + List<Rec> result = new ArrayList<>(); |
| 127 | + Field<?>[] pk = pk(); |
| 128 | + |
| 129 | + for (T object : objects) { |
| 130 | + Rec record = unmapper.unmap(object); |
| 131 | + record.attach(configSupplier.get().configuration()); |
| 132 | + |
| 133 | + if (forUpdate && pk != null) |
| 134 | + for (Field<?> field : pk) |
| 135 | + record.changed(field, false); |
| 136 | + |
| 137 | + resetChangedOnNotNull(record); |
| 138 | + result.add(record); |
| 139 | + } |
| 140 | + |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + // Copy pasted from jOOQ's Tools.java |
| 145 | + /** |
| 146 | + * [#2700] [#3582] If a POJO attribute is NULL, but the column is NOT NULL |
| 147 | + * then we should let the database apply DEFAULT values |
| 148 | + */ |
| 149 | + private static final void resetChangedOnNotNull(Record record) { |
| 150 | + int size = record.size(); |
| 151 | + |
| 152 | + for (int i = 0; i < size; i++) |
| 153 | + if (record.get(i) == null) |
| 154 | + if (!record.field(i).getDataType().nullable()) |
| 155 | + record.changed(i, false); |
| 156 | + } |
| 157 | +} |
0 commit comments