From aff6227f80185328c56a44184ae7f5d9913234c0 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Mon, 13 Jan 2025 14:32:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=BC=E5=87=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/console/dataview/DataView.java | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/backend/framework/src/main/java/org/jumpserver/chen/framework/console/dataview/DataView.java b/backend/framework/src/main/java/org/jumpserver/chen/framework/console/dataview/DataView.java index 890d427..f2d5e34 100644 --- a/backend/framework/src/main/java/org/jumpserver/chen/framework/console/dataview/DataView.java +++ b/backend/framework/src/main/java/org/jumpserver/chen/framework/console/dataview/DataView.java @@ -20,8 +20,10 @@ import java.nio.file.Files; import java.sql.Clob; import java.sql.SQLException; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -101,13 +103,17 @@ private void fullData(SQLQueryResult result) { this.data.getData().clear(); this.getStateManager().getState().setTotal(result.getTotal()); + this.fullDataViewData(this.data, result); + } + + private void fullDataViewData(DataViewData viewData, SQLQueryResult result) { - this.data.setFields(result.getFields()); + viewData.setFields(result.getFields()); Map fieldNumMap = new HashMap<>(); - this.data.getFields().forEach(field -> { + viewData.getFields().forEach(field -> { if (fieldNumMap.containsKey(field.getName())) { var fieldName = field.getName(); var num = fieldNumMap.get(field.getName()); @@ -118,16 +124,16 @@ private void fullData(SQLQueryResult result) { } }); - for (List row : result.getData()) { Map map = new HashMap<>(); for (int i = 0; i < row.size(); i++) { - map.put(this.data.getFields().get(i).getName(), row.get(i)); + map.put(viewData.getFields().get(i).getName(), row.get(i)); } - this.data.getData().add(map); + viewData.getData().add(map); } } + private static void writeString(BufferedWriter writer, Object object) throws IOException { var str = object.toString(); @@ -137,6 +143,35 @@ private static void writeString(BufferedWriter writer, Object object) throws IOE writer.write(str); } + private void writeCSVData(BufferedWriter writer, DataViewData viewData) throws IOException, SQLException { + + for (Field field : viewData.getFields()) { + writeString(writer, field.getName()); + writer.write(","); + } + for (Map row : viewData.getData()) { + for (Field field : viewData.getFields()) { + var obj = row.get(field.getName()); + if (obj == null) { + writer.write("NULL"); + writer.write(","); + } else if (obj instanceof Clob clob) { + writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length()))); + writer.write(","); + } else if (obj instanceof Date) { + SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + writeString(writer, fmt.format(obj)); + } else { + writeString(writer, row.get(field.getName())); + writer.write(","); + } + } + writer.newLine(); + } + + writer.newLine(); + } + public void export(String scope) throws SQLException { var session = SessionManager.getCurrentSession(); @@ -155,27 +190,7 @@ public void export(String scope) throws SQLException { var writer = Files.newBufferedWriter(f.toPath()); if (scope.equals("current")) { - for (Field field : this.data.getFields()) { - writeString(writer, field.getName()); - writer.write(","); - } - writer.newLine(); - - for (Map row : this.data.getData()) { - for (Field field : this.data.getFields()) { - if (row.get(field.getName()) == null) { - writer.write("NULL"); - writer.write(","); - } else if (row.get(field.getName()) instanceof Clob clob) { - writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length()))); - writer.write(","); - } else { - writeString(writer, row.get(field.getName())); - writer.write(","); - } - } - writer.newLine(); - } + this.writeCSVData(writer, this.data); command.setOutput(String.format("%d rows exported", this.data.getData().size())); } @@ -183,28 +198,8 @@ public void export(String scope) throws SQLException { SQLQueryParams queryParams = new SQLQueryParams(); queryParams.setLimit(-1); var result = this.loadDataInterface.loadData(queryParams); - - for (Field field : result.getFields()) { - writer.write(field.getName()); - writer.write(","); - } - writer.newLine(); - - for (List row : result.getData()) { - for (Object o : row) { - if (o == null) { - writer.write("NULL"); - writer.write(","); - } else if (o instanceof Clob clob) { - writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length()))); - writer.write(","); - } else { - writer.write(o.toString()); - writer.write(","); - } - } - writer.newLine(); - } + var viewData = new DataViewData(); + this.fullDataViewData(viewData, result); command.setOutput(String.format("%d rows exported", result.getData().size())); } writer.flush();