Commit 01bd2920 authored by 雍欢's avatar 雍欢

自定义查询SQL方言

parent b34ad564
...@@ -23,11 +23,7 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion { ...@@ -23,11 +23,7 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion {
/** /**
* 实体对象 * 实体对象
*/ */
private final Map<String, Query> querys; private final List<Map<String, Query>> querys;
/**
* @since 1.1.3
*/
private final Map<String, Query> dialectQuerys;
/** /**
* 版本 * 版本
...@@ -36,19 +32,18 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion { ...@@ -36,19 +32,18 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion {
private List<String> configFilePaths; private List<String> configFilePaths;
public QueryXmlModel(QueryMappings queryMappings, QueryMappings dialectQueryMappings) { public QueryXmlModel(QueryMappings queryMapping) {
if (queryMappings != null) { this(Collections.singletonList(queryMapping));
querys = Arrays.stream(queryMappings.getQueryArray()) }
.collect(Collectors.toMap(Query::getName, query -> query));
} else { /**
querys = Collections.emptyMap(); * @since 1.1.3
} */
if (dialectQueryMappings != null) { public QueryXmlModel(List<QueryMappings> queryMappings) {
dialectQuerys = Arrays.stream(dialectQueryMappings.getQueryArray()) querys = queryMappings.stream()
.collect(Collectors.toMap(Query::getName, query -> query)); .filter(Objects::nonNull)
} else { .map(queryMapping -> Arrays.stream(queryMapping.getQueryArray()).collect(Collectors.toMap(Query::getName, query -> query)))
dialectQuerys = Collections.emptyMap(); .collect(Collectors.toList());
}
} }
public String getName() { public String getName() {
...@@ -59,24 +54,15 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion { ...@@ -59,24 +54,15 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion {
this.name = name; this.name = name;
} }
@Deprecated
public Map<String, Query> getQuerys() { public Map<String, Query> getQuerys() {
return querys; return querys.get(0);
}
/**
* @since 1.1.3
*/
public Map<String, Query> getDialectQuerys() {
return dialectQuerys;
} }
public Query getQuery(String name) { public Query getQuery(String name) {
// 优先从方言中获取 return querys.stream().map(query -> query.get(name))
Query query = dialectQuerys.get(name); .filter(Objects::nonNull)
if (query == null) { .findFirst().get();
query = querys.get(name);
}
return query;
} }
public void setVersion(Long version) { public void setVersion(Long version) {
...@@ -93,6 +79,10 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion { ...@@ -93,6 +79,10 @@ public class QueryXmlModel implements Serializable, ConfigFileVersion {
return configFilePaths.get(0); return configFilePaths.get(0);
} }
/**
* @deprecated 已被 {@link #setConfigFilePaths(List)} 替代。
*/
@Deprecated
public void setConfigFilePath(String configFilePath) { public void setConfigFilePath(String configFilePath) {
this.configFilePaths = Collections.singletonList(configFilePath); this.configFilePaths = Collections.singletonList(configFilePath);
} }
......
...@@ -2,17 +2,26 @@ package com.huigou.uasp.bmp.common.query; ...@@ -2,17 +2,26 @@ package com.huigou.uasp.bmp.common.query;
import com.huigou.data.query.QueryXmlLoadInterface; import com.huigou.data.query.QueryXmlLoadInterface;
import com.huigou.data.query.QueryXmlModel; import com.huigou.data.query.QueryXmlModel;
import com.huigou.exception.ApplicationException;
import com.huigou.exception.ResourceLoadException; import com.huigou.exception.ResourceLoadException;
import com.huigou.uasp.bmp.query.QueryMappingsDocument; import com.huigou.uasp.bmp.query.QueryMappingsDocument;
import com.huigou.util.ResourceLoadManager; import com.huigou.util.ResourceLoadManager;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.XmlException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* 查询模型管理类 * 查询模型管理类
...@@ -35,43 +44,45 @@ public class QueryXmlManager extends ResourceLoadManager<QueryXmlModel> implemen ...@@ -35,43 +44,45 @@ public class QueryXmlManager extends ResourceLoadManager<QueryXmlModel> implemen
*/ */
@Override @Override
public QueryXmlModel loadConfigFile(String path) throws ResourceLoadException { public QueryXmlModel loadConfigFile(String path) throws ResourceLoadException {
InputStream inputStream = null, dialectXmlStream = null; List<InputStream> xmlStreams = Collections.emptyList();
try { try {
// 默认SQL XML 文件 List<String> paths = new ArrayList<>(2);
ClassPathResource resource = getResource(path); // 方言xml放在最前面,从而达到优先使用方言xml的目的
// 方言SQL XML文件 if (StringUtils.isNotBlank(sqlDialect)) {
ClassPathResource dialectXml = getResource(getDialectXmlPath(path)); paths.add(getDialectXmlPath(path));
Assert.isTrue(resource.exists() || dialectXml.exists(), "文件不存在");
QueryMappingsDocument.QueryMappings queryMappings = null;
if (resource.exists()) {
inputStream = resource.getInputStream();
queryMappings = QueryMappingsDocument.Factory.parse(inputStream).getQueryMappings();
}
QueryMappingsDocument.QueryMappings dialectQueryMappings = null;
if (dialectXml.exists()) {
dialectXmlStream = dialectXml.getInputStream();
dialectQueryMappings = QueryMappingsDocument.Factory.parse(dialectXmlStream)
.getQueryMappings();
} }
paths.add(path);
List<Resource> resources = paths.stream()
.map(ClassPathResource::new)
.filter(Resource::exists)
.collect(Collectors.toList());
QueryXmlModel model = new QueryXmlModel(queryMappings, dialectQueryMappings); xmlStreams = resources.stream()
// 文件最后修改时间 .map(xml -> {
long version; try {
if (resource.exists() && dialectXml.exists()) { return xml.getInputStream();
version = resource.lastModified() > dialectXml.lastModified() } catch (IOException ioe) {
? resource.lastModified() throw new ApplicationException(ioe);
: dialectXml.lastModified(); }
} else { })
version = resource.exists() ? resource.lastModified() : dialectXml.lastModified(); .collect(Collectors.toList());
}
model.setVersion(version); List<QueryMappingsDocument.QueryMappings> mappings = xmlStreams.stream().map(is -> {
model.setConfigFilePath(path); try {
return QueryMappingsDocument.Factory.parse(is).getQueryMappings();
} catch (XmlException | IOException e) {
throw new ApplicationException(e);
}
}).collect(Collectors.toList());
QueryXmlModel model = new QueryXmlModel(mappings);
model.setVersion(maxLastModified(resources));
model.setConfigFilePaths(paths);
return model; return model;
} catch (Exception e) { } catch (Exception e) {
throw new ResourceLoadException(String.format("读取配置文件[%s]失败:%s", path, e.getMessage())); throw new ResourceLoadException(String.format("读取配置文件[%s]失败:%s", path, e.getMessage()));
} finally { } finally {
IOUtils.closeQuietly(inputStream); xmlStreams.forEach(IOUtils::closeQuietly);
IOUtils.closeQuietly(dialectXmlStream);
} }
} }
......
...@@ -77,10 +77,10 @@ public class TreeViewManager extends ResourceLoadManager<TreeViewMappingModel> i ...@@ -77,10 +77,10 @@ public class TreeViewManager extends ResourceLoadManager<TreeViewMappingModel> i
} }
}).collect(Collectors.toList()); }).collect(Collectors.toList());
TreeViewMappingModel mappingModel = new TreeViewMappingModel(mappings); TreeViewMappingModel model = new TreeViewMappingModel(mappings);
mappingModel.setVersions(maxLastModified(resources)); model.setVersions(maxLastModified(resources));
mappingModel.setConfigFilePaths(paths); model.setConfigFilePaths(paths);
return mappingModel; return model;
} catch (Exception e) { } catch (Exception e) {
throw new ResourceLoadException(String.format("读取配置文件[%s]失败:%s", defaultPath, e.getMessage())); throw new ResourceLoadException(String.format("读取配置文件[%s]失败:%s", defaultPath, e.getMessage()));
} finally { } finally {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment