Search in sources :

Example 6 with SqlToyConfig

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

the class SqlConfigParseUtils method parseSqlToyConfig.

/**
 * @todo 将动态的sql解析组合成一个SqlToyConfig模型,以便统一处理
 * @param querySql
 * @param dialect
 * @param sqlType
 * @param functionConverts
 * @return
 */
public static SqlToyConfig parseSqlToyConfig(String querySql, String dialect, SqlType sqlType, List<IFunction> functionConverts) {
    SqlToyConfig sqlToyConfig = new SqlToyConfig();
    // debug模式下面关闭sql打印
    sqlToyConfig.setShowSql(!StringUtil.matches(querySql, SqlToyConstants.NOT_PRINT_REGEX));
    // 清理sql中的一些注释、以及特殊的符号
    String originalSql = StringUtil.clearMistyChars(SqlUtil.clearMark(querySql), BLANK).concat(BLANK);
    // 对sql中的函数进行特定数据库方言转换
    originalSql = convertFunctions(functionConverts, dialect, originalSql);
    // 判定是否有with查询模式
    sqlToyConfig.setHasWith(SqlConfigParseUtils.hasWith(originalSql));
    /**
     * 只有在查询模式前提下才支持fastPage机制
     */
    if (sqlType.equals(SqlType.search)) {
        // sqlToyConfig.setHasFast(hasFast(originalSql));
        // 判断是否有快速分页@fast 宏
        Matcher matcher = FAST_PATTERN.matcher(originalSql);
        if (matcher.find()) {
            int start = matcher.start();
            String preSql = originalSql.substring(0, start);
            String matchedFastSql = matcher.group();
            int endMarkIndex = StringUtil.getSymMarkIndex("(", ")", matchedFastSql, 0);
            // 得到分页宏处理器中的sql
            String fastSql = matchedFastSql.substring(matchedFastSql.indexOf("(") + 1, endMarkIndex);
            String tailSql = originalSql.substring(start + endMarkIndex + 1);
            // sql剔除掉快速分页宏,在分页查询时再根据presql和tailsql、fastsql自行组装,从而保障正常的非分页查询直接提取sql
            sqlToyConfig.setSql(preSql.concat(" (").concat(fastSql).concat(") ").concat(tailSql));
            sqlToyConfig.setFastSql(fastSql);
            sqlToyConfig.setFastPreSql(preSql);
            sqlToyConfig.setFastTailSql(tailSql);
            // 判断是否有快速分页
            sqlToyConfig.setHasFast(true);
        } else
            sqlToyConfig.setSql(originalSql);
    } else
        sqlToyConfig.setSql(originalSql);
    // 提取with fast查询语句
    processFastWith(sqlToyConfig);
    // 提取sql中的参数名称
    sqlToyConfig.setParamsName(getSqlParamsName(sqlToyConfig.getSql(), true));
    return sqlToyConfig;
}
Also used : Matcher(java.util.regex.Matcher) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 7 with SqlToyConfig

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

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param pageModel
 * @return
 * @throws Exception
 */
public PaginationModel findPage(PaginationModel pageModel) throws Exception {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql);
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null)
        throw new Exception(ERROR_MESSAGE);
    // 最后的执行语句
    String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig));
    return findPage(new MongoTemplate(getMongoDbFactory(noSqlModel.getMongoFactory())), sqlToyConfig, pageModel, realMql, queryExecutor.getResultTypeName());
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate)

Example 8 with SqlToyConfig

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

the class ResultUtils method getPivotCategory.

/**
 * @todo 提取数据旋转对应的sql查询结果
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param conn
 * @param dialect
 * @return
 * @throws Exception
 */
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, String dialect) throws Exception {
    if (sqlToyConfig.getResultProcessor() != null && !sqlToyConfig.getResultProcessor().isEmpty()) {
        List resultProcessors = sqlToyConfig.getResultProcessor();
        Object processor;
        for (int i = 0; i < resultProcessors.size(); i++) {
            processor = resultProcessors.get(i);
            // 数据旋转只能存在一个
            if (processor instanceof PivotModel) {
                PivotModel pivotModel = (PivotModel) processor;
                if (pivotModel.getCategorySql() != null) {
                    SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search), queryExecutor, dialect, false);
                    SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(), queryExecutor.getParamsName(pivotSqlConfig), queryExecutor.getParamsValue(pivotSqlConfig));
                    List pivotCategory = SqlUtil.findByJdbcQuery(pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, conn);
                    // 行转列返回
                    return CollectionUtil.convertColToRow(pivotCategory, null);
                }
            }
        }
    }
    return null;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) List(java.util.List) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 9 with SqlToyConfig

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

the class ResultUtils method getPivotCategory.

/**
 * @todo 提取数据旋转对应的sql查询结果
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param conn
 * @param dbType
 * @param dialect
 * @return
 * @throws Exception
 */
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, final Integer dbType, String dialect) throws Exception {
    List resultProcessors = new ArrayList();
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    if (!sqlToyConfig.getResultProcessor().isEmpty()) {
        resultProcessors.addAll(sqlToyConfig.getResultProcessor());
    }
    // QueryExecutor中扩展的计算
    if (extend != null && !extend.calculators.isEmpty()) {
        resultProcessors.addAll(extend.calculators);
    }
    Object processor;
    for (int i = 0; i < resultProcessors.size(); i++) {
        processor = resultProcessors.get(i);
        // 数据旋转只能存在一个
        if (processor instanceof PivotModel) {
            PivotModel pivotModel = (PivotModel) processor;
            if (pivotModel.getCategorySql() != null) {
                SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search, ""), queryExecutor, dialect, false);
                SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(dialect), extend.getParamsName(pivotSqlConfig), extend.getParamsValue(sqlToyContext, pivotSqlConfig), dialect);
                List pivotCategory = SqlUtil.findByJdbcQuery(sqlToyContext.getTypeHandler(), pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, null, conn, dbType, sqlToyConfig.isIgnoreEmpty(), null, SqlToyConstants.FETCH_SIZE, -1);
                // 行转列返回
                return CollectionUtil.convertColToRow(pivotCategory, null);
            }
        }
    }
    return null;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 10 with SqlToyConfig

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

the class SqlConfigParseUtilsTest method testParseSql.

@Test
public void testParseSql() throws Exception {
    String sqlFile = "classpath:scripts/showcase-sql.sql.xml";
    List<SqlToyConfig> result = new ArrayList<SqlToyConfig>();
    InputStream fileIS = FileUtil.getFileInputStream(sqlFile);
    domFactory.setFeature(SqlToyConstants.XML_FETURE, false);
    DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
    Document doc = domBuilder.parse(fileIS);
    NodeList sqlElts = doc.getDocumentElement().getChildNodes();
    if (sqlElts == null || sqlElts.getLength() == 0)
        return;
    // 解析单个sql
    Element sqlElt;
    Node obj;
    for (int i = 0; i < sqlElts.getLength(); i++) {
        obj = sqlElts.item(i);
        if (obj.getNodeType() == Node.ELEMENT_NODE) {
            sqlElt = (Element) obj;
            result.add(SqlXMLConfigParse.parseSingleSql(sqlElt, "mysql"));
        }
    }
    for (SqlToyConfig config : result) {
        System.err.println(config.getSql());
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

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