Search in sources :

Example 76 with SqlToyConfig

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

the class PageOptimizeTest method main.

public static void main(String[] args) {
    SqlToyConfig sqlToyConfig = new SqlToyConfig("mysql");
    sqlToyConfig.setId("sqltoy_showcase");
    PageOptimize pageOptimize = new PageOptimize();
    // 60秒
    pageOptimize.aliveSeconds(60);
    // 200个
    pageOptimize.aliveMax(200);
    // 模仿60个用户
    for (int i = 0; i < 60; i++) {
        PageOptimizeThread thread = new PageOptimizeThread(sqlToyConfig, pageOptimize, i);
        thread.start();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) PageOptimize(org.sagacity.sqltoy.config.model.PageOptimize)

Example 77 with SqlToyConfig

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

the class Mongo method findTop.

/**
 * @todo 查询前多少条记录
 * @param topSize
 * @return
 */
public List<?> findTop(final Float topSize) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findTop(getMongoTemplate(), sqlToyConfig, topSize, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 78 with SqlToyConfig

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

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param page
 * @return
 */
public Page findPage(Page page) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findPage(getMongoTemplate(), sqlToyConfig, page, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 79 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig 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 80 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig 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

SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)82 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)22 Serializable (java.io.Serializable)20 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)20 QueryResult (org.sagacity.sqltoy.model.QueryResult)20 Connection (java.sql.Connection)19 ArrayList (java.util.ArrayList)19 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)19 List (java.util.List)16 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)16 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)15 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)12 BaseException (org.sagacity.sqltoy.exception.BaseException)10 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)5 DataSource (javax.sql.DataSource)4 Test (org.junit.jupiter.api.Test)4 ParallQueryExecutor (org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor)4 QueryExecutor (org.sagacity.sqltoy.executor.QueryExecutor)4 InputStream (java.io.InputStream)3 Document (org.w3c.dom.Document)3