Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 导出数据日期格式错误 #78

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Integer> 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());
Expand All @@ -118,16 +124,16 @@ private void fullData(SQLQueryResult result) {
}
});


for (List<Object> row : result.getData()) {
Map<String, Object> 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();

Expand All @@ -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<String, Object> 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();

Expand All @@ -155,56 +190,16 @@ 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<String, Object> 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()));
}

if (scope.equals("all")) {
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<Object> 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();
Expand Down
Loading