From 017df98c4fc65701f41d4e4c175cf598c2717b8c Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 26 Jun 2024 19:10:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/meta/AbstractSchemaMetadata.java | 95 +++++++++++-------- .../metadata/AbstractTableOrViewMetadata.java | 6 ++ .../rdb/metadata/TableOrViewMetadata.java | 8 +- .../commons/RDBTableMetadataParser.java | 18 ++-- .../rdb/supports/BasicReactiveTests.java | 28 ++++++ .../oracle/OracleTableMetaParserTest.java | 34 ++++++- 6 files changed, 138 insertions(+), 51 deletions(-) diff --git a/hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/meta/AbstractSchemaMetadata.java b/hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/meta/AbstractSchemaMetadata.java index 77fe9350..38106b12 100644 --- a/hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/meta/AbstractSchemaMetadata.java +++ b/hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/meta/AbstractSchemaMetadata.java @@ -52,8 +52,9 @@ public List getObject(ObjectType type) { if (typeMapping == null) { List all = loadMetadata(type); - Map group = all.stream() - .collect(toMap(ObjectMetadata::getName, Function.identity(), (_1, _2) -> _1, ConcurrentHashMap::new)); + Map group = all + .stream() + .collect(toMap(ObjectMetadata::getName, Function.identity(), (_1, _2) -> _1, ConcurrentHashMap::new)); typeMapping = metaRepository.put(type.getId(), group); if (typeMapping != null) { @@ -68,34 +69,34 @@ public Flux getObjectReactive(ObjectType type) { Map typeMapping = metaRepository.get(type.getId()); if (typeMapping == null) { return loadMetadataReactive(type) - .collectMap(ObjectMetadata::getName, Function.identity()) - .flatMapMany(group -> { - Map 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 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 List loadMetadata(ObjectType type) { return getParser(type) - .map(ObjectMetadataParser::parseAll) - .map(CastUtil::>cast) - .orElseGet(Collections::emptyList); + .map(ObjectMetadataParser::parseAll) + .map(CastUtil::>cast) + .orElseGet(Collections::emptyList); } protected 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::cast) - .orElse(null); + .flatMap(parser -> { + log.debug("load {} metadata {} ,use parser:{}", type, name, parser.getClass().getSimpleName()); + return parser.parseByName(name); + }) + .map(CastUtil::cast) + .orElse(null); } protected Flux loadMetadataReactive(ObjectType type) { @@ -108,8 +109,8 @@ protected Mono 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); @@ -125,9 +126,10 @@ protected Optional getParser(ObjectType type) { } public void addObject(ObjectMetadata metadata) { - Map repo = metaRepository.computeIfAbsent(metadata - .getObjectType() - .getId(), t -> new ConcurrentHashMap<>()); + Map repo = metaRepository + .computeIfAbsent(metadata + .getObjectType() + .getId(), t -> new ConcurrentHashMap<>()); repo.put(metadata.getName(), metadata); if (metadata.getAlias() != null) { @@ -140,23 +142,29 @@ public Optional 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 Mono getObjectReactive(ObjectType type, String name, boolean autoLoad) { Objects.requireNonNull(name, "name"); Map 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.cast(metadata)); + } + + protected ObjectMetadata getLoadedObject0(Map from, String name) { + ObjectMetadata metadata = from.get(name); + if (metadata == null) { + metadata = from.get(name.toLowerCase()); + } + return metadata; } public Mono getObjectReactive(ObjectType type, String name) { @@ -171,9 +179,22 @@ public Optional getObject(ObjectType type, String @Override public Optional 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 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) { 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 537f87ce..11f0b1f3 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 @@ -41,6 +41,8 @@ public abstract class AbstractTableOrViewMetadata implements TableOrViewMetadata private RDBSchemaMetadata schema; + private String realName; + @Setter private Consumer onColumnAdded; @@ -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; } 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 d871f850..524b425e 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 @@ -139,11 +139,15 @@ default void fireEvent(EventType eventType, Consumer contextConsum } } + default String getRealName() { + return getName(); + } + default String getFullName() { return StringUtils.concat( getSchema().getName(), ".", - getName() + getRealName() ); } @@ -151,7 +155,7 @@ default String getQuoteName() { return getDialect().quote(getSchema().getName(), false) + "." - + getDialect().quote(getName(), false); + + getDialect().quote(getRealName(), false); } @Override diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/commons/RDBTableMetadataParser.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/commons/RDBTableMetadataParser.java index 1e4422e3..4996556e 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/commons/RDBTableMetadataParser.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/commons/RDBTableMetadataParser.java @@ -213,12 +213,13 @@ protected Flux 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(); @@ -270,12 +271,13 @@ protected List 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(); diff --git a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java index c976b3f7..d9db24eb 100644 --- a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java +++ b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java @@ -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; @@ -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 diff --git a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/oracle/OracleTableMetaParserTest.java b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/oracle/OracleTableMetaParserTest.java index 18230fdb..4525f207 100644 --- a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/oracle/OracleTableMetaParserTest.java +++ b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/oracle/OracleTableMetaParserTest.java @@ -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; @@ -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