feat: 添加 applyFunc(), apply形式的"selectFunc"

This commit is contained in:
yulichang 2024-05-24 06:03:33 +08:00
parent 93d3ab6e1c
commit 321abffa01
3 changed files with 19 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.kt.interfaces.CompareIfExists; import com.github.yulichang.kt.interfaces.CompareIfExists;
import com.github.yulichang.kt.interfaces.Func; import com.github.yulichang.kt.interfaces.Func;
import com.github.yulichang.kt.interfaces.OnCompare; import com.github.yulichang.kt.interfaces.OnCompare;
import com.github.yulichang.kt.segments.FuncArgs;
import com.github.yulichang.toolkit.KtUtils; import com.github.yulichang.toolkit.KtUtils;
import com.github.yulichang.toolkit.MPJSqlInjectionUtils; import com.github.yulichang.toolkit.MPJSqlInjectionUtils;
import com.github.yulichang.toolkit.Ref; import com.github.yulichang.toolkit.Ref;
@ -25,15 +26,13 @@ import com.github.yulichang.wrapper.enums.PrefixEnum;
import com.github.yulichang.wrapper.interfaces.CompareStrIfExists; import com.github.yulichang.wrapper.interfaces.CompareStrIfExists;
import com.github.yulichang.wrapper.interfaces.FuncStr; import com.github.yulichang.wrapper.interfaces.FuncStr;
import com.github.yulichang.wrapper.interfaces.Join; import com.github.yulichang.wrapper.interfaces.Join;
import com.github.yulichang.wrapper.segments.SelectFunc;
import kotlin.reflect.KProperty; import kotlin.reflect.KProperty;
import lombok.Getter; import lombok.Getter;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiPredicate; import java.util.function.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*; import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY; import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
@ -310,6 +309,18 @@ public abstract class KtAbstractWrapper<T, Children extends KtAbstractWrapper<T,
() -> formatSqlMaybeWithParam(applySql, null, values))); () -> formatSqlMaybeWithParam(applySql, null, values)));
} }
public Children applyFunc(String applySql, Function<FuncArgs, SelectFunc.Arg[]> consumerFunction, Object... values) {
return applyFunc(true, applySql, consumerFunction, values);
}
public Children applyFunc(boolean condition, String applySql,
Function<FuncArgs, SelectFunc.Arg[]> consumerFunction, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(APPLY,
() -> formatSqlMaybeWithParam(String.format(applySql,
Arrays.stream(consumerFunction.apply(new FuncArgs())).map(func ->
columnToString(index, null, (KProperty<?>) func.getProperty(), false, PrefixEnum.CD_FIRST)).toArray()), null, values)));
}
@Override @Override
public Children last(boolean condition, String lastSql) { public Children last(boolean condition, String lastSql) {
if (condition) { if (condition) {

View File

@ -16,7 +16,7 @@ import java.util.Arrays;
public class FuncArgs { public class FuncArgs {
public SelectFunc.Arg[] accept(KProperty<?>... kProperty) { public SelectFunc.Arg[] accept(KProperty<?>... kProperty) {
return Arrays.stream(kProperty).map(i -> new SelectFunc.Arg(KtUtils.ref(i), i.getName(), false, null)).toArray(SelectFunc.Arg[]::new); return Arrays.stream(kProperty).map(i -> new SelectFunc.Arg(KtUtils.ref(i), i.getName(), false, null, i)).toArray(SelectFunc.Arg[]::new);
} }
} }

View File

@ -75,9 +75,9 @@ public class SelectFunc implements Select {
boolean ins = i instanceof Fun; boolean ins = i instanceof Fun;
if (ins) { if (ins) {
Fun<?, ?> f = (Fun<?, ?>) i; Fun<?, ?> f = (Fun<?, ?>) i;
return new Arg(LambdaUtils.getEntityClass(f.getFunc()), LambdaUtils.getName(f.getFunc()), true, f.getAlias()); return new Arg(LambdaUtils.getEntityClass(f.getFunc()), LambdaUtils.getName(f.getFunc()), true, f.getAlias(), null);
} else { } else {
return new Arg(LambdaUtils.getEntityClass(i), LambdaUtils.getName(i), false, null); return new Arg(LambdaUtils.getEntityClass(i), LambdaUtils.getName(i), false, null, null);
} }
}).toArray(Arg[]::new); }).toArray(Arg[]::new);
this.cache = null; this.cache = null;
@ -164,5 +164,6 @@ public class SelectFunc implements Select {
private final String prop; private final String prop;
private final boolean hasTableAlias; private final boolean hasTableAlias;
private final String tableAlias; private final String tableAlias;
private final Object property;
} }
} }