Search in sources :

Example 1 with MergeSegments

use of com.baomidou.mybatisplus.core.conditions.segments.MergeSegments in project mybaits-plus-join by Createsequence.

the class JoinWrapper method instance.

// ============================== override ==============================
/**
 * 获取实例用于or或and的嵌套查询
 *
 * @return com.xiajibagao.top.mybatis.plus.concatSegment.wrapper.JoinMapper<T,R>
 * @author huangchengxing
 * @date 2022/2/9 15:39
 */
@Override
protected JoinWrapper<T, R> instance() {
    JoinWrapper<T, R> instance = new JoinWrapper<>(targetClass, resultClass, false);
    instance.alisa = this.alisa;
    instance.joinTableSeq = this.joinTableSeq;
    instance.joinTableList = this.joinTableList;
    instance.paramNameSeq = this.paramNameSeq;
    instance.paramNameValuePairs = this.paramNameValuePairs;
    instance.expression = new MergeSegments();
    instance.selectColumns = Collections.emptyList();
    instance.lastSql = lastSql;
    instance.sqlComment = sqlComment;
    instance.sqlFirst = sqlFirst;
    return instance;
}
Also used : MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments)

Example 2 with MergeSegments

use of com.baomidou.mybatisplus.core.conditions.segments.MergeSegments in project chao-cloud by chaojunzi.

the class MybatisUtil method buildApplySql.

/**
 * 构造applysql
 *
 * @param <T>       实体类类型
 * @param wrapper   构造条件
 * @param beanClass 实体类类型
 * @return {@link ApplySql}
 */
static <T> ApplySql buildApplySql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
    // sql片段
    ApplySqlSegment normalSegment = null;
    List<String> groupByList = Collections.emptyList();
    ApplySqlSegment havingSegment = null;
    List<String> orderByList = Collections.emptyList();
    // 判断bean是否加载
    TableInfo info = FunctionUtil.buildTableInfo(beanClass);
    Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
    // 获取groupBy和orderBy
    MergeSegments segments = wrapper.getExpression();
    if (segments == null) {
        return null;
    }
    // 1.where之后的条件
    normalSegment = buildApplySqlSegment(segments.getNormal(), wrapper.getParamNameValuePairs());
    // 2.group by
    groupByList = CollUtil.map(segments.getGroupBy(), ISqlSegment::getSqlSegment, true);
    // 3.having 之后的条件
    havingSegment = buildApplySqlSegment(segments.getHaving(), wrapper.getParamNameValuePairs());
    // 4.order by
    orderByList = CollUtil.map(segments.getOrderBy(), ISqlSegment::getSqlSegment, true);
    return // 
    ApplySql.of().setNormalSegment(// 
    normalSegment).setGroupByList(// 
    groupByList).setHavingSegment(// 
    havingSegment).setOrderByList(orderByList);
}
Also used : MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)

Example 3 with MergeSegments

use of com.baomidou.mybatisplus.core.conditions.segments.MergeSegments in project chao-cloud by chaojunzi.

the class DateTableNameHandler method parseDate.

private List<Date> parseDate(SqlCommandType type, String table, Object parameter) {
    String column = rule.getColumn();
    List<Date> dateList = CollUtil.newArrayList();
    if (type == SqlCommandType.INSERT) {
        // insert默认解析对象
        TableInfo tableInfo = TableInfoHelper.getTableInfo(table);
        if (tableInfo == null) {
            return dateList;
        }
        if (tableInfo.getEntityType() == ClassUtil.getClass(parameter)) {
            List<TableFieldInfo> fieldList = tableInfo.getFieldList();
            TableFieldInfo field = CollUtil.findOne(fieldList, f -> StrUtil.equals(f.getColumn(), column));
            if (field == null) {
                log.warn("[{}]: 分片字段无效 {}", table, column);
                return dateList;
            }
            Object v = BeanUtil.getFieldValue(parameter, field.getProperty());
            dateList.add((Date) v);
            return dateList;
        }
    }
    // 参数解析
    if (parameter instanceof ParamMap) {
        ParamMap paramMap = (ParamMap) parameter;
        Collection values = paramMap.values();
        for (Object val : values) {
            if (val instanceof AbstractWrapper) {
                AbstractWrapper wrapper = (AbstractWrapper) val;
                MergeSegments segments = wrapper.getExpression();
                if (segments == null || segments.getNormal() == null) {
                    continue;
                }
                NormalSegmentList segmentList = segments.getNormal();
                for (int i = 0; i < segmentList.size(); i++) {
                    String dbColumn = segmentList.get(i).getSqlSegment();
                    if (StrUtil.equalsIgnoreCase(dbColumn, column)) {
                        // 匹配到参数
                        i++;
                        ISqlSegment keyword = CollUtil.get(segmentList, i);
                        if (CollUtil.contains(supportSqlKeywords, keyword)) {
                            // 获取到值索引
                            i++;
                            ISqlSegment keySegment = CollUtil.get(segmentList, i);
                            // 索引越界
                            if (keySegment == null) {
                                continue;
                            }
                            // 构造dateList
                            dateList.addAll(getDateByParam(parameter, keySegment));
                            // between ? and ? -> (跳2格)
                            i = i + 2;
                            if (keyword == SqlKeyword.BETWEEN) {
                                keySegment = CollUtil.get(segmentList, i);
                                dateList.addAll(getDateByParam(parameter, keySegment));
                            }
                        }
                    }
                }
                break;
            }
        }
    }
    return dateList;
}
Also used : TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) ParamMap(org.apache.ibatis.binding.MapperMethod.ParamMap) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper) Date(java.util.Date) MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments) Collection(java.util.Collection) ISqlSegment(com.baomidou.mybatisplus.core.conditions.ISqlSegment) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) NormalSegmentList(com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList)

Aggregations

MergeSegments (com.baomidou.mybatisplus.core.conditions.segments.MergeSegments)3 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)2 AbstractWrapper (com.baomidou.mybatisplus.core.conditions.AbstractWrapper)1 ISqlSegment (com.baomidou.mybatisplus.core.conditions.ISqlSegment)1 NormalSegmentList (com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList)1 TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)1 ApplySqlSegment (com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)1 Collection (java.util.Collection)1 Date (java.util.Date)1 ParamMap (org.apache.ibatis.binding.MapperMethod.ParamMap)1