Skip to content

Commit

Permalink
feat: insert or replace
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Dec 14, 2023
1 parent 7175fb7 commit 50f5ae5
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public boolean isTsurugi() {
return this.isTsurugi;
}

public DbManagerPurpose getPurpose() {
return this.purpose;
}

// DAO

public synchronized MeasurementMasterDao getMeasurementMasterDao() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface ItemConstructionMasterDao {
*/
int insert(ItemConstructionMaster entity);

int[] insertBatch(Collection<ItemConstructionMaster> entityList);
int[] insertBatch(Collection<ItemConstructionMaster> entityList, boolean insertOnly);

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ public interface ItemMasterDao {

/**
* <pre>
* insert into item_master
* insert (or replace) into item_master
* values(:entity)
* </pre>
*/
int insert(ItemMaster entity);

/**
* <pre>
* insert into item_master
* values(:entity)
* </pre>
*/
int insertOnly(ItemMaster entity);

int[] insertBatch(Collection<ItemMaster> entityList);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public int deleteAll() {

@Override
public int insert(CostMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<CostMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int deleteAll() {

@Override
public int insert(FactoryMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public abstract class IceaxeDao<E> {
private final String tableName;
private final List<IceaxeColumn<E, ?>> columnList;
private final Supplier<E> entitySupplier;
protected final String insert;

protected static <E, T> void add(List<IceaxeColumn<E, ?>> list, TgBindVariable<T> variable, BiConsumer<E, T> entitySetter, Function<E, T> entityGetter, RecordGetter<T> recordGetter) {
add(list, variable, entitySetter, entityGetter, recordGetter, false);
Expand All @@ -70,6 +71,7 @@ public IceaxeDao(CostBenchDbManagerIceaxe dbManager, String tableName, List<Icea
this.tableName = tableName;
this.columnList = columnList;
this.entitySupplier = entitySupplier;
this.insert = BenchConst.sqlInsert(dbManager.getPurpose());
}

protected final TsurugiSession getSession() {
Expand Down Expand Up @@ -101,24 +103,24 @@ protected final int doDeleteAll() {
}
}

protected final int doInsert(E entity) {
var ps = insertCache.get();
protected final int doInsert(E entity, boolean insertOnly) {
var ps = getInsertPs(insertOnly);
return executeAndGetCount(ps, entity);
}

protected final int[] doInsert(Collection<E> entityList) {
protected final int[] doInsert(Collection<E> entityList, boolean insertOnly) {
// TODO batch insertに切り替え
switch (1) {
default:
case 0:
return doInsertWait(entityList);
return doInsertWait(entityList, insertOnly);
case 1:
return doInsertNoWait(entityList);
return doInsertNoWait(entityList, insertOnly);
}
}

private int[] doInsertWait(Collection<E> entityList) {
var ps = insertCache.get();
private int[] doInsertWait(Collection<E> entityList, boolean insertOnly) {
var ps = getInsertPs(insertOnly);
var result = new int[entityList.size()];
int i = 0;
for (var entity : entityList) {
Expand All @@ -127,12 +129,20 @@ private int[] doInsertWait(Collection<E> entityList) {
return result;
}

private int[] doInsertNoWait(Collection<E> entityList) {
var ps = insertCache.get();
private int[] doInsertNoWait(Collection<E> entityList, boolean insertOnly) {
var ps = getInsertPs(insertOnly);
return executeAndGetCount(ps, entityList);
}

private final CachePreparedStatement<E> insertCache = new CachePreparedStatement<>() {
private TsurugiSqlPreparedStatement<E> getInsertPs(boolean insertOnly) {
if (insertOnly) {
return insertOnlyCache.get();
} else {
return insertCache.get();
}
}

private final CachePreparedStatement<E> insertOnlyCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
var names = getColumnNames();
Expand All @@ -142,6 +152,16 @@ protected void initialize() {
}
};

private final CachePreparedStatement<E> insertCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
var names = getColumnNames();
var values = columnList.stream().map(c -> c.getSqlName()).collect(Collectors.joining(","));
this.sql = insert + " into " + tableName + "(" + names + ") values (" + values + ")";
this.parameterMapping = getEntityParameterMapping();
}
};

private TgEntityParameterMapping<E> getEntityParameterMapping() {
var parameterMapping = TgEntityParameterMapping.<E>of();
for (var column : columnList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public int deleteAll() {

@Override
public int insert(ItemConstructionMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ItemConstructionMaster> entityList) {
return doInsert(entityList);
public int[] insertBatch(Collection<ItemConstructionMaster> entityList, boolean insertOnly) {
return doInsert(entityList, insertOnly);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public int deleteAll() {

@Override
public int insert(ItemManufacturingMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ItemManufacturingMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ public int deleteAll() {

@Override
public int insert(ItemMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int insertOnly(ItemMaster entity) {
return doInsert(entity, true);
}

@Override
public int[] insertBatch(Collection<ItemMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int deleteAll() {

@Override
public int insert(MeasurementMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ protected void initialize() {

@Override
public int insert(ResultTable entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ResultTable> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public int deleteAll() {

@Override
public int insert(StockHistory entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<StockHistory> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int insert(ItemConstructionMaster entity) {
}

@Override
public int[] insertBatch(Collection<ItemConstructionMaster> entityList) {
public int[] insertBatch(Collection<ItemConstructionMaster> entityList, boolean insertOnly) {
return doInsert(entityList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public int insert(ItemMaster entity) {
return doInsert(entity);
}

@Override
public int insertOnly(ItemMaster entity) {
return doInsert(entity);
}

@Override
public int[] insertBatch(Collection<ItemMaster> entityList) {
return doInsert(entityList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public int deleteAll() {

@Override
public int insert(CostMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<CostMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int deleteAll() {

@Override
public int insert(FactoryMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public int deleteAll() {

@Override
public int insert(ItemConstructionMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ItemConstructionMaster> entityList) {
return doInsert(entityList);
public int[] insertBatch(Collection<ItemConstructionMaster> entityList, boolean insertOnly) {
return doInsert(entityList, insertOnly);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public int deleteAll() {

@Override
public int insert(ItemManufacturingMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ItemManufacturingMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ public int deleteAll() {

@Override
public int insert(ItemMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int insertOnly(ItemMaster entity) {
return doInsert(entity, true);
}

@Override
public int[] insertBatch(Collection<ItemMaster> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int deleteAll() {

@Override
public int insert(MeasurementMaster entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public int deleteByProductId(int factoryId, LocalDate date, int productId) {

@Override
public int insert(ResultTable entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<ResultTable> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public int deleteAll() {

@Override
public int insert(StockHistory entity) {
return doInsert(entity);
return doInsert(entity, false);
}

@Override
public int[] insertBatch(Collection<StockHistory> entityList) {
return doInsert(entityList);
return doInsert(entityList, false);
}

@Override
Expand Down
Loading

0 comments on commit 50f5ae5

Please sign in to comment.