Search in sources :

Example 46 with SqlToyConfig

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

the class DialectFactory method findPage.

/**
 * @todo 分页查询, pageNo为负一表示取全部记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param pageNo
 * @param pageSize
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findPage(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final long pageNo, final Integer pageSize, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findPage operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "findPage", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, true);
                QueryResult queryResult = null;
                Long recordCnt = null;
                // 通过查询条件构造唯一的key
                String pageQueryKey = PageOptimizeUtils.generateOptimizeKey(sqlToyConfig, queryExecutor);
                // 需要进行分页查询优化
                if (null != pageQueryKey) {
                    // 从缓存中提取总记录数
                    recordCnt = PageOptimizeUtils.getPageTotalCount(sqlToyConfig, pageQueryKey);
                    // 缓存中没有则重新查询
                    if (null == recordCnt) {
                        recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                        // 将总记录数登记到缓存
                        PageOptimizeUtils.registPageTotalCount(sqlToyConfig, pageQueryKey, recordCnt);
                    }
                } else
                    recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                // pageNo=-1时的提取数据量限制
                int limitSize = sqlToyContext.getPageFetchSizeLimit();
                // pageNo=-1时,总记录数超出限制则返回空集合
                boolean illegal = (pageNo == -1 && (limitSize != -1 && recordCnt > limitSize));
                if (recordCnt == 0 || illegal) {
                    queryResult = new QueryResult();
                    queryResult.setPageNo(pageNo);
                    queryResult.setPageSize(pageSize);
                    queryResult.setRecordCount(new Long(0));
                    if (illegal)
                        logger.warn("非法进行分页查询,提取记录总数为:{},sql={}", recordCnt, sqlToyConfig.getSql());
                } else {
                    // 合法的全记录提取,设置页号为1按记录数
                    if (pageNo == -1) {
                        // 通过参数处理最终的sql和参数值
                        SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                        queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
                        long totalRecord = (queryResult.getRows() == null) ? 0 : queryResult.getRows().size();
                        queryResult.setPageNo(1L);
                        queryResult.setPageSize(new Long(totalRecord).intValue());
                        queryResult.setRecordCount(totalRecord);
                    } else {
                        // 实际开始页(页数据超出总记录,则从第一页重新开始,相反如继续按指定的页查询则记录为空,且实际页号也不存在)
                        long realStartPage = (pageNo * pageSize >= (recordCnt + pageSize)) ? 1 : pageNo;
                        queryResult = getDialectSqlWrapper(dbType).findPageBySql(sqlToyContext, realSqlToyConfig, queryExecutor, realStartPage, pageSize, conn);
                        queryResult.setPageNo(realStartPage);
                        queryResult.setPageSize(pageSize);
                        queryResult.setRecordCount(recordCnt);
                    }
                    // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                    List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                    ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                    // 结果映射成对象
                    if (queryExecutor.getResultType() != null) {
                        queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                    }
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult) QueryResult(org.sagacity.sqltoy.model.QueryResult) List(java.util.List) ArrayList(java.util.ArrayList)

Example 47 with SqlToyConfig

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

the class DialectFactory method batchUpdate.

/**
 * @todo 批量执行sql修改或删除操作
 * @param sqlToyContext
 * @param sqlOrNamedSql
 * @param dataSet
 * @param batchSize
 * @param reflectPropertyHandler
 * @param insertCallhandler
 * @param autoCommit
 * @param dataSource
 * @return
 * @throws Exception
 */
public Long batchUpdate(final SqlToyContext sqlToyContext, final String sqlOrNamedSql, final List dataSet, final int batchSize, final ReflectPropertyHandler reflectPropertyHandler, final InsertRowCallbackHandler insertCallhandler, final Boolean autoCommit, final DataSource dataSource) throws Exception {
    try {
        final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sqlOrNamedSql, SqlType.update);
        SqlExecuteStat.start(sqlToyConfig.getId(), "batchUpdate", sqlToyConfig.isShowSql());
        return (Long) DataSourceUtils.processDataSource(sqlToyContext, dataSource, new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                String realSql = sqlToyContext.convertFunctions(sqlToyConfig.getSql(), dialect);
                Integer[] fieldTypes = null;
                List values = dataSet;
                // sql中存在:named参数模式,通过sql提取参数名称
                if (sqlToyConfig.getParamsName() != null) {
                    // 替换sql中:name为?并提取参数名称归集成数组
                    SqlParamsModel sqlParamsModel = SqlConfigParseUtils.processNamedParamsQuery(realSql);
                    values = BeanUtil.reflectBeansToList(dataSet, sqlParamsModel.getParamsName(), reflectPropertyHandler, false, 0);
                    fieldTypes = BeanUtil.matchMethodsType(dataSet.get(0).getClass(), sqlParamsModel.getParamsName());
                    realSql = sqlParamsModel.getSql();
                }
                SqlExecuteStat.showSql(realSql, null);
                this.setResult(SqlUtil.batchUpdateByJdbc(realSql, values, batchSize, insertCallhandler, fieldTypes, autoCommit, conn));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) SqlParamsModel(org.sagacity.sqltoy.config.model.SqlParamsModel) List(java.util.List) ArrayList(java.util.ArrayList) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException)

Example 48 with SqlToyConfig

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

the class SqlXMLConfigParse method parseSingleFile.

/**
 * @todo <b>解析单个sql对应的xml文件</b>
 * @param xmlFile
 * @param cache
 * @param encoding
 * @param dialect
 * @throws Exception
 */
public static void parseSingleFile(Object xmlFile, ConcurrentHashMap<String, SqlToyConfig> cache, String encoding, String dialect) throws Exception {
    InputStream fileIS = null;
    InputStreamReader ir = null;
    try {
        if (xmlFile instanceof File) {
            File file = (File) xmlFile;
            filesLastModifyMap.put(file.getName(), new Long(file.lastModified()));
            fileIS = new FileInputStream(file);
            if (logger.isDebugEnabled())
                logger.debug("正在解析sql文件,对应文件={}", file.getName());
        } else {
            fileIS = getResourceAsStream((String) xmlFile);
            if (logger.isDebugEnabled())
                logger.debug("正在解析sql文件,对应文件={}", (String) xmlFile);
        }
        if (fileIS != null) {
            if (encoding != null)
                ir = new InputStreamReader(fileIS, encoding);
            else
                ir = new InputStreamReader(fileIS);
            SAXReader saxReader = new SAXReader();
            saxReader.setFeature(SqlToyConstants.XML_FETURE, false);
            if (StringUtil.isNotBlank(encoding))
                saxReader.setEncoding(encoding);
            Document doc = saxReader.read(ir);
            List<Element> sqlElts = doc.getRootElement().elements();
            if (sqlElts == null || sqlElts.isEmpty())
                return;
            // 解析单个sql
            SqlToyConfig sqlToyConfig;
            for (Iterator<Element> iter = sqlElts.iterator(); iter.hasNext(); ) {
                sqlToyConfig = parseSingleSql(iter.next(), dialect);
                if (sqlToyConfig != null) {
                    // 去除sql中的注释语句并放入缓存
                    if (cache.get(sqlToyConfig.getId()) != null)
                        logger.warn("发现重复的SQL语句,id={},将被覆盖!", sqlToyConfig.getId());
                    cache.put(sqlToyConfig.getId(), sqlToyConfig);
                }
            }
        }
    } catch (DocumentException de) {
        de.printStackTrace();
        logger.error("读取sql对应的xml文件失败,对应文件={}", xmlFile, de);
        throw de;
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("解析xml中对应的sql失败,对应文件={},正确的配置为<sql|mql|eql id=\"\"><![CDATA[]]></sql|mql|eql>或<sql|mql|eql id=\"\"><desc></desc><value><![CDATA[]]></value></sql|mql|eql>", xmlFile, e);
        throw e;
    } finally {
        if (ir != null)
            ir.close();
        if (fileIS != null)
            fileIS.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Element(org.dom4j.Element) Document(org.dom4j.Document) FileInputStream(java.io.FileInputStream) DocumentException(org.dom4j.DocumentException) DocumentException(org.dom4j.DocumentException) File(java.io.File)

Example 49 with SqlToyConfig

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

the class DialectFactory method updateFetchRandom.

@Deprecated
public QueryResult updateFetchRandom(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer random, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchRandom", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                QueryResult queryResult = getDialectSqlWrapper(dbType).updateFetchRandom(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), random, updateRowHandler, conn);
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 50 with SqlToyConfig

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

the class DialectFactory method findByQuery.

/**
 * @todo 查询符合条件的数据集合
 * @param sqlToyContext
 * @param queryExecutor
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findByQuery(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findByQuery operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "query", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                // 通过参数处理最终的sql和参数值
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                QueryResult queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                // 结果映射成对象
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

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