提交 b4c406aa authored 作者: songchuancai's avatar songchuancai

优化代码

上级 535adc44
...@@ -10,6 +10,7 @@ import org.springframework.util.CollectionUtils; ...@@ -10,6 +10,7 @@ import org.springframework.util.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
/** /**
* @author : scc * @author : scc
...@@ -44,9 +45,9 @@ public class ColumnDataRangeFilter { ...@@ -44,9 +45,9 @@ public class ColumnDataRangeFilter {
.append(".") .append(".")
.append(field.getColumnName()) .append(field.getColumnName())
.append(" as ") .append(" as ")
.append(this.getTableName()) .append(this.getTableName().toUpperCase(Locale.ROOT))
.append("_") .append("_")
.append(field.getColumnName()) .append(field.getColumnName().toUpperCase(Locale.ROOT))
.append(","); .append(",");
} }
...@@ -83,9 +84,9 @@ public class ColumnDataRangeFilter { ...@@ -83,9 +84,9 @@ public class ColumnDataRangeFilter {
head = new ArrayList<>(); head = new ArrayList<>();
headBuilder = new StringBuilder(); headBuilder = new StringBuilder();
headBuilder headBuilder
.append(this.getTableName()) .append(this.getTableName().toUpperCase(Locale.ROOT))
.append("_") .append("_")
.append(field.getColumnName()); .append(field.getColumnName().toUpperCase(Locale.ROOT));
head.add(headBuilder.toString()); head.add(headBuilder.toString());
heads.add(head); heads.add(head);
} }
......
...@@ -29,11 +29,11 @@ public class QueryDataTask<T> implements Callable<T> { ...@@ -29,11 +29,11 @@ public class QueryDataTask<T> implements Callable<T> {
public T call() throws Exception { public T call() throws Exception {
if (StringUtils.isEmpty(querySql)) { if (StringUtils.isEmpty(querySql)) {
return (T) JdbcUtil.executeCountSql(dataApiDataSource, countSql, jdbcParamValues); Result<Long> result = JdbcUtil.executeCountSql(dataApiDataSource, countSql, jdbcParamValues);
return (T) result.getData();
} else { } else {
List<List<Object>> data = new ArrayList<>(); List<List<Object>> data = new ArrayList<>();
Result<List<JSONObject>> dataResult = JdbcUtil.executeSql(dataApiDataSource, querySql, jdbcParamValues, queryDataTotalPerThread); Result<List<JSONObject>> dataResult = JdbcUtil.executeSql(dataApiDataSource, querySql, jdbcParamValues, queryDataTotalPerThread);
Result<List<List<Object>>> result = new Result<>();
List<JSONObject> dataJsonList = dataResult.getData(); List<JSONObject> dataJsonList = dataResult.getData();
for (JSONObject dataJson : dataJsonList) { for (JSONObject dataJson : dataJsonList) {
List<Object> lineData = new ArrayList<>(); List<Object> lineData = new ArrayList<>();
...@@ -44,10 +44,7 @@ public class QueryDataTask<T> implements Callable<T> { ...@@ -44,10 +44,7 @@ public class QueryDataTask<T> implements Callable<T> {
} }
data.add(lineData); data.add(lineData);
} }
result.setMsg(dataResult.getMsg()); return (T) data;
result.setData(data);
result.setMsg(dataResult.getMsg());
return (T) result;
} }
} }
} }
......
...@@ -16,8 +16,8 @@ public class BaseExceptionController { ...@@ -16,8 +16,8 @@ public class BaseExceptionController {
@ExceptionHandler({Exception.class}) @ExceptionHandler({Exception.class})
@ResponseBody @ResponseBody
public Result<String> exceptionHandler(HttpServletRequest request, Exception e) { public Result<String> exceptionHandler(HttpServletRequest request, Exception e) {
log.error("发生了异常:", e); log.error("系统异常:", e);
String msg = e.getMessage(); String msg = "系统异常: " + e.getMessage();
return new Result(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),msg,null); return new Result(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),msg,null);
} }
...@@ -25,8 +25,8 @@ public class BaseExceptionController { ...@@ -25,8 +25,8 @@ public class BaseExceptionController {
@ExceptionHandler({IllegalArgumentException.class}) @ExceptionHandler({IllegalArgumentException.class})
@ResponseBody @ResponseBody
public Result<String> exceptionHandler(HttpServletRequest request, IllegalArgumentException e) { public Result<String> exceptionHandler(HttpServletRequest request, IllegalArgumentException e) {
log.error("参数不合法:", e); log.error("系统异常,参数不合法:", e);
String msg = e.getMessage(); String msg = "系统异常,参数不合法:" + e.getMessage();
return new Result(String.valueOf(HttpStatus.BAD_REQUEST.value()),msg,null); return new Result(String.valueOf(HttpStatus.BAD_REQUEST.value()),msg,null);
} }
......
...@@ -67,6 +67,10 @@ public class DataApiModelConfig extends BaseEntity{ ...@@ -67,6 +67,10 @@ public class DataApiModelConfig extends BaseEntity{
@Column(name = "sql", columnDefinition = "varchar2(1000)", nullable = true) @Column(name = "sql", columnDefinition = "varchar2(1000)", nullable = true)
private String fullSql; private String fullSql;
// SQL语句,记录生成的countSql
@Column(name = "count_sql", columnDefinition = "varchar2(1000)", nullable = true)
private String countSql;
// 根据订阅系统的配置生成sql行列筛选条件 // 根据订阅系统的配置生成sql行列筛选条件
// 生成的时候需要进行校验 // 生成的时候需要进行校验
...@@ -106,28 +110,29 @@ public class DataApiModelConfig extends BaseEntity{ ...@@ -106,28 +110,29 @@ public class DataApiModelConfig extends BaseEntity{
this.setFromSql(fromSql); this.setFromSql(fromSql);
this.setSelectSql(selectSql); this.setSelectSql(selectSql);
this.setWhereSql(whereSql); this.setWhereSql(whereSql);
this.setCountSql(generateDataCountSql());
return fullSql; return fullSql;
} }
// 生成数据量大小查询sql // 生成数据量大小查询sql
public String generateDataCountSql() { public String generateDataCountSql() {
// 已经通过校验行列筛选条件 // 已经通过校验行列筛选条件
StringBuilder fullSqlBuilder = new StringBuilder(); StringBuilder countSqlBuilder = new StringBuilder();
// 拼接select条件 // 拼接select条件
fullSqlBuilder.append(" select count(1) "); countSqlBuilder.append(" select count(1) ");
// 拼接from条件 // 拼接from条件
String fromSql = this.getFromSql(); String fromSql = this.getFromSql();
fullSqlBuilder.append(fromSql); countSqlBuilder.append(fromSql);
// 拼接where条件 // 拼接where条件
String whereSql = this.getWhereSql(); String whereSql = this.getWhereSql();
fullSqlBuilder.append(" where ").append(whereSql); countSqlBuilder.append(" where ").append(whereSql);
// 生成完成的sql // 生成完成的sql
String fullSql = fullSqlBuilder.toString(); String countSql = countSqlBuilder.toString();
return fullSql; return countSql;
} }
/** /**
......
package com.hisense.dataservice.exceptions;
/**
* @author : scc
* @date : 2023/03/21
**/
public class DataQueryException extends Exception{
public DataQueryException(String msg){
super("数据查询异常:" + msg);
}
public DataQueryException(String msg, Throwable cause){
super("数据查询异常:" + msg, cause);
}
}
...@@ -29,6 +29,7 @@ import org.springframework.util.StringUtils; ...@@ -29,6 +29,7 @@ import org.springframework.util.StringUtils;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -489,26 +490,29 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage ...@@ -489,26 +490,29 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage
return failure("调试数据api服务失败--生成sql失败", null); return failure("调试数据api服务失败--生成sql失败", null);
} }
// 查询出前10条数据进行返回 // 查询出前10条数据进行返回
Result<List<List<Object>>> executeSqlResult = new Result<>(); List<List<Object>> executeSqlDataList = new ArrayList<>();
List<List<Object>> data = new ArrayList<>(); List<List<Object>> data = new ArrayList<>();
List<Object> jdbcParams = getJdbcParams(dataApiModelConfig, dataApiDataSource.getType()); List<Object> jdbcParams = getJdbcParams(dataApiModelConfig, dataApiDataSource.getType());
String errorMsg = "";
try{ try{
executeSqlResult = executeDataQueryTask(fullSql, dataApiModelConfig, dataApiDataSource,jdbcParams); executeSqlDataList = executeDataQueryTask(fullSql, dataApiModelConfig, dataApiDataSource,jdbcParams);
dataApiModel.setDebugStatus(DebugStatusEnum.SUCCESS); dataApiModel.setDebugStatus(DebugStatusEnum.SUCCESS);
data = executeSqlResult.getData();
}catch (Exception e){ }catch (Exception e){
log.error("执行数据查询失败:", e); log.error("执行数据查询失败:", e);
errorMsg = e.getMessage();
dataApiModel.setDebugStatus(DebugStatusEnum.FAIL); dataApiModel.setDebugStatus(DebugStatusEnum.FAIL);
} }
// 保存调试结果 // 保存调试结果
dataApiModelRepository.save(dataApiModel); dataApiModelRepository.save(dataApiModel);
if(DebugStatusEnum.FAIL.equals(dataApiModel.getDebugStatus())){
return failure(errorMsg);
}
// 获取数据的列名 // 获取数据的列名
List<List<String>> heads = ColumnDataRangeFilter.generateExcelHead(dataApiModelConfig.getColumnDataRangeConfig()); List<List<String>> heads = ColumnDataRangeFilter.generateExcelHead(dataApiModelConfig.getColumnDataRangeConfig());
result.put("data", data); result.put("data", data);
result.put("heads", heads); result.put("heads", heads);
if(DebugStatusEnum.FAIL.equals(dataApiModel.getDebugStatus())){
return failure(executeSqlResult.getMsg());
}
return success("数据api服务调试成功", result); return success("数据api服务调试成功", result);
} }
...@@ -553,9 +557,9 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage ...@@ -553,9 +557,9 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage
return success(dataApiModelDetailVo); return success(dataApiModelDetailVo);
} }
private Result<List<List<Object>>> executeDataQueryTask(String dataSql, DataApiModelConfig dataApiModelConfig, DataApiDataSource dataSource, List<Object> jdbcParamValues) throws Exception{ private List<List<Object>> executeDataQueryTask(String dataSql, DataApiModelConfig dataApiModelConfig, DataApiDataSource dataSource, List<Object> jdbcParamValues) throws ExecutionException,InterruptedException {
QueryDataTask queryDataTask = new QueryDataTask(); QueryDataTask<List<List<Object>>> queryDataTask = new QueryDataTask();
queryDataTask.setDataApiDataSource(dataSource); queryDataTask.setDataApiDataSource(dataSource);
queryDataTask.setQuerySql(dataSql); queryDataTask.setQuerySql(dataSql);
queryDataTask.setPageNumber(1L); queryDataTask.setPageNumber(1L);
...@@ -563,8 +567,8 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage ...@@ -563,8 +567,8 @@ public class DataApiServiceManagementServiceImpl implements DataApiServiceManage
queryDataTask.setJdbcParamValues(jdbcParamValues); queryDataTask.setJdbcParamValues(jdbcParamValues);
Future<?> future = debugQueryDataThreadPool.addExecuteTask(queryDataTask); Future<?> future = debugQueryDataThreadPool.addExecuteTask(queryDataTask);
Result<List<List<Object>>> result = new Result<>(); List<List<Object>> result = new ArrayList<>();
result = (Result<List<List<Object>>>) future.get(); result = (List<List<Object>>) future.get();
return result; return result;
} }
private List<Object> getJdbcParams(DataApiModelConfig dataApiModelConfig, DataSourceTypeEnum dataSourceType){ private List<Object> getJdbcParams(DataApiModelConfig dataApiModelConfig, DataSourceTypeEnum dataSourceType){
......
...@@ -4,6 +4,7 @@ import com.alibaba.druid.pool.DruidPooledConnection; ...@@ -4,6 +4,7 @@ import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hisense.dataservice.entity.DataApiDataSource; import com.hisense.dataservice.entity.DataApiDataSource;
import com.hisense.dataservice.exceptions.DataQueryException;
import com.hisense.dataservice.library.model.Result; import com.hisense.dataservice.library.model.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -61,22 +62,22 @@ public class JdbcUtil { ...@@ -61,22 +62,22 @@ public class JdbcUtil {
log.info("thread: {}, 返回的数据:{}", threadName, result); log.info("thread: {}, 返回的数据:{}", threadName, result);
return new Result("200", "查询成功", result); return new Result("200", "查询成功", result);
} catch (SQLException sqlException) { } catch (SQLException sqlException) {
log.error("[{}] 数据库连接失败:", datasource.getSourceName(), sqlException); log.error("sql: {}, params: {},数据查询异常:", sql, jdbcParamValues, sqlException);
return new Result("300", "数据源异常:" + sqlException.getMessage(), result); throw new DataQueryException(sqlException.getMessage(), sqlException.getCause());
} catch (Exception e) { } catch (Exception e) {
log.error("thread: {}, sql: {}, params: {}, 分页查询数据异常:", threadName, sql, jdbcParamValues, e); log.error("sql: {}, params: {}, 分页查询数据异常:", sql, jdbcParamValues, e);
return new Result("400", "系统异常:" + e.getMessage(), result); throw new RuntimeException("系统异常:" + e.getMessage());
} finally { } finally {
try { try {
if (connection != null) if (connection != null)
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
log.error("关闭数据库连接异常信息:{}", e.getMessage()); log.error("关闭数据库连接异常:{}", e.getMessage());
} }
} }
} }
public static Result<Long> executeCountSql(DataApiDataSource datasource, String sql, List<Object> jdbcParamValues) { public static Result<Long> executeCountSql(DataApiDataSource datasource, String sql, List<Object> jdbcParamValues) throws DataQueryException {
log.info("sql:{}", sql); log.info("sql:{}", sql);
DruidPooledConnection connection = null; DruidPooledConnection connection = null;
try { try {
...@@ -100,14 +101,15 @@ public class JdbcUtil { ...@@ -100,14 +101,15 @@ public class JdbcUtil {
log.info("返回的数据:{}", count); log.info("返回的数据:{}", count);
return new Result("200", "查询成功", count); return new Result("200", "查询成功", count);
} catch (SQLException e) { } catch (SQLException e) {
log.error("异常信息:{}", e.getMessage()); throw new DataQueryException(e.getMessage());
return new Result().setError("500", e.getMessage()); }catch (Exception e){
throw new RuntimeException("系统异常:" + e.getMessage());
} finally { } finally {
try { try {
if (connection != null) if (connection != null)
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
log.error("错误信息:{}", e.getMessage()); log.error("关闭数据库连接异常:{}", e.getMessage());
} }
} }
} }
......
...@@ -48,7 +48,7 @@ public class ThreadPoolManagerUtil { ...@@ -48,7 +48,7 @@ public class ThreadPoolManagerUtil {
new NamedThreadFactoryImpl(poolName)){ new NamedThreadFactoryImpl(poolName)){
public void afterExecute(Runnable r, Throwable t) { public void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t); super.afterExecute(r, t);
printException(r, t); // printException(r, t);
} }
}; };
} }
......
...@@ -9,6 +9,7 @@ import com.hisense.dataservice.entity.DataApiModel; ...@@ -9,6 +9,7 @@ import com.hisense.dataservice.entity.DataApiModel;
import com.hisense.dataservice.entity.DataApiModelConfig; import com.hisense.dataservice.entity.DataApiModelConfig;
import com.hisense.dataservice.entity.DataApiSubscribeConfig; import com.hisense.dataservice.entity.DataApiSubscribeConfig;
import com.hisense.dataservice.enums.*; import com.hisense.dataservice.enums.*;
import com.hisense.dataservice.library.model.Result;
import com.hisense.dataservice.repository.DataApiDataSourceRepository; import com.hisense.dataservice.repository.DataApiDataSourceRepository;
import com.hisense.dataservice.repository.DataApiModelConfigRepository; import com.hisense.dataservice.repository.DataApiModelConfigRepository;
import com.hisense.dataservice.repository.DataApiModelRepository; import com.hisense.dataservice.repository.DataApiModelRepository;
...@@ -213,13 +214,13 @@ public class DataApiCommonServiceTest { ...@@ -213,13 +214,13 @@ public class DataApiCommonServiceTest {
List<ColumnDataRangeFilter> columnDataRangeConfig = dataApiModelConfig.getColumnDataRangeConfig(); List<ColumnDataRangeFilter> columnDataRangeConfig = dataApiModelConfig.getColumnDataRangeConfig();
List<List<String>> lists = ColumnDataRangeFilter.generateExcelHead(columnDataRangeConfig); List<List<String>> lists = ColumnDataRangeFilter.generateExcelHead(columnDataRangeConfig);
List<List<List<Object>>> datas = dataApiModelService.asyncExecuteSql(lists, new DataApiDataSource(), "select * from A where name = ? ", "select count(1) from A where name = ?", jdbcParams, DataSourceTypeEnum.ORACLE); // List<List<List<Object>>> datas = dataApiModelService.asyncExecuteSql(lists, new DataApiDataSource(), "select * from A where name = ? ", "select count(1) from A where name = ?", jdbcParams, DataSourceTypeEnum.ORACLE);
List<List<String>> heads = ColumnDataRangeFilter.generateExcelHead(dataApiModelConfig.getColumnDataRangeConfig()); // List<List<String>> heads = ColumnDataRangeFilter.generateExcelHead(dataApiModelConfig.getColumnDataRangeConfig());
//
String tmpFileName = FileUtil.getPath() + System.currentTimeMillis() + ".xlsx"; // String tmpFileName = FileUtil.getPath() + System.currentTimeMillis() + ".xlsx";
//
dataApiModelService.saveTmpDataFile(tmpFileName, datas, heads); // dataApiModelService.saveTmpDataFile(tmpFileName, datas, heads);
log.info("时间:{} ms", System.currentTimeMillis() - start); // log.info("时间:{} ms", System.currentTimeMillis() - start);
} }
@Test @Test
...@@ -485,5 +486,12 @@ public class DataApiCommonServiceTest { ...@@ -485,5 +486,12 @@ public class DataApiCommonServiceTest {
} }
} }
@Test
public void testQueryDataLatest(){
Result<JSONObject> staging = dataApiModelService.queryDataLatest("staging", "448086965d904841b9deb8d93d54fd8a");
log.info(String.valueOf(staging));
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论