Skip to content

Commit

Permalink
feat:[uno-data]add query operator for tree select new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearXs committed Apr 12, 2024
1 parent b7270e6 commit 97093a0
Show file tree
Hide file tree
Showing 24 changed files with 358 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public synchronized <K extends T> Mono<K> 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);
}
});
}
Expand Down Expand Up @@ -297,7 +297,7 @@ private <K extends T> Mono<K> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.lang.annotation.*;
import java.util.Objects;
import java.util.function.Supplier;

/**
* DSL操作
Expand All @@ -26,10 +27,27 @@ public interface Operator<T extends Operator<T>> {
* 解析DSL
*
* @param dsl dsl
* @return SQLOperator
* @return self
*/
T parse(String dsl);

/**
* @see #customize(String)
*/
default T customize(Supplier<String> 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
*/
Expand All @@ -51,7 +69,7 @@ public interface Operator<T extends Operator<T>> {
* 如果给定原始为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);
Expand Down Expand Up @@ -81,13 +99,14 @@ default <O extends Operator<?>> O castReality(Class<O> realityType) {
* @param operatorType operatorType
* @return {@link Operator} or parent type
*/
static Class<? extends Operator<?>> getHireachialType(Class<? extends Operator<?>> operatorType) {
if (!operatorType.isInterface()) {
Class<?>[] interfaces = operatorType.getInterfaces();
for (Class<?> hireachial : interfaces) {
if (Operator.class.isAssignableFrom(hireachial)) {
return (Class<? extends Operator<?>>) hireachial;
}
static Class<? extends Operator<?>> getHierarchicalType(Class<? extends Operator<?>> operatorType) {
if (operatorType.isInterface()) {
return operatorType;
}
Class<?>[] interfaces = operatorType.getInterfaces();
for (Class<?> hierarchy : interfaces) {
if (Operator.class.isAssignableFrom(hierarchy)) {
return (Class<? extends Operator<?>>) hierarchy;
}
}
return operatorType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public OperatorTraitGroup() {
*/
public OperatorTrait find(Class<? extends Operator<?>> operatorClazz) {
// find operator class parent
Class<? extends Operator<?>> parent = Operator.getHireachialType(operatorClazz);
Class<? extends Operator<?>> parent = Operator.getHierarchicalType(operatorClazz);
return this.traits.stream()
.filter(trait -> trait.getParent().equals(parent))
.findFirst()
Expand Down Expand Up @@ -209,7 +209,7 @@ static class OperatorTrait {

public OperatorTrait(Class<? extends Operator<?>> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,21 +24,21 @@ public interface QueryOperator extends PrepareOperator<QueryOperator>, TableOper
// ============================== SELECT PART ==============================

/**
* 添加'SELECT'条件
* the select field
*
* @param reference 方法引用
* @return Select
* @return self
*/
default <R> QueryOperator select(MethodReferenceColumn<R> reference) {
return select(reference.getColumn());
}

/**
* 添加'SELECT'条件
* the select field
*
* @param reference 方法引用
* @param alias alias
* @return Select
* @return self
*/
default <R> QueryOperator select(MethodReferenceColumn<R> reference, String alias) {
return select(reference.getColumn(), alias);
Expand All @@ -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<String> fieldNames) {
return selects(fieldNames.stream().map(DSLName::of).toList());
}

/**
* the select field
*
* @param entityType the entity type
* @return self
*/
default <T> QueryOperator select(Class<T> entityType) {
Collection<DSLName> columns = PojoWrapper.findColumns(entityType);
return selects(columns);
}

/**
* 批量条件'SELECT'条件
*
* @param sqlNames sqlNames
* @param dslNames dslNames
* @return Select
*/
QueryOperator selects(Collection<DSLName> sqlNames);
QueryOperator selects(Collection<DSLName> dslNames);

/**
* obtain select columns
*/
List<String> obtainSelectColumns();

/**
* 添加 distinct
Expand Down Expand Up @@ -717,4 +735,17 @@ default QueryOperator groupByOne(Collection<String> fieldNames) {
* @return Group对象
*/
QueryOperator groupByOnes(Collection<DSLName> fieldNames);

// ====================== advanced method ======================

/**
* build tree query operator
* <p><b>the entity field muse contains id and parent_id</b></p>
*
* @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);

}
Original file line number Diff line number Diff line change
@@ -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}
* <p>
* for example determine {@link ExecutorOptions#isSystemDefault()} is true, if true then set db type as for default system env key
* </p>
* <p>
* all relevant {@link CommandExecutorLoader} subclasses should be extended
* </p>
*
* @author j.x
* @date 2024/4/12 18:05
* @since 1.1.8
*/
public abstract class BaseCommandExecutorLoader<E extends AggregateCommandExecutor> implements CommandExecutorLoader<E> {

@Override
public E load(List<Interceptor> 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<Interceptor> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static <O extends Operator<?>> CommandType getByOperatorClass(Class<O> o)
return null;
}
// try get hirachical
Class<? extends Operator<?>> hireachialType = Operator.getHireachialType(o);
Class<? extends Operator<?>> hireachialType = Operator.getHierarchicalType(o);
for (CommandType commandType : values()) {
if (commandType == UNKNOWN || commandType == FLUSH) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DSLName> sqlNames) {
return queryOperator.selects(sqlNames);
public QueryOperator selects(Collection<DSLName> dslNames) {
return queryOperator.selects(dslNames);
}

@Override
public List<String> obtainSelectColumns() {
return queryOperator.obtainSelectColumns();
}

@Override
Expand Down Expand Up @@ -250,6 +255,11 @@ public QueryOperator groupByOnes(Collection<DSLName> fieldNames) {
return queryOperator.groupByOnes(fieldNames);
}

@Override
public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) {
return queryOperator.tree(baseQuery, subQuery);
}

@Override
public QueryOperator self() {
return queryOperator;
Expand Down
Loading

0 comments on commit 97093a0

Please sign in to comment.