From 275a71cc16aafcffe529452af128a8c259dfb749 Mon Sep 17 00:00:00 2001 From: zhou-hao Date: Fri, 18 Dec 2020 20:39:19 +0800 Subject: [PATCH] =?UTF-8?q?ddl=E5=A2=9E=E5=8A=A0merge=E9=80=89=E9=A1=B9,?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEfalse=E6=97=B6=E7=9B=B4=E6=8E=A5=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metadata/AbstractTableOrViewMetadata.java | 13 +++++++++++-- .../ezorm/rdb/metadata/RDBTableMetadata.java | 1 - .../rdb/metadata/TableOrViewMetadata.java | 2 ++ .../rdb/operator/ddl/DefaultTableBuilder.java | 19 +++++++++++++++++-- .../ezorm/rdb/operator/ddl/TableBuilder.java | 2 ++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/AbstractTableOrViewMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/AbstractTableOrViewMetadata.java index 4510e97c..3c3cd6ff 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/AbstractTableOrViewMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/AbstractTableOrViewMetadata.java @@ -17,6 +17,7 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function; import java.util.stream.Collectors; @@ -47,9 +48,9 @@ public RDBColumnMetadata get(Object key) { } }; - protected List foreignKey = new ArrayList<>(); + protected List foreignKey = new CopyOnWriteArrayList<>(); - protected Map features = new HashMap<>(); + protected Map features = new ConcurrentHashMap<>(); public AbstractTableOrViewMetadata() { //注册默认的where条件构造器 @@ -218,4 +219,12 @@ public void merge(TableOrViewMetadata metadata) { metadata.getColumns().forEach(this::addColumn); } + + @Override + public void replace(TableOrViewMetadata metadata) { + foreignKey.clear(); + features.clear(); + allColumns.clear(); + merge(metadata); + } } diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBTableMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBTableMetadata.java index a7eab824..c15ea540 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBTableMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBTableMetadata.java @@ -85,6 +85,5 @@ public RDBTableMetadata clone() { @Override public void merge(TableOrViewMetadata metadata) { super.merge(metadata); - } } diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/TableOrViewMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/TableOrViewMetadata.java index 4cd32912..194133df 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/TableOrViewMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/TableOrViewMetadata.java @@ -155,4 +155,6 @@ default List findFeatures() { } void merge(TableOrViewMetadata metadata); + + void replace(TableOrViewMetadata metadata); } diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/DefaultTableBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/DefaultTableBuilder.java index df30245c..e3abc6bc 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/DefaultTableBuilder.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/DefaultTableBuilder.java @@ -28,6 +28,7 @@ public class DefaultTableBuilder implements TableBuilder { private boolean dropColumn = false; private boolean allowAlter = true; private boolean autoLoad = true; + private boolean merge=true; public DefaultTableBuilder(RDBTableMetadata table) { this.table = table; @@ -110,6 +111,12 @@ public TableBuilder autoLoad(boolean autoLoad) { return this; } + @Override + public TableBuilder merge(boolean merge) { + this.merge=merge; + return this; + } + private SqlRequest buildAlterSql(RDBTableMetadata oldTable) { return schema .findFeatureNow(AlterTableSqlBuilder.ID) @@ -134,7 +141,11 @@ public Boolean sync() { if (oldTable != null) { sqlRequest = buildAlterSql(oldTable); - whenComplete = () -> oldTable.merge(table); + if(merge){ + whenComplete = () -> oldTable.merge(table); + }else { + whenComplete = () -> oldTable.replace(table); + } } else { //create sqlRequest = schema.findFeatureNow(CreateTableSqlBuilder.ID).build(table); @@ -162,7 +173,11 @@ public Mono reactive() { .map(oldTable -> { SqlRequest request = buildAlterSql(oldTable); if (request.isEmpty()) { - oldTable.merge(table); + if(merge) { + oldTable.merge(table); + }else { + oldTable.replace(table); + } return Mono.just(true); } return sqlExecutor.execute(request) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/TableBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/TableBuilder.java index f06c49ea..5f2540da 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/TableBuilder.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/ddl/TableBuilder.java @@ -29,6 +29,8 @@ public interface TableBuilder { TableBuilder autoLoad(boolean autoLoad); + TableBuilder merge(boolean merge); + IndexBuilder index(); ForeignKeyDSLBuilder foreignKey();