From a2331ad71a7fb2f3b3180afc945a3829ed979037 Mon Sep 17 00:00:00 2001 From: Redkale Date: Tue, 13 Dec 2022 21:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DefaultDataBatch=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redkale/source/AbstractDataSource.java | 326 ++++++++++++++++++ .../java/org/redkale/source/DataBatch.java | 313 +---------------- 2 files changed, 343 insertions(+), 296 deletions(-) diff --git a/src/main/java/org/redkale/source/AbstractDataSource.java b/src/main/java/org/redkale/source/AbstractDataSource.java index 31611886e..6457057f3 100644 --- a/src/main/java/org/redkale/source/AbstractDataSource.java +++ b/src/main/java/org/redkale/source/AbstractDataSource.java @@ -1132,4 +1132,330 @@ public abstract class AbstractDataSource extends AbstractService implements Data return querySheetAsync(clazz, selects, flipper, FilterNodeBean.createFilterNode(bean)); } + protected static class DefaultDataBatch implements DataBatch { + + //-------------------- 新增操作 -------------------- + @Comment("新增Entity对象") + public Map insertEntitys; + + //-------------------- 删除操作 -------------------- + @Comment("删除Entity对象") + public Map deleteEntitys; + + @Comment("根据主键值删除") + public Map> deleteActions1; + + @Comment("根据FilterNode删除") + public Map> deleteActions2; + + //-------------------- 修改操作 -------------------- + @Comment("修改Entity对象") + public Map updateEntitys; + + @Comment("根据主键值修改部分字段") + public Map> updateActions1; + + @Comment("根据FilterNode修改部分字段") + public Map> updateActions2; + + @Comment("根据FilterNode修改Entity部分字段") + public Map> updateActions3; + + @Comment("根据FilterNode修改Entity的SelectColumn选定字段") + public Map> updateActions4; + + protected DefaultDataBatch() { + } + + @Override + public DataBatch insert(T... entitys) { + if (this.insertEntitys == null) { + this.insertEntitys = new HashMap<>(); + } + for (T t : entitys) { + Objects.requireNonNull(t); + if (t.getClass().getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); + } + this.insertEntitys.computeIfAbsent(t.getClass(), c -> new ArrayList<>()).add(t); + } + return this; + } + + @Override + public DataBatch delete(T... entitys) { + if (this.deleteEntitys == null) { + this.deleteEntitys = new HashMap<>(); + } + for (T t : entitys) { + Objects.requireNonNull(t); + if (t.getClass().getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); + } + this.deleteEntitys.computeIfAbsent(t.getClass(), c -> new ArrayList<>()).add(t); + } + return this; + } + + @Override + public DataBatch delete(Class clazz, Serializable... pks) { + Objects.requireNonNull(clazz); + if (clazz.getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); + } + if (pks.length < 1) { + throw new RuntimeException("delete pk length is zero "); + } + for (Serializable pk : pks) { + Objects.requireNonNull(pk); + } + if (this.deleteActions1 == null) { + this.deleteActions1 = new HashMap<>(); + } + this.deleteActions1.computeIfAbsent(clazz, c -> new ArrayList<>()).add(new DeleteBatchAction1(clazz, pks)); + return this; + } + + @Override + public DataBatch delete(Class clazz, FilterNode node) { + return delete(clazz, node, (Flipper) null); + } + + @Override + public DataBatch delete(Class clazz, FilterNode node, Flipper flipper) { + Objects.requireNonNull(clazz); + if (clazz.getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); + } + if (this.deleteActions2 == null) { + this.deleteActions2 = new HashMap<>(); + } + this.deleteActions2.computeIfAbsent(clazz, c -> new ArrayList<>()).add(new DeleteBatchAction2(clazz, node, flipper)); + return this; + } + + @Override + public DataBatch update(T... entitys) { + if (this.updateEntitys == null) { + this.updateEntitys = new HashMap<>(); + } + for (T t : entitys) { + Objects.requireNonNull(t); + if (t.getClass().getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); + } + this.updateEntitys.computeIfAbsent(t.getClass(), c -> new ArrayList<>()).add(t); + } + return this; + } + + @Override + public DataBatch update(Class clazz, Serializable pk, String column, Serializable value) { + return update(clazz, pk, ColumnValue.mov(column, value)); + } + + @Override + public DataBatch update(Class clazz, Serializable pk, ColumnValue... values) { + Objects.requireNonNull(clazz); + if (clazz.getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); + } + Objects.requireNonNull(pk); + if (values.length < 1) { + throw new RuntimeException("update column-value length is zero "); + } + for (ColumnValue val : values) { + Objects.requireNonNull(val); + } + if (this.updateActions1 == null) { + this.updateActions1 = new HashMap<>(); + } + this.updateActions1.computeIfAbsent(clazz, c -> new ArrayList<>()).add(new UpdateBatchAction1(clazz, pk, values)); + return this; + } + + @Override + public DataBatch update(Class clazz, FilterNode node, String column, Serializable value) { + return update(clazz, node, (Flipper) null, ColumnValue.mov(column, value)); + } + + @Override + public DataBatch update(Class clazz, FilterNode node, ColumnValue... values) { + return update(clazz, node, (Flipper) null, values); + } + + @Override + public DataBatch update(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values) { + Objects.requireNonNull(clazz); + if (clazz.getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); + } + if (values.length < 1) { + throw new RuntimeException("update column-value length is zero "); + } + for (ColumnValue val : values) { + Objects.requireNonNull(val); + } + if (this.updateActions2 == null) { + this.updateActions2 = new HashMap<>(); + } + this.updateActions2.computeIfAbsent(clazz, c -> new ArrayList<>()).add(new UpdateBatchAction2(clazz, node, flipper, values)); + return this; + } + + @Override + public DataBatch updateColumn(T entity, final String... columns) { + return updateColumn(entity, (FilterNode) null, columns); + } + + @Override + public DataBatch updateColumn(T entity, final FilterNode node, final String... columns) { + Objects.requireNonNull(entity); + if (entity.getClass().getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + entity.getClass() + " must be on Annotation @Entity"); + } + if (columns.length < 1) { + throw new RuntimeException("update column length is zero "); + } + for (String val : columns) { + Objects.requireNonNull(val); + } + if (this.updateActions3 == null) { + this.updateActions3 = new HashMap<>(); + } + this.updateActions3.computeIfAbsent(entity.getClass(), c -> new ArrayList<>()).add(new UpdateBatchAction3(entity, node, columns)); + return this; + } + + @Override + public DataBatch updateColumn(T entity, SelectColumn selects) { + return updateColumn(entity, (FilterNode) null, selects); + } + + @Override + public DataBatch updateColumn(T entity, final FilterNode node, SelectColumn selects) { + Objects.requireNonNull(entity); + if (entity.getClass().getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + entity.getClass() + " must be on Annotation @Entity"); + } + Objects.requireNonNull(selects); + if (this.updateActions4 == null) { + this.updateActions4 = new HashMap<>(); + } + this.updateActions4.computeIfAbsent(entity.getClass(), c -> new ArrayList<>()).add(new UpdateBatchAction4(entity, node, selects)); + return this; + } + + } + + protected static class DeleteBatchAction1 { + + public Class clazz; + + public Serializable[] pks; + + public DeleteBatchAction1(Class clazz, Serializable... pks) { + this.clazz = clazz; + this.pks = pks; + } + } + + protected static class DeleteBatchAction2 { + + public Class clazz; + + public FilterNode node; + + public Flipper flipper; + + public DeleteBatchAction2(Class clazz, FilterNode node) { + this.clazz = clazz; + this.node = node; + } + + public DeleteBatchAction2(Class clazz, FilterNode node, Flipper flipper) { + this.clazz = clazz; + this.node = node; + this.flipper = flipper; + } + } + + protected static class UpdateBatchAction1 { + + public Class clazz; + + public Serializable pk; + + public ColumnValue[] values; + + public UpdateBatchAction1(Class clazz, Serializable pk, ColumnValue... values) { + this.clazz = clazz; + this.pk = pk; + this.values = values; + } + } + + protected static class UpdateBatchAction2 { + + public Class clazz; + + public FilterNode node; + + public Flipper flipper; + + public ColumnValue[] values; + + public UpdateBatchAction2(Class clazz, FilterNode node, ColumnValue... values) { + this.clazz = clazz; + this.node = node; + this.values = values; + } + + public UpdateBatchAction2(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values) { + this.clazz = clazz; + this.node = node; + this.flipper = flipper; + this.values = values; + } + } + + protected static class UpdateBatchAction3 { + + public Object entity; + + public FilterNode node; + + public String[] columns; + + public UpdateBatchAction3(Object entity, String... columns) { + this.entity = entity; + this.columns = columns; + } + + public UpdateBatchAction3(Object entity, FilterNode node, String... columns) { + this.entity = entity; + this.node = node; + this.columns = columns; + } + } + + protected static class UpdateBatchAction4 { + + public Object entity; + + public FilterNode node; + + public SelectColumn selects; + + public UpdateBatchAction4(Object entity, SelectColumn selects) { + this.entity = entity; + this.selects = selects; + } + + public UpdateBatchAction4(Object entity, FilterNode node, SelectColumn selects) { + this.entity = entity; + this.node = node; + this.selects = selects; + } + } } diff --git a/src/main/java/org/redkale/source/DataBatch.java b/src/main/java/org/redkale/source/DataBatch.java index 0ef63997c..fbe161cd1 100644 --- a/src/main/java/org/redkale/source/DataBatch.java +++ b/src/main/java/org/redkale/source/DataBatch.java @@ -4,8 +4,6 @@ package org.redkale.source; import java.io.Serializable; -import java.util.*; -import javax.persistence.Entity; import org.redkale.util.*; /** @@ -18,317 +16,40 @@ import org.redkale.util.*; * @author zhangjx */ @SuppressWarnings("unchecked") -public class DataBatch { - - //-------------------- 新增操作 -------------------- - @Comment("新增对象") - protected List insertEntitys; - - //-------------------- 删除操作 -------------------- - @Comment("删除对象") - protected List deleteEntitys; - - @Comment("删除对象") - protected List deleteActions1; - - @Comment("删除对象") - protected List deleteActions2; - - //-------------------- 修改操作 -------------------- - @Comment("修改对象") - protected List updateEntitys; - - @Comment("修改对象") - protected List updateActions1; - - @Comment("修改对象") - protected List updateActions2; - - @Comment("修改对象") - protected List updateActions3; - - @Comment("修改对象") - protected List updateActions4; - - protected DataBatch() { - } +public interface DataBatch { public static DataBatch create() { - return new DataBatch(); + return new AbstractDataSource.DefaultDataBatch(); } - public DataBatch insert(T... entitys) { - if (this.insertEntitys == null) { - this.insertEntitys = new ArrayList(); - } - for (T t : entitys) { - Objects.requireNonNull(t); - if (t.getClass().getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); - } - this.insertEntitys.add(t); - } - return this; - } + public DataBatch insert(T... entitys); - public DataBatch delete(T... entitys) { - if (this.deleteEntitys == null) { - this.deleteEntitys = new ArrayList(); - } - for (T t : entitys) { - Objects.requireNonNull(t); - if (t.getClass().getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); - } - this.deleteEntitys.add(t); - } - return this; - } + public DataBatch delete(T... entitys); - public DataBatch delete(Class clazz, Serializable... pks) { - Objects.requireNonNull(clazz); - if (clazz.getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); - } - if (pks.length < 1) { - throw new RuntimeException("delete pk length is zero "); - } - for (Serializable pk : pks) { - Objects.requireNonNull(pk); - } - if (this.deleteActions1 == null) { - this.deleteActions1 = new ArrayList(); - } - this.deleteActions1.add(new DeleteBatchAction1(clazz, pks)); - return this; - } + public DataBatch delete(Class clazz, Serializable... pks); - public DataBatch delete(Class clazz, FilterNode node) { - return delete(clazz, node, (Flipper) null); - } + public DataBatch delete(Class clazz, FilterNode node); - public DataBatch delete(Class clazz, FilterNode node, Flipper flipper) { - Objects.requireNonNull(clazz); - if (clazz.getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); - } - if (this.deleteActions2 == null) { - this.deleteActions2 = new ArrayList(); - } - this.deleteActions2.add(new DeleteBatchAction2(clazz, node, flipper)); - return this; - } + public DataBatch delete(Class clazz, FilterNode node, Flipper flipper); - public DataBatch update(T... entitys) { - if (this.updateEntitys == null) { - this.updateEntitys = new ArrayList(); - } - for (T t : entitys) { - Objects.requireNonNull(t); - if (t.getClass().getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + t.getClass() + " must be on Annotation @Entity"); - } - this.updateEntitys.add(t); - } - return this; - } + public DataBatch update(T... entitys); - public DataBatch update(Class clazz, Serializable pk, String column, Serializable value) { - return update(clazz, pk, ColumnValue.mov(column, value)); - } + public DataBatch update(Class clazz, Serializable pk, String column, Serializable value); - public DataBatch update(Class clazz, Serializable pk, ColumnValue... values) { - Objects.requireNonNull(clazz); - if (clazz.getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); - } - Objects.requireNonNull(pk); - if (values.length < 1) { - throw new RuntimeException("update column-value length is zero "); - } - for (ColumnValue val : values) { - Objects.requireNonNull(val); - } - if (this.updateActions1 == null) { - this.updateActions1 = new ArrayList(); - } - this.updateActions1.add(new UpdateBatchAction1(clazz, pk, values)); - return this; - } + public DataBatch update(Class clazz, Serializable pk, ColumnValue... values); - public DataBatch update(Class clazz, FilterNode node, String column, Serializable value) { - return update(clazz, node, (Flipper) null, ColumnValue.mov(column, value)); - } + public DataBatch update(Class clazz, FilterNode node, String column, Serializable value); - public DataBatch update(Class clazz, FilterNode node, ColumnValue... values) { - return update(clazz, node, (Flipper) null, values); - } + public DataBatch update(Class clazz, FilterNode node, ColumnValue... values); - public DataBatch update(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values) { - Objects.requireNonNull(clazz); - if (clazz.getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); - } - if (values.length < 1) { - throw new RuntimeException("update column-value length is zero "); - } - for (ColumnValue val : values) { - Objects.requireNonNull(val); - } - if (this.updateActions2 == null) { - this.updateActions2 = new ArrayList(); - } - this.updateActions2.add(new UpdateBatchAction2(clazz, node, flipper, values)); - return this; - } + public DataBatch update(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values); - public DataBatch updateColumn(T entity, final String... columns) { - return updateColumn(entity, (FilterNode) null, columns); - } + public DataBatch updateColumn(T entity, final String... columns); - public DataBatch updateColumn(T entity, final FilterNode node, final String... columns) { - Objects.requireNonNull(entity); - if (entity.getClass().getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + entity.getClass() + " must be on Annotation @Entity"); - } - if (columns.length < 1) { - throw new RuntimeException("update column length is zero "); - } - for (String val : columns) { - Objects.requireNonNull(val); - } - if (this.updateActions3 == null) { - this.updateActions3 = new ArrayList(); - } - this.updateActions3.add(new UpdateBatchAction3(entity, node, columns)); - return this; - } + public DataBatch updateColumn(T entity, final FilterNode node, final String... columns); - public DataBatch updateColumn(T entity, SelectColumn selects) { - return updateColumn(entity, (FilterNode) null, selects); - } + public DataBatch updateColumn(T entity, SelectColumn selects); - public DataBatch updateColumn(T entity, final FilterNode node, SelectColumn selects) { - Objects.requireNonNull(entity); - if (entity.getClass().getAnnotation(Entity.class) == null) { - throw new RuntimeException("Entity Class " + entity.getClass() + " must be on Annotation @Entity"); - } - Objects.requireNonNull(selects); - if (this.updateActions4 == null) { - this.updateActions4 = new ArrayList(); - } - this.updateActions4.add(new UpdateBatchAction4(entity, node, selects)); - return this; - } + public DataBatch updateColumn(T entity, final FilterNode node, SelectColumn selects); - static class DeleteBatchAction1 { - - public Class clazz; - - public Serializable[] pks; - - public DeleteBatchAction1(Class clazz, Serializable... pks) { - this.clazz = clazz; - this.pks = pks; - } - } - - static class DeleteBatchAction2 { - - public Class clazz; - - public FilterNode node; - - public Flipper flipper; - - public DeleteBatchAction2(Class clazz, FilterNode node) { - this.clazz = clazz; - this.node = node; - } - - public DeleteBatchAction2(Class clazz, FilterNode node, Flipper flipper) { - this.clazz = clazz; - this.node = node; - this.flipper = flipper; - } - } - - static class UpdateBatchAction1 { - - public Class clazz; - - public Serializable pk; - - public ColumnValue[] values; - - public UpdateBatchAction1(Class clazz, Serializable pk, ColumnValue... values) { - this.clazz = clazz; - this.pk = pk; - this.values = values; - } - } - - static class UpdateBatchAction2 { - - public Class clazz; - - public FilterNode node; - - public Flipper flipper; - - public ColumnValue[] values; - - public UpdateBatchAction2(Class clazz, FilterNode node, ColumnValue... values) { - this.clazz = clazz; - this.node = node; - this.values = values; - } - - public UpdateBatchAction2(Class clazz, FilterNode node, Flipper flipper, ColumnValue... values) { - this.clazz = clazz; - this.node = node; - this.flipper = flipper; - this.values = values; - } - } - - static class UpdateBatchAction3 { - - public Object entity; - - public FilterNode node; - - public String[] columns; - - public UpdateBatchAction3(Object entity, String... columns) { - this.entity = entity; - this.columns = columns; - } - - public UpdateBatchAction3(Object entity, FilterNode node, String... columns) { - this.entity = entity; - this.node = node; - this.columns = columns; - } - } - - static class UpdateBatchAction4 { - - public Object entity; - - public FilterNode node; - - public SelectColumn selects; - - public UpdateBatchAction4(Object entity, SelectColumn selects) { - this.entity = entity; - this.selects = selects; - } - - public UpdateBatchAction4(Object entity, FilterNode node, SelectColumn selects) { - this.entity = entity; - this.node = node; - this.selects = selects; - } - } }