fix(数据源): 校验状态没有记录改变

This commit is contained in:
taojinlong 2024-01-25 12:53:40 +08:00
parent 3401731a91
commit 3fca6dce19
2 changed files with 95 additions and 93 deletions

View File

@ -144,7 +144,7 @@ public class CalciteProvider {
String querySql = getTablesSql(datasourceRequest).get(0);
try (Connection con = getConnection(datasourceRequest.getDatasource()); Statement statement = getStatement(con, 30); ResultSet resultSet = statement.executeQuery(querySql)) {
} catch (Exception e) {
DEException.throwException(e.getMessage());
throw e;
}
return "Success";
}

View File

@ -66,7 +66,6 @@ import static io.dataease.datasource.server.DatasourceTaskServer.ScheduleType.RI
@RestController
@RequestMapping("/datasource")
@Transactional
public class DatasourceServer implements DatasourceApi {
@Resource
private CoreDatasourceMapper datasourceMapper;
@ -120,48 +119,6 @@ public class DatasourceServer implements DatasourceApi {
}
}
public void move(DatasourceDTO dataSourceDTO) throws DEException {
switch (dataSourceDTO.getAction()) {
case "move" -> {
if (dataSourceDTO.getPid() == null) {
DEException.throwException("目录必选!");
}
if (Objects.equals(dataSourceDTO.getId(), dataSourceDTO.getPid())) {
DEException.throwException(Translator.get("i18n_pid_not_eq_id"));
}
if (dataSourceDTO.getPid() != 0) {
List<Long> ids = new ArrayList<>();
getParents(dataSourceDTO.getPid(), ids);
if (ids.contains(dataSourceDTO.getId())) {
DEException.throwException(Translator.get("i18n_pid_not_eq_id"));
}
}
dataSourceManage.move(dataSourceDTO);
}
case "rename" -> {
if (StringUtils.isEmpty(dataSourceDTO.getName())) {
DEException.throwException("名称不能为空!");
}
CoreDatasource datasource = datasourceMapper.selectById(dataSourceDTO.getId());
datasource.setName(dataSourceDTO.getName());
dataSourceManage.innerEdit(datasource);
}
case "create" -> {
CoreDatasource coreDatasource = new CoreDatasource();
BeanUtils.copyBean(coreDatasource, dataSourceDTO);
coreDatasource.setCreateTime(System.currentTimeMillis());
coreDatasource.setUpdateTime(System.currentTimeMillis());
coreDatasource.setTaskStatus(TaskStatus.WaitingForExecution.name());
coreDatasource.setType(dataSourceDTO.getNodeType());
coreDatasource.setId(IDUtils.snowID());
coreDatasource.setConfiguration("");
dataSourceManage.innerSave(coreDatasource);
}
default -> {
}
}
}
private void filterDs(List<BusiNodeVO> busiNodeVOS, List<Long> ids, String type, Long id) {
for (BusiNodeVO busiNodeVO : busiNodeVOS) {
if (busiNodeVO.getType() != null && busiNodeVO.getType().equalsIgnoreCase(type)) {
@ -231,16 +188,53 @@ public class DatasourceServer implements DatasourceApi {
return hasRepeat;
}
@Transactional
public DatasourceDTO move(DatasourceDTO dataSourceDTO) {
if (dataSourceDTO.getPid() == null) {
DEException.throwException("目录必选!");
}
if (Objects.equals(dataSourceDTO.getId(), dataSourceDTO.getPid())) {
DEException.throwException(Translator.get("i18n_pid_not_eq_id"));
}
if (dataSourceDTO.getPid() != 0) {
List<Long> ids = new ArrayList<>();
getParents(dataSourceDTO.getPid(), ids);
if (ids.contains(dataSourceDTO.getId())) {
DEException.throwException(Translator.get("i18n_pid_not_eq_id"));
}
}
dataSourceManage.move(dataSourceDTO);
return dataSourceDTO;
}
@Transactional
public DatasourceDTO reName(DatasourceDTO dataSourceDTO) {
if (StringUtils.isEmpty(dataSourceDTO.getName())) {
DEException.throwException("名称不能为空!");
}
CoreDatasource datasource = datasourceMapper.selectById(dataSourceDTO.getId());
datasource.setName(dataSourceDTO.getName());
dataSourceManage.innerEdit(datasource);
return dataSourceDTO;
}
@Transactional
public DatasourceDTO createFolder(DatasourceDTO dataSourceDTO) {
dataSourceDTO.setCreateTime(System.currentTimeMillis());
dataSourceDTO.setUpdateTime(System.currentTimeMillis());
dataSourceDTO.setType(dataSourceDTO.getNodeType());
dataSourceDTO.setId(IDUtils.snowID());
dataSourceDTO.setConfiguration("");
CoreDatasource coreDatasource = new CoreDatasource();
coreDatasource.setTaskStatus(TaskStatus.WaitingForExecution.name());
BeanUtils.copyBean(coreDatasource, dataSourceDTO);
dataSourceManage.innerSave(coreDatasource);
return dataSourceDTO;
}
@Transactional
@Override
public DatasourceDTO save(DatasourceDTO dataSourceDTO) throws DEException {
if (StringUtils.isNotEmpty(dataSourceDTO.getAction())) {
move(dataSourceDTO);
return dataSourceDTO;
}
if (StringUtils.isNotEmpty(dataSourceDTO.getNodeType()) && dataSourceDTO.getNodeType().equalsIgnoreCase("folder")) {
dataSourceDTO.setType("folder");
dataSourceDTO.setConfiguration("");
}
if (dataSourceDTO.getId() != null && dataSourceDTO.getId() > 0) {
return update(dataSourceDTO);
}
@ -256,6 +250,7 @@ public class DatasourceServer implements DatasourceApi {
try {
checkDatasourceStatus(coreDatasource);
} catch (Exception ignore) {
coreDatasource.setStatus("Error");
}
coreDatasource.setTaskStatus(TaskStatus.WaitingForExecution.name());
coreDatasource.setCreateBy(AuthUtils.getUser().getUserId().toString());
@ -316,32 +311,6 @@ public class DatasourceServer implements DatasourceApi {
return dataSourceDTO;
}
private static void checkParams(String configurationStr) {
DatasourceConfiguration configuration = JsonUtil.parseObject(configurationStr, DatasourceConfiguration.class);
if (configuration.getInitialPoolSize() < configuration.getMinPoolSize()) {
DEException.throwException("初始连接数不能小于最小连接数!");
}
if (configuration.getInitialPoolSize() > configuration.getMaxPoolSize()) {
DEException.throwException("初始连接数不能大于最大连接数!");
}
if (configuration.getMaxPoolSize() < configuration.getMinPoolSize()) {
DEException.throwException("最大连接数不能小于最小连接数!");
}
if (configuration.getQueryTimeout() < 0) {
DEException.throwException("查询超时不能小于0");
}
}
private static void checkName(List<String> tables) {
for (int i = 0; i < tables.size() - 1; i++) {
for (int j = i + 1; j < tables.size(); j++) {
if (tables.get(i).equalsIgnoreCase(tables.get(j))) {
DEException.throwException(Translator.get("i18n_table_name_repeat") + tables.get(i));
}
}
}
}
public DatasourceDTO update(DatasourceDTO dataSourceDTO) throws DEException {
Long pk = null;
if (ObjectUtils.isEmpty(pk = dataSourceDTO.getId())) {
@ -358,7 +327,8 @@ public class DatasourceServer implements DatasourceApi {
requestDatasource.setUpdateBy(AuthUtils.getUser().getUserId());
try {
checkDatasourceStatus(requestDatasource);
} catch (Exception ignore) {
} catch (Exception e) {
requestDatasource.setStatus("Error");
}
DatasourceRequest sourceTableRequest = new DatasourceRequest();
@ -455,6 +425,32 @@ public class DatasourceServer implements DatasourceApi {
return dataSourceDTO;
}
private static void checkParams(String configurationStr) {
DatasourceConfiguration configuration = JsonUtil.parseObject(configurationStr, DatasourceConfiguration.class);
if (configuration.getInitialPoolSize() < configuration.getMinPoolSize()) {
DEException.throwException("初始连接数不能小于最小连接数!");
}
if (configuration.getInitialPoolSize() > configuration.getMaxPoolSize()) {
DEException.throwException("初始连接数不能大于最大连接数!");
}
if (configuration.getMaxPoolSize() < configuration.getMinPoolSize()) {
DEException.throwException("最大连接数不能小于最小连接数!");
}
if (configuration.getQueryTimeout() < 0) {
DEException.throwException("查询超时不能小于0");
}
}
private static void checkName(List<String> tables) {
for (int i = 0; i < tables.size() - 1; i++) {
for (int j = i + 1; j < tables.size(); j++) {
if (tables.get(i).equalsIgnoreCase(tables.get(j))) {
DEException.throwException(Translator.get("i18n_table_name_repeat") + tables.get(i));
}
}
}
}
private String excelDataTableName(String name) {
return StringUtils.substring(name, 6, name.length() - 11);
}
@ -553,6 +549,7 @@ public class DatasourceServer implements DatasourceApi {
return datasourceDTO;
}
@Transactional
@DeLog(id = "#p0", ot = LogOT.DELETE, st = LogST.DATASOURCE)
@Override
@XpackInteract(value = "datasourceResourceTree", before = false)
@ -615,7 +612,8 @@ public class DatasourceServer implements DatasourceApi {
@Override
public DatasourceDTO validate(Long datasourceId) throws DEException {
CoreDatasource coreDatasource = datasourceMapper.selectById(datasourceId);
CoreDatasource coreDatasource = new CoreDatasource();
BeanUtils.copyBean(coreDatasource, datasourceMapper.selectById(datasourceId));
return validate(coreDatasource);
}
@ -645,19 +643,24 @@ public class DatasourceServer implements DatasourceApi {
}
private DatasourceDTO validate(CoreDatasource coreDatasource) {
checkDatasourceStatus(coreDatasource);
DatasourceDTO datasourceDTO = new DatasourceDTO();
BeanUtils.copyBean(datasourceDTO, coreDatasource);
CoreDatasource record = new CoreDatasource();
record.setStatus(coreDatasource.getStatus());
QueryWrapper<CoreDatasource> wrapper = new QueryWrapper<>();
wrapper.eq("id", coreDatasource.getId());
CoreDatasource originData = datasourceMapper.selectById(coreDatasource.getId());
String originStatus = originData.getStatus();
if (StringUtils.equals(coreDatasource.getStatus(), originStatus)) {
return datasourceDTO;
try {
checkDatasourceStatus(coreDatasource);
} catch (Exception e) {
coreDatasource.setStatus("Error");
DEException.throwException(e.getMessage());
} finally {
datasourceDTO.setStatus(coreDatasource.getStatus());
QueryWrapper<CoreDatasource> wrapper = new QueryWrapper<>();
wrapper.eq("id", coreDatasource.getId());
CoreDatasource originData = datasourceMapper.selectById(coreDatasource.getId());
String originStatus = originData.getStatus();
if (!StringUtils.equals(coreDatasource.getStatus(), originStatus)) {
dataSourceManage.innerEditStatus(coreDatasource);
}
}
dataSourceManage.innerEditStatus(coreDatasource);
datasourceDTO.setConfiguration("");
return datasourceDTO;
}
@ -838,7 +841,7 @@ public class DatasourceServer implements DatasourceApi {
}
}
public void checkDatasourceStatus(CoreDatasource coreDatasource) throws DEException {
public void checkDatasourceStatus(CoreDatasource coreDatasource) {
if (coreDatasource.getType().equals(DatasourceConfiguration.DatasourceType.Excel.name()) || coreDatasource.getType().equals(DatasourceConfiguration.DatasourceType.folder.name())) {
return;
}
@ -853,8 +856,7 @@ public class DatasourceServer implements DatasourceApi {
}
coreDatasource.setStatus(status);
} catch (Exception e) {
coreDatasource.setStatus("Error");
DEException.throwException("校验失败: " + e.getMessage());
DEException.throwException(e.getMessage());
}
}