Search in sources :

Example 6 with ParamFilterModel

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

the class SqlScriptLoader method getSqlConfig.

/**
 * @todo 提供根据sql或sqlId获取sql配置模型
 * @param sqlKey
 * @param sqlType
 * @param dialect
 * @return
 */
public SqlToyConfig getSqlConfig(String sqlKey, SqlType sqlType, String dialect) {
    if (StringUtil.isBlank(sqlKey)) {
        throw new IllegalArgumentException("sql or sqlId is null!");
    }
    SqlToyConfig result = null;
    String realDialect = (dialect == null) ? "" : dialect.toLowerCase();
    // sqlId形式
    if (SqlConfigParseUtils.isNamedQuery(sqlKey)) {
        if (!realDialect.equals("")) {
            // sqlId_dialect
            result = sqlCache.get(sqlKey.concat("_").concat(realDialect));
            // dialect_sqlId
            if (result == null) {
                result = sqlCache.get(realDialect.concat("_").concat(sqlKey));
            }
            // 兼容一下sqlserver的命名
            if (result == null && realDialect.equals(Dialect.SQLSERVER)) {
                result = sqlCache.get(sqlKey.concat("_mssql"));
                if (result == null) {
                    result = sqlCache.get("mssql_".concat(sqlKey));
                }
            }
            // 兼容一下postgres的命名
            if (result == null && realDialect.equals(Dialect.POSTGRESQL)) {
                result = sqlCache.get(sqlKey.concat("_postgres"));
                if (result == null) {
                    result = sqlCache.get("postgres_".concat(sqlKey));
                }
            }
        }
        if (result == null) {
            result = sqlCache.get(sqlKey);
            if (result == null) {
                throw new DataAccessException("\n发生错误:sqlId=[" + sqlKey + "]无对应的sql配置,请检查对应的sql.xml文件是否被正确加载!\n" + "/*----------------------错误可能的原因如下---------------------*/\n" + "/* 1、检查: spring.sqltoy.sqlResourcesDir=[" + sqlResourcesDir + "]配置(如:字母拼写),会导致sql文件没有被加载;\n" + "/* 2、sql.xml文件没有被编译到classes目录下面;请检查maven的编译配置                        \n" + "/* 3、sqlId对应的文件内部错误!版本合并或书写错误会导致单个文件解析错误                          \n" + "/* ------------------------------------------------------------*/");
            }
        }
    } else {
        result = codeSqlCache.get(sqlKey);
        if (result == null) {
            result = SqlConfigParseUtils.parseSqlToyConfig(sqlKey, realDialect, sqlType);
            // 设置默认空白查询条件过滤filter,便于直接传递sql语句情况下查询条件的处理
            result.addFilter(new ParamFilterModel("blank", new String[] { "*" }));
            // 限制数量的原因是存在部分代码中的sql会拼接条件参数值,导致不同的sql无限增加
            if (codeSqlCache.size() < SqlToyConstants.getMaxCodeSqlCount()) {
                codeSqlCache.put(sqlKey, result);
            }
        }
    }
    return result;
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 7 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 sqlToyConfig
 * @param filterSet
 * @param blankToNull
 * @param local        命名空间前缀
 */
private static void parseFilters(SqlToyConfig sqlToyConfig, NodeList filterSet, int blankToNull, String local) {
    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.getLength() > 0) {
        String filterType;
        boolean blank = false;
        Element filter;
        int prefixIndex;
        for (int i = 0; i < filterSet.getLength(); i++) {
            if (filterSet.item(i).getNodeType() == Node.ELEMENT_NODE) {
                filter = (Element) filterSet.item(i);
                blank = false;
                // 剔除xml命名空间的前缀部分
                filterType = filter.getNodeName();
                prefixIndex = filterType.indexOf(":");
                if (prefixIndex > 0) {
                    filterType = filterType.substring(prefixIndex + 1);
                }
                // 当开发者配置了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(sqlToyConfig, filterModel, filter, local);
                    filterModels.add(filterModel);
                }
            }
        }
    }
    // 当没有特定配置时,默认将所有空白当做null处理
    if (!hasBlank && blankToNull == -1) {
        filterModels.add(0, new ParamFilterModel("blank", new String[] { "*" }));
    }
    if (filterModels.isEmpty()) {
        return;
    }
    sqlToyConfig.addFilters(filterModels);
}
Also used : ParamFilterModel(org.sagacity.sqltoy.config.model.ParamFilterModel) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

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