diff --git a/pom.xml b/pom.xml index d859a1a9..870e47a3 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ - 21 + 22 9.44.0.Final 2023.0.3 3.2.4 diff --git a/uno-core/src/main/java/cc/allio/uno/core/bean/BeanInfoWrapper.java b/uno-core/src/main/java/cc/allio/uno/core/bean/BeanInfoWrapper.java index 786896b1..45c1e4e7 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/bean/BeanInfoWrapper.java +++ b/uno-core/src/main/java/cc/allio/uno/core/bean/BeanInfoWrapper.java @@ -241,7 +241,7 @@ public synchronized Mono setCoverage(K target, String name, boo .flatMap(descriptor -> write(target, descriptor, forceCoverage, value)) .onErrorContinue((error, o) -> { if (log.isWarnEnabled()) { - log.warn("target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), name); + log.warn("Target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), name); } }); } @@ -297,7 +297,7 @@ private Mono write(K target, PropertyDescriptor descriptor, boo } } catch (Throwable err) { if (log.isWarnEnabled()) { - log.warn("Target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), descriptor.getName()); + log.warn("Target {} setValue field {} value error. the error msg: {}", target.getClass().getSimpleName(), descriptor.getName(), err.getMessage()); } } return Mono.just(target); diff --git a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/ComparableElement.java b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/ComparableElement.java index c238273b..6ef80571 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/ComparableElement.java +++ b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/ComparableElement.java @@ -3,6 +3,13 @@ import java.io.Serializable; import java.util.Comparator; +/** + * comparable tree element base on {@link Comparator} + * + * @author j.x + * @date 2023/4/11 22:21 + * @since 1.1.4 + */ public class ComparableElement> extends DefaultElement { private final Comparator comparator; @@ -16,7 +23,12 @@ public ComparableElement(Serializable id, Comparator comparator) { public void addChildren(T element) { super.addChildren(element); if (comparator != null) { - getChildren().sort((o1, o2) -> comparator.compare((T) o1, (T) o2)); + getChildren().sort(comparator); } } + + @Override + public Element obtainSentinel() { + return new ComparableElement(-1, comparator); + } } diff --git a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/Element.java b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/Element.java index 375e7b35..a491a00a 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/Element.java +++ b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/Element.java @@ -157,7 +157,7 @@ default void accept(Visitor visitor) { /** * 获取Sentinel结点 */ - static Element getRootSentinel() { + default Element obtainSentinel() { return new DefaultElement<>(-1, -1); } } diff --git a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/TreeSupport.java b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/TreeSupport.java index 80b44d38..d03f0354 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/TreeSupport.java +++ b/uno-core/src/main/java/cc/allio/uno/core/datastructure/tree/TreeSupport.java @@ -88,15 +88,22 @@ public static > List adjust(List elements) { } } - Element sentinel = Element.getRootSentinel(); - for (R virtual : idElement.values()) { - if (virtual.getDepth() == Element.ROOT_NODE) { - // 触发Element添加结点的特性,如排序 - sentinel.addChildren(virtual); - } - } + // base on first element (if empty then return empty list) get sentinel element then traversal each element and add children (maybe trigger element feature, like as sort) + return idElement.values() + .stream() + .findFirst() + .map(Element::obtainSentinel) + .map(sentinel -> { + for (R virtual : idElement.values()) { + if (virtual.getDepth() == Element.ROOT_NODE) { + // 触发Element添加结点的特性,如排序 + sentinel.addChildren(virtual); + } + } + return sentinel.getChildren(); + }) + .orElse(Collections.emptyList()); - return sentinel.getChildren(); } /** diff --git a/uno-core/src/main/java/cc/allio/uno/core/type/IntegerTypeOperator.java b/uno-core/src/main/java/cc/allio/uno/core/type/IntegerTypeOperator.java index 777da6c4..fdcf471c 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/type/IntegerTypeOperator.java +++ b/uno-core/src/main/java/cc/allio/uno/core/type/IntegerTypeOperator.java @@ -17,6 +17,8 @@ public Integer convert(Object target, Class maybeType) { return d.intValue(); } else if (target instanceof Float f) { return f.intValue(); + } else if (target instanceof Short s) { + return s.intValue(); } throw new IllegalArgumentException(String.format("target %s can't cast type %s", target, maybeType)); } diff --git a/uno-core/src/main/java/cc/allio/uno/core/type/Types.java b/uno-core/src/main/java/cc/allio/uno/core/type/Types.java index 2528c630..d76c935a 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/type/Types.java +++ b/uno-core/src/main/java/cc/allio/uno/core/type/Types.java @@ -244,7 +244,6 @@ public static boolean isArray(Class clazz) { return clazz.isArray(); } - /** * 判断给定的class是否是Bean * @@ -252,7 +251,10 @@ public static boolean isArray(Class clazz) { * @return true false */ public static boolean isBean(Class clazz) { - return !SIMPLE_TYPES.contains(clazz) && !isList(clazz) && !isMap(clazz); + return !SIMPLE_TYPES.contains(clazz) + && !isList(clazz) + && !isMap(clazz) + && !clazz.isEnum(); } /** diff --git a/uno-core/src/main/java/cc/allio/uno/core/util/template/ExpressionTemplate.java b/uno-core/src/main/java/cc/allio/uno/core/util/template/ExpressionTemplate.java index 720de139..c73f7c74 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/util/template/ExpressionTemplate.java +++ b/uno-core/src/main/java/cc/allio/uno/core/util/template/ExpressionTemplate.java @@ -216,7 +216,7 @@ static String parse(String template, String k1, Object v1, String k2, Object v2) * @see #parseTemplate(String, String, Object, String, Object, String, Object) */ static String parse(String template, String k1, Object v1, String k2, Object v2, String k3, Object v3) { - return defaultTemplate().parseTemplate(template, k1, v2, k2, v2, k3, v3); + return defaultTemplate().parseTemplate(template, k1, v1, k2, v2, k3, v3); } /** diff --git a/uno-core/src/test/java/cc/allio/uno/core/type/TypeValueTest.java b/uno-core/src/test/java/cc/allio/uno/core/type/TypeValueTest.java index 31d9ff88..dda08296 100644 --- a/uno-core/src/test/java/cc/allio/uno/core/type/TypeValueTest.java +++ b/uno-core/src/test/java/cc/allio/uno/core/type/TypeValueTest.java @@ -2,7 +2,9 @@ import cc.allio.uno.core.BaseTestCase; import com.google.common.collect.Lists; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.Getter; import org.junit.jupiter.api.Test; import java.lang.reflect.Field; @@ -59,6 +61,14 @@ void testCompositeType() { assertEquals("n", children.getName()); } + @Test + void testEnumType() { + Object success = new TypeValue(Status.class, "SUCCESS").tryConvert(); + + assertNotNull(success); + assertEquals(Status.SUCCESS, success); + } + public static class TypeSet { ENUM[] enums; List enumsList; @@ -82,4 +92,14 @@ public static class Children { public static class Parent { private Children children; } + + + @Getter + @AllArgsConstructor + public enum Status { + SUCCESS("SUCCESS"), + FAILED("FAILED"); + + private final String value; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java index 96f79f10..c91713d7 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java @@ -5,6 +5,7 @@ import java.lang.annotation.*; import java.util.Objects; +import java.util.function.Supplier; /** * DSL操作 @@ -26,10 +27,27 @@ public interface Operator> { * 解析DSL * * @param dsl dsl - * @return SQLOperator + * @return self */ T parse(String dsl); + /** + * @see #customize(String) + */ + default T customize(Supplier dslSupplier) { + return customize(dslSupplier.get()); + } + + /** + * support custom dsl string, assure cover all condition + * + * @param dsl the dsl syntax + * @return self + */ + default T customize(String dsl) { + return parse(dsl); + } + /** * reset operator */ @@ -51,7 +69,7 @@ public interface Operator> { * 如果给定原始为null,则返回{@link ValueWrapper#EMPTY_VALUE} * * @param originalValue originalValue - * @return + * @return origin value or {@link ValueWrapper#EMPTY_VALUE} */ default Object getValueIfNull(Object originalValue) { return Objects.requireNonNullElse(originalValue, ValueWrapper.EMPTY_VALUE); @@ -81,13 +99,14 @@ default > O castReality(Class realityType) { * @param operatorType operatorType * @return {@link Operator} or parent type */ - static Class> getHireachialType(Class> operatorType) { - if (!operatorType.isInterface()) { - Class[] interfaces = operatorType.getInterfaces(); - for (Class hireachial : interfaces) { - if (Operator.class.isAssignableFrom(hireachial)) { - return (Class>) hireachial; - } + static Class> getHierarchicalType(Class> operatorType) { + if (operatorType.isInterface()) { + return operatorType; + } + Class[] interfaces = operatorType.getInterfaces(); + for (Class hierarchy : interfaces) { + if (Operator.class.isAssignableFrom(hierarchy)) { + return (Class>) hierarchy; } } return operatorType; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java index b6b19333..2ade0907 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java @@ -170,7 +170,7 @@ public OperatorTraitGroup() { */ public OperatorTrait find(Class> operatorClazz) { // find operator class parent - Class> parent = Operator.getHireachialType(operatorClazz); + Class> parent = Operator.getHierarchicalType(operatorClazz); return this.traits.stream() .filter(trait -> trait.getParent().equals(parent)) .findFirst() @@ -209,7 +209,7 @@ static class OperatorTrait { public OperatorTrait(Class> clazz) { this.clazz = clazz; - this.parent = Operator.getHireachialType(clazz); + this.parent = Operator.getHierarchicalType(clazz); if (this.parent == null) { throw new DSLException(String.format("operator impl %s not implement any Operator", clazz.getName())); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java index 0b4226eb..f00ce6fd 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java @@ -4,10 +4,12 @@ import cc.allio.uno.core.function.lambda.MethodReferenceColumn; import cc.allio.uno.data.orm.dsl.Func; import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; import cc.allio.uno.data.orm.dsl.word.Distinct; import com.google.common.collect.Lists; import java.util.Collection; +import java.util.List; /** * Query Operator @@ -22,21 +24,21 @@ public interface QueryOperator extends PrepareOperator, TableOper // ============================== SELECT PART ============================== /** - * 添加'SELECT'条件 + * the select field * * @param reference 方法引用 - * @return Select + * @return self */ default QueryOperator select(MethodReferenceColumn reference) { return select(reference.getColumn()); } /** - * 添加'SELECT'条件 + * the select field * * @param reference 方法引用 * @param alias alias - * @return Select + * @return self */ default QueryOperator select(MethodReferenceColumn reference, String alias) { return select(reference.getColumn(), alias); @@ -52,71 +54,87 @@ default QueryOperator selectAll() { } /** - * 添加'SELECT'条件 + * the select field * * @param fieldName java variable name - * @return Select + * @return self */ default QueryOperator select(String fieldName) { return select(DSLName.of(fieldName)); } /** - * 添加'SELECT'条件 + * the select field * - * @param sqlName sqlName - * @return Select + * @param dslName dslName + * @return self */ - QueryOperator select(DSLName sqlName); + QueryOperator select(DSLName dslName); /** - * 添加'SELECT'条件 + * the select field * * @param fieldName java variable name * @param alias alias - * @return Select + * @return self */ default QueryOperator select(String fieldName, String alias) { return select(DSLName.of(fieldName), alias); } /** - * 添加'SELECT'条件 + * the select field * - * @param sqlName sqlName + * @param dslName dslName * @param alias alias - * @return Select + * @return self */ - QueryOperator select(DSLName sqlName, String alias); + QueryOperator select(DSLName dslName, String alias); /** - * 批量添加'SELECT'条件 + * the select field * * @param fieldNames java variable name - * @return Select + * @return self */ default QueryOperator select(String[] fieldNames) { return select(Lists.newArrayList(fieldNames)); } /** - * 批量条件'SELECT'条件 + * the select field * * @param fieldNames java variable name - * @return Select + * @return self */ default QueryOperator select(Collection fieldNames) { return selects(fieldNames.stream().map(DSLName::of).toList()); } + /** + * the select field + * + * @param entityType the entity type + * @return self + */ + default QueryOperator select(Class entityType) { + Collection columns = PojoWrapper.findColumns(entityType); + return selects(columns); + } + /** * 批量条件'SELECT'条件 * - * @param sqlNames sqlNames + * @param dslNames dslNames * @return Select */ - QueryOperator selects(Collection sqlNames); + QueryOperator selects(Collection dslNames); + + /** + * obtain select columns + */ + List obtainSelectColumns(); /** * 添加 distinct @@ -717,4 +735,17 @@ default QueryOperator groupByOne(Collection fieldNames) { * @return Group对象 */ QueryOperator groupByOnes(Collection fieldNames); + + // ====================== advanced method ====================== + + /** + * build tree query operator + *

the entity field muse contains id and parent_id

+ * + * @param baseQuery the build tree query base query operator + * @param subQuery the build tree query for sub query operator + * @return self + */ + QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery); + } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/BaseCommandExecutorLoader.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/BaseCommandExecutorLoader.java new file mode 100644 index 00000000..0e12fe96 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/BaseCommandExecutorLoader.java @@ -0,0 +1,59 @@ +package cc.allio.uno.data.orm.executor; + +import cc.allio.uno.core.env.Envs; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.interceptor.Interceptor; +import cc.allio.uno.data.orm.executor.options.ExecutorOptions; + +import java.util.List; + +/** + * ingest general method for public base class on {@link CommandExecutorLoader} + *

+ * for example determine {@link ExecutorOptions#isSystemDefault()} is true, if true then set db type as for default system env key + *

+ *

+ * all relevant {@link CommandExecutorLoader} subclasses should be extended + *

+ * + * @author j.x + * @date 2024/4/12 18:05 + * @since 1.1.8 + */ +public abstract class BaseCommandExecutorLoader implements CommandExecutorLoader { + + @Override + public E load(List interceptors) { + return onLoad(interceptors); + } + + @Override + public E load(ExecutorOptions executorOptions) { + if (executorOptions == null) { + throw Exceptions.unNull("ExecutorOptions"); + } + boolean systemDefault = executorOptions.isSystemDefault(); + if (systemDefault) { + DBType dbType = executorOptions.getDbType(); + Envs.setProperty(DBType.DB_TYPE_CONFIG_KEY, dbType.getName()); + } + return onLoad(executorOptions); + } + + /** + * sub-class implementation, base on {@link Interceptor} create {@link AggregateCommandExecutor} sub-class-instance + * + * @param interceptors the list of interceptors + * @return {@link AggregateCommandExecutor} instance + */ + protected abstract E onLoad(List interceptors); + + /** + * sub-class implementation, base on {@link ExecutorOptions} create {@link AggregateCommandExecutor} sub-class-instance + * + * @param executorOptions the {@link ExecutorOptions} + * @return {@link AggregateCommandExecutor} instance + */ + protected abstract E onLoad(ExecutorOptions executorOptions); +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java index c5810a77..06d299aa 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java @@ -46,7 +46,7 @@ public static > CommandType getByOperatorClass(Class o) return null; } // try get hirachical - Class> hireachialType = Operator.getHireachialType(o); + Class> hireachialType = Operator.getHierarchicalType(o); for (CommandType commandType : values()) { if (commandType == UNKNOWN || commandType == FLUSH) { continue; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java index 92fc68c9..731ed5f4 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java @@ -196,18 +196,23 @@ public QueryOperator nor() { } @Override - public QueryOperator select(DSLName sqlName) { - return queryOperator.select(sqlName); + public QueryOperator select(DSLName dslName) { + return queryOperator.select(dslName); } @Override - public QueryOperator select(DSLName sqlName, String alias) { - return queryOperator.select(sqlName, alias); + public QueryOperator select(DSLName dslName, String alias) { + return queryOperator.select(dslName, alias); } @Override - public QueryOperator selects(Collection sqlNames) { - return queryOperator.selects(sqlNames); + public QueryOperator selects(Collection dslNames) { + return queryOperator.selects(dslNames); + } + + @Override + public List obtainSelectColumns() { + return queryOperator.obtainSelectColumns(); } @Override @@ -250,6 +255,11 @@ public QueryOperator groupByOnes(Collection fieldNames) { return queryOperator.groupByOnes(fieldNames); } + @Override + public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + return queryOperator.tree(baseQuery, subQuery); + } + @Override public QueryOperator self() { return queryOperator; diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorLoader.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorLoader.java index 11ba9ddd..9c0331ab 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorLoader.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorLoader.java @@ -9,6 +9,7 @@ import com.google.auto.service.AutoService; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; import org.springframework.boot.jdbc.DataSourceBuilder; @@ -26,17 +27,21 @@ * @since 1.1.7 */ @AutoService(CommandExecutorLoader.class) -public class DbCommandExecutorLoader implements CommandExecutorLoader { +public class DbCommandExecutorLoader extends BaseCommandExecutorLoader { private final DbMybatisConfiguration configuration; private final AtomicInteger createCount = new AtomicInteger(0); + public DbCommandExecutorLoader() { + this.configuration = new DbMybatisConfiguration(new Configuration()); + } + public DbCommandExecutorLoader(DbMybatisConfiguration configuration) { this.configuration = configuration; } @Override - public DbCommandExecutor load(List interceptors) { + public DbCommandExecutor onLoad(List interceptors) { DataSource dataSource = configuration.getEnvironment().getDataSource(); DBType dbType = DataSourceHelper.getDbType(dataSource); String username = DataSourceHelper.getUsername(dataSource); @@ -58,9 +63,10 @@ public DbCommandExecutor load(List interceptors) { } @Override - public DbCommandExecutor load(ExecutorOptions executorOptions) { - String jdbcUrl = executorOptions.getDbType().parseTemplate(executorOptions.getAddress(), executorOptions.getDatabase()); - JdbcConnectionDetails connectionDetails = new JdbcConnectionDetailsImpl(executorOptions.getUsername(), executorOptions.getUsername(), jdbcUrl); + public DbCommandExecutor onLoad(ExecutorOptions executorOptions) { + DBType dbType = executorOptions.getDbType(); + String jdbcUrl = dbType.parseTemplate(executorOptions.getAddress(), executorOptions.getDatabase()); + JdbcConnectionDetails connectionDetails = new JdbcConnectionDetailsImpl(executorOptions.getUsername(), executorOptions.getPassword(), jdbcUrl); HikariDataSource dataSource = DataSourceBuilder.create(ClassLoader.getSystemClassLoader()) .type(HikariDataSource.class) diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorProcessor.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorProcessor.java index 9fa41afa..2d7681dc 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorProcessor.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutorProcessor.java @@ -30,7 +30,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw proxyFactory.addAspect(DbCommandExecutorAspect.class); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.setMappedName("getExecutor"); - MethodInterceptor methodInterceptor = invocation -> CommandExecutorFactory.getDSLExecutor(ExecutorKey.DB); + MethodInterceptor methodInterceptor = _ -> CommandExecutorFactory.getDSLExecutor(ExecutorKey.DB); proxyFactory.addAdvisor(new DefaultPointcutAdvisor(pointcut, methodInterceptor)); return proxyFactory.getProxy(); } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java index aaaec3ad..009f98ad 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java @@ -17,6 +17,7 @@ import cc.allio.uno.core.StringPool; import cc.allio.uno.core.util.CollectionUtils; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import com.google.common.collect.Lists; import java.util.Collection; import java.util.Collections; @@ -33,10 +34,11 @@ @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) public class EsQueryOperator extends EsGenericWhereOperator implements QueryOperator { - private DBType dbType; + private final DBType dbType; private SearchRequest searchRequest; private SearchRequest.Builder searchBuilder; private Table table; + private List columns = Lists.newArrayList(); private static final String ERROR_MSG = "elasticsearch query operator not support that operator"; @@ -68,6 +70,7 @@ public void reset() { super.reset(); searchRequest = null; searchBuilder = new SearchRequest.Builder(); + columns = Lists.newArrayList(); } @Override @@ -103,24 +106,31 @@ public Table getTable() { } @Override - public QueryOperator select(DSLName sqlName) { - return this.selects(Collections.singleton(sqlName)); + public QueryOperator select(DSLName dslName) { + return this.selects(Collections.singleton(dslName)); } @Override - public QueryOperator select(DSLName sqlName, String alias) { - return this.selects(Collections.singleton(sqlName)); + public QueryOperator select(DSLName dslName, String alias) { + return this.selects(Collections.singleton(dslName)); } @Override - public QueryOperator selects(Collection sqlNames) { - for (DSLName sqlName : sqlNames) { - FieldAndFormat ff = FieldAndFormat.of(f -> f.field(sqlName.format())); + public QueryOperator selects(Collection dslNames) { + for (DSLName dslName : dslNames) { + String name = dslName.format(); + FieldAndFormat ff = FieldAndFormat.of(f -> f.field(name)); searchBuilder = searchBuilder.fields(ff); + this.columns.add(name); } return self(); } + @Override + public List obtainSelectColumns() { + return columns; + } + @Override public QueryOperator distinct() { throw new DSLException(ERROR_MSG); @@ -197,6 +207,11 @@ public QueryOperator groupByOnes(Collection fieldNames) { throw new DSLException(ERROR_MSG); } + @Override + public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + throw Exceptions.unOperate("tree"); + } + /** * 获取es{@link SearchRequest}实例 * diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java index fd6fda8f..206b32c3 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java @@ -2,6 +2,7 @@ import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.BaseCommandExecutorLoader; import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.data.orm.executor.CommandExecutorLoader; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; @@ -20,7 +21,7 @@ * @since 1.1.7 */ @AutoService(CommandExecutorLoader.class) -public class EsCommandExecutorLoader implements CommandExecutorLoader { +public class EsCommandExecutorLoader extends BaseCommandExecutorLoader { private final RestClientBuilder restClientBuilder; @@ -29,14 +30,14 @@ public EsCommandExecutorLoader(RestClientBuilder restClientBuilder) { } @Override - public EsCommandExecutor load(List interceptors) { + public EsCommandExecutor onLoad(List interceptors) { ExecutorOptions executorOptions = new ExecutorOptionsImpl(DBType.ELASTIC_SEARCH, ExecutorKey.ELASTICSEARCH, OperatorKey.ELASTICSEARCH); executorOptions.addInterceptors(interceptors); return load(executorOptions); } @Override - public EsCommandExecutor load(ExecutorOptions executorOptions) { + public EsCommandExecutor onLoad(ExecutorOptions executorOptions) { return new EsCommandExecutor(executorOptions, restClientBuilder); } diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java index 14c05b9f..8b7fa162 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java @@ -2,7 +2,7 @@ import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.type.DBType; -import cc.allio.uno.data.orm.executor.CommandExecutorLoader; +import cc.allio.uno.data.orm.executor.BaseCommandExecutorLoader; import cc.allio.uno.data.orm.executor.interceptor.Interceptor; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; @@ -15,15 +15,15 @@ * @date 2024/4/1 17:21 * @since 1.1.8 */ -public class InfluxdbCommandExecutorLoader implements CommandExecutorLoader { +public class InfluxdbCommandExecutorLoader extends BaseCommandExecutorLoader { @Override - public InfluxdbCommandExecutor load(List interceptors) { + public InfluxdbCommandExecutor onLoad(List interceptors) { throw Exceptions.unOperate("load by interceptors"); } @Override - public InfluxdbCommandExecutor load(ExecutorOptions executorOptions) { + public InfluxdbCommandExecutor onLoad(ExecutorOptions executorOptions) { return new InfluxdbCommandExecutor(executorOptions); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java index e8502411..6aa7053b 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java @@ -15,6 +15,7 @@ import org.bson.conversions.Bson; import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -98,23 +99,28 @@ public Table getTable() { } @Override - public QueryOperator select(DSLName sqlName) { + public QueryOperator select(DSLName dslName) { // nothing to do return self(); } @Override - public QueryOperator select(DSLName sqlName, String alias) { + public QueryOperator select(DSLName dslName, String alias) { // nothing to do return self(); } @Override - public QueryOperator selects(Collection sqlNames) { + public QueryOperator selects(Collection dslNames) { // nothing to do return self(); } + @Override + public List obtainSelectColumns() { + return Collections.emptyList(); + } + @Override public QueryOperator distinct() { // nothing to do @@ -177,4 +183,9 @@ public QueryOperator groupByOnes(Collection fieldNames) { } return self(); } + + @Override + public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + throw Exceptions.unOperate("tree"); + } } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorLoader.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorLoader.java index 708db5e5..3b1f7b03 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorLoader.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorLoader.java @@ -1,6 +1,7 @@ package cc.allio.uno.data.orm.executor.mongodb; import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.BaseCommandExecutorLoader; import cc.allio.uno.data.orm.executor.CommandExecutorLoader; import cc.allio.uno.data.orm.executor.interceptor.Interceptor; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; @@ -16,15 +17,15 @@ * @since 1.1.7 */ @AutoService(CommandExecutorLoader.class) -public class MongodbCommandExecutorLoader implements CommandExecutorLoader { +public class MongodbCommandExecutorLoader extends BaseCommandExecutorLoader { @Override - public MongodbCommandExecutor load(List interceptors) { + public MongodbCommandExecutor onLoad(List interceptors) { return null; } @Override - public MongodbCommandExecutor load(ExecutorOptions executorOptions) { + public MongodbCommandExecutor onLoad(ExecutorOptions executorOptions) { return new MongodbCommandExecutor(executorOptions); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbQueryCommandExecutor.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbQueryCommandExecutor.java index 8c09e16a..12c4f641 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbQueryCommandExecutor.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbQueryCommandExecutor.java @@ -102,6 +102,9 @@ public List doExec(MongodbQueryOperator operator, ListResultSetHandler han * @return the java value */ public Object toJavaValue(Object v) { + if (v == null) { + return null; + } if (Types.isArray(v.getClass())) { return Arrays.stream(((Object[]) v)).map(this::toJavaValue).toArray(Object[]::new); } else if (v instanceof Collection coll) { diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java index 69cf7642..2b8d4077 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java @@ -3,6 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.core.util.ClassUtils; import cc.allio.uno.core.util.StringUtils; +import cc.allio.uno.core.util.template.ExpressionTemplate; import cc.allio.uno.data.orm.dsl.Func; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.DSLName; @@ -47,6 +48,8 @@ public class SQLQueryOperator extends SQLWhereOperatorImpl implem private SQLTableSource tableSource; private Table table; + private SQLWithSubqueryClause subqueryClause; + // order by private SQLOrderBy orderBy; // group by @@ -57,6 +60,14 @@ public class SQLQueryOperator extends SQLWhereOperatorImpl implem // select columns缓存 private final List columns = Lists.newArrayList(); + /** + * tree query template, like as + *

+ * WITH RECURSIVE biz_tree AS (SELECT xxx FROM dual WHERE xxx UNION (SELECT sub.* FROM ((SELECT xxx FROM dual WHERE xxx) sub INNER JOIN biz_tree P ON P.ID = sub.parent_id))) SELECT xxx FROM biz_tree + *

+ */ + static final String TREE_QUERY_TEMPLATE = "WITH RECURSIVE biz_tree AS (#{baseQuery} UNION (SELECT sub.* FROM ((#{subQuery}) sub INNER JOIN biz_tree P ON P.ID = sub.PARENT_ID))) SELECT #{columns} FROM biz_tree"; + public SQLQueryOperator() { this(DBType.getSystemDbType()); } @@ -90,20 +101,26 @@ public QueryOperator parse(String dsl) { throw new DSLException("not found select statement field"); } ClassUtils.setAccessible(selectField); + SQLSelect sqlSelect; try { - SQLSelect sqlSelect = (SQLSelect) selectField.get(sqlStatement); - SQLSelectQueryBlock queryBlock = sqlSelect.getQueryBlock(); - this.selectQuery = queryBlock; - this.tableSource = queryBlock.getFrom(); - List selectList = queryBlock.getSelectList(); - for (SQLSelectItem selectItem : selectList) { - SQLExpr expr = selectItem.getExpr(); - String exprColumn = SQLSupport.getExprColumn(expr); - addColumns(exprColumn, null, false); - } + sqlSelect = (SQLSelect) selectField.get(sqlStatement); } catch (Throwable ex) { throw new DSLException(ex); } + SQLSelectQueryBlock queryBlock = sqlSelect.getQueryBlock(); + this.selectQuery = queryBlock; + this.tableSource = queryBlock.getFrom(); + List selectList = queryBlock.getSelectList(); + for (SQLSelectItem selectItem : selectList) { + SQLExpr expr = selectItem.getExpr(); + String exprColumn = SQLSupport.getExprColumn(expr); + addColumns(exprColumn, null, false); + } + // parse to sub query clause + SQLWithSubqueryClause withSubQuery = sqlSelect.getWithSubQuery(); + if (withSubQuery != null) { + this.subqueryClause = withSubQuery; + } return self(); } @@ -116,6 +133,7 @@ public void reset() { this.orderBy = null; this.groupBy = null; this.sqlLimit = null; + this.subqueryClause = null; this.columns.clear(); } @@ -132,25 +150,30 @@ public DBType getDBType() { } @Override - public QueryOperator select(DSLName sqlName) { - addColumns(sqlName.format(), null, true); + public QueryOperator select(DSLName dslName) { + addColumns(dslName.format(), null, true); return self(); } @Override - public QueryOperator select(DSLName sqlName, String alias) { - addColumns(sqlName.format(), alias, true); + public QueryOperator select(DSLName dslName, String alias) { + addColumns(dslName.format(), alias, true); return self(); } @Override - public QueryOperator selects(Collection sqlNames) { - for (DSLName sqlName : sqlNames) { + public QueryOperator selects(Collection dslNames) { + for (DSLName sqlName : dslNames) { addColumns(sqlName.format(), null, true); } return self(); } + @Override + public List obtainSelectColumns() { + return columns; + } + @Override public QueryOperator distinct() { SQLSelectItem sqlSelectItem = new SQLSelectItem(new SQLAggregateExpr("", SQLAggregateOption.DISTINCT)); @@ -261,6 +284,19 @@ public QueryOperator groupByOnes(Collection fieldNames) { return self(); } + @Override + public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + String baseQueryDsl = baseQuery.getDSL(); + String subQueryDsl = subQuery.getDSL(); + String treeQuery = + ExpressionTemplate.parse( + TREE_QUERY_TEMPLATE, + "baseQuery", baseQueryDsl, + "subQuery", subQueryDsl, + "columns", String.join(StringPool.COMMA, baseQuery.obtainSelectColumns())); + return customize(treeQuery); + } + /** * 获取 order by子句 * @@ -309,7 +345,15 @@ public String getPrepareDSL() { selectQuery.setLimit(sqlLimit); } selectQuery.setFrom(tableSource); - return SQLUtils.toSQLString(selectQuery, druidDbType); + if (subqueryClause != null) { + SQLSelect sqlSelect = new SQLSelect(); + sqlSelect.setQuery(selectQuery); + sqlSelect.setWithSubQuery(subqueryClause); + return SQLUtils.toSQLString(sqlSelect, druidDbType); + } else { + + return SQLUtils.toSQLString(selectQuery, druidDbType); + } } @Override diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java index ede5c8ed..ded2422a 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java @@ -235,4 +235,27 @@ void testParse() { }); } + @Test + void testRecursive() { + String sql = "WITH RECURSIVE biz_tree AS (SELECT *\n" + + "FROM org\n" + + " UNION (SELECT sub.* FROM ((SELECT *\n" + + "FROM org\n" + + ") sub INNER JOIN biz_tree P ON P.ID = sub.parent_id))) SELECT * FROM biz_tree"; + QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, DBType.MYSQL); + String dsl = operator.parse(sql).getDSL(); + assertEquals("WITH RECURSIVE biz_tree AS (\n" + + "\t\tSELECT *\n" + + "\t\tFROM org\n" + + "\t\tUNION\n" + + "\t\t(SELECT sub.*\n" + + "\t\tFROM (\n" + + "\t\t\tSELECT *\n" + + "\t\t\tFROM org\n" + + "\t\t) sub\n" + + "\t\t\tINNER JOIN biz_tree P ON P.ID = sub.parent_id)\n" + + "\t)\n" + + "SELECT *\n" + + "FROM biz_tree", dsl); + } } diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java index 59c210fd..0deaa1ed 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.ServiceLoader; /** @@ -61,8 +62,8 @@ public CommandExecutorInjectRunner() { public void onRefreshComplete(CoreTest coreTest) throws Throwable { Container container = coreTest.getContainer(); if (container != null) { - Object testcase = coreTest.getTestInstance(); - if (testcase instanceof CommandExecutorSetter setter + Object testInstance = coreTest.getTestInstance(); + if (testInstance instanceof CommandExecutorSetter setter && !commandExecutorLoaders.isEmpty()) { ExecutorOptions executorOptions = drain(container); if (executorOptions != null) { @@ -83,12 +84,13 @@ E loadCommandExecutor(ExecutorOptions execu DBType dbType = executorOptions.getDbType(); try { return (E) commandExecutorLoaders.stream() + .filter(Objects::nonNull) .filter(loader -> loader.match(dbType)) .map(loader -> loader.load(executorOptions)) .findFirst() .orElse(null); } catch (Throwable ex) { - log.error("Failed to load dbtype [{}] to command executor", dbType, ex); + log.error("Failed to load database type [{}] to command executor", dbType, ex); } return null; } @@ -117,6 +119,8 @@ private ExecutorOptions drain(Container container) { executorOptions.setDatabase(database); executorOptions.setUsername(username); executorOptions.setPassword(password); + boolean isDefault = translator.withDefault(); + executorOptions.setSystemDefault(isDefault); return executorOptions; } } \ No newline at end of file diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java index 4fb29287..7f4d5b71 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java @@ -74,4 +74,11 @@ default String withAddress(Container testContainer) { * @return the db password */ String withPassword(Container testContainer); + + /** + * return is default database, the default is false + */ + default boolean withDefault() { + return false; + } } diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MSSQLTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MSSQLTranslator.java index 6d331f3d..a88fcc98 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MSSQLTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MSSQLTranslator.java @@ -1,7 +1,10 @@ package cc.allio.uno.data.test.executor.translator; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.test.testcontainers.Container; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.MSSQLServerContainer; /** * mssql impl translator @@ -12,6 +15,13 @@ */ public class MSSQLTranslator extends RDBTranslator { + @Override + public String withAddress(Container testContainer) { + String host = super.withAddress(testContainer); + GenericContainer internal = testContainer.getInternal(); + return host + StringPool.COLON + internal.getMappedPort(MSSQLServerContainer.MS_SQL_SERVER_PORT); + } + @Override public DBType withDBType(Container testContainer) { return DBType.SQLSERVER; diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MySQLTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MySQLTranslator.java index 0b01aaab..a872cbea 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MySQLTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/MySQLTranslator.java @@ -1,7 +1,10 @@ package cc.allio.uno.data.test.executor.translator; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.test.testcontainers.Container; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.MySQLContainer; /** * mysql impl translator @@ -12,6 +15,13 @@ */ public class MySQLTranslator extends RDBTranslator { + @Override + public String withAddress(Container testContainer) { + String host = super.withAddress(testContainer); + GenericContainer internal = testContainer.getInternal(); + return host + StringPool.COLON + internal.getMappedPort(MySQLContainer.MYSQL_PORT); + } + @Override public DBType withDBType(Container testContainer) { return DBType.MYSQL; diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/PostgreSQLTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/PostgreSQLTranslator.java index 87e6b20b..20542220 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/PostgreSQLTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/PostgreSQLTranslator.java @@ -1,7 +1,10 @@ package cc.allio.uno.data.test.executor.translator; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.test.testcontainers.Container; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.PostgreSQLContainer; /** * pg impl translator @@ -12,6 +15,13 @@ */ public class PostgreSQLTranslator extends RDBTranslator { + @Override + public String withAddress(Container testContainer) { + String host = super.withAddress(testContainer); + GenericContainer internal = testContainer.getInternal(); + return host + StringPool.COLON + internal.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT); + } + @Override public DBType withDBType(Container testContainer) { return DBType.POSTGRESQL; diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/RDBTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/RDBTranslator.java index d597698a..39d3361f 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/RDBTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/RDBTranslator.java @@ -22,4 +22,9 @@ public ExecutorKey withExecutorKey(Container testContainer) { public OperatorKey withOperatorKey(Container testContainer) { return OperatorKey.SQL; } + + @Override + public boolean withDefault() { + return true; + } }