diff --git a/src/main/java/org/redkale/source/AbstractDataSource.java b/src/main/java/org/redkale/source/AbstractDataSource.java index 399926ecf..31611886e 100644 --- a/src/main/java/org/redkale/source/AbstractDataSource.java +++ b/src/main/java/org/redkale/source/AbstractDataSource.java @@ -167,7 +167,7 @@ public abstract class AbstractDataSource extends AbstractService implements Data public String username; public String password; - + public String encoding; @Override @@ -297,7 +297,9 @@ public abstract class AbstractDataSource extends AbstractService implements Data for (T val : entitys) { if (clazz == null) { clazz = val.getClass(); - if (clazz.getAnnotation(Entity.class) == null) throw new RuntimeException("Entity Class " + clazz + " must be on annotation @Entity"); + if (clazz.getAnnotation(Entity.class) == null) { + throw new RuntimeException("Entity Class " + clazz + " must be on Annotation @Entity"); + } continue; } if (clazz != val.getClass()) { diff --git a/src/main/java/org/redkale/source/ColumnValue.java b/src/main/java/org/redkale/source/ColumnValue.java index 8d2eabfb6..7893e55ba 100644 --- a/src/main/java/org/redkale/source/ColumnValue.java +++ b/src/main/java/org/redkale/source/ColumnValue.java @@ -6,6 +6,7 @@ package org.redkale.source; import java.io.Serializable; +import java.util.Objects; import static org.redkale.source.ColumnExpress.*; /** @@ -34,6 +35,7 @@ public class ColumnValue { } public ColumnValue(String column, ColumnExpress express, Serializable value) { + Objects.requireNonNull(column); this.column = column; this.express = express == null ? ColumnExpress.MOV : express; this.value = value; diff --git a/src/main/java/org/redkale/source/DataBatch.java b/src/main/java/org/redkale/source/DataBatch.java new file mode 100644 index 000000000..0ef63997c --- /dev/null +++ b/src/main/java/org/redkale/source/DataBatch.java @@ -0,0 +1,334 @@ +/* + * + */ +package org.redkale.source; + +import java.io.Serializable; +import java.util.*; +import javax.persistence.Entity; +import org.redkale.util.*; + +/** + * DataSource批量操作对象,操作类型只能是增删改
+ * 非线程安全类 + * + *

+ * 详情见: https://redkale.org + * + * @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 static DataBatch create() { + return new DataBatch(); + } + + 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 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(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, FilterNode node) { + return delete(clazz, node, (Flipper) null); + } + + 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 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(Class clazz, Serializable pk, String column, Serializable value) { + return update(clazz, pk, ColumnValue.mov(column, 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, FilterNode node, String column, Serializable value) { + return update(clazz, node, (Flipper) null, ColumnValue.mov(column, value)); + } + + public DataBatch update(Class clazz, FilterNode node, ColumnValue... values) { + return update(clazz, node, (Flipper) null, 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 updateColumn(T entity, final String... columns) { + return updateColumn(entity, (FilterNode) null, 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, SelectColumn selects) { + return updateColumn(entity, (FilterNode) null, 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; + } + + 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; + } + } +} diff --git a/src/main/java/org/redkale/source/DataSource.java b/src/main/java/org/redkale/source/DataSource.java index 4a82502c4..29f60c4e1 100644 --- a/src/main/java/org/redkale/source/DataSource.java +++ b/src/main/java/org/redkale/source/DataSource.java @@ -58,8 +58,26 @@ public interface DataSource extends Resourcable { */ public void compile(Class clazz); + //----------------------batchAsync----------------------------- + /** + * 增删改的批量操作 + * + * @param bath 批量对象 + * + * @return -1表示失败,正数为成功 + */ + //public int batch(final DataBatch bath); + + /** + * 增删改的批量操作 + * + * @param bath 批量对象 + * + * @return -1表示失败,正数为成功 + */ + //public CompletableFuture batchAsync(final DataBatch bath); + //----------------------insertAsync----------------------------- - //insert 暂时不支持Collection、Stream, 因为存在@RpcCall问题 /** * 新增记录, 多对象必须是同一个Entity类且必须在同一张表中
*