use of org.sagacity.sqltoy.config.model.TranslateCacheModel in project sagacity-sqltoy by chenrenfei.
the class TranslateManager method parseTranslate.
/**
* @todo 解析缓存翻译的配置文件
* @param configFile
* @throws Exception
*/
public void parseTranslate(String configFile) throws Exception {
if (StringUtil.isBlank(configFile))
return;
InputStream fileIS = null;
InputStreamReader ir = null;
try {
fileIS = CommonUtils.getFileInputStream(configFile);
if (fileIS != null) {
SAXReader saxReader = new SAXReader();
saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
ir = new InputStreamReader(fileIS);
Document doc = saxReader.read(ir);
Element root = doc.getRootElement();
List translates = root.elements("translate");
if (translates == null || translates.isEmpty())
return;
Element translate;
for (Iterator iter = translates.iterator(); iter.hasNext(); ) {
translate = (Element) iter.next();
TranslateCacheModel translateCacheModel = new TranslateCacheModel();
// 对应缓存
translateCacheModel.setCacheName(translate.attributeValue("cache"));
// key对应的数据列
if (translate.attribute("key-index") != null) {
translateCacheModel.setKeyIndex(Integer.parseInt(translate.attributeValue("key-index")));
}
// 查询数据的service
if (translate.attribute("service") != null) {
translateCacheModel.setService(translate.attributeValue("service"));
}
// 查询数据的service对应的方法
if (translate.attribute("method") != null) {
translateCacheModel.setServiceMethod(translate.attributeValue("method"));
}
// 设置缓存管理器
if (translate.attribute("cache-manager") != null) {
translateCacheModel.setTranslateCacheManager(translate.attributeValue("cache-manager"));
} else if (translate.attribute("manager") != null) {
translateCacheModel.setTranslateCacheManager(translate.attributeValue("manager"));
}
// 过期时长
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setExpireSeconds(SqlToyConstants.getCacheExpireSeconds());
if (translate.attribute("expire-seconds") != null) {
cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("expire-seconds")));
} else if (translate.attribute("keep-alive") != null) {
cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("keep-alive")));
}
// sql对应的dataSource
if (translate.attribute("datasource") != null)
translateCacheModel.setDataSource(translate.attributeValue("datasource"));
else if (translate.attribute("dataSource") != null)
translateCacheModel.setDataSource(translate.attributeValue("dataSource"));
// 基于简单的sql查询对应的sql
if (translate.attribute("sql") != null) {
translateCacheModel.setSql(translate.attributeValue("sql"));
} else if (StringUtil.isNotBlank(translate.getTextTrim())) {
translateCacheModel.setSql(translate.getText());
} else if (translate.element("sql") != null) {
translateCacheModel.setSql(translate.element("sql").getText());
}
translateMap.put(translateCacheModel.getCacheName(), translateCacheModel);
}
}
} catch (DocumentException de) {
de.printStackTrace();
logger.error("读取translate对应的xml文件失败,对应文件={}", configFile, de);
throw de;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (ir != null)
ir.close();
if (fileIS != null)
fileIS.close();
}
}
use of org.sagacity.sqltoy.config.model.TranslateCacheModel in project sagacity-sqltoy by chenrenfei.
the class TranslateManager method getCache.
/**
* @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存
* @param sqlToyContext
* @param conn
* @param cacheName
* @param cacheType
* 一般为null,不为空时一般用于数据字典等同于dictType
* @return
* @throws Exception
*/
public HashMap<String, Object[]> getCache(final SqlToyContext sqlToyContext, Connection conn, String cacheName, String cacheType) throws Exception {
// 获取缓存翻译的管理器
TranslateCacheModel cacheModel = translateMap.get(cacheName);
if (cacheModel == null) {
// cacheName);
return null;
}
TranslateCacheManager manager = getTranslateCacheManager(cacheModel.getTranslateCacheManager());
HashMap<String, Object[]> result = manager.getCache(cacheName, cacheType);
if (result == null || result.isEmpty()) {
final Object[] args = StringUtil.isBlank(cacheType) ? null : new Object[] { cacheType };
// sql 查询模式
if (StringUtil.isNotBlank(cacheModel.getSql())) {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(cacheModel.getSql(), SqlType.search);
// 避免sql中有:name 参数名称
String realSql = SqlConfigParseUtils.processNamedParamsQuery(sqlToyConfig.getSql()).getSql();
String dataSourceName = cacheModel.getDataSource();
if (dataSourceName == null)
dataSourceName = sqlToyConfig.getDataSource();
// 设置默认数据库
if (null == conn && StringUtil.isBlank(dataSourceName))
dataSourceName = getDefaultDataSource();
List cacheResult = null;
// 缓存sql来源于不同数据库
if (sqlToyConfig.getDataSourceShardingStragety() != null || StringUtil.isNotBlank(dataSourceName)) {
DataSource dataBase = null;
if (StringUtil.isNotBlank(dataSourceName))
dataBase = sqlToyContext.getDataSource(dataSourceName);
else {
// 考虑存在分库策略,update 2017-12-8
dataBase = ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(cacheModel.getSql(), null, args), null);
}
cacheResult = (List) DataSourceUtils.processDataSource(sqlToyContext, dataBase, new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, dialect), args, null, conn, 0, -1, -1).getRows());
}
});
} else {
cacheResult = DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, DataSourceUtils.getDialect(DataSourceUtils.getDbType(conn))), args, null, conn, 0, -1, -1).getRows();
}
result = new HashMap<String, Object[]>();
int cacheIndex = cacheModel.getKeyIndex();
List row;
for (int i = 0, n = cacheResult.size(); i < n; i++) {
row = (List) cacheResult.get(i);
Object[] rowAry = new Object[row.size()];
for (int j = 0, t = rowAry.length; j < t; j++) {
rowAry[j] = row.get(j);
}
result.put(rowAry[cacheIndex].toString(), rowAry);
}
} else {
// 通过spring 调用具体的bean 方法获取数据,必须返回的是HashMap结果
try {
result = (HashMap<String, Object[]>) sqlToyContext.getServiceData(cacheModel.getService(), cacheModel.getServiceMethod(), args);
} catch (Exception e) {
e.printStackTrace();
logger.error("缓存翻译通过接口服务:{}.{} 返回的结果必须是HashMap<key, Object[]{key,name1,..,nameX}> 类型的数据格式!", cacheModel.getService(), cacheModel.getServiceMethod());
}
}
// 放入缓存
if (result != null && !result.isEmpty()) {
manager.put(cacheModel.getCacheConfig(), cacheName, cacheType, result);
}
}
return result;
}
use of org.sagacity.sqltoy.config.model.TranslateCacheModel in project sagacity-sqltoy by chenrenfei.
the class TranslateManager method getTranslates.
/**
* @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存(多个translate对应的多个缓存结果)
* @param sqlToyContext
* @param conn
* @param translates
* @return
* @throws Exception
*/
public HashMap<String, HashMap<String, Object[]>> getTranslates(SqlToyContext sqlToyContext, Connection conn, HashMap<String, TranslateModel> translates) throws Exception {
HashMap<String, HashMap<String, Object[]>> result = new HashMap<String, HashMap<String, Object[]>>();
TranslateModel translate;
String cacheName;
HashMap<String, Object[]> cache;
TranslateCacheModel cacheModel;
for (Map.Entry<String, TranslateModel> entry : translates.entrySet()) {
translate = entry.getValue();
if (translateMap.containsKey(translate.getCache())) {
cacheModel = translateMap.get(translate.getCache());
cacheName = cacheModel.getCacheName();
cache = getCache(sqlToyContext, conn, cacheName, translate.getDictType());
if (cache != null)
result.put(translate.getColumn(), cache);
else {
result.put(translate.getColumn(), new HashMap<String, Object[]>());
logger.warn("sqltoy translate:cacheName={},cache-type={},column={}配置不正确,未获取对应cache数据!", cacheName, translate.getDictType(), translate.getColumn());
}
}
}
return result;
}
Aggregations