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

优化代码

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