Search in sources :

Example 36 with SqlToyConfig

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

the class ClickHouseDialect method load.

@Override
public Serializable load(SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, Integer dbType, String dialect, String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search, dialect);
    String loadSql = sqlToyConfig.getSql(dialect);
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn, dbType);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 37 with SqlToyConfig

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

the class TranslateConfigParse method parseTranslateConfig.

/**
 * @todo 解析translate配置文件
 * @param translateMap
 * @param checker
 * @param translateConfig
 * @param charset
 * @throws Exception
 */
public static DefaultConfig parseTranslateConfig(final SqlToyContext sqlToyContext, final HashMap<String, TranslateConfigModel> translateMap, final List<CheckerConfigModel> checker, String translateConfig, String charset) throws Exception {
    return (DefaultConfig) XMLUtil.readXML(translateConfig, charset, false, new XMLCallbackHandler() {

        @Override
        public Object process(Document doc, Element root) throws Exception {
            // 解析缓存翻译配置
            Element node = root.element("cache-translates");
            DefaultConfig defaultConfig = new DefaultConfig();
            if (node.attribute("disk-store-path") != null)
                defaultConfig.setDiskStorePath(node.attributeValue("disk-store-path"));
            List<Element> elts;
            for (String translateType : TRANSLATE_TYPES) {
                elts = node.elements(translateType.concat(TRANSLATE_SUFFIX));
                if (elts != null && !elts.isEmpty()) {
                    int index = 1;
                    for (Element elt : elts) {
                        TranslateConfigModel translateCacheModel = new TranslateConfigModel();
                        XMLUtil.setAttributes(elt, translateCacheModel);
                        translateCacheModel.setType(translateType);
                        if (translateType.equals("sql")) {
                            if (StringUtil.isBlank(translateCacheModel.getSql())) {
                                String sql = (elt.element("sql") != null) ? elt.elementText("sql") : elt.getText();
                                String sqlId = "SQLTOY_TRANSLATE_Cache_ID_00" + index;
                                boolean isShowSql = StringUtil.matches(sql, SqlToyConstants.NOT_PRINT_REGEX);
                                SqlToyConfig sqlToyConfig = new SqlToyConfig(sqlId, StringUtil.clearMistyChars(SqlUtil.clearMark(sql), " "));
                                sqlToyConfig.setShowSql(!isShowSql);
                                sqlToyContext.putSqlToyConfig(sqlToyConfig);
                                translateCacheModel.setSql(sqlId);
                            }
                            index++;
                        }
                        // 过期时长
                        if (translateCacheModel.getKeepAlive() <= 0)
                            translateCacheModel.setKeepAlive(SqlToyConstants.getCacheExpireSeconds());
                        translateMap.put(translateCacheModel.getCache(), translateCacheModel);
                    }
                }
            }
            // 解析更新检测器
            node = root.element("cache-update-checkers");
            if (node != null) {
                for (String translateType : TRANSLATE_TYPES) {
                    elts = node.elements(translateType.concat(CHECKER_SUFFIX));
                    if (elts != null && !elts.isEmpty()) {
                        int index = 1;
                        for (Element elt : elts) {
                            CheckerConfigModel checherConfigModel = new CheckerConfigModel();
                            XMLUtil.setAttributes(elt, checherConfigModel);
                            checherConfigModel.setType(translateType);
                            if (translateType.equals("sql")) {
                                if (StringUtil.isBlank(checherConfigModel.getSql())) {
                                    String sqlId = "SQLTOY_TRANSLATE_Check_ID_00" + index;
                                    String sql = (elt.element("sql") != null) ? elt.elementText("sql") : elt.getText();
                                    boolean isShowSql = StringUtil.matches(sql, SqlToyConstants.NOT_PRINT_REGEX);
                                    SqlToyConfig sqlToyConfig = new SqlToyConfig(sqlId, StringUtil.clearMistyChars(SqlUtil.clearMark(sql), " "));
                                    sqlToyConfig.setShowSql(!isShowSql);
                                    sqlToyContext.putSqlToyConfig(sqlToyConfig);
                                    checherConfigModel.setSql(sqlId);
                                }
                                index++;
                            }
                            // 剔除tab\回车等特殊字符
                            String frequency = StringUtil.clearMistyChars(checherConfigModel.getCheckFrequency(), "");
                            List<TimeSection> timeSections = new ArrayList<TimeSection>();
                            // frequency的格式 frequency="0..12?15,12..18:30?10,18:30..24?60"
                            if (StringUtil.isNotBlank(frequency)) {
                                // 统一格式,去除全角字符,去除空白
                                frequency = frequency.replaceAll("\\;", ",").replaceAll("\\?", "?").replaceAll("\\.", ".").replaceAll("\\。", ".").replaceAll("\\,", ",").trim();
                                // 可以是单个频率值,表示0到24小时采用统一的频率
                                if (CommonUtils.isInteger(frequency)) {
                                    TimeSection section = new TimeSection();
                                    section.setStart(0);
                                    section.setEnd(2400);
                                    section.setIntervalSeconds(Integer.parseInt(frequency));
                                    timeSections.add(section);
                                } else {
                                    // 归整分割符号统一为逗号,将时间格式由HH:mm 转为HHmm格式
                                    String[] sectionsStr = frequency.split("\\,");
                                    for (int i = 0; i < sectionsStr.length; i++) {
                                        TimeSection section = new TimeSection();
                                        // 问号切割获取时间区间和时间间隔
                                        String[] sectionPhase = sectionsStr[i].split("\\?");
                                        // 获取开始和结束时间点
                                        String[] startEnd = sectionPhase[0].split("\\.{2}");
                                        section.setIntervalSeconds(Integer.parseInt(sectionPhase[1].trim()));
                                        section.setStart(getHourMinute(startEnd[0].trim()));
                                        section.setEnd(getHourMinute(startEnd[1].trim()));
                                        timeSections.add(section);
                                    }
                                }
                            }
                            checherConfigModel.setTimeSections(timeSections);
                            checker.add(checherConfigModel);
                        }
                    }
                }
            }
            return defaultConfig;
        }
    });
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) TranslateConfigModel(org.sagacity.sqltoy.translate.model.TranslateConfigModel) TimeSection(org.sagacity.sqltoy.translate.model.TimeSection) DefaultConfig(org.sagacity.sqltoy.translate.model.DefaultConfig) XMLCallbackHandler(org.sagacity.sqltoy.callback.XMLCallbackHandler) CheckerConfigModel(org.sagacity.sqltoy.translate.model.CheckerConfigModel)

Example 38 with SqlToyConfig

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

the class SqlToyDaoSupport method executeSql.

/**
 * @todo 解析sql中的参数名称,以此名称到entity中提取对应的值作为查询条件值执行sql
 * @param sqlOrNamedSql
 * @param entity
 * @param reflectPropertyHandler
 * @return
 * @throws Exception
 */
protected Long executeSql(final String sqlOrNamedSql, final Serializable entity, final ReflectPropertyHandler reflectPropertyHandler) throws Exception {
    SqlToyConfig sqlToyConfig = getSqlToyConfig(sqlOrNamedSql, SqlType.update);
    // 根据sql中的变量从entity对象中提取参数值
    Object[] paramValues = SqlConfigParseUtils.reflectBeanParams(sqlToyConfig.getParamsName(), entity, reflectPropertyHandler);
    return executeSql(sqlOrNamedSql, sqlToyConfig.getParamsName(), paramValues, false, this.getDataSource(null));
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 39 with SqlToyConfig

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

the class Elastic method findPage.

/**
 * @todo 分页查询
 * @param pageModel
 * @return
 * @throws Exception
 */
public PaginationModel findPage(PaginationModel pageModel) throws Exception {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql);
    if (sqlToyConfig.getNoSqlConfigModel() == null)
        throw new Exception(ERROR_MESSAGE);
    if (sqlToyConfig.getNoSqlConfigModel().isSqlMode())
        return ElasticSqlPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
    return ElasticSearchPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
}
Also used : QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 40 with SqlToyConfig

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

the class DB2Dialect method load.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#load(java.io.Serializable,
	 * java.util.List, java.sql.Connection)
	 */
@Override
public Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
    String loadSql = sqlToyConfig.getSql();
    if (lockMode != null) {
        switch(lockMode) {
            case UPGRADE_NOWAIT:
            case UPGRADE:
                loadSql = loadSql + " for update with rs";
                break;
        }
    }
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

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