From 321abffa01387160c610f2e9ec319034d34d761b Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Fri, 24 May 2024 06:03:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20applyFunc(),=20app?= =?UTF-8?q?ly=E5=BD=A2=E5=BC=8F=E7=9A=84"selectFunc"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yulichang/kt/KtAbstractWrapper.java | 19 +++++++++++++++---- .../yulichang/kt/segments/FuncArgs.java | 2 +- .../wrapper/segments/SelectFunc.java | 5 +++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java index abf744f..0fb6fd1 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java @@ -15,6 +15,7 @@ import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.kt.interfaces.CompareIfExists; import com.github.yulichang.kt.interfaces.Func; 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.MPJSqlInjectionUtils; 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.FuncStr; import com.github.yulichang.wrapper.interfaces.Join; +import com.github.yulichang.wrapper.segments.SelectFunc; import kotlin.reflect.KProperty; import lombok.Getter; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.BiPredicate; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.function.Supplier; +import java.util.function.*; import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*; import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY; @@ -310,6 +309,18 @@ public abstract class KtAbstractWrapper formatSqlMaybeWithParam(applySql, null, values))); } + public Children applyFunc(String applySql, Function consumerFunction, Object... values) { + return applyFunc(true, applySql, consumerFunction, values); + } + + public Children applyFunc(boolean condition, String applySql, + Function 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 public Children last(boolean condition, String lastSql) { if (condition) { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/segments/FuncArgs.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/segments/FuncArgs.java index aa4f413..17f2610 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/segments/FuncArgs.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/segments/FuncArgs.java @@ -16,7 +16,7 @@ import java.util.Arrays; public class FuncArgs { 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); } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java index aa70315..396a30d 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java @@ -75,9 +75,9 @@ public class SelectFunc implements Select { boolean ins = i instanceof Fun; if (ins) { 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 { - 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); this.cache = null; @@ -164,5 +164,6 @@ public class SelectFunc implements Select { private final String prop; private final boolean hasTableAlias; private final String tableAlias; + private final Object property; } }