mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
feat: selectFunc support Object param
This commit is contained in:
parent
51f39d1129
commit
047faf4891
@ -19,12 +19,10 @@ import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.Chain;
|
||||
import com.github.yulichang.wrapper.interfaces.MBiPredicate;
|
||||
import com.github.yulichang.wrapper.interfaces.MFunction;
|
||||
import com.github.yulichang.wrapper.interfaces.SelectWrapper;
|
||||
import com.github.yulichang.wrapper.resultmap.Label;
|
||||
import com.github.yulichang.wrapper.segments.Select;
|
||||
import com.github.yulichang.wrapper.segments.SelectApt;
|
||||
import com.github.yulichang.wrapper.segments.SelectCache;
|
||||
import com.github.yulichang.wrapper.segments.SelectSub;
|
||||
import com.github.yulichang.wrapper.segments.*;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.*;
|
||||
@ -192,6 +190,19 @@ public class AptQueryWrapper<T> extends AptAbstractWrapper<T, AptQueryWrapper<T>
|
||||
return selectAll(getBaseColumn());
|
||||
}
|
||||
|
||||
@Override
|
||||
public AptQueryWrapper<T> selectFunc(String sql, MFunction<AptConsumer> column, String alias) {
|
||||
AptConsumer apply = column.apply(new AptConsumer());
|
||||
String formatSql;
|
||||
if (ArrayUtils.isEmpty(apply.getValues())) {
|
||||
formatSql = sql;
|
||||
} else {
|
||||
formatSql = formatSqlMaybeWithParam(sql, null, apply.getValues());
|
||||
}
|
||||
getSelectColum().add(new SelectApt(apply.getColumns(), () -> formatSql, alias));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
public <E, F> AptQueryWrapper<T> selectSub(BaseColumn<E> baseColumn, Consumer<AptQueryWrapper<E>> consumer, SFunction<F, ?> alias) {
|
||||
return selectSub(baseColumn, consumer, LambdaUtils.getName(alias));
|
||||
}
|
||||
|
@ -12,11 +12,11 @@ import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.extension.apt.matedata.Column;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.extension.apt.interfaces.CompareIfExists;
|
||||
import com.github.yulichang.extension.apt.interfaces.Func;
|
||||
import com.github.yulichang.extension.apt.interfaces.OnCompare;
|
||||
import com.github.yulichang.extension.apt.matedata.Column;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.MPJSqlInjectionUtils;
|
||||
import com.github.yulichang.toolkit.Ref;
|
||||
@ -283,15 +283,19 @@ public abstract class JoinAbstractWrapper<T, Children extends JoinAbstractWrappe
|
||||
() -> formatSqlMaybeWithParam(applySql, null, values)));
|
||||
}
|
||||
|
||||
public Children applyFunc(String applySql, SFunction<AptConsumer, Column[]> consumerFunction, Object... values) {
|
||||
public Children applyFunc(String applySql, MFunction<AptConsumer> consumerFunction, Object... values) {
|
||||
return applyFunc(true, applySql, consumerFunction, values);
|
||||
}
|
||||
|
||||
public Children applyFunc(boolean condition, String applySql,
|
||||
SFunction<AptConsumer, Column[]> consumerFunction, Object... values) {
|
||||
MFunction<AptConsumer> consumerFunction, Object... values) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(APPLY,
|
||||
() -> formatSqlMaybeWithParam(String.format(applySql,
|
||||
Arrays.stream(consumerFunction.apply(AptConsumer.func)).map(this::columnToString).toArray()), null, values)));
|
||||
() -> {
|
||||
AptConsumer apply = consumerFunction.apply(new AptConsumer());
|
||||
return formatSqlMaybeWithParam(String.format(applySql,
|
||||
Arrays.stream(apply.getColumns()).map(this::columnToString).toArray()), null,
|
||||
ArrayUtils.isNotEmpty(values) ? values : apply.getValues());
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -13,11 +13,11 @@ import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.toolkit.support.FieldCache;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.MFunction;
|
||||
import com.github.yulichang.wrapper.segments.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -115,7 +115,7 @@ public interface Query<Children> extends Serializable {
|
||||
/**
|
||||
* 别名查询
|
||||
*/
|
||||
default <S> Children selectAs(Column column, String alias) {
|
||||
default Children selectAs(Column column, String alias) {
|
||||
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(column.getClazz());
|
||||
SelectCache cache = cacheMap.get(column.getProperty());
|
||||
getSelectColum().add(new SelectApt(cache, column, alias));
|
||||
@ -165,27 +165,24 @@ public interface Query<Children> extends Serializable {
|
||||
* @param column 函数作用的字段
|
||||
* @param alias 别名
|
||||
*/
|
||||
default <S> Children selectFunc(BaseFuncEnum funcEnum, Column column, String alias) {
|
||||
default Children selectFunc(BaseFuncEnum funcEnum, Column column, String alias) {
|
||||
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(column.getClazz());
|
||||
getSelectColum().add(new SelectApt(cacheMap.get(column.getProperty()), column, funcEnum, alias));
|
||||
return getChildren();
|
||||
}
|
||||
|
||||
default <S, X> Children selectFunc(BaseFuncEnum funcEnum, Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectFunc(BaseFuncEnum funcEnum, Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
|
||||
}
|
||||
|
||||
default <S> Children selectFunc(BaseFuncEnum funcEnum, Column column) {
|
||||
default Children selectFunc(BaseFuncEnum funcEnum, Column column) {
|
||||
return selectFunc(funcEnum, column, column.getProperty());
|
||||
}
|
||||
|
||||
|
||||
default <X> Children selectFunc(String sql, Function<AptConsumer, Column[]> column, String alias) {
|
||||
getSelectColum().add(new SelectApt(column.apply(AptConsumer.func), () -> sql, alias));
|
||||
return getChildren();
|
||||
}
|
||||
Children selectFunc(String sql, MFunction<AptConsumer> column, String alias);
|
||||
|
||||
default <X, S> Children selectFunc(String sql, Function<AptConsumer, Column[]> column, SFunction<S, ?> alias) {
|
||||
default <S> Children selectFunc(String sql, MFunction<AptConsumer> column, SFunction<S, ?> alias) {
|
||||
return selectFunc(sql, column, LambdaUtils.getName(alias));
|
||||
}
|
||||
|
||||
@ -194,90 +191,90 @@ public interface Query<Children> extends Serializable {
|
||||
/**
|
||||
* SUM()
|
||||
*/
|
||||
default <S> Children selectSum(Column column) {
|
||||
default Children selectSum(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.SUM, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectSum(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectSum(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.SUM, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectSum(Column column, String alias) {
|
||||
default Children selectSum(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.SUM, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* COUNT()
|
||||
*/
|
||||
default <S> Children selectCount(Column column) {
|
||||
default Children selectCount(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectCount(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectCount(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectCount(Column column, String alias) {
|
||||
default Children selectCount(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* MAX()
|
||||
*/
|
||||
default <S> Children selectMax(Column column) {
|
||||
default Children selectMax(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.MAX, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectMax(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectMax(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.MAX, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectMax(Column column, String alias) {
|
||||
default Children selectMax(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.MAX, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* MIN()
|
||||
*/
|
||||
default <S> Children selectMin(Column column) {
|
||||
default Children selectMin(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.MIN, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectMin(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectMin(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.MIN, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectMin(Column column, String alias) {
|
||||
default Children selectMin(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.MIN, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* MIN()
|
||||
*/
|
||||
default <S> Children selectAvg(Column column) {
|
||||
default Children selectAvg(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.AVG, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectAvg(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectAvg(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.AVG, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectAvg(Column column, String alias) {
|
||||
default Children selectAvg(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.AVG, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* LEN()
|
||||
*/
|
||||
default <S> Children selectLen(Column column) {
|
||||
default Children selectLen(Column column) {
|
||||
return selectFunc(DefaultFuncEnum.LEN, column);
|
||||
}
|
||||
|
||||
default <S, X> Children selectLen(Column column, SFunction<X, ?> alias) {
|
||||
default <X> Children selectLen(Column column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.LEN, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectLen(Column column, String alias) {
|
||||
default Children selectLen(Column column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.LEN, column, alias);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.extension.kt.interfaces.Query;
|
||||
import com.github.yulichang.extension.kt.interfaces.QueryLabel;
|
||||
import com.github.yulichang.extension.kt.segments.FuncArgs;
|
||||
import com.github.yulichang.extension.kt.toolkit.KtWrapperUtils;
|
||||
import com.github.yulichang.extension.kt.toolkit.KtWrappers;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
@ -14,6 +15,7 @@ import com.github.yulichang.toolkit.KtUtils;
|
||||
import com.github.yulichang.toolkit.TableList;
|
||||
import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.wrapper.interfaces.Chain;
|
||||
import com.github.yulichang.wrapper.interfaces.MFunction;
|
||||
import com.github.yulichang.wrapper.interfaces.SelectWrapper;
|
||||
import com.github.yulichang.wrapper.resultmap.Label;
|
||||
import com.github.yulichang.wrapper.segments.*;
|
||||
@ -233,6 +235,20 @@ public class KtLambdaWrapper<T> extends KtAbstractLambdaWrapper<T, KtLambdaWrapp
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KtLambdaWrapper<T> selectFunc(String sql, MFunction<FuncArgs> column, String alias) {
|
||||
FuncArgs apply = column.apply(new FuncArgs());
|
||||
String formatSql;
|
||||
if (ArrayUtils.isEmpty(apply.getValues())) {
|
||||
formatSql = sql;
|
||||
} else {
|
||||
formatSql = formatSqlMaybeWithParam(sql, null, apply.getValues());
|
||||
}
|
||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> formatSql, apply.getFuncArg(),
|
||||
isHasAlias(), getAlias()));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* union
|
||||
* <p>
|
||||
|
@ -12,6 +12,7 @@ import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.toolkit.support.FieldCache;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.MFunction;
|
||||
import com.github.yulichang.wrapper.segments.*;
|
||||
import kotlin.reflect.KProperty;
|
||||
|
||||
@ -19,7 +20,6 @@ import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -216,16 +216,10 @@ public interface Query<Children> extends Serializable {
|
||||
}
|
||||
|
||||
|
||||
default Children selectFunc(String sql, Function<FuncArgs, SelectFunc.Arg[]> column, String alias) {
|
||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new FuncArgs()),
|
||||
isHasAlias(), getAlias()));
|
||||
return getChildren();
|
||||
}
|
||||
Children selectFunc(String sql, MFunction<FuncArgs> column, String alias);
|
||||
|
||||
default Children selectFunc(String sql, Function<FuncArgs, SelectFunc.Arg[]> column, KProperty<?> alias) {
|
||||
getSelectColum().add(new SelectFunc(alias.getName(), getIndex(), () -> sql,
|
||||
column.apply(new FuncArgs()), isHasAlias(), getAlias()));
|
||||
return getChildren();
|
||||
default Children selectFunc(String sql, MFunction<FuncArgs> column, KProperty<?> alias) {
|
||||
return selectFunc(sql, column, alias.getName());
|
||||
}
|
||||
|
||||
/* 默认聚合函数扩展 */
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.extension.kt.segments;
|
||||
import com.github.yulichang.toolkit.KtUtils;
|
||||
import com.github.yulichang.wrapper.segments.SelectFunc;
|
||||
import kotlin.reflect.KProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -12,11 +13,26 @@ import java.util.Arrays;
|
||||
* @author yulichang
|
||||
* @since 1.4.6
|
||||
*/
|
||||
@Data
|
||||
@SuppressWarnings("unused")
|
||||
public class FuncArgs {
|
||||
|
||||
public SelectFunc.Arg[] accept(KProperty<?>... kProperty) {
|
||||
return Arrays.stream(kProperty).map(i ->
|
||||
private KProperty<?>[] args;
|
||||
|
||||
private Object[] values;
|
||||
|
||||
public FuncArgs accept(KProperty<?>... kProperty) {
|
||||
this.args = kProperty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FuncArgs values(Object... values) {
|
||||
this.values = values;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectFunc.Arg[] getFuncArg() {
|
||||
return Arrays.stream(args).map(i ->
|
||||
new SelectFunc.Arg(KtUtils.ref(i), i.getName(), false, null, i))
|
||||
.toArray(SelectFunc.Arg[]::new);
|
||||
}
|
||||
|
@ -27,7 +27,10 @@ import lombok.Getter;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.*;
|
||||
import java.util.function.BiPredicate;
|
||||
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.WrapperKeyword.APPLY;
|
||||
@ -329,21 +332,24 @@ public abstract class JoinAbstractWrapper<T, Children extends JoinAbstractWrappe
|
||||
() -> formatSqlMaybeWithParam(applySql, null, values)));
|
||||
}
|
||||
|
||||
public Children applyFunc(String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||
public Children applyFunc(String applySql, MFunction<FuncConsumer> consumerFunction, Object... values) {
|
||||
return applyFunc(true, applySql, consumerFunction, values);
|
||||
}
|
||||
|
||||
public Children applyFunc(boolean condition, String applySql,
|
||||
Function<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||
MFunction<FuncConsumer> consumerFunction, Object... values) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(APPLY,
|
||||
() -> formatSqlMaybeWithParam(String.format(applySql,
|
||||
Arrays.stream(consumerFunction.apply(FuncConsumer.func)).map(func -> {
|
||||
if (func instanceof Fun) {
|
||||
Fun<?, ?> fun = (Fun<?, ?>) func;
|
||||
return columnToString(index, fun.getAlias(), fun.getFunc(), false, PrefixEnum.CD_FIRST, false);
|
||||
}
|
||||
return columnToString(index, null, func, false, PrefixEnum.CD_FIRST, false);
|
||||
}).toArray()), null, values)));
|
||||
() -> {
|
||||
FuncConsumer funcConsumer = consumerFunction.apply(new FuncConsumer());
|
||||
return formatSqlMaybeWithParam(String.format(applySql,
|
||||
Arrays.stream(funcConsumer.getArgs()).map(func -> {
|
||||
if (func instanceof Fun) {
|
||||
Fun<?, ?> fun = (Fun<?, ?>) func;
|
||||
return columnToString(index, fun.getAlias(), fun.getFunc(), false, PrefixEnum.CD_FIRST, false);
|
||||
}
|
||||
return columnToString(index, null, func, false, PrefixEnum.CD_FIRST, false);
|
||||
}).toArray()), null, ArrayUtils.isEmpty(values) ? funcConsumer.getValues() : values);
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -234,6 +234,20 @@ public class MPJLambdaWrapper<T> extends JoinAbstractLambdaWrapper<T, MPJLambdaW
|
||||
return selectAll(getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPJLambdaWrapper<T> selectFunc(String sql, MFunction<FuncConsumer> column, String alias) {
|
||||
FuncConsumer funcConsumer = column.apply(new FuncConsumer());
|
||||
String formatSql;
|
||||
if (ArrayUtils.isEmpty(funcConsumer.getValues())) {
|
||||
formatSql = sql;
|
||||
} else {
|
||||
formatSql = formatSqlMaybeWithParam(sql, null, funcConsumer.getValues());
|
||||
}
|
||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> formatSql,
|
||||
funcConsumer.getArgs(), isHasAlias(), getAlias()));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 子查询
|
||||
*/
|
||||
|
@ -10,6 +10,7 @@ import com.github.yulichang.adapter.AdapterHelper;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.ReflectionKit;
|
||||
import com.github.yulichang.toolkit.*;
|
||||
import com.github.yulichang.wrapper.interfaces.MFunction;
|
||||
import com.github.yulichang.wrapper.interfaces.Update;
|
||||
import com.github.yulichang.wrapper.interfaces.UpdateChain;
|
||||
import com.github.yulichang.wrapper.segments.FuncConsumer;
|
||||
@ -127,14 +128,14 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateJoinWrapper<T> setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||
public UpdateJoinWrapper<T> setApply(boolean condition, String applySql, MFunction<FuncConsumer> consumerFunction, Object... values) {
|
||||
if (condition && StringUtils.isNotBlank(applySql)) {
|
||||
SFunction<?, ?>[] arg = consumerFunction.apply(FuncConsumer.func);
|
||||
FuncConsumer funcConsumer = consumerFunction.apply(new FuncConsumer());
|
||||
UpdateSet set = new UpdateSet();
|
||||
set.setApply(true);
|
||||
set.setFormat(applySql);
|
||||
set.setColumns(arg);
|
||||
set.setArgs(values);
|
||||
set.setColumns(funcConsumer.getArgs());
|
||||
set.setArgs(ArrayUtils.isNotEmpty(values) ? values : funcConsumer.getValues());
|
||||
getUpdateSet().add(set);
|
||||
}
|
||||
return typedThis;
|
||||
|
@ -17,7 +17,6 @@ import com.github.yulichang.wrapper.segments.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -263,16 +262,10 @@ public interface Query<Children> extends Serializable {
|
||||
}
|
||||
|
||||
|
||||
default Children selectFunc(String sql, Function<FuncConsumer, SFunction<?, ?>[]> column, String alias) {
|
||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(FuncConsumer.func),
|
||||
isHasAlias(), getAlias()));
|
||||
return getChildren();
|
||||
}
|
||||
Children selectFunc(String sql, MFunction<FuncConsumer> column, String alias);
|
||||
|
||||
default <S> Children selectFunc(String sql, Function<FuncConsumer, SFunction<?, ?>[]> column, SFunction<S, ?> alias) {
|
||||
getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql,
|
||||
column.apply(FuncConsumer.func), isHasAlias(), getAlias()));
|
||||
return getChildren();
|
||||
default <S> Children selectFunc(String sql, MFunction<FuncConsumer> column, SFunction<S, ?> alias) {
|
||||
return selectFunc(sql, column, LambdaUtils.getName(alias));
|
||||
}
|
||||
|
||||
/* 默认聚合函数扩展 */
|
||||
|
@ -80,11 +80,11 @@ public interface Update<Children> extends Serializable {
|
||||
|
||||
<R> Children setDecrBy(boolean condition, SFunction<R, ?> column, Number val);
|
||||
|
||||
default Children setApply(String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
|
||||
default Children setApply(String applySql, MFunction<FuncConsumer> consumerFunction, Object... values) {
|
||||
return setApply(true, applySql, consumerFunction, values);
|
||||
}
|
||||
|
||||
Children setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values);
|
||||
Children setApply(boolean condition, String applySql, MFunction<FuncConsumer> consumerFunction, Object... values);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.wrapper.segments;
|
||||
|
||||
import com.github.yulichang.extension.apt.matedata.Column;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@ -11,12 +12,22 @@ import java.io.Serializable;
|
||||
* @author yulichang
|
||||
* @since 1.4.13
|
||||
*/
|
||||
@Data
|
||||
public class AptConsumer implements Serializable {
|
||||
|
||||
public static final AptConsumer func = new AptConsumer();
|
||||
private Column[] columns;
|
||||
|
||||
public final Column[] accept(Column... a) {
|
||||
return a;
|
||||
private Object[] values;
|
||||
|
||||
|
||||
public final AptConsumer accept(Column... a) {
|
||||
this.columns = a;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final AptConsumer accept(Object... a) {
|
||||
this.values = a;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.wrapper.segments;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用于selectFunc 和 applyFunc中的参数填充
|
||||
@ -9,48 +10,66 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
* @author yulichang
|
||||
* @since 1.4.13
|
||||
*/
|
||||
@Data
|
||||
public class FuncConsumer {
|
||||
|
||||
public static final FuncConsumer func = new FuncConsumer();
|
||||
private SFunction<?, ?>[] args;
|
||||
|
||||
public final <A> SFunction<?, ?>[] accept(SFunction<A, ?> a) {
|
||||
return new SFunction[]{a};
|
||||
private Object[] values;
|
||||
|
||||
public final <A> FuncConsumer accept(SFunction<A, ?> a) {
|
||||
this.args = new SFunction[]{a};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b) {
|
||||
return new SFunction[]{a, b};
|
||||
public final <A, B> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b) {
|
||||
this.args = new SFunction[]{a, b};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c) {
|
||||
return new SFunction[]{a, b, c};
|
||||
public final <A, B, C> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c) {
|
||||
this.args = new SFunction[]{a, b, c};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d) {
|
||||
return new SFunction[]{a, b, c, d};
|
||||
public final <A, B, C, D> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d) {
|
||||
this.args = new SFunction[]{a, b, c, d};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e) {
|
||||
return new SFunction[]{a, b, c, d, e};
|
||||
public final <A, B, C, D, E> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e) {
|
||||
this.args = new SFunction[]{a, b, c, d, e};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f) {
|
||||
return new SFunction[]{a, b, c, d, e, f};
|
||||
public final <A, B, C, D, E, F> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f) {
|
||||
this.args = new SFunction[]{a, b, c, d, e, f};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g};
|
||||
public final <A, B, C, D, E, F, G> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g) {
|
||||
this.args = new SFunction[]{a, b, c, d, e, f, g};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h};
|
||||
public final <A, B, C, D, E, F, G, H> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h) {
|
||||
this.args = new SFunction[]{a, b, c, d, e, f, g, h};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H, I> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h, i};
|
||||
public final <A, B, C, D, E, F, G, H, I> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i) {
|
||||
this.args = new SFunction[]{a, b, c, d, e, f, g, h, i};
|
||||
return this;
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H, I, J> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i, SFunction<J, ?> j) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h, i, j};
|
||||
public final <A, B, C, D, E, F, G, H, I, J> FuncConsumer accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i, SFunction<J, ?> j) {
|
||||
this.args = new SFunction[]{a, b, c, d, e, f, g, h, i, j};
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public final FuncConsumer values(Object... values) {
|
||||
this.values = values;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -34,8 +34,15 @@
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.github.yulichang</groupId>-->
|
||||
<!-- <artifactId>mybatis-plus-join-processor</artifactId>-->
|
||||
<!-- <version>1.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@ -47,7 +54,7 @@
|
||||
<path>
|
||||
<groupId>com.github.yulichang</groupId>
|
||||
<artifactId>mybatis-plus-join-processor</artifactId>
|
||||
<version>${revision}</version>
|
||||
<version>1.5.1</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@ -0,0 +1,50 @@
|
||||
package com.github.yulichang.test.join.unit;
|
||||
|
||||
import com.github.yulichang.test.join.dto.UserDTO;
|
||||
import com.github.yulichang.test.join.entity.AddressDO;
|
||||
import com.github.yulichang.test.join.entity.UserDO;
|
||||
import com.github.yulichang.test.util.Reset;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
import com.github.yulichang.toolkit.JoinWrappers;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class SelectFuncTest {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
Reset.reset();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void applyFunc() {
|
||||
ThreadLocalUtils.set("""
|
||||
SELECT t.id, t.pid, t.`name`, t.`json`,
|
||||
t.sex, t.head_img, t.create_time, t.address_id,
|
||||
t.address_id2, t.del, t.create_by, t.update_by,
|
||||
concat(t.id, t1.address, ?, ?) AS address
|
||||
FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id)
|
||||
WHERE t.del = false AND t1.del = false
|
||||
AND (concat(t.id, t1.user_id, ?) IS NOT NULL AND t.id >= ?)
|
||||
""");
|
||||
List<UserDTO> list = JoinWrappers.lambda(UserDO.class)
|
||||
.selectAll(UserDO.class)
|
||||
.selectFunc("concat(%s,%s,{0},{1})",
|
||||
arg -> arg
|
||||
.accept(UserDO::getId, AddressDO::getAddress)
|
||||
.values("aaa", 123),
|
||||
UserDTO::getAddress)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(UserDO::getId, AddressDO::getUserId), "12")
|
||||
.ge(UserDO::getId, 0)
|
||||
.list(UserDTO.class);
|
||||
list.forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user