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

自定义查询SQL方言

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