瀏覽代碼

1、`NamedTable`、`Where`增加`clone`方法

mxd 3 年之前
父節點
當前提交
20709c12a9

+ 42 - 3
magic-api/src/main/java/org/ssssssss/magicapi/modules/table/NamedTable.java

@@ -7,11 +7,14 @@ import org.ssssssss.magicapi.modules.BoundSql;
 import org.ssssssss.magicapi.modules.SQLModule;
 import org.ssssssss.script.annotation.Comment;
 
+import java.io.Serializable;
 import java.util.*;
 import java.util.function.Function;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 public class NamedTable {
+
 	String tableName;
 
 	SQLModule sqlModule;
@@ -62,6 +65,30 @@ public class NamedTable {
 		}
 	}
 
+	private NamedTable() {
+	}
+
+	@Comment("克隆")
+	public NamedTable clone(){
+		NamedTable namedTable = new NamedTable();
+		namedTable.tableName = this.tableName;
+		namedTable.sqlModule = this.sqlModule;
+		namedTable.primary = this.primary;
+		namedTable.logicDeleteValue = this.logicDeleteValue;
+		namedTable.logicDeleteColumn = this.logicDeleteColumn;
+		namedTable.columns = new HashMap<>(this.columns);
+		namedTable.fields = new ArrayList<>(fields);
+		namedTable.groups = new ArrayList<>(groups);
+		namedTable.orders = new ArrayList<>(orders);
+		namedTable.excludeColumns = new HashSet<>(excludeColumns);
+		namedTable.rowMapColumnMapper = this.rowMapColumnMapper;
+		namedTable.defaultPrimaryValue = this.defaultPrimaryValue;
+		namedTable.useLogic = this.useLogic;
+		namedTable.withBlank = this.withBlank;
+		namedTable.where = new Where(namedTable);
+		return namedTable;
+	}
+
 	@Comment("使用逻辑删除")
 	public NamedTable logic() {
 		this.useLogic = true;
@@ -76,11 +103,19 @@ public class NamedTable {
 
 	@Comment("设置主键名,update时使用")
 	public NamedTable primary(String primary) {
-		return primary(primary, null);
+		this.primary = rowMapColumnMapper.apply(primary);
+		return this;
 	}
 
 	@Comment("设置主键名,并设置默认主键值(主要用于insert)")
-	public NamedTable primary(String primary, Object defaultPrimaryValue) {
+	public NamedTable primary(String primary, Serializable defaultPrimaryValue) {
+		this.primary = rowMapColumnMapper.apply(primary);
+		this.defaultPrimaryValue = defaultPrimaryValue;
+		return this;
+	}
+
+	@Comment("设置主键名,并设置默认主键值(主要用于insert)")
+	public NamedTable primary(String primary, Supplier<Object> defaultPrimaryValue) {
 		this.primary = rowMapColumnMapper.apply(primary);
 		this.defaultPrimaryValue = defaultPrimaryValue;
 		return this;
@@ -194,7 +229,11 @@ public class NamedTable {
 			data.forEach((key, value) -> this.columns.put(rowMapColumnMapper.apply(key), value));
 		}
 		if (this.defaultPrimaryValue != null && StringUtils.isBlank(Objects.toString(this.columns.getOrDefault(this.primary, "")))) {
-			this.columns.put(this.primary, this.defaultPrimaryValue);
+			if (this.defaultPrimaryValue instanceof Supplier){
+				this.columns.put(this.primary, ((Supplier<?>) this.defaultPrimaryValue).get());
+			}else{
+				this.columns.put(this.primary, this.defaultPrimaryValue);
+			}
 		}
 		Collection<Map.Entry<String, Object>> entries = filterNotBlanks();
 		if (entries.isEmpty()) {

+ 10 - 0
magic-api/src/main/java/org/ssssssss/magicapi/modules/table/Where.java

@@ -30,6 +30,16 @@ public class Where {
 		this.needWhere = needWhere;
 	}
 
+	@Comment("克隆")
+	public Where clone(){
+		Where where = new Where(this.namedTable, this.needWhere);
+		where.tokens.addAll(this.tokens);
+		where.params.addAll(this.params);
+		where.notNull = this.notNull;
+		where.notBlank = this.notBlank;
+		return where;
+	}
+
 	void appendAnd() {
 		remove();
 		tokens.add("and");