Search in sources :

Example 1 with AbstractWrapper

use of com.baomidou.mybatisplus.core.conditions.AbstractWrapper in project solon by noear.

the class OptimisticLockerInnerInterceptor method setVersionByWrapper.

private void setVersionByWrapper(Map<String, Object> map, String msId) {
    Object ew = map.get(Constants.WRAPPER);
    if (null != ew && ew instanceof AbstractWrapper && ew instanceof Update) {
        Class<?> entityClass = ENTITY_CLASS_CACHE.get(msId);
        if (null == entityClass) {
            try {
                final String className = msId.substring(0, msId.lastIndexOf('.'));
                entityClass = GenericTypeUtil.getSuperClassGenericType(Class.forName(className), Mapper.class, 0);
                ENTITY_CLASS_CACHE.put(msId, entityClass);
            } catch (ClassNotFoundException e) {
                throw ExceptionUtils.mpe(e);
            }
        }
        final TableFieldInfo versionField = getVersionFieldInfo(entityClass);
        if (null == versionField) {
            return;
        }
        final String versionColumn = versionField.getColumn();
        final FieldEqFinder fieldEqFinder = new FieldEqFinder(versionColumn, (Wrapper<?>) ew);
        if (!fieldEqFinder.isPresent()) {
            return;
        }
        final Map<String, Object> paramNameValuePairs = ((AbstractWrapper<?, ?, ?>) ew).getParamNameValuePairs();
        final Object originalVersionValue = paramNameValuePairs.get(fieldEqFinder.valueKey);
        if (originalVersionValue == null) {
            return;
        }
        final Object updatedVersionVal = getUpdatedVersionVal(originalVersionValue.getClass(), originalVersionValue);
        if (originalVersionValue == updatedVersionVal) {
            return;
        }
        // 拼接新的version值
        paramNameValuePairs.put(UPDATED_VERSION_VAL_KEY, updatedVersionVal);
        ((Update<?, ?>) ew).setSql(String.format("%s = #{%s.%s}", versionColumn, "ew.paramNameValuePairs", UPDATED_VERSION_VAL_KEY));
    }
}
Also used : Mapper(com.baomidou.mybatisplus.core.mapper.Mapper) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper) Update(com.baomidou.mybatisplus.core.conditions.update.Update)

Example 2 with AbstractWrapper

use of com.baomidou.mybatisplus.core.conditions.AbstractWrapper in project solon by noear.

the class OptimisticLockerInnerInterceptor method doOptimisticLocker.

protected void doOptimisticLocker(Map<String, Object> map, String msId) {
    // updateById(et), update(et, wrapper);
    Object et = map.getOrDefault(Constants.ENTITY, null);
    if (Objects.nonNull(et)) {
        // version field
        TableFieldInfo fieldInfo = this.getVersionFieldInfo(et.getClass());
        if (null == fieldInfo) {
            return;
        }
        try {
            Field versionField = fieldInfo.getField();
            // 旧的 version 值
            Object originalVersionVal = versionField.get(et);
            if (originalVersionVal == null) {
                if (null != exception) {
                    /**
                     * 自定义异常处理
                     */
                    throw exception;
                }
                return;
            }
            String versionColumn = fieldInfo.getColumn();
            // 新的 version 值
            Object updatedVersionVal = this.getUpdatedVersionVal(fieldInfo.getPropertyType(), originalVersionVal);
            String methodName = msId.substring(msId.lastIndexOf(StringPool.DOT) + 1);
            if ("update".equals(methodName)) {
                AbstractWrapper<?, ?, ?> aw = (AbstractWrapper<?, ?, ?>) map.getOrDefault(Constants.WRAPPER, null);
                if (aw == null) {
                    UpdateWrapper<?> uw = new UpdateWrapper<>();
                    uw.eq(versionColumn, originalVersionVal);
                    map.put(Constants.WRAPPER, uw);
                } else {
                    aw.apply(versionColumn + " = {0}", originalVersionVal);
                }
            } else {
                map.put(Constants.MP_OPTLOCK_VERSION_ORIGINAL, originalVersionVal);
            }
            versionField.set(et, updatedVersionVal);
        } catch (IllegalAccessException e) {
            throw ExceptionUtils.mpe(e);
        }
    } else // update(LambdaUpdateWrapper) or update(UpdateWrapper)
    if (wrapperMode && map.entrySet().stream().anyMatch(t -> Objects.equals(t.getKey(), Constants.WRAPPER))) {
        setVersionByWrapper(map, msId);
    }
}
Also used : Field(java.lang.reflect.Field) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper)

Example 3 with AbstractWrapper

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

AbstractWrapper (com.baomidou.mybatisplus.core.conditions.AbstractWrapper)3 TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)3 ISqlSegment (com.baomidou.mybatisplus.core.conditions.ISqlSegment)1 MergeSegments (com.baomidou.mybatisplus.core.conditions.segments.MergeSegments)1 NormalSegmentList (com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList)1 Update (com.baomidou.mybatisplus.core.conditions.update.Update)1 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 Mapper (com.baomidou.mybatisplus.core.mapper.Mapper)1 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 Date (java.util.Date)1 ParamMap (org.apache.ibatis.binding.MapperMethod.ParamMap)1