Skip to content

Commit

Permalink
ddl增加merge选项,设置false时直接替换
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 18, 2020
1 parent a87bf79 commit 275a71c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,9 +48,9 @@ public RDBColumnMetadata get(Object key) {
}
};

protected List<ForeignKeyMetadata> foreignKey = new ArrayList<>();
protected List<ForeignKeyMetadata> foreignKey = new CopyOnWriteArrayList<>();

protected Map<String, Feature> features = new HashMap<>();
protected Map<String, Feature> features = new ConcurrentHashMap<>();

public AbstractTableOrViewMetadata() {
//注册默认的where条件构造器
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@ public RDBTableMetadata clone() {
@Override
public void merge(TableOrViewMetadata metadata) {
super.merge(metadata);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,6 @@ default List<Feature> findFeatures() {
}

void merge(TableOrViewMetadata metadata);

void replace(TableOrViewMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -162,7 +173,11 @@ public Mono<Boolean> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface TableBuilder {

TableBuilder autoLoad(boolean autoLoad);

TableBuilder merge(boolean merge);

IndexBuilder index();

ForeignKeyDSLBuilder foreignKey();
Expand Down

0 comments on commit 275a71c

Please sign in to comment.