表数据导出支持
This commit is contained in:
@@ -4,12 +4,15 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
import com.zyplayer.doc.core.annotation.AuthMan;
|
||||
import com.zyplayer.doc.core.exception.ConfirmException;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.core.util.StringUtil;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocAuthConst;
|
||||
import com.zyplayer.doc.db.controller.param.DataViewParam;
|
||||
import com.zyplayer.doc.db.controller.vo.TableColumnVo;
|
||||
import com.zyplayer.doc.db.framework.consts.DbAuthType;
|
||||
import com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteParam;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteResult;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteType;
|
||||
@@ -18,8 +21,10 @@ import com.zyplayer.doc.db.framework.json.DocDbResponseJson;
|
||||
import com.zyplayer.doc.db.framework.utils.JSONUtil;
|
||||
import com.zyplayer.doc.db.service.DbBaseFactory;
|
||||
import com.zyplayer.doc.db.service.DbBaseService;
|
||||
import com.zyplayer.doc.db.service.download.BaseDownloadService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -27,7 +32,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 表数据查看控制器
|
||||
@@ -45,30 +53,26 @@ public class DbDataViewController {
|
||||
SqlExecutor sqlExecutor;
|
||||
@Resource
|
||||
DbBaseFactory dbBaseFactory;
|
||||
@Resource
|
||||
BaseDownloadService baseDownloadService;
|
||||
|
||||
@PostMapping(value = "/query")
|
||||
public ResponseJson execute(DataViewParam dataViewParam) {
|
||||
boolean manageAuth = DocUserUtil.haveAuth(DocAuthConst.DB_DATASOURCE_MANAGE);
|
||||
boolean select = DocUserUtil.haveCustomAuth(DbAuthType.SELECT.getName(), DocAuthConst.DB + dataViewParam.getSourceId());
|
||||
boolean update = DocUserUtil.haveCustomAuth(DbAuthType.UPDATE.getName(), DocAuthConst.DB + dataViewParam.getSourceId());
|
||||
if (!manageAuth && !select && !update) {
|
||||
return DocDbResponseJson.warn("没有该数据源的执行权限");
|
||||
}
|
||||
public ResponseJson query(DataViewParam param) {
|
||||
// 数据查询
|
||||
ExecuteType executeType = (manageAuth || update) ? ExecuteType.ALL : ExecuteType.SELECT;
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(dataViewParam.getSourceId());
|
||||
String queryPageSql = dbBaseService.getQueryPageSql(dataViewParam);
|
||||
ExecuteResult executeResult = this.query(dataViewParam.getSourceId(), dataViewParam.getExecuteId(), executeType, queryPageSql);
|
||||
ExecuteType executeType = this.getExecuteType(param.getSourceId());
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
String queryPageSql = dbBaseService.getQueryPageSql(param);
|
||||
ExecuteResult executeResult = this.query(param.getSourceId(), param.getExecuteId(), executeType, queryPageSql);
|
||||
// 数据组装
|
||||
List<String> resultList = new LinkedList<>();
|
||||
resultList.add(JSON.toJSONString(executeResult, JSONUtil.serializeConfig, SerializerFeature.WriteMapNullValue));
|
||||
DocDbResponseJson responseJson = DocDbResponseJson.ok(resultList);
|
||||
// 计算总条数,第一页才查询总条数
|
||||
if (CollectionUtils.isNotEmpty(executeResult.getResult()) && Objects.equals(dataViewParam.getPageNum(), 1)) {
|
||||
if (CollectionUtils.isNotEmpty(executeResult.getResult()) && Objects.equals(param.getPageNum(), 1)) {
|
||||
responseJson.setTotal((long) executeResult.getResult().size());
|
||||
if (executeResult.getResult().size() >= dataViewParam.getPageSize()) {
|
||||
String queryCountSql = dbBaseService.getQueryCountSql(dataViewParam);
|
||||
ExecuteResult countResult = this.query(dataViewParam.getSourceId(), dataViewParam.getExecuteId(), executeType, queryCountSql);
|
||||
if (executeResult.getResult().size() >= param.getPageSize()) {
|
||||
String queryCountSql = dbBaseService.getQueryCountSql(param);
|
||||
ExecuteResult countResult = this.query(param.getSourceId(), param.getExecuteId(), executeType, queryCountSql);
|
||||
if (CollectionUtils.isNotEmpty(countResult.getResult()) && MapUtils.isNotEmpty(countResult.getResult().get(0))) {
|
||||
Map<String, Object> countMap = countResult.getResult().get(0);
|
||||
Optional<Object> countAny = countMap.values().stream().findAny();
|
||||
@@ -79,6 +83,80 @@ public class DbDataViewController {
|
||||
return responseJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单表数据下载
|
||||
*
|
||||
* @param response
|
||||
* @param param
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/download")
|
||||
public ResponseJson download(HttpServletResponse response, DataViewParam param) {
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
// 获取列信息等
|
||||
TableColumnVo tableColumnVo = dbBaseService.getTableColumnList(param.getSourceId(), param.getDbName(), param.getTableName());
|
||||
Set<String> conditionSet = StringUtils.isBlank(param.getConditionColumn()) ? Collections.emptySet() : Stream.of(param.getConditionColumn().split(",")).collect(Collectors.toSet());
|
||||
Set<String> retainColumnSet = StringUtils.isBlank(param.getRetainColumn()) ? Collections.emptySet() : Stream.of(param.getRetainColumn().split(",")).collect(Collectors.toSet());
|
||||
List<TableColumnDescDto> columnList = tableColumnVo.getColumnList().stream().filter(item -> retainColumnSet.isEmpty() || retainColumnSet.contains(item.getName())).collect(Collectors.toList());
|
||||
// 数据查询
|
||||
String queryAllSql = dbBaseService.getQueryAllSql(param);
|
||||
ExecuteParam executeParam = new ExecuteParam();
|
||||
executeParam.setDatasourceId(param.getSourceId());
|
||||
executeParam.setExecuteId(param.getExecuteId());
|
||||
executeParam.setExecuteType(this.getExecuteType(param.getSourceId()));
|
||||
executeParam.setSql(queryAllSql);
|
||||
// 最大支持10w行数据,总得有个上限
|
||||
executeParam.setMaxRows(100000);
|
||||
try {
|
||||
dbBaseService.downloadSingleTableData(response, param, executeParam, columnList, conditionSet);
|
||||
} catch (Exception e) {
|
||||
return DocDbResponseJson.error("导出失败:" + e.getMessage());
|
||||
}
|
||||
return DocDbResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多表下载
|
||||
*
|
||||
* @param response
|
||||
* @param param
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/downloadMultiple")
|
||||
public ResponseJson downloadMultiple(HttpServletResponse response, DataViewParam param) {
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
if (StringUtils.isBlank(param.getTableName())) {
|
||||
return DocDbResponseJson.warn("请选择导出的表");
|
||||
}
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
String[] tableNameArr = param.getTableName().split(",");
|
||||
try {
|
||||
for (String tabName : tableNameArr) {
|
||||
// 获取列信息等
|
||||
TableColumnVo tableColumnVo = dbBaseService.getTableColumnList(param.getSourceId(), param.getDbName(), tabName);
|
||||
Set<String> conditionSet = StringUtils.isBlank(param.getConditionColumn()) ? Collections.emptySet() : Stream.of(param.getConditionColumn().split(",")).collect(Collectors.toSet());
|
||||
List<TableColumnDescDto> columnList = tableColumnVo.getColumnList();
|
||||
// 数据查询
|
||||
String queryAllSql = dbBaseService.getQueryAllSql(param);
|
||||
ExecuteParam executeParam = new ExecuteParam();
|
||||
executeParam.setDatasourceId(param.getSourceId());
|
||||
executeParam.setExecuteId(param.getExecuteId());
|
||||
executeParam.setExecuteType(this.getExecuteType(param.getSourceId()));
|
||||
executeParam.setSql(queryAllSql);
|
||||
// 最大支持10w行数据,总得有个上限
|
||||
executeParam.setMaxRows(100000);
|
||||
String downloadTableData = dbBaseService.getDownloadTableData(param, executeParam, columnList, conditionSet);
|
||||
resultSb.append(String.format("-- 导出数据表:`%s`.`%s` --\n", param.getDbName(), tabName));
|
||||
resultSb.append(downloadTableData).append("\n");
|
||||
}
|
||||
String prefix = Objects.equals(param.getDownloadType(), "json") ? ".json" : ".sql";
|
||||
baseDownloadService.sendResponse(response, param.getTableName(), prefix, resultSb.toString());
|
||||
} catch (Exception e) {
|
||||
return DocDbResponseJson.error("导出失败:" + e.getMessage());
|
||||
}
|
||||
return DocDbResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行数据查询
|
||||
*
|
||||
@@ -102,5 +180,15 @@ public class DbDataViewController {
|
||||
return ExecuteResult.error(StringUtil.getException(e), executeSql);
|
||||
}
|
||||
}
|
||||
|
||||
private ExecuteType getExecuteType(Long sourceId) {
|
||||
boolean manageAuth = DocUserUtil.haveAuth(DocAuthConst.DB_DATASOURCE_MANAGE);
|
||||
boolean select = DocUserUtil.haveCustomAuth(DbAuthType.SELECT.getName(), DocAuthConst.DB + sourceId);
|
||||
boolean update = DocUserUtil.haveCustomAuth(DbAuthType.UPDATE.getName(), DocAuthConst.DB + sourceId);
|
||||
if (!manageAuth && !select && !update) {
|
||||
throw new ConfirmException("没有该数据源的执行权限");
|
||||
}
|
||||
return (manageAuth || update) ? ExecuteType.ALL : ExecuteType.SELECT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.zyplayer.doc.db.controller.param;
|
||||
|
||||
public class DataDownloadParam {
|
||||
private Long sourceId;
|
||||
private String dbName;
|
||||
private String tableName;
|
||||
private String downloadType;
|
||||
private String conditionColumn;
|
||||
|
||||
public Long getSourceId() {
|
||||
return sourceId;
|
||||
}
|
||||
|
||||
public void setSourceId(Long sourceId) {
|
||||
this.sourceId = sourceId;
|
||||
}
|
||||
|
||||
public String getDbName() {
|
||||
return dbName;
|
||||
}
|
||||
|
||||
public void setDbName(String dbName) {
|
||||
this.dbName = dbName;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getDownloadType() {
|
||||
return downloadType;
|
||||
}
|
||||
|
||||
public void setDownloadType(String downloadType) {
|
||||
this.downloadType = downloadType;
|
||||
}
|
||||
|
||||
public String getConditionColumn() {
|
||||
return conditionColumn;
|
||||
}
|
||||
|
||||
public void setConditionColumn(String conditionColumn) {
|
||||
this.conditionColumn = conditionColumn;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,11 @@ public class DataViewParam {
|
||||
private String orderColumn;
|
||||
private String orderType;
|
||||
private String condition;
|
||||
private String downloadType;
|
||||
private String retainColumn;
|
||||
private String conditionColumn;
|
||||
private Integer dropTableFlag;
|
||||
private Integer createTableFlag;
|
||||
|
||||
public Integer getOffset() {
|
||||
return ((this.pageNum - 1) * this.pageSize);
|
||||
@@ -86,4 +91,44 @@ public class DataViewParam {
|
||||
public void setCondition(String condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
public String getDownloadType() {
|
||||
return downloadType;
|
||||
}
|
||||
|
||||
public void setDownloadType(String downloadType) {
|
||||
this.downloadType = downloadType;
|
||||
}
|
||||
|
||||
public String getConditionColumn() {
|
||||
return conditionColumn;
|
||||
}
|
||||
|
||||
public void setConditionColumn(String conditionColumn) {
|
||||
this.conditionColumn = conditionColumn;
|
||||
}
|
||||
|
||||
public Integer getDropTableFlag() {
|
||||
return dropTableFlag;
|
||||
}
|
||||
|
||||
public void setDropTableFlag(Integer dropTableFlag) {
|
||||
this.dropTableFlag = dropTableFlag;
|
||||
}
|
||||
|
||||
public Integer getCreateTableFlag() {
|
||||
return createTableFlag;
|
||||
}
|
||||
|
||||
public void setCreateTableFlag(Integer createTableFlag) {
|
||||
this.createTableFlag = createTableFlag;
|
||||
}
|
||||
|
||||
public String getRetainColumn() {
|
||||
return retainColumn;
|
||||
}
|
||||
|
||||
public void setRetainColumn(String retainColumn) {
|
||||
this.retainColumn = retainColumn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,16 @@ public class SqlExecutor {
|
||||
return this.execute(factoryBean, param, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行sql,返回结果
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年8月18日
|
||||
*/
|
||||
public ExecuteResult execute(ExecuteParam param, ResultHandler handler) {
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getOrCreateFactoryById(param.getDatasourceId());
|
||||
return this.execute(factoryBean, param, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行sql,可通过handler回调每一行的结果
|
||||
* @author 暮光:城中城
|
||||
|
||||
@@ -56,7 +56,7 @@ public class PoiUtil {
|
||||
.append("-- 表结构:" + entry.getKey() + "\n")
|
||||
.append("-- ----------------------------\n")
|
||||
.append("DROP TABLE IF EXISTS `" + entry.getKey() + "`;\n")
|
||||
.append(entry.getValue()).append(";\n\n");
|
||||
.append(entry.getValue()).append("\n\n");
|
||||
}
|
||||
IoUtil.write(response.getOutputStream(), "utf-8", true, ddlSqlSb.toString());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.zyplayer.doc.db.service;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zyplayer.doc.core.exception.ConfirmException;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocAuthConst;
|
||||
@@ -14,12 +17,16 @@ import com.zyplayer.doc.db.framework.db.bean.DatabaseRegistrationBean;
|
||||
import com.zyplayer.doc.db.framework.db.dto.*;
|
||||
import com.zyplayer.doc.db.framework.db.enums.DatabaseProductEnum;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteParam;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteResult;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.SqlExecutor;
|
||||
import com.zyplayer.doc.db.service.download.BaseDownloadService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -34,6 +41,8 @@ public abstract class DbBaseService {
|
||||
@Resource
|
||||
SqlExecutor sqlExecutor;
|
||||
@Resource
|
||||
BaseDownloadService baseDownloadService;
|
||||
@Resource
|
||||
DatabaseRegistrationBean databaseRegistrationBean;
|
||||
|
||||
/**
|
||||
@@ -337,6 +346,23 @@ public abstract class DbBaseService {
|
||||
throw new ConfirmException("暂未支持的数据库类型");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全量数据查询的SQL
|
||||
*
|
||||
* @return 分页查询的SQL
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年4月24日
|
||||
*/
|
||||
public String getQueryAllSql(DataViewParam dataViewParam) {
|
||||
String queryColumns = StringUtils.defaultIfBlank(dataViewParam.getRetainColumn(), "*");
|
||||
StringBuilder sqlSb = new StringBuilder();
|
||||
sqlSb.append(String.format("select %s from %s.%s", queryColumns, dataViewParam.getDbName(), dataViewParam.getTableName()));
|
||||
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
|
||||
sqlSb.append(String.format(" where %s", dataViewParam.getCondition()));
|
||||
}
|
||||
return sqlSb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分页查询的SQL
|
||||
*
|
||||
@@ -372,4 +398,40 @@ public abstract class DbBaseService {
|
||||
}
|
||||
return sqlSb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出单表数据
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年6月5日
|
||||
*/
|
||||
public void downloadSingleTableData(HttpServletResponse response, DataViewParam param, ExecuteParam executeParam, List<TableColumnDescDto> dataCols, Set<String> conditionSet) throws Exception {
|
||||
if (Objects.equals(param.getDownloadType(), "insert")) {
|
||||
String resultStr = baseDownloadService.downloadDataByInsert(param, executeParam, dataCols, conditionSet);
|
||||
baseDownloadService.sendResponse(response, param.getTableName(), ".sql", resultStr);
|
||||
} else if (Objects.equals(param.getDownloadType(), "update")) {
|
||||
String resultStr = baseDownloadService.downloadDataByUpdate(param, executeParam, dataCols, conditionSet);
|
||||
baseDownloadService.sendResponse(response, param.getTableName(), ".sql", resultStr);
|
||||
} else if (Objects.equals(param.getDownloadType(), "json")) {
|
||||
String resultStr = baseDownloadService.downloadDataByJson(param, executeParam, dataCols, conditionSet);
|
||||
baseDownloadService.sendResponse(response, param.getTableName(), ".json", resultStr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表数据
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年6月5日
|
||||
*/
|
||||
public String getDownloadTableData(DataViewParam param, ExecuteParam executeParam, List<TableColumnDescDto> dataCols, Set<String> conditionSet) throws Exception {
|
||||
if (Objects.equals(param.getDownloadType(), "insert")) {
|
||||
return baseDownloadService.downloadDataByInsert(param, executeParam, dataCols, conditionSet);
|
||||
} else if (Objects.equals(param.getDownloadType(), "update")) {
|
||||
return baseDownloadService.downloadDataByUpdate(param, executeParam, dataCols, conditionSet);
|
||||
} else if (Objects.equals(param.getDownloadType(), "json")) {
|
||||
return baseDownloadService.downloadDataByJson(param, executeParam, dataCols, conditionSet);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class MysqlServiceImpl extends DbBaseService {
|
||||
tableDdlVo.setOracle("// TODO 等待大佬来实现转换");
|
||||
// TODO 将建表语句转换为其他数据库的,还不知道怎么做,先这样留着,看有没大佬来实现
|
||||
if (CollectionUtils.isNotEmpty(tableDdlList)) {
|
||||
tableDdlVo.setMysql(tableDdlList.get(0).get("Create Table"));
|
||||
tableDdlVo.setMysql(tableDdlList.get(0).get("Create Table") + ";");
|
||||
}
|
||||
return tableDdlVo;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package com.zyplayer.doc.db.service.download;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zyplayer.doc.db.controller.param.DataViewParam;
|
||||
import com.zyplayer.doc.db.controller.vo.TableDdlVo;
|
||||
import com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteParam;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.SqlExecutor;
|
||||
import com.zyplayer.doc.db.service.DbBaseFactory;
|
||||
import com.zyplayer.doc.db.service.DbBaseService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 基础的数据导出服务类,按照MySQL规范写的,不满足的可新增类来实现
|
||||
*/
|
||||
@Service
|
||||
public class BaseDownloadService {
|
||||
|
||||
@Resource
|
||||
SqlExecutor sqlExecutor;
|
||||
@Resource
|
||||
DbBaseFactory dbBaseFactory;
|
||||
|
||||
/**
|
||||
* 导出数据为insert语句格式
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年6月5日
|
||||
*/
|
||||
public String downloadDataByInsert(DataViewParam param, ExecuteParam executeParam, List<TableColumnDescDto> dataCols, Set<String> conditionSet) throws Exception {
|
||||
String dbTableName = String.format("`%s`.`%s`", param.getDbName(), param.getTableName());
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
if (Objects.equals(param.getCreateTableFlag(), 1)) {
|
||||
resultSb.append("-- 导出表 ").append(dbTableName).append(" 结构\n");
|
||||
if (Objects.equals(param.getDropTableFlag(), 1)) {
|
||||
resultSb.append("DROP TABLE IF EXISTS ").append(dbTableName).append(";\n");
|
||||
}
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
TableDdlVo tableDdlVo = dbBaseService.getTableDdl(param.getSourceId(), param.getDbName(), param.getTableName());
|
||||
resultSb.append(tableDdlVo.getTableDDLByType()).append("\n\n");
|
||||
}
|
||||
resultSb.append("-- 导出表 ").append(dbTableName).append(" 数据\n");
|
||||
Pattern pattern = Pattern.compile("\t|\r\n|\r|\n|\\s+");
|
||||
String executeSql = pattern.matcher(executeParam.getSql()).replaceAll(" ");
|
||||
resultSb.append("-- 导出查询SQL:").append(executeSql).append(";\n");
|
||||
// 执行数据查询操作
|
||||
sqlExecutor.execute(executeParam, item -> {
|
||||
StringBuilder names = new StringBuilder();
|
||||
for (TableColumnDescDto dataCol : dataCols) {
|
||||
if (names.length() > 0) names.append(", ");
|
||||
names.append(dataCol.getName());
|
||||
}
|
||||
StringBuilder values = new StringBuilder();
|
||||
for (TableColumnDescDto dataCol : dataCols) {
|
||||
if (values.length() > 0) values.append(", ");
|
||||
Object val = item.get(dataCol.getName());
|
||||
if (this.isNumber(dataCol.getType())) {
|
||||
values.append(val);
|
||||
} else {
|
||||
val = (val == null) ? "" : val;
|
||||
val = val.toString().replaceAll("'", "''");
|
||||
values.append("'").append(val).append("'");
|
||||
}
|
||||
}
|
||||
String resultData = "insert into " + dbTableName + " (" + names + ") values (" + values + ");\n";
|
||||
resultSb.append(resultData);
|
||||
});
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据为update语句格式
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年6月5日
|
||||
*/
|
||||
public String downloadDataByUpdate(DataViewParam param, ExecuteParam executeParam, List<TableColumnDescDto> dataCols, Set<String> conditionSet) throws Exception {
|
||||
String dbTableName = String.format("`%s`.`%s`", param.getDbName(), param.getTableName());
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
Pattern pattern = Pattern.compile("\t|\r\n|\r|\n|\\s+");
|
||||
String executeSql = pattern.matcher(executeParam.getSql()).replaceAll(" ");
|
||||
resultSb.append("-- 导出查询SQL:").append(executeSql).append(";\n");
|
||||
// 执行数据查询操作
|
||||
sqlExecutor.execute(executeParam, item -> {
|
||||
StringBuilder values = new StringBuilder();
|
||||
StringBuilder where = new StringBuilder();
|
||||
for (TableColumnDescDto dataCol : dataCols) {
|
||||
if (values.length() > 0) values.append(", ");
|
||||
values.append(dataCol.getName()).append("=");
|
||||
Object val = item.get(dataCol.getName());
|
||||
if (this.isNumber(dataCol.getType())) {
|
||||
values.append(val);
|
||||
if (conditionSet.contains(dataCol.getName())) {
|
||||
if (where.length() > 0) where.append(" and ");
|
||||
where.append(dataCol.getName()).append(" = ").append(val);
|
||||
}
|
||||
} else {
|
||||
val = (val == null) ? "" : val;
|
||||
val = val.toString().replaceAll("'", "''");
|
||||
values.append("'").append(val).append("'");
|
||||
if (conditionSet.contains(dataCol.getName())) {
|
||||
if (where.length() > 0) where.append(" and ");
|
||||
where.append(dataCol.getName()).append(" = ").append("'").append(val).append("'");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (where.length() > 0) where.insert(0, " where ");
|
||||
String resultData = "update " + dbTableName + " set " + values + where + ";\n";
|
||||
resultSb.append(resultData);
|
||||
});
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据为json格式
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年6月5日
|
||||
*/
|
||||
public String downloadDataByJson(DataViewParam param, ExecuteParam executeParam, List<TableColumnDescDto> dataCols, Set<String> conditionSet) throws Exception {
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
resultSb.append("[");
|
||||
sqlExecutor.execute(executeParam, item -> {
|
||||
JSONObject result = new JSONObject();
|
||||
for (TableColumnDescDto dataCol : dataCols) {
|
||||
result.put(dataCol.getName(), item.get(dataCol.getName()));
|
||||
}
|
||||
String resultData = result.toJSONString();
|
||||
if (resultSb.length() > 1) {
|
||||
resultSb.append(",");
|
||||
}
|
||||
resultSb.append(resultData);
|
||||
});
|
||||
resultSb.append("]");
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
public void sendResponse(HttpServletResponse response, String tableName, String prefix, String dataStr) throws Exception {
|
||||
String fileNameOrigin = tableName + "." + DateTime.now().toString("yyyyMMddHHmmss");
|
||||
String fileName = URLEncoder.encode(fileNameOrigin, "UTF-8") + prefix;
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
IoUtil.write(response.getOutputStream(), "utf-8", true, dataStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是数值类型
|
||||
*
|
||||
* @param type 类型
|
||||
* @return 结果
|
||||
*/
|
||||
public boolean isNumber(String type) {
|
||||
return type.contains("int")
|
||||
|| type.contains("bit")
|
||||
|| type.contains("float")
|
||||
|| type.contains("double")
|
||||
|| type.contains("decimal")
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.168eac24.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.168eac24.js></script></body></html>
|
||||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.fca745a8.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.fca745a8.js></script></body></html>
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user