Search in sources :

Example 1 with SqlType

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

the class SqlXMLConfigParse method parseSingleSql.

/**
 * @todo 解析单个sql element元素,update 2020-7-2 支持外部集成命名空间前缀适配
 * @param sqlElt
 * @param dialect
 * @return
 * @throws Exception
 */
public static SqlToyConfig parseSingleSql(Element sqlElt, String dialect) throws Exception {
    String realDialect = dialect;
    String nodeName = sqlElt.getNodeName().toLowerCase();
    // 剔除前缀
    int prefixIndex = nodeName.indexOf(":");
    if (prefixIndex > 0) {
        nodeName = nodeName.substring(prefixIndex + 1);
    }
    // 目前只支持传统sql、elastic、mongo三种类型的语法
    if (!nodeName.equals("sql") && !nodeName.equals("eql") && !nodeName.equals("mql")) {
        return null;
    }
    String id = sqlElt.getAttribute("id");
    if (id == null) {
        throw new RuntimeException("请检查sql配置,没有给定sql对应的 id值!");
    }
    // 获取元素的namespace前缀
    String localName = getElementPrefixName(sqlElt);
    String local = StringUtil.isBlank(localName) ? "" : localName.concat(":");
    // 判断是否xml为精简模式即只有<sql id=""><![CDATA[]]></sql>模式
    NodeList nodeList = sqlElt.getElementsByTagName(local.concat("value"));
    String sqlContent = null;
    if (nodeList.getLength() > 0) {
        sqlContent = StringUtil.trim(nodeList.item(0).getTextContent());
    } else {
        sqlContent = StringUtil.trim(sqlElt.getTextContent());
    }
    if (StringUtil.isBlank(sqlContent)) {
        throw new RuntimeException("请检查sql-id='" + id + "' 的配置,没有正确填写sql内容!");
    }
    nodeList = sqlElt.getElementsByTagName(local.concat("count-sql"));
    String countSql = null;
    if (nodeList.getLength() > 0) {
        countSql = StringUtil.trim(nodeList.item(0).getTextContent());
    }
    // 替换全角空格
    sqlContent = sqlContent.replaceAll("\u3000", " ");
    if (countSql != null) {
        countSql = countSql.replaceAll("\u3000", " ");
    }
    SqlType type = sqlElt.hasAttribute("type") ? SqlType.getSqlType(sqlElt.getAttribute("type")) : SqlType.search;
    // 是否nosql模式
    boolean isNoSql = false;
    if (nodeName.equals("mql") || nodeName.equals("eql")) {
        if (nodeName.equals("mql")) {
            realDialect = DataSourceUtils.Dialect.MONGO;
        } else if (nodeName.equals("eql")) {
            realDialect = DataSourceUtils.Dialect.ES;
        }
        isNoSql = true;
    }
    SqlToyConfig sqlToyConfig = SqlConfigParseUtils.parseSqlToyConfig(sqlContent, realDialect, type);
    sqlToyConfig.setId(id);
    sqlToyConfig.setSqlType(type);
    // 为sql提供特定数据库的扩展
    if (sqlElt.hasAttribute("dataSource")) {
        sqlToyConfig.setDataSource(sqlElt.getAttribute("dataSource"));
    } else if (sqlElt.hasAttribute("datasource")) {
        sqlToyConfig.setDataSource(sqlElt.getAttribute("datasource"));
    }
    if (countSql != null) {
        // 清理sql中的一些注释、以及特殊的符号
        countSql = SqlUtil.clearMistyChars(SqlUtil.clearMark(countSql), " ").concat(" ");
        countSql = FunctionUtils.getDialectSql(countSql, dialect);
        countSql = ReservedWordsUtil.convertSql(countSql, DataSourceUtils.getDBType(dialect));
        sqlToyConfig.setCountSql(countSql);
    }
    // select 1 from,减少不必要的执行运算,提升效率)
    if (sqlElt.hasAttribute("union-all-count")) {
        sqlToyConfig.setUnionAllCount(Boolean.parseBoolean(sqlElt.getAttribute("union-all-count")));
    }
    // 解析sql对应dataSource的sharding配置
    parseShardingDataSource(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("sharding-datasource")));
    // 解析sql对应的table的sharding配置
    parseShardingTables(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("sharding-table")));
    // 解析格式化
    parseFormat(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("date-format")), sqlElt.getElementsByTagName(local.concat("number-format")));
    // 参数值为空白是否当中null处理,默认为-1
    int blankToNull = -1;
    if (sqlElt.hasAttribute("blank-to-null")) {
        blankToNull = (Boolean.parseBoolean(sqlElt.getAttribute("blank-to-null"))) ? 1 : 0;
    }
    // 参数加工过滤器
    nodeList = sqlElt.getElementsByTagName(local.concat("filters"));
    // 解析参数过滤器
    if (nodeList.getLength() > 0) {
        parseFilters(sqlToyConfig, nodeList.item(0).getChildNodes(), blankToNull, local);
    } else {
        parseFilters(sqlToyConfig, null, blankToNull, local);
    }
    // 解析分页优化器
    // <page-optimize parallel="true" alive-max="100" alive-seconds="90"
    // parallel-maxwait-seconds="600"/>
    nodeList = sqlElt.getElementsByTagName(local.concat("page-optimize"));
    if (nodeList.getLength() > 0) {
        PageOptimize optimize = new PageOptimize();
        Element pageOptimize = (Element) nodeList.item(0);
        // 保留不同条件的count缓存记录量
        if (pageOptimize.hasAttribute("alive-max")) {
            optimize.aliveMax(Integer.parseInt(pageOptimize.getAttribute("alive-max")));
        }
        // 不同sql条件分页记录数量保存有效时长(默认90秒)
        if (pageOptimize.hasAttribute("alive-seconds")) {
            optimize.aliveSeconds(Integer.parseInt(pageOptimize.getAttribute("alive-seconds")));
        }
        // 是否支持并行查询
        if (pageOptimize.hasAttribute("parallel")) {
            optimize.parallel(Boolean.parseBoolean(pageOptimize.getAttribute("parallel")));
        }
        // 最大并行等待时长(秒)
        if (pageOptimize.hasAttribute("parallel-maxwait-seconds")) {
            optimize.parallelMaxWaitSeconds(Long.parseLong(pageOptimize.getAttribute("parallel-maxwait-seconds")));
        }
        sqlToyConfig.setPageOptimize(optimize);
    }
    // 解析翻译器
    parseTranslate(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("translate")));
    // 解析link
    parseLink(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("link")), local);
    // 解析对结果的运算
    parseCalculator(sqlToyConfig, sqlElt, local);
    // 解析解密字段
    parseSecureDecrypt(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("secure-decrypt")));
    // 解析安全脱敏配置
    parseSecureMask(sqlToyConfig, sqlElt.getElementsByTagName(local.concat("secure-mask")));
    // mongo/elastic查询语法
    if (isNoSql) {
        parseNoSql(sqlToyConfig, sqlElt, local);
    }
    return sqlToyConfig;
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) NodeList(org.w3c.dom.NodeList) PageOptimize(org.sagacity.sqltoy.config.model.PageOptimize) Element(org.w3c.dom.Element) SqlType(org.sagacity.sqltoy.config.model.SqlType)

Aggregations

PageOptimize (org.sagacity.sqltoy.config.model.PageOptimize)1 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)1 SqlType (org.sagacity.sqltoy.config.model.SqlType)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1