Skip to content

Commit

Permalink
fix: 修复无法根据带有引用符的名称获取元数据
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jun 28, 2024
1 parent 11de673 commit 0ef1207
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Setter;
import lombok.SneakyThrows;
import org.hswebframework.ezorm.core.CastUtil;
import org.hswebframework.ezorm.core.utils.StringUtils;
import reactor.core.publisher.Mono;

import java.util.*;
Expand All @@ -13,7 +14,7 @@
import static java.util.Optional.*;

public abstract class AbstractDatabaseMetadata<S extends SchemaMetadata>
implements DatabaseMetadata<S>, FeatureSupportedMetadata {
implements DatabaseMetadata<S>, FeatureSupportedMetadata {

private Map<String, S> schemas = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -59,11 +60,12 @@ public <T extends ObjectMetadata> Optional<T> getObject(String name, BiFunction<
return empty();
}
if (name.contains(".")) {
String[] arr = name.split("[.]");
return this.getSchema(arr[0])
.flatMap(schema -> mapper.apply(schema, arr[1]));
String[] arr = StringUtils.getPlainName(name.split("[.]"));
return this
.getSchema(arr[0])
.flatMap(schema -> mapper.apply(schema, arr[1]));
}
return mapper.apply(this.getCurrentSchema(), name);
return mapper.apply(this.getCurrentSchema(), StringUtils.getPlainName(name));
}

@Override
Expand All @@ -72,11 +74,12 @@ public <T extends ObjectMetadata> Mono<T> getObjectReactive(String name, BiFunct
return Mono.empty();
}
if (name.contains(".")) {
String[] arr = name.split("[.]");
return Mono.justOrEmpty(this.getSchema(arr[0]))
.flatMap(schema -> mapper.apply(schema, arr[1]));
String[] arr = StringUtils.getPlainName(name.split("[.]"));
return Mono
.justOrEmpty(this.getSchema(arr[0]))
.flatMap(schema -> mapper.apply(schema, arr[1]));
}
return mapper.apply(this.getCurrentSchema(), name);
return mapper.apply(this.getCurrentSchema(), StringUtils.getPlainName(name));
}

@Override
Expand All @@ -99,10 +102,10 @@ public AbstractDatabaseMetadata<S> clone() {
AbstractDatabaseMetadata<S> metadata = (AbstractDatabaseMetadata) super.clone();
metadata.schemas = new ConcurrentHashMap<>();
getSchemas()
.stream()
.map(SchemaMetadata::clone)
.map(CastUtil::<S>cast)
.forEach(metadata::addSchema);
.stream()
.map(SchemaMetadata::clone)
.map(CastUtil::<S>cast)
.forEach(metadata::addSchema);

metadata.features = new ConcurrentHashMap<>(getFeatures());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.CastUtil;
import org.hswebframework.ezorm.core.FeatureId;
import org.hswebframework.ezorm.core.utils.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -90,10 +90,11 @@ protected <T extends ObjectMetadata> List<T> loadMetadata(ObjectType type) {
}

protected <T extends ObjectMetadata> T loadMetadata(ObjectType type, String name) {
String fName = StringUtils.getPlainName(name);
return getParser(type)
.flatMap(parser -> {
log.debug("load {} metadata {} ,use parser:{}", type, name, parser.getClass().getSimpleName());
return parser.parseByName(name);
log.debug("load {} metadata {} ,use parser:{}", type, fName, parser.getClass().getSimpleName());
return parser.parseByName(fName);
})
.map(CastUtil::<T>cast)
.orElse(null);
Expand All @@ -106,12 +107,13 @@ protected <T extends ObjectMetadata> Flux<T> loadMetadataReactive(ObjectType typ
}

protected <T extends ObjectMetadata> Mono<T> loadMetadataReactive(ObjectType type, String name) {
String fName = StringUtils.getPlainName(name);
return Mono.justOrEmpty(getParser(type))
.flatMap(parser -> {
log.debug("reactive load {} [{}] metadata ,use parser:{}", type, name, parser
log.debug("reactive load {} [{}] metadata ,use parser:{}", type, fName, parser
.getClass()
.getSimpleName());
return parser.parseByNameReactive(name);
return parser.parseByNameReactive(fName);
})
.map(CastUtil::cast);
}
Expand Down Expand Up @@ -140,20 +142,20 @@ public void addObject(ObjectMetadata metadata) {
@Override
public <T extends ObjectMetadata> Optional<T> removeObject(ObjectType type, String name) {
Objects.requireNonNull(name, "name");

return ofNullable(metaRepository.get(type.getId()))
.map(repo -> repo.remove(name))
.map(repo -> repo.remove(StringUtils.getPlainName(name)))
.map(CastUtil::cast);
}

public <T extends ObjectMetadata> Mono<T> getObjectReactive(ObjectType type, String name, boolean autoLoad) {
Objects.requireNonNull(name, "name");
String fname = StringUtils.getPlainName(name);
Map<String, ObjectMetadata> mapping = metaRepository.computeIfAbsent(type.getId(), t -> new ConcurrentHashMap<>());

ObjectMetadata metadata = getLoadedObject0(mapping, name);
ObjectMetadata metadata = getLoadedObject0(mapping, fname);
if (metadata == null && autoLoad) {
return loadMetadataReactive(type, name)
.doOnNext(obj -> mapping.put(name, obj))
return loadMetadataReactive(type, fname)
.doOnNext(obj -> mapping.put(fname, obj))
.map(CastUtil::cast);
}
return Mono.justOrEmpty(CastUtil.<T>cast(metadata));
Expand All @@ -179,6 +181,7 @@ public <T extends ObjectMetadata> Optional<T> getObject(ObjectType type, String
@Override
public <T extends ObjectMetadata> Optional<T> getObject(ObjectType type, String name, boolean autoLoad) {
Objects.requireNonNull(name, "name");
name = StringUtils.getPlainName(name);
Map<String, ObjectMetadata> mapping = metaRepository.computeIfAbsent(type.getId(), t -> new ConcurrentHashMap<>());

ObjectMetadata metadata = getLoadedObject0(mapping, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,20 @@ public static String toLowerCaseFirstOne(String str) {
return new String(chars);
}
}


public static String[] getPlainName(String[] name) {
for (int i = 0; i < name.length; i++) {
name[i] = getPlainName(name[i]);
}
return name;
}

public static String getPlainName(String name) {
char str = name.charAt(0);
if (str == '`' || str == '\"' || str == '[') {
return name.substring(1, name.length() - 1);
}
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void initMapping(Class<E> entityType) {

this.mapping = LazyEntityColumnMapping.of(() -> getTable()
.<EntityColumnMapping>findFeature(MappingFeatureType.columnPropertyMapping.createFeatureId(entityType))
.orElseThrow(() -> new UnsupportedOperationException("unsupported columnPropertyMapping feature")));
.orElseThrow(() -> new UnsupportedOperationException("unsupported columnPropertyMapping feature for "+entityType)));
defaultContextKeyValue.add(MappingContextKeys.columnMapping(mapping));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public void addColumn(RDBColumnMetadata column) {
column.setOwner(this);
allColumns.put(column.getName(), column);
allColumns.put(column.getAlias(), column);
allColumns.put(column.getRealName(),column);
if (onColumnAdded != null) {
onColumnAdded.accept(column);
}
Expand All @@ -150,12 +151,13 @@ public void addColumn(RDBColumnMetadata column) {

@Override
public List<RDBColumnMetadata> getColumns() {
return new ArrayList<>(allColumns
.values()
.stream()
.sorted()
.collect(Collectors.toMap(RDBColumnMetadata::getName, Function.identity(), (_1, _2) -> _1, LinkedHashMap::new))
.values());
return new ArrayList<>(
allColumns
.values()
.stream()
.sorted()
.collect(Collectors.toMap(RDBColumnMetadata::getName, Function.identity(), (_1, _2) -> _1, LinkedHashMap::new))
.values());
}

@Override
Expand All @@ -177,7 +179,7 @@ public Optional<RDBColumnMetadata> getColumn(String name) {
if (StringUtils.isNullOrEmpty(name)) {
return Optional.empty();
}
return Optional.ofNullable(allColumns.get(name));
return Optional.ofNullable(allColumns.get(StringUtils.getPlainName(name)));
}

@Override
Expand All @@ -194,7 +196,7 @@ private Optional<RDBColumnMetadata> findNestColumn(String name) {
}

if (name.contains(".")) {
String[] arr = name.split("[.]");
String[] arr = StringUtils.getPlainName(name.split("[.]"));
if (arr.length == 2) { //table.name
return findColumnFromSchema(schema, arr[0], arr[1]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public Optional<TableOrViewMetadata> getTableOrView(String name) {
return this.getObject(name, RDBSchemaMetadata::getTableOrView);
}

public Optional<TableOrViewMetadata> getTableOrView(String name, boolean autoLoad) {
return this.getObject(name, (schema, _name) -> schema.getTableOrView(_name, autoLoad));
}

public Optional<RDBTableMetadata> getTable(String name) {
return this.getObject(name, RDBSchemaMetadata::getTable);
}
Expand All @@ -29,6 +33,10 @@ public Mono<TableOrViewMetadata> getTableOrViewReactive(String name) {
return this.getObjectReactive(name, RDBSchemaMetadata::getTableOrViewReactive);
}

public Mono<TableOrViewMetadata> getTableOrViewReactive(String name, boolean autoLoad) {
return this.getObjectReactive(name, (schema, _name) -> schema.getTableOrViewReactive(_name, autoLoad));
}

public Mono<RDBTableMetadata> getTableReactive(String name) {
return this.getObjectReactive(name, RDBSchemaMetadata::getTableReactive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ public void addTable(RDBTableMetadata metadata) {

public Mono<TableOrViewMetadata> findTableOrViewReactive(String name) {

return getTableOrViewReactive(name, false)
.switchIfEmpty(getDatabase().getTableOrViewReactive(name));
return this
.getTableOrViewReactive(name, false)
.switchIfEmpty(getDatabase().getTableOrViewReactive(name, false));
}

public Optional<TableOrViewMetadata> findTableOrView(String name) {
Optional<TableOrViewMetadata> current = getTableOrView(name, false);
if (current.isPresent()) {
return current;
}
return getDatabase().getTableOrView(name);
return getDatabase().getTableOrView(name, false);
}

public Optional<TableOrViewMetadata> getTableOrView(String name, boolean autoLoad) {
return Optional.of(getTable(name, autoLoad)
.map(AbstractTableOrViewMetadata.class::cast))
.filter(Optional::isPresent)
.orElseGet(() -> getView(name, autoLoad)
.map(AbstractTableOrViewMetadata.class::cast))
.map(TableOrViewMetadata.class::cast);
return Optional
.of(getTable(name, autoLoad).map(AbstractTableOrViewMetadata.class::cast))
.filter(Optional::isPresent)
.orElseGet(() -> getView(name, autoLoad).map(AbstractTableOrViewMetadata.class::cast))
.map(TableOrViewMetadata.class::cast);
}

public Optional<TableOrViewMetadata> getTableOrView(String name) {
Expand Down

0 comments on commit 0ef1207

Please sign in to comment.