All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yulichang.wrapper.MPJLambdaWrapper Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
package com.github.yulichang.wrapper;

import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.TableList;
import com.github.yulichang.toolkit.WrapperUtils;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum;
import com.github.yulichang.wrapper.interfaces.*;
import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.segments.*;
import lombok.Getter;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * 参考 {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper}
 * Lambda 语法使用 Wrapper
 *
 * @author yulichang
 */
@SuppressWarnings({"unused", "DuplicatedCode"})
public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper> implements Fill>,
        Query>, QueryLabel>, Chain, SelectWrapper> {

    /**
     * 查询字段 sql
     */
    private SharedString sqlSelect = new SharedString();
    /**
     * 是否 select distinct
     */
    private boolean selectDistinct = false;
    /**
     * 查询的字段
     */
    @Getter
    private final List getSelectColum() {
        return this.selectColumns;
    }

    @Override
    public void addLabel(Label label, boolean isCollection) {
        if (isCollection) {
            this.resultMapCollection = true;
        }
        this.resultMap = true;
        this.resultMapMybatisLabel.add(label);
    }

    @Override
    public MPJLambdaWrapper getChildren() {
        return typedThis;
    }


    /**
     * 设置查询字段
     *
     * @param columns 字段数组
     * @return children
     */
    @SafeVarargs
    public final  MPJLambdaWrapper select(SFunction... columns) {
        if (ArrayUtils.isNotEmpty(columns)) {
            Class aClass = LambdaUtils.getEntityClass(columns[0]);
            Map cacheMap = ColumnCache.getMapField(aClass);
            for (SFunction s : columns) {
                SelectCache cache = cacheMap.get(LambdaUtils.getName(s));
                getSelectColum().add(new SelectNormal(cache, index, hasAlias, alias));
            }
        }
        return typedThis;
    }

    @Override
    public MPJLambdaWrapper selectAll(Class clazz) {
        return Query.super.selectAll(clazz);
    }

    /**
     * 查询实体类全部字段
     *
     * @param clazz   查询的实体类
     * @param exclude 排除字段
     */
    @Override
    @SafeVarargs
    public final  MPJLambdaWrapper selectAll(Class clazz, SFunction... exclude) {
        return Query.super.selectAll(clazz, exclude);
    }

    /**
     * 查询实体类全部字段
     *
     * @param clazz   查询的实体类
     * @param exclude 排除字段
     */
    @Override
    @SafeVarargs
    public final  MPJLambdaWrapper selectAll(Class clazz, String prefix, SFunction... exclude) {
        return Query.super.selectAll(clazz, prefix, exclude);
    }

    /**
     * 查询主表全部字段
     * 

* 需要使用 使用 JoinWrappers.lambda(clazz) 或者 new MPJLambdaQueryWrapper<<(clazz) 构造 * * @return children */ public MPJLambdaWrapper selectAll() { Assert.notNull(getEntityClass(), "使用 JoinWrappers.lambda(clazz) 或者 new MPJLambdaQueryWrapper<>(clazz)"); return selectAll(getEntityClass()); } @Override public MPJLambdaWrapper selectFunc(String sql, MFunction 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; } /** * 子查询 */ public MPJLambdaWrapper selectSub(Class clazz, Consumer> consumer, SFunction alias) { return selectSub(clazz, ConfigProperties.subQueryAlias, consumer, alias); } public MPJLambdaWrapper selectSub(Class clazz, String st, Consumer> consumer, SFunction alias) { return selectSub(clazz, st, consumer, LambdaUtils.getName(alias)); } /** * 子查询 */ public MPJLambdaWrapper selectSub(Class clazz, String st, Consumer> consumer, String alias) { MPJLambdaWrapper wrapper = subInstance(clazz, st); consumer.accept(wrapper); this.selectColumns.add(new SelectSub(() -> WrapperUtils.buildSubSqlByWrapper(clazz, wrapper, alias), hasAlias, this.alias, alias)); return typedThis; } /** * union *

* 例: wrapper.union(UserDO.class, union -> union.selectAll(UserDO.class)) * * @param clazz union语句的主表类型 * @since 1.4.8 */ public MPJLambdaWrapper union(Class clazz, Consumer> consumer) { MPJLambdaWrapper unionWrapper = fromInstance(clazz); consumer.accept(unionWrapper); String sb = " UNION " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); } unionSql.setStringValue(unionSql.getStringValue() + sb); return typedThis; } @Override public MPJLambdaWrapper addFill(MConsumer consumer) { if (this.fill == null) { this.fill = new ArrayList<>(); } this.fill.add(consumer); return typedThis; } @Override public void doFill(Object obj) { if (this.fill != null) { this.fill.forEach(c -> c.accept(obj)); } } /** * union *

* 例: wrapper.unionAll(UserDO.class, union -> union.selectAll(UserDO.class)) * * @param clazz union语句的主表类型 * @since 1.4.8 */ public MPJLambdaWrapper unionAll(Class clazz, Consumer> consumer) { MPJLambdaWrapper unionWrapper = fromInstance(clazz); consumer.accept(unionWrapper); String sb = " UNION ALL " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); } unionSql.setStringValue(unionSql.getStringValue() + sb); return typedThis; } /** * 查询条件 SQL 片段 */ @Override public String getSqlSelect() { if (StringUtils.isBlank(sqlSelect.getStringValue()) && CollectionUtils.isNotEmpty(selectColumns)) { String s = selectColumns.stream().map(i -> { if (i.isStr()) { return i.getColumn(); } String prefix; if (i.isHasTableAlias()) { prefix = i.getTableAlias(); } else { prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), i.isLabel()); } String str = prefix + StringPool.DOT + i.getColumn(); if (i.isFunc()) { SelectFunc.Arg[] args = i.getArgs(); if (Objects.isNull(args) || args.length == 0) { return String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias(); } else { return String.format(i.getFunc().getSql(), Arrays.stream(args).map(arg -> { if (arg.isSub()) { Object o = arg.getSubFunc().apply(subInstance(arg.getClazz())); return WrapperUtils.buildUnionSqlByWrapper(arg.getClazz(), (MPJLambdaWrapper) o); } else { String pf = arg.isHasTableAlias() ? arg.getTableAlias() : tableList.getPrefixByClass(arg.getClazz()); Map mapField = ColumnCache.getMapField(arg.getClazz()); SelectCache cache = mapField.get(arg.getProp()); return pf + StringPool.DOT + cache.getColumn(); } }).toArray()) + Constant.AS + i.getAlias(); } } else { return i.isHasAlias() ? (str + Constant.AS + i.getAlias()) : str; } }).collect(Collectors.joining(StringPool.COMMA)); sqlSelect.setStringValue(s); } return sqlSelect.getStringValue(); } @Override public String getUnionSql() { return Optional.ofNullable(unionSql).map(SharedString::getStringValue).orElse(StringPool.EMPTY); } public boolean getSelectDistinct() { return selectDistinct; } /** * 用于生成嵌套 sql *

故 sqlSelect 不向下传递

*/ @Override protected MPJLambdaWrapper instance() { return instance(index, null, null, null); } @Override protected MPJLambdaWrapper instanceEmpty() { return new MPJLambdaWrapper<>(); } @Override protected MPJLambdaWrapper instance(Integer index, String keyWord, Class joinClass, String tableName) { return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(), this.paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), this.tableList, index, keyWord, joinClass, tableName, ifExists); } @Override public void clear() { super.clear(); selectDistinct = false; sqlSelect.toNull(); selectColumns.clear(); if (Objects.nonNull(unionSql)) unionSql.toEmpty(); resultMapMybatisLabel.clear(); ifExists = ConfigProperties.ifExists; fill = null; } }