mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-01 18:53:35 +08:00
Merge remote-tracking branch 'origin/developing' into developing
This commit is contained in:
@ -52,6 +52,10 @@
|
||||
<groupId>com.dtflys.forest</groupId>
|
||||
<artifactId>forest-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -4,19 +4,15 @@ import java.util.List;
|
||||
|
||||
import ai.chat2db.server.domain.api.param.DlExecuteParam;
|
||||
import ai.chat2db.server.domain.api.service.DlTemplateService;
|
||||
import ai.chat2db.spi.model.ExecuteResult;
|
||||
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
|
||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
||||
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
||||
import ai.chat2db.server.web.api.controller.rdb.request.DataExportRequest;
|
||||
import ai.chat2db.server.web.api.controller.rdb.request.DdlCountRequest;
|
||||
import ai.chat2db.server.web.api.controller.rdb.request.DmlRequest;
|
||||
import ai.chat2db.server.web.api.controller.rdb.vo.ExecuteResultVO;
|
||||
|
||||
import ai.chat2db.spi.model.ExecuteResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@ -40,39 +36,6 @@ public class RdbDmlController {
|
||||
@Autowired
|
||||
private DlTemplateService dlTemplateService;
|
||||
|
||||
/**
|
||||
* 导出结果集Excel
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/export/excel")
|
||||
public ActionResult export(DataExportRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出结果集Insert
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/export/insert")
|
||||
public ActionResult exportInsert(DataExportRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出选中行Insert
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/export/insert/selected")
|
||||
public ActionResult exportInsertSelected(DataExportRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增删改查等数据运维
|
||||
*
|
||||
|
@ -0,0 +1,134 @@
|
||||
package ai.chat2db.server.web.api.controller.rdb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import com.alibaba.excel.util.BeanMapUtils;
|
||||
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
|
||||
import ai.chat2db.server.domain.api.enums.ExportSizeEnum;
|
||||
import ai.chat2db.server.domain.api.enums.ExportTypeEnum;
|
||||
import ai.chat2db.server.domain.api.service.DlTemplateService;
|
||||
import ai.chat2db.server.tools.common.exception.ParamBusinessException;
|
||||
import ai.chat2db.server.tools.common.util.EasyCollectionUtils;
|
||||
import ai.chat2db.server.tools.common.util.EasyEnumUtils;
|
||||
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
||||
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
||||
import ai.chat2db.server.web.api.controller.rdb.request.DataExportRequest;
|
||||
import ai.chat2db.spi.sql.Chat2DBContext;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import com.google.common.collect.Lists;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* Export Database Exclusive
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
@ConnectionInfoAspect
|
||||
@RequestMapping("/api/rdb/dml")
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class RdbDmlExportController {
|
||||
|
||||
@Autowired
|
||||
private RdbWebConverter rdbWebConverter;
|
||||
|
||||
@Autowired
|
||||
private DlTemplateService dlTemplateService;
|
||||
|
||||
/**
|
||||
* export data
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/export")
|
||||
public void export(@Valid @RequestBody DataExportRequest request, HttpServletResponse response) throws IOException {
|
||||
ExportSizeEnum exportSize = EasyEnumUtils.getEnum(ExportSizeEnum.class, request.getExportSize());
|
||||
ExportTypeEnum exportType = EasyEnumUtils.getEnum(ExportTypeEnum.class, request.getExportType());
|
||||
BeanMap beanMap = BeanMap.create(request);
|
||||
log.info("x:{}", beanMap.get("sql"));
|
||||
|
||||
com.alibaba.excel.support.cglib.beans.BeanMap beanMap2 = BeanMapUtils.create(request);
|
||||
log.info("te:{}", beanMap2.get("sql"));
|
||||
|
||||
if (exportType == ExportTypeEnum.CSV) {
|
||||
doExportCsv(exportSize, request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void doExportCsv(ExportSizeEnum exportSize, DataExportRequest request, HttpServletResponse response)
|
||||
throws IOException {
|
||||
String sql;
|
||||
if (exportSize == ExportSizeEnum.CURRENT_PAGE) {
|
||||
sql = request.getSql();
|
||||
} else {
|
||||
sql = request.getOriginalSql();
|
||||
}
|
||||
if (StringUtils.isBlank(sql)) {
|
||||
throw new ParamBusinessException("exportSize");
|
||||
}
|
||||
//
|
||||
//ublic void download(HttpServletResponse response) throws IOException {
|
||||
// // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
||||
response.setContentType("text/csv");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
||||
String fileName = URLEncoder.encode("测试", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".csv");
|
||||
// EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
|
||||
|
||||
ExcelWrapper excelWrapper = new ExcelWrapper();
|
||||
try {
|
||||
ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(response.getOutputStream())
|
||||
.excelType(ExcelTypeEnum.CSV);
|
||||
excelWrapper.setExcelWriterBuilder(excelWriterBuilder);
|
||||
SQLExecutor.getInstance().executeSql(Chat2DBContext.getConnection(), sql, headerList -> {
|
||||
excelWriterBuilder.head(
|
||||
EasyCollectionUtils.toList(headerList, header -> Lists.newArrayList(header.getName())));
|
||||
excelWrapper.setExcelWriter(excelWriterBuilder.build());
|
||||
excelWrapper.setWriteSheet(EasyExcel.writerSheet(0).build());
|
||||
}, dataList -> {
|
||||
List<List<String>> writeDataList = Lists.newArrayList();
|
||||
writeDataList.add(dataList);
|
||||
excelWrapper.getExcelWriter().write(writeDataList, excelWrapper.getWriteSheet());
|
||||
});
|
||||
} finally {
|
||||
if (excelWrapper.getExcelWriter() != null) {
|
||||
excelWrapper.getExcelWriter().finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ExcelWrapper {
|
||||
private ExcelWriterBuilder excelWriterBuilder;
|
||||
private ExcelWriter excelWriter;
|
||||
private WriteSheet writeSheet;
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
package ai.chat2db.server.web.api.controller.rdb.request;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.server.domain.api.enums.ExportSizeEnum;
|
||||
import ai.chat2db.server.domain.api.enums.ExportTypeEnum;
|
||||
import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequest;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequest;
|
||||
import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequest;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@ -16,21 +13,29 @@ import lombok.Data;
|
||||
*/
|
||||
@Data
|
||||
public class DataExportRequest extends DataSourceBaseRequest {
|
||||
|
||||
/**
|
||||
* 导出sql语句
|
||||
* Executed SQL
|
||||
*/
|
||||
@NotNull
|
||||
private String sql;
|
||||
|
||||
/**
|
||||
* 控制台id
|
||||
* Original SQL without pagination
|
||||
*/
|
||||
@NotNull
|
||||
private Long consoleId;
|
||||
private String originalSql;
|
||||
|
||||
/**
|
||||
* 导出行ID列表
|
||||
* export type
|
||||
*
|
||||
* @see ExportTypeEnum
|
||||
*/
|
||||
private List<Long> exportIds;
|
||||
@NotNull
|
||||
private String exportType;
|
||||
|
||||
/**
|
||||
* How much data is currently needed at the beginning
|
||||
*
|
||||
* @see ExportSizeEnum
|
||||
*/
|
||||
@NotNull
|
||||
private String exportSize;
|
||||
}
|
||||
|
Reference in New Issue
Block a user