Search in sources :

Example 6 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo 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)

Example 7 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project onex-boot by zhangchaoxu.

the class LogicDeleteBatchByIdsWithFill method injectMappedStatement.

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BATCH_BY_IDS;
    String sql;
    if (tableInfo.isWithLogicDelete()) {
        // 包含->逻辑删除
        // 自动填充字段
        List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream().filter(TableFieldInfo::isWithUpdateFill).collect(toList());
        // 自动填充sql
        String sqlSet;
        if (CollectionUtils.isNotEmpty(fieldInfos)) {
            // 包含->自动填充字段
            sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY)) + tableInfo.getLogicDeleteSql(false, false);
        } else {
            // 不包含->自动填充字段
            sqlSet = sqlLogicSet(tableInfo);
        }
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach("#{item}", COLLECTION, null, "item", COMMA), tableInfo.getLogicDeleteSql(true, true));
    } else {
        // 不包含->逻辑删除
        sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach("#{item}", COLLECTION, null, "item", COMMA));
    }
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
}
Also used : TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Example 8 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project onex-boot by zhangchaoxu.

the class LogicDeleteByIdWithFill method injectMappedStatement.

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
    String sql;
    if (tableInfo.isWithLogicDelete()) {
        // 包含->逻辑删除
        // 自动填充字段
        List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream().filter(TableFieldInfo::isWithUpdateFill).collect(toList());
        // 自动填充sql
        String sqlSet;
        if (CollectionUtils.isNotEmpty(fieldInfos)) {
            // 包含->自动填充字段
            sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY)) + tableInfo.getLogicDeleteSql(false, false);
        } else {
            // 不包含->自动填充字段
            sqlSet = sqlLogicSet(tableInfo);
        }
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
    } else {
        // 不包含->逻辑删除
        sqlMethod = SqlMethod.DELETE_BY_ID;
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
    }
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
}
Also used : TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Example 9 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project onex-boot by zhangchaoxu.

the class LogicDeleteByWrapperWithFill method injectMappedStatement.

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
    String sql;
    if (tableInfo.isWithLogicDelete()) {
        // 包含->逻辑删除
        // 自动填充字段
        List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream().filter(TableFieldInfo::isWithUpdateFill).collect(toList());
        // 自动填充sql
        String sqlSet;
        if (CollectionUtils.isNotEmpty(fieldInfos)) {
            // 包含->自动填充字段
            sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY)) + tableInfo.getLogicDeleteSql(false, false);
        } else {
            // 不包含->自动填充字段
            sqlSet = sqlLogicSet(tableInfo);
        }
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
    } else {
        // 不包含->逻辑删除
        sqlMethod = SqlMethod.DELETE;
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
    }
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return this.addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
}
Also used : TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Example 10 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project ballcat by ballcat-projects.

the class InsertBatchSomeColumnByCollection method injectMappedStatement.

@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
    SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
    List<TableFieldInfo> fieldList = tableInfo.getFieldList();
    String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) + this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
    String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
    String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) + this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
    insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
    // 从 list 改为 collection. 允许传入除 list外的参数类型
    String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "collection", null, ENTITY, COMMA);
    String keyProperty = null;
    String keyColumn = null;
    // 表包含主键处理逻辑,如果不包含主键当普通字段处理
    if (tableInfo.havePK()) {
        if (tableInfo.getIdType() == IdType.AUTO) {
            /* 自增主键 */
            keyGenerator = Jdbc3KeyGenerator.INSTANCE;
            keyProperty = tableInfo.getKeyProperty();
            keyColumn = tableInfo.getKeyColumn();
        } else {
            if (null != tableInfo.getKeySequence()) {
                keyGenerator = TableInfoHelper.genKeyGenerator(this.methodName, tableInfo, builderAssistant);
                keyProperty = tableInfo.getKeyProperty();
                keyColumn = tableInfo.getKeyColumn();
            }
        }
    }
    String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return this.addInsertMappedStatement(mapperClass, modelClass, this.methodName, sqlSource, keyGenerator, keyProperty, keyColumn);
}
Also used : SqlScriptUtils(com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod) Setter(lombok.Setter) Accessors(lombok.experimental.Accessors) Predicate(java.util.function.Predicate) InsertBatchSomeColumn(com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) IdType(com.baomidou.mybatisplus.annotation.IdType) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) MappedStatement(org.apache.ibatis.mapping.MappedStatement) SqlSource(org.apache.ibatis.mapping.SqlSource) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Aggregations

TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)10 SqlMethod (com.baomidou.mybatisplus.core.enums.SqlMethod)5 SqlSource (org.apache.ibatis.mapping.SqlSource)5 AbstractWrapper (com.baomidou.mybatisplus.core.conditions.AbstractWrapper)3 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)3 IdType (com.baomidou.mybatisplus.annotation.IdType)2 AbstractMethod (com.baomidou.mybatisplus.core.injector.AbstractMethod)2 TableInfoHelper (com.baomidou.mybatisplus.core.metadata.TableInfoHelper)2 SqlScriptUtils (com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils)2 Field (java.lang.reflect.Field)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 Setter (lombok.Setter)2 Accessors (lombok.experimental.Accessors)2 Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)2 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)2 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 ISqlSegment (com.baomidou.mybatisplus.core.conditions.ISqlSegment)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1