Skip to content

Commit

Permalink
refactor: 优化表名称逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jun 26, 2024
1 parent 2ad7d62 commit 017df98
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public <T extends ObjectMetadata> List<T> getObject(ObjectType type) {
if (typeMapping == null) {

List<T> all = loadMetadata(type);
Map<String, ObjectMetadata> group = all.stream()
.collect(toMap(ObjectMetadata::getName, Function.identity(), (_1, _2) -> _1, ConcurrentHashMap::new));
Map<String, ObjectMetadata> group = all
.stream()
.collect(toMap(ObjectMetadata::getName, Function.identity(), (_1, _2) -> _1, ConcurrentHashMap::new));

typeMapping = metaRepository.put(type.getId(), group);
if (typeMapping != null) {
Expand All @@ -68,34 +69,34 @@ public <T extends ObjectMetadata> Flux<T> getObjectReactive(ObjectType type) {
Map<String, ObjectMetadata> typeMapping = metaRepository.get(type.getId());
if (typeMapping == null) {
return loadMetadataReactive(type)
.collectMap(ObjectMetadata::getName, Function.identity())
.flatMapMany(group -> {
Map<String, ObjectMetadata> mapping = metaRepository.put(type.getId(), group);
if (mapping != null) {
mapping.forEach(group::putIfAbsent);
}
return Flux.fromIterable(group.values());
})
.map(CastUtil::cast);
.collectMap(ObjectMetadata::getName, Function.identity())
.flatMapMany(group -> {
Map<String, ObjectMetadata> mapping = metaRepository.put(type.getId(), group);
if (mapping != null) {
mapping.forEach(group::putIfAbsent);
}
return Flux.fromIterable(group.values());
})
.map(CastUtil::cast);
}
return Flux.fromIterable(typeMapping.values()).map(CastUtil::cast);
}

protected <T extends ObjectMetadata> List<T> loadMetadata(ObjectType type) {
return getParser(type)
.map(ObjectMetadataParser::parseAll)
.map(CastUtil::<List<T>>cast)
.orElseGet(Collections::emptyList);
.map(ObjectMetadataParser::parseAll)
.map(CastUtil::<List<T>>cast)
.orElseGet(Collections::emptyList);
}

protected <T extends ObjectMetadata> T loadMetadata(ObjectType type, String name) {
return getParser(type)
.flatMap(parser -> {
log.debug("load {} metadata {} ,use parser:{}", type, name, parser.getClass().getSimpleName());
return parser.parseByName(name);
})
.map(CastUtil::<T>cast)
.orElse(null);
.flatMap(parser -> {
log.debug("load {} metadata {} ,use parser:{}", type, name, parser.getClass().getSimpleName());
return parser.parseByName(name);
})
.map(CastUtil::<T>cast)
.orElse(null);
}

protected <T extends ObjectMetadata> Flux<T> loadMetadataReactive(ObjectType type) {
Expand All @@ -108,8 +109,8 @@ protected <T extends ObjectMetadata> Mono<T> loadMetadataReactive(ObjectType typ
return Mono.justOrEmpty(getParser(type))
.flatMap(parser -> {
log.debug("reactive load {} [{}] metadata ,use parser:{}", type, name, parser
.getClass()
.getSimpleName());
.getClass()
.getSimpleName());
return parser.parseByNameReactive(name);
})
.map(CastUtil::cast);
Expand All @@ -125,9 +126,10 @@ protected Optional<ObjectMetadataParser> getParser(ObjectType type) {
}

public void addObject(ObjectMetadata metadata) {
Map<String, ObjectMetadata> repo = metaRepository.computeIfAbsent(metadata
.getObjectType()
.getId(), t -> new ConcurrentHashMap<>());
Map<String, ObjectMetadata> repo = metaRepository
.computeIfAbsent(metadata
.getObjectType()
.getId(), t -> new ConcurrentHashMap<>());

repo.put(metadata.getName(), metadata);
if (metadata.getAlias() != null) {
Expand All @@ -140,23 +142,29 @@ public <T extends ObjectMetadata> Optional<T> removeObject(ObjectType type, Stri
Objects.requireNonNull(name, "name");

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

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

if (mapping.get(name) == null && autoLoad) {
ObjectMetadata metadata = getLoadedObject0(mapping, name);
if (metadata == null && autoLoad) {
return loadMetadataReactive(type, name)
.doOnNext(obj -> mapping.put(name, obj))
.map(CastUtil::cast);
.doOnNext(obj -> mapping.put(name, obj))
.map(CastUtil::cast);
}
return Mono
.justOrEmpty(mapping.get(name))
.map(CastUtil::cast)
;
return Mono.justOrEmpty(CastUtil.<T>cast(metadata));
}

protected ObjectMetadata getLoadedObject0(Map<String, ObjectMetadata> from, String name) {
ObjectMetadata metadata = from.get(name);
if (metadata == null) {
metadata = from.get(name.toLowerCase());
}
return metadata;
}

public <T extends ObjectMetadata> Mono<T> getObjectReactive(ObjectType type, String name) {
Expand All @@ -171,9 +179,22 @@ 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");
return of(metaRepository.computeIfAbsent(type.getId(), t -> new ConcurrentHashMap<>()))
.map(repo -> repo.computeIfAbsent(name, __ -> autoLoad ? loadMetadata(type, name) : null))
.map(CastUtil::cast);
Map<String, ObjectMetadata> mapping = metaRepository.computeIfAbsent(type.getId(), t -> new ConcurrentHashMap<>());

ObjectMetadata metadata = getLoadedObject0(mapping, name);
if (metadata == null && autoLoad) {
synchronized (this) {
metadata = getLoadedObject0(mapping, name);
if (metadata != null) {
return Optional.of(CastUtil.cast(metadata));
}
metadata = loadMetadata(type, name);
if (null != metadata) {
mapping.put(name, metadata);
}
}
}
return Optional.ofNullable(CastUtil.cast(metadata));
}

public void addFeature(Feature feature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public abstract class AbstractTableOrViewMetadata implements TableOrViewMetadata

private RDBSchemaMetadata schema;

private String realName;

@Setter
private Consumer<RDBColumnMetadata> onColumnAdded;

Expand Down Expand Up @@ -90,6 +92,10 @@ public String getFullName() {
return fullName == null ? fullName = TableOrViewMetadata.super.getFullName() : fullName;
}

public String getRealName() {
return realName == null ? name : realName;
}

public boolean isTable() {
return this instanceof RDBTableMetadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,23 @@ default void fireEvent(EventType eventType, Consumer<EventContext> contextConsum
}
}

default String getRealName() {
return getName();
}

default String getFullName() {
return StringUtils.concat(
getSchema().getName(),
".",
getName()
getRealName()
);
}

default String getQuoteName() {

return getDialect().quote(getSchema().getName(), false)
+ "."
+ getDialect().quote(getName(), false);
+ getDialect().quote(getRealName(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ protected Flux<RDBTableMetadata> fastParseAllReactive() {
.doOnNext(record -> {
String tableName = record
.getString("table_name")
.map(String::toLowerCase)
.orElseThrow(() -> new NullPointerException("table_name is null"));
RDBTableMetadata tableMetadata = metadata.computeIfAbsent(tableName, __t -> {
RDBTableMetadata metaData = createTable(__t);
metaData.setName(__t);
metaData.setAlias(__t);
String lowerName = __t.toLowerCase();
RDBTableMetadata metaData = createTable(lowerName);
metaData.setName(lowerName);
metaData.setAlias(lowerName);
metaData.setRealName(__t);
return metaData;
});
RDBColumnMetadata column = tableMetadata.newColumn();
Expand Down Expand Up @@ -270,12 +271,13 @@ protected List<RDBTableMetadata> fastParseAll() {
consumer(new RecordResultWrapper(), record -> {
String tableName = record
.getString("table_name")
.map(String::toLowerCase)
.orElseThrow(() -> new NullPointerException("table_name is null"));
RDBTableMetadata tableMetadata = metadata.computeIfAbsent(tableName, __t -> {
RDBTableMetadata metaData = createTable(__t);
metaData.setName(__t);
metaData.setAlias(__t);
String lowerName = __t.toLowerCase();
RDBTableMetadata metaData = createTable(lowerName);
metaData.setName(lowerName);
metaData.setAlias(lowerName);
metaData.setRealName(__t);
return metaData;
});
RDBColumnMetadata column = tableMetadata.newColumn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.hswebframework.ezorm.rdb.metadata.dialect.Dialect;
import org.hswebframework.ezorm.rdb.operator.DatabaseOperator;
import org.hswebframework.ezorm.rdb.operator.DefaultDatabaseOperator;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.NativeSql;
import org.hswebframework.ezorm.rdb.operator.dml.Terms;
import org.hswebframework.ezorm.rdb.operator.dml.insert.InsertOperator;
import org.hswebframework.ezorm.rdb.operator.dml.query.NativeSelectColumn;
Expand Down Expand Up @@ -576,6 +577,33 @@ public void testEnums() {

}

@Test
public void testUpdateByNative(){
BasicTestEntity entity = BasicTestEntity
.builder()
.id("testUpdateByNative")
.name("testUpdateByNative")
.state((byte) 1)
.build();

repository
.insert(entity)
.as(StepVerifier::create)
.expectNext(1)
.verifyComplete();

repository.createUpdate()
.set(BasicTestEntity::getState, NativeSql.of("state + 1"))
.where(entity::getId)
.lte(BasicTestEntity::getState,NativeSql.of("state + 1"))
.execute()
.as(StepVerifier::create)
.expectNext(1)
.verifyComplete();


}

@Test
public void testNotOrNULL() {
BasicTestEntity entity = BasicTestEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.hswebframework.ezorm.rdb.executor.SqlRequests;
import org.hswebframework.ezorm.rdb.executor.SyncSqlExecutor;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.metadata.RDBFeatureType;
import org.hswebframework.ezorm.rdb.metadata.RDBTableMetadata;
import org.hswebframework.ezorm.rdb.operator.builder.Paginator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -53,15 +55,39 @@ public void testParseReactive() {
// .verify();
}

@Test
public void testParseAll() {
executor.execute(SqlRequests.of("CREATE TABLE \"test_table_all\"(" +
"\"id\" varchar2(32) primary key," +
"name varchar2(128) not null," +
"age number(10)" +
")"));
try {
for (RDBTableMetadata rdbTableMetadata : parser.parseAll()) {
System.out.println(rdbTableMetadata.getName() + " => " + rdbTableMetadata.getRealName());
schema.addTable(rdbTableMetadata);
}

RDBTableMetadata metadata = schema
.getTable("test_table_all", false)
.orElseThrow(NullPointerException::new);

Assert.assertEquals("test_table_all", metadata.getRealName());

} finally {
executor.execute(prepare("drop table \"test_table_all\""));
}
}

@Test
public void testParse() {

try {
executor.execute(SqlRequests.of("CREATE TABLE test_table(" +
"id varchar2(32) primary key," +
"name varchar2(128) not null," +
"age number(10)" +
")"));
"id varchar2(32) primary key," +
"name varchar2(128) not null," +
"age number(10)" +
")"));
RDBTableMetadata metaData = parser.parseByName("test_table").orElseThrow(NullPointerException::new);

//id
Expand Down

0 comments on commit 017df98

Please sign in to comment.