Search in sources :

Example 1 with ParamFilterModel

use of org.sagacity.sqltoy.config.model.ParamFilterModel in project sagacity-sqltoy by chenrenfei.

the class ParamFilterUtils method combineFilters.

/**
 * @TODO 整合sql中定义的filter和代码中自定义的filters
 * @param filters
 * @param extFilters
 * @return
 */
public static List<ParamFilterModel> combineFilters(List<ParamFilterModel> filters, List<ParamsFilter> extFilters) {
    if (extFilters == null || extFilters.isEmpty()) {
        return filters;
    }
    List<ParamFilterModel> result = new ArrayList<ParamFilterModel>();
    if (filters != null && !filters.isEmpty()) {
        result.addAll(filters);
    }
    for (ParamsFilter filter : extFilters) {
        ParamFilterModel paramFilter = new ParamFilterModel();
        paramFilter.setFilterType(filter.getType());
        paramFilter.setParams(filter.getParams());
        if (filter.getParams().length == 1) {
            paramFilter.setParam(filter.getParams()[0]);
        }
        if (filter.getExcludes() != null) {
            for (String s : filter.getExcludes()) {
                paramFilter.addExclude(s);
            }
        }
        paramFilter.setFormat(filter.getDateType());
        paramFilter.setValues(filter.getValue());
        // 加减天数
        paramFilter.setIncrementTime(Double.valueOf(filter.getIncrease()));
        paramFilter.setTimeUnit(filter.getTimeUnit());
        result.add(paramFilter);
    }
    return result;
}
Also used : ParamsFilter(org.sagacity.sqltoy.model.ParamsFilter) ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel) ArrayList(java.util.ArrayList)

Example 2 with ParamFilterModel

use of org.sagacity.sqltoy.config.model.ParamFilterModel in project sagacity-sqltoy by chenrenfei.

the class ParamFilterUtils method filterValue.

/**
 * @todo 对查询条件参数进行filter过滤加工处理(如:判断是否为null、日期格式转换等等)
 * @param sqlToyContext
 * @param paramArgs
 * @param values
 * @param filters
 * @return
 */
public static Object[] filterValue(SqlToyContext sqlToyContext, String[] paramArgs, Object[] values, List<ParamFilterModel> filters) {
    if ((filters == null || filters.size() == 0) || values == null || values.length == 0) {
        return values;
    }
    // update 2020-09-08 当全是?模式传参时,在非分页等场景下paramNames会为null导致正常params="*" 的blank过滤无效
    // 构造出参数便于统一处理
    String[] paramsName;
    if ((paramArgs == null || paramArgs.length == 0)) {
        paramsName = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            paramsName[i] = "param_" + i;
        }
    } else {
        paramsName = paramArgs;
    }
    HashMap<String, Integer> paramIndexMap = new HashMap<String, Integer>();
    int paramSize = paramsName.length;
    Object[] paramValues = new Object[paramSize];
    for (int i = 0; i < paramSize; i++) {
        paramIndexMap.put(paramsName[i].toLowerCase(), i);
        paramValues[i] = values[i];
    }
    String[] filterParams;
    int index;
    String filterParam;
    String filterType;
    for (ParamFilterModel paramFilterModel : filters) {
        filterParams = paramFilterModel.getParams();
        filterType = paramFilterModel.getFilterType();
        // 通配符表示针对所有参数
        if (filterParams.length == 1 && filterParams[0].equals("*")) {
            filterParams = paramsName;
        }
        // 排他性参数(当某些参数值都不为null,则设置其他参数值为null)
        if (filterType.equals("exclusive") && paramFilterModel.getUpdateParams() != null) {
            filterExclusive(paramIndexMap, paramFilterModel, paramValues);
        } else // 缓存中提取精准查询参数作为sql查询条件值
        if (filterType.equals("cache-arg")) {
            filterCache(sqlToyContext, paramIndexMap, paramFilterModel, paramValues);
        } else if (filterType.equals("clone")) {
            filterClone(paramIndexMap, paramFilterModel, paramValues);
        } else // 决定性参数不为null时即条件成立时,需要保留的参数(其他的参数全部设置为null)
        if (filterType.equals("primary")) {
            filterParam = paramFilterModel.getParam().toLowerCase();
            index = (paramIndexMap.get(filterParam) == null) ? -1 : paramIndexMap.get(filterParam);
            // 决定性参数值不为null
            if (index != -1 && paramValues[index] != null) {
                for (int j = 0; j < paramSize; j++) {
                    // 排除自身
                    if (j != index && !paramFilterModel.getExcludes().contains(paramsName[j].toLowerCase())) {
                        paramValues[j] = null;
                    }
                }
            }
        } else {
            for (int i = 0, n = filterParams.length; i < n; i++) {
                filterParam = filterParams[i].toLowerCase();
                index = (paramIndexMap.get(filterParam) == null) ? -1 : paramIndexMap.get(filterParam);
                if (index != -1 && paramValues[index] != null) {
                    if (!paramFilterModel.getExcludes().contains(filterParam)) {
                        paramValues[index] = filterSingleParam(paramValues[index], paramFilterModel);
                    }
                }
            }
        }
    }
    return paramValues;
}
Also used : BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel)

Example 3 with ParamFilterModel

use of org.sagacity.sqltoy.config.model.ParamFilterModel in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseFilters.

/**
 * @todo 解析3.0版本 filters xml元素
 * @param filterSet
 * @param blankToNull
 * @return
 */
public static ParamFilterModel[] parseFilters(List<Element> filterSet, int blankToNull) {
    List<ParamFilterModel> filterModels = new ArrayList<ParamFilterModel>();
    // 1:强制将空白当做null;0:强制对空白不作为null处理;-1:默认值,用户不配置blank过滤器则视同为1,配置了则视同为0
    if (blankToNull == 1)
        filterModels.add(new ParamFilterModel("blank", new String[] { "*" }));
    boolean hasBlank = false;
    if (filterSet != null && !filterSet.isEmpty()) {
        String filterType;
        boolean blank = false;
        for (Element filter : filterSet) {
            blank = false;
            filterType = filter.getName();
            // 当开发者配置了blank过滤器时,则表示关闭默认将全部空白当做null处理的逻辑
            if (filterType.equals("blank")) {
                hasBlank = true;
                blank = true;
            }
            // [非强制且是blank ] 或者 [ 非blank]
            if ((blank && blankToNull != 1) || !blank) {
                ParamFilterModel filterModel = new ParamFilterModel();
                // 统一过滤的类别,避免不同版本和命名差异
                if (filterType.equals("equals") || filterType.equals("any") || filterType.equals("in"))
                    filterType = "eq";
                else if (filterType.equals("moreThan") || filterType.equals("more"))
                    filterType = "gt";
                else if (filterType.equals("moreEquals") || filterType.equals("more-equals"))
                    filterType = "gte";
                else if (filterType.equals("lessThan") || filterType.equals("less"))
                    filterType = "lt";
                else if (filterType.equals("lessEquals") || filterType.equals("less-equals"))
                    filterType = "lte";
                else if (filterType.equals("not-any"))
                    filterType = "neq";
                else if (filterType.equals("dateFormat"))
                    filterType = "date-format";
                filterModel.setFilterType(filterType);
                parseFilterElt(filterModel, filter);
                filterModels.add(filterModel);
            }
        }
    }
    // 当没有特定配置时,默认将所有空白当做null处理
    if (!hasBlank && blankToNull == -1)
        filterModels.add(0, new ParamFilterModel("blank", new String[] { "*" }));
    if (filterModels.isEmpty())
        return null;
    ParamFilterModel[] result = new ParamFilterModel[filterModels.size()];
    filterModels.toArray(result);
    return result;
}
Also used : ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

Example 4 with ParamFilterModel

use of org.sagacity.sqltoy.config.model.ParamFilterModel in project sagacity-sqltoy by chenrenfei.

the class SqlToyContext method getSqlToyConfig.

/**
 * @todo 获取sql对应的配置模型
 * @param sqlKey
 * @param type
 * @return
 */
public SqlToyConfig getSqlToyConfig(String sqlKey, SqlType type) {
    SqlToyConfig result = scriptLoader.getSqlConfig(sqlKey);
    if (null == result) {
        // 判断是否是sqlId,非在xml中定义id的sql
        if (!SqlConfigParseUtils.isNamedQuery(sqlKey)) {
            result = SqlConfigParseUtils.parseSqlToyConfig(sqlKey, (null == scriptLoader) ? null : scriptLoader.getDialect(), type, (null == scriptLoader) ? null : scriptLoader.getFunctionConverts());
            // 设置默认空白查询条件过滤filter,便于直接传递sql语句情况下查询条件的处理
            ParamFilterModel[] filters = new ParamFilterModel[1];
            filters[0] = new ParamFilterModel("blank", new String[] { "*" });
            result.setFilters(filters);
        } else {
            // 这一步理论上不应该执行
            result = new SqlToyConfig();
            result.setSql(sqlKey);
        }
    }
    return result;
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel)

Example 5 with ParamFilterModel

use of org.sagacity.sqltoy.config.model.ParamFilterModel in project sagacity-sqltoy by chenrenfei.

the class ParamFilterUtils method filterValue.

/**
 * @todo 对查询条件参数进行filter过滤加工处理(如:判断是否为null、日期格式转换等等)
 * @param paramsName
 * @param values
 * @param filters
 * @return
 */
public static Object[] filterValue(String[] paramsName, Object[] values, ParamFilterModel[] filters) {
    if (paramsName == null || paramsName.length == 0 || filters == null || filters.length == 0)
        return values;
    HashMap<String, Integer> paramIndexMap = new HashMap<String, Integer>();
    int paramSize = paramsName.length;
    Object[] paramValues = new Object[paramSize];
    for (int i = 0; i < paramSize; i++) {
        paramIndexMap.put(paramsName[i].toLowerCase(), i);
        paramValues[i] = values[i];
    }
    String[] filterParams;
    int index;
    HashMap<String, String> retainMap;
    String filterParam;
    boolean hasPrimary = false;
    for (ParamFilterModel paramFilterModel : filters) {
        filterParams = paramFilterModel.getParams();
        // 通配符表示针对所有参数
        if (filterParams.length == 1 && filterParams[0].equals("*"))
            filterParams = paramsName;
        // 排他性参数(当某些参数值都不为null,则设置其他参数值为null)
        if (paramFilterModel.getFilterType().equals("exclusive") && paramFilterModel.getUpdateParams() != null) {
            filterExclusive(paramIndexMap, paramFilterModel, paramValues);
        } else // 决定性参数不为null时即条件成立时,需要保留的参数(其他的参数全部设置为null)
        if (paramFilterModel.getFilterType().equals("primary") && !hasPrimary) {
            retainMap = paramFilterModel.getExcludesMap();
            index = (paramIndexMap.get(paramFilterModel.getParam()) == null) ? -1 : paramIndexMap.get(paramFilterModel.getParam());
            // 决定性参数值不为null
            if (index != -1 && paramValues[index] != null) {
                for (int j = 0; j < paramSize; j++) {
                    // 排除自身
                    if (j != index && (retainMap == null || !retainMap.containsKey(paramsName[j].toLowerCase()))) {
                        paramValues[j] = null;
                    }
                }
                hasPrimary = true;
            }
        } else {
            for (int i = 0, n = filterParams.length; i < n; i++) {
                filterParam = filterParams[i].toLowerCase();
                index = (paramIndexMap.get(filterParam) == null) ? -1 : paramIndexMap.get(filterParam);
                if (index != -1 && paramValues[index] != null) {
                    if (paramFilterModel.getExcludesMap() == null || !paramFilterModel.getExcludesMap().containsKey(filterParam)) {
                        paramValues[index] = filterSingleParam(paramValues[index], paramFilterModel);
                    }
                }
            }
        }
    }
    return paramValues;
}
Also used : HashMap(java.util.HashMap) ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel)

Aggregations

ParamFilterModel (org.sagacity.sqltoy.config.model.ParamFilterModel)7 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)2 BigInteger (java.math.BigInteger)1 Element (org.dom4j.Element)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 ParamsFilter (org.sagacity.sqltoy.model.ParamsFilter)1 Element (org.w3c.dom.Element)1