Search in sources :

Example 1 with ISqlSegment

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

the class MybatisUtil method parseApplySql.

/**
 * 解析{@link ApplySql}
 *
 * @param <T>     实体类类型
 * @param wrapper 构造条件
 * @param v       ApplySql->实体类型
 */
static <T> void parseApplySql(AbstractWrapper<T, ?, ?> wrapper, Object v) {
    if (v instanceof ApplySql) {
        ApplySql as = (ApplySql) v;
        ApplySqlSegment normalSegment = as.getNormalSegment();
        List<String> groupByList = as.getGroupByList();
        List<String> orderByList = as.getOrderByList();
        ApplySqlSegment havingSegment = as.getHavingSegment();
        // 自定义sql
        if (normalSegment != null) {
            wrapper.apply(normalSegment.getSqlSegment(), getValueArray(normalSegment.getValueList()));
        }
        Method doItMethod = ReflectUtil.getMethodByName(wrapper.getClass(), SqlTemplate.DO_IT.getTemplate());
        // group by
        if (CollUtil.isNotEmpty(groupByList)) {
            ISqlSegment seg = () -> CollUtil.join(groupByList, StrUtil.COMMA);
            ReflectUtil.invoke(wrapper, doItMethod, true, new ISqlSegment[] { SqlKeyword.GROUP_BY, seg });
        }
        // having
        if (havingSegment != null) {
            wrapper.having(havingSegment.getSqlSegment(), getValueArray(havingSegment.getValueList()));
        }
        // order by
        if (CollUtil.isNotEmpty(orderByList)) {
            ISqlSegment seg = () -> CollUtil.join(orderByList, StrUtil.COMMA);
            ReflectUtil.invoke(wrapper, doItMethod, true, new ISqlSegment[] { SqlKeyword.ORDER_BY, seg });
        }
    }
}
Also used : ISqlSegment(com.baomidou.mybatisplus.core.conditions.ISqlSegment) Method(java.lang.reflect.Method) ApplySql(com.chao.cloud.common.extra.mybatis.common.ApplySql) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)

Example 2 with ISqlSegment

use of com.baomidou.mybatisplus.core.conditions.ISqlSegment 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

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