|
|
@@ -1,16 +1,18 @@
|
|
|
package com.template.services.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
import com.template.common.utils.CommonUtil;
|
|
|
+import com.template.common.utils.DBUtil;
|
|
|
import com.template.common.utils.QuartzJobUtils;
|
|
|
import com.template.mapper.SmartDataSourceMapper;
|
|
|
import com.template.mapper.SmartDataTaskMapper;
|
|
|
-import com.template.model.pojo.SmartDataSource;
|
|
|
-import com.template.model.pojo.SmartDataSourceLog;
|
|
|
-import com.template.model.pojo.SmartDataTask;
|
|
|
+import com.template.model.pojo.*;
|
|
|
import com.template.model.result.CommonResult;
|
|
|
import com.template.model.result.PageUtils;
|
|
|
import com.template.services.SmartDataTaskService;
|
|
|
@@ -24,6 +26,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.sql.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.format.DateTimeParseException;
|
|
|
@@ -54,16 +57,26 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
@Override
|
|
|
public Map<String, Object> insertSmartDataTask(SmartDataTask smartDataTask) {
|
|
|
// 检测参数,还有是否存在重复记录
|
|
|
+ // 任务属性
|
|
|
if (smartDataTask.getTkName() == null) {
|
|
|
return CommonUtil.getReturnMap("1", "【任务名称】不能为空!");
|
|
|
}
|
|
|
+ if (!CommonUtil.checkStrByRegx("^[a-zA-Z0-9]{5,32}$", smartDataTask.getTkName())) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【任务名称】只能包含字母、数字,且长度为5-32位!");
|
|
|
+ }
|
|
|
QueryWrapper<SmartDataTask> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq(smartDataTask.getTkName() != null, "tk_name", smartDataTask.getTkName());
|
|
|
SmartDataTask sdt = smartDataTaskMapper.selectOne(queryWrapper);
|
|
|
if (sdt != null) {
|
|
|
return CommonUtil.getReturnMap("1", "任务名有重名!");
|
|
|
}
|
|
|
- // 检测必要参数是否为null
|
|
|
+ if (smartDataTask.getTkDtId() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【部门id】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataTask.getTkSyncPolicy() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【同步策略】不能为空!");
|
|
|
+ }
|
|
|
+ // 来源库设置
|
|
|
if (smartDataTask.getTkDsIdSource() == null) {
|
|
|
return CommonUtil.getReturnMap("1", "【来源数据源id】不能为空!");
|
|
|
}
|
|
|
@@ -71,15 +84,13 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
if (smartDataSource == null) {
|
|
|
return CommonUtil.getReturnMap("1", "选择的【来源数据源】不存在!");
|
|
|
}
|
|
|
- if (smartDataTask.getTkSyncPolicy() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【同步策略】不能为空!");
|
|
|
- }
|
|
|
if (smartDataTask.getTkExchangeType() == null) {
|
|
|
return CommonUtil.getReturnMap("1", "【交换方式】不能为空!");
|
|
|
}
|
|
|
if (smartDataTask.getTkSql() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【数据来源SQL语句】不能为空!");
|
|
|
+ return CommonUtil.getReturnMap("1", "【自定义SQL语句】不能为空!");
|
|
|
}
|
|
|
+ // 目的库设置
|
|
|
if (smartDataTask.getTkDsIdDestination() == null) {
|
|
|
return CommonUtil.getReturnMap("1", "【目标数据源id】不能为空!");
|
|
|
}
|
|
|
@@ -90,42 +101,54 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
if (smartDataTask.getTkDestTable() == null) {
|
|
|
return CommonUtil.getReturnMap("1", "【目标数据表】不能为空!");
|
|
|
}
|
|
|
- if (smartDataTask.getTkManualOrAuto() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【手动或定时执行】不能为空!");
|
|
|
+ // 高级设置
|
|
|
+ if (smartDataTask.getTkExchangeServer() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【交换服务器】不能为空!");
|
|
|
}
|
|
|
- if (smartDataTask.getTkManualOrAuto() == 0) {
|
|
|
- if (smartDataTask.getTkExeType() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【执行方式】不能为空!");
|
|
|
+ if (smartDataTask.getTkExchangeServer() == 1) {
|
|
|
+ if (smartDataTask.getTkExchangeServerId() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【指定服务器id】不能为空!");
|
|
|
}
|
|
|
- if (smartDataTask.getTkRepetTime() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【重复时间】不能为空!");
|
|
|
- }
|
|
|
- // 生成cron表达式
|
|
|
- Map<String, Object> stringObjectMap = generateCron(smartDataTask.getTkExeType(), smartDataTask.getTkRepetTime());
|
|
|
- if (stringObjectMap.get("code") == "1") {
|
|
|
- return stringObjectMap;
|
|
|
+ }
|
|
|
+ if (smartDataTask.getTkOptCfgAutoManual()== null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【运行参数配置】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataTask.getTkOptCfgAutoManual() == 1){
|
|
|
+ if (smartDataTask.getTkOptCfgRsNum() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【运行参数配置:记录数】不能为空!");
|
|
|
}
|
|
|
- String cron = (String) stringObjectMap.get("msg");
|
|
|
- if (CronExpression.isValidExpression(cron)) {
|
|
|
- smartDataTask.setTkCron(cron);
|
|
|
- } else {
|
|
|
- return CommonUtil.getReturnMap("1", "生成的【定时表达式】不正确!请联系管理员!" + cron);
|
|
|
+ if (smartDataTask.getTkOptCfgThreadsNum() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【运行参数配置:线程数】不能为空!");
|
|
|
}
|
|
|
- } else {
|
|
|
- smartDataTask.setTkExeType(-1);
|
|
|
- smartDataTask.setTkRepetTime("");
|
|
|
}
|
|
|
- if (smartDataTask.getTkDescrition() == null) {
|
|
|
- return CommonUtil.getReturnMap("1", "【任务描述】不能为空!");
|
|
|
+ if (smartDataTask.getTkRsIncorrectData() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【是否记录错误数据】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataTask.getTkDsSourceCharset() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【来源数据源字符集】不能为空!");
|
|
|
}
|
|
|
+ if (smartDataTask.getTkDsDestinationCharset() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【目标数据源字符集】不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ queryWrapper.eq(smartDataTask.getTkDtId() != null, "tk_dt_id", smartDataTask.getTkDtId());
|
|
|
queryWrapper.eq(smartDataTask.getTkDsIdSource() != null, "tk_ds_id_source", smartDataTask.getTkDsIdSource());
|
|
|
queryWrapper.eq(smartDataTask.getTkSyncPolicy() != null, "tk_sync_policy", smartDataTask.getTkSyncPolicy());
|
|
|
queryWrapper.eq(smartDataTask.getTkExchangeType() != null, "tk_exchange_type", smartDataTask.getTkExchangeType());
|
|
|
queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkSql()), "tk_sql", smartDataTask.getTkSql());
|
|
|
queryWrapper.eq(smartDataTask.getTkDsIdDestination() != null, "tk_ds_id_destination", smartDataTask.getTkDsIdDestination());
|
|
|
queryWrapper.eq(smartDataTask.getTkDestTable() != null, "tk_dest_table", smartDataTask.getTkDestTable());
|
|
|
- queryWrapper.eq(smartDataTask.getTkManualOrAuto() != null, "tk_manual_or_auto", smartDataTask.getTkManualOrAuto());
|
|
|
- queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkDescrition()), "tk_descrition", smartDataTask.getTkDescrition());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkExchangeServer() != null, "tk_exchange_server", smartDataTask.getTkExchangeServer());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkExchangeServer() != null && smartDataTask.getTkExchangeServerId() != null,
|
|
|
+ "tk_exchange_server_id", smartDataTask.getTkExchangeServerId());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null, "tk_opt_cfg_auto_manual", smartDataTask.getTkOptCfgAutoManual());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null && smartDataTask.getTkOptCfgRsNum() != null,
|
|
|
+ "tk_opt_cfg_rs_num", smartDataTask.getTkOptCfgRsNum());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null && smartDataTask.getTkOptCfgThreadsNum() != null,
|
|
|
+ "tk_opt_cfg_threads_num", smartDataTask.getTkOptCfgThreadsNum());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkRsIncorrectData() != null, "tk_rs_incorrect_data", smartDataTask.getTkRsIncorrectData());
|
|
|
+ queryWrapper.eq(smartDataTask.getTkDsSourceCharset() != null, "tk_ds_source_charset", smartDataTask.getTkDsSourceCharset());
|
|
|
+ queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkDsDestinationCharset()), "tk_ds_destination_charset", smartDataTask.getTkDsDestinationCharset());
|
|
|
sdt = smartDataTaskMapper.selectOne(queryWrapper);
|
|
|
if (sdt != null) {
|
|
|
return CommonUtil.getReturnMap("1", "有重复记录!");
|
|
|
@@ -137,6 +160,35 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
} else {
|
|
|
return CommonUtil.getReturnMap("1", "任务添加失败!");
|
|
|
}
|
|
|
+
|
|
|
+// if (smartDataTask.getTkManualOrAuto() == null) {
|
|
|
+// return CommonUtil.getReturnMap("1", "【手动或定时执行】不能为空!");
|
|
|
+// }
|
|
|
+// if (smartDataTask.getTkManualOrAuto() == 0) {
|
|
|
+// if (smartDataTask.getTkExeType() == null) {
|
|
|
+// return CommonUtil.getReturnMap("1", "【执行方式】不能为空!");
|
|
|
+// }
|
|
|
+// if (smartDataTask.getTkRepetTime() == null) {
|
|
|
+// return CommonUtil.getReturnMap("1", "【重复时间】不能为空!");
|
|
|
+// }
|
|
|
+// // 生成cron表达式
|
|
|
+// Map<String, Object> stringObjectMap = generateCron(smartDataTask.getTkExeType(), smartDataTask.getTkRepetTime());
|
|
|
+// if (stringObjectMap.get("code") == "1") {
|
|
|
+// return stringObjectMap;
|
|
|
+// }
|
|
|
+// String cron = (String) stringObjectMap.get("msg");
|
|
|
+// if (CronExpression.isValidExpression(cron)) {
|
|
|
+// smartDataTask.setTkCron(cron);
|
|
|
+// } else {
|
|
|
+// return CommonUtil.getReturnMap("1", "生成的【定时表达式】不正确!请联系管理员!" + cron);
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// smartDataTask.setTkExeType(-1);
|
|
|
+// smartDataTask.setTkRepetTime("");
|
|
|
+// }
|
|
|
+// if (smartDataTask.getTkDescrition() == null) {
|
|
|
+// return CommonUtil.getReturnMap("1", "【任务描述】不能为空!");
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
// 生成cron表达式
|
|
|
@@ -214,7 +266,7 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
int minute = Integer.parseInt(hour_minute[1]);
|
|
|
|
|
|
return CommonUtil.getReturnMap("0", String.format("0 %d %d ? * %s", minute, hour, weekday));
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
return CommonUtil.getReturnMap("1", "【重复时间】格式错误3,要求:周日 09:40");
|
|
|
}
|
|
|
} else if (exeType == 4) {
|
|
|
@@ -234,7 +286,7 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
int minute = Integer.parseInt(hour_minute[1]);
|
|
|
|
|
|
return CommonUtil.getReturnMap("0", String.format("0 %d %d %d * ?", minute, hour, day));
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
return CommonUtil.getReturnMap("1", "【重复时间】格式错误3,要求:19 09:40");
|
|
|
}
|
|
|
} else {
|
|
|
@@ -414,7 +466,17 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
}
|
|
|
|
|
|
SmartDataTask smartDataTask_return = (SmartDataTask) tmp_map.get("msg");
|
|
|
- Map<String, Object> returnMap = QuartzJobUtils.createScheduleJob(scheduler, smartDataTask_return);
|
|
|
+ // 来源数据源id
|
|
|
+ Integer tkDsIdSource = smartDataTask_return.getTkDsIdSource();
|
|
|
+ // 目标数据源id
|
|
|
+ Integer tkDsIdDestination = smartDataTask_return.getTkDsIdDestination();
|
|
|
+ // 根据id,获取数据源url、user、password、driver等
|
|
|
+ SmartDataSourceJobParams dsSourceInfo = smartDataSourceMapper.getDataSourceInfo(tkDsIdSource);
|
|
|
+ dsSourceInfo.setSql(smartDataTask_return.getTkSql());
|
|
|
+ SmartDataSourceJobParams dsDestinationInfo = smartDataSourceMapper.getDataSourceInfo(tkDsIdDestination);
|
|
|
+ dsDestinationInfo.setSql(smartDataTask_return.getTkSql());
|
|
|
+
|
|
|
+ Map<String, Object> returnMap = QuartzJobUtils.createScheduleJob(scheduler, smartDataTask_return, dsSourceInfo, dsDestinationInfo);
|
|
|
if ("0".equals(returnMap.get("code"))) {
|
|
|
smartDataTask.setTkId(smartDataTask_return.getTkId());
|
|
|
// 下次执行的时间
|
|
|
@@ -510,4 +572,192 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
|
|
|
|
|
|
return QuartzJobUtils.runOnce(scheduler, smartDataTask_return.getTkName());
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getDepart() {
|
|
|
+ // 获取部门
|
|
|
+ List<SmartDepartment> depart = smartDataSourceMapper.getDepart();
|
|
|
+ if (depart != null) {
|
|
|
+ return CommonUtil.getReturnMap("0", depart);
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap("1", "部门为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getSyncPolicy() {
|
|
|
+ // 获取同步策略: 0插入更新,1更新标记,2清空插入
|
|
|
+ Map<String, Object> syncPolicy1 = new HashMap<>();
|
|
|
+ Map<String, Object> syncPolicy2 = new HashMap<>();
|
|
|
+ Map<String, Object> syncPolicy3 = new HashMap<>();
|
|
|
+ syncPolicy1.put("name", "插入更新");
|
|
|
+ syncPolicy1.put("value", 0);
|
|
|
+
|
|
|
+ syncPolicy2.put("name", "更新标记");
|
|
|
+ syncPolicy2.put("value", 1);
|
|
|
+
|
|
|
+ syncPolicy3.put("name", "清空插入");
|
|
|
+ syncPolicy3.put("value", 2);
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ list.add(syncPolicy1);
|
|
|
+ list.add(syncPolicy2);
|
|
|
+ list.add(syncPolicy3);
|
|
|
+
|
|
|
+ return CommonUtil.getReturnMap("0", list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getExchangeType() {
|
|
|
+ // 获取同步策略: 0插入更新,1更新标记,2清空插入
|
|
|
+ Map<String, Object> syncPolicy1 = new HashMap<>();
|
|
|
+ Map<String, Object> syncPolicy2 = new HashMap<>();
|
|
|
+ Map<String, Object> syncPolicy3 = new HashMap<>();
|
|
|
+ syncPolicy1.put("name", "自定义SQL语句");
|
|
|
+ syncPolicy1.put("value", 0);
|
|
|
+
|
|
|
+ syncPolicy2.put("name", "数据视图");
|
|
|
+ syncPolicy2.put("value", 1);
|
|
|
+
|
|
|
+ syncPolicy3.put("name", "数据表");
|
|
|
+ syncPolicy3.put("value", 2);
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ list.add(syncPolicy1);
|
|
|
+ list.add(syncPolicy2);
|
|
|
+ list.add(syncPolicy3);
|
|
|
+
|
|
|
+ return CommonUtil.getReturnMap("0", list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> testSql(String json) {
|
|
|
+ if (json == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "json参数为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ int dsIdSource, exchangeType;
|
|
|
+ String sql;
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ dsIdSource = jsonObject.getInteger("dsIdSource");
|
|
|
+ exchangeType = jsonObject.getInteger("exchangeType");
|
|
|
+ sql = jsonObject.getString("sql");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return CommonUtil.getReturnMap("1", "请检查参数:【数据源id】、【交换方式】、【自定义sql语句】是否为空");
|
|
|
+ }
|
|
|
+ // 获取数据源id对应的数据源
|
|
|
+ SmartDataSourceJobParams dataSourceInfo = smartDataSourceMapper.getDataSourceInfo(dsIdSource);
|
|
|
+ // 只有sql语句的交换方式才需要检查
|
|
|
+ if (exchangeType == 0) {
|
|
|
+ // sql语句
|
|
|
+ if (dataSourceInfo != null) {
|
|
|
+ DBUtil dbUtil = new DBUtil(dataSourceInfo.getDsUrl(), dataSourceInfo.getDsUser(), dataSourceInfo.getDsPassword(), dataSourceInfo.getDsClsDriver());
|
|
|
+ Map<String, Object> map_return = dbUtil.getConnection();
|
|
|
+ if (map_return.get("code") == "0") {
|
|
|
+ Map<String, Object> metaDataBySql = dbUtil.getMetaDataBySql((Connection) map_return.get("msg"), sql);
|
|
|
+ if (metaDataBySql.get("code") == "0") {
|
|
|
+ return CommonUtil.getReturnMap("0", "SQL语句正确");
|
|
|
+ // 解析字段名称、字段类型、字段大小等
|
|
|
+// return CommonUtil.getReturnMap("0", metaDataBySql.get("msg"));
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap("1", metaDataBySql.get("msg"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap("1", map_return.get("msg"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap("1", "来源数据源不存在");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap("1", "只有【自定义SQL语句】的交换方式才需要测试sql");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ // 获取表
|
|
|
+ public Map<String, Object> getTables(String json) {
|
|
|
+ // 查询数据库中的表
|
|
|
+ String sql = "SELECT TABLE_NAME tname,TABLE_COMMENT tcomment FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{db}';";
|
|
|
+ // 调用getTablesOrViews方法,传入json和sql,获取表
|
|
|
+ return getTablesOrViews(json, sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ //获取视图
|
|
|
+ public Map<String, Object> getViews(String json) {
|
|
|
+ // 查询数据库中指定数据库的视图
|
|
|
+ String sql = "SELECT TABLE_NAME tname,TABLE_COMMENT tcomment FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{db}' AND TABLE_TYPE = 'VIEW';";
|
|
|
+ // 返回视图
|
|
|
+ return getTablesOrViews(json, sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取表或视图信息
|
|
|
+ private Map<String, Object> getTablesOrViews(String json, String sql) {
|
|
|
+ // 判断json是否为空
|
|
|
+ if (json == null) {
|
|
|
+ // 如果为空,返回提示信息
|
|
|
+ return CommonUtil.getReturnMap("1", "json参数为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ int dsIdSource;
|
|
|
+ try {
|
|
|
+ // 将json字符串转换为JSONObject对象
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ // 从JSONObject对象中获取dsIdSource的值
|
|
|
+ dsIdSource = jsonObject.getInteger("dsIdSource");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果获取失败,返回提示信息
|
|
|
+ return CommonUtil.getReturnMap("1", "请检查参数:【数据源id】是否为空");
|
|
|
+ }
|
|
|
+ // 获取数据源信息
|
|
|
+ SmartDataSourceJobParams dataSourceInfo = smartDataSourceMapper.getDataSourceInfo(dsIdSource);
|
|
|
+ if (dataSourceInfo != null) {
|
|
|
+ // 创建DBUtil对象
|
|
|
+ DBUtil dbUtil = new DBUtil(dataSourceInfo.getDsUrl(), dataSourceInfo.getDsUser(), dataSourceInfo.getDsPassword(), dataSourceInfo.getDsClsDriver());
|
|
|
+ // 获取连接
|
|
|
+ Map<String, Object> map_return = dbUtil.getConnection();
|
|
|
+ if (map_return.get("code") == "0") {
|
|
|
+ // 获取连接
|
|
|
+ Connection conn = (Connection) map_return.get("msg");
|
|
|
+ // 获取数据库名
|
|
|
+ String db = dataSourceInfo.getDsUrl().substring(dataSourceInfo.getDsUrl().lastIndexOf("/") + 1);
|
|
|
+ // 查询表或视图信息
|
|
|
+ sql = sql.replace("{db}", db);
|
|
|
+ Map<String, Object> tableMetaData = dbUtil.query(conn, sql);
|
|
|
+ if (tableMetaData.get("code") == "0") {
|
|
|
+ Map<String, Object> map = (Map<String, Object>) tableMetaData.get("msg");
|
|
|
+ ResultSet rs = (ResultSet) map.get("rs");
|
|
|
+ PreparedStatement stmt = (PreparedStatement) map.get("stmt");
|
|
|
+ try {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ while (rs.next()) {
|
|
|
+ // 将表名和表注释添加到list中
|
|
|
+ list.add(rs.getString("tname") + " [" + rs.getString("tcomment") + "]");
|
|
|
+ }
|
|
|
+ // 关闭结果集
|
|
|
+ dbUtil.closeResultSet(rs);
|
|
|
+ // 关闭预处理语句
|
|
|
+ dbUtil.closeStatement(stmt);
|
|
|
+ // 关闭连接
|
|
|
+ dbUtil.closeConnection(conn);
|
|
|
+ // 返回表信息
|
|
|
+ return CommonUtil.getReturnMap("0", list);
|
|
|
+ } catch (SQLException e) {
|
|
|
+ // 如果出现异常,返回异常信息
|
|
|
+ return CommonUtil.getReturnMap("1", e.getMessage());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果查询失败,返回查询失败信息
|
|
|
+ return CommonUtil.getReturnMap("1", tableMetaData.get("msg"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果获取连接失败,返回获取连接失败信息
|
|
|
+ return CommonUtil.getReturnMap("1", map_return.get("msg"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果数据源不存在,返回提示信息
|
|
|
+ return CommonUtil.getReturnMap("1", "数据源不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|