Skip to content

Commit

Permalink
Merge pull request #58 from jumpserver/pr@v3@fix_query_bug
Browse files Browse the repository at this point in the history
fix: 修复导出数据到 csv , 当字段中存在逗号时造成的错行问题
  • Loading branch information
Aaron3S authored Oct 23, 2024
2 parents 05f89bb + 96d3c1a commit aeee0fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jumpserver.chen.framework.session.SessionManager;
import org.jumpserver.chen.framework.ws.io.PacketIO;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
Expand Down Expand Up @@ -125,6 +126,15 @@ private void fullData(SQLQueryResult result) {
}
}

private static void writeString(BufferedWriter writer, Object object) throws IOException {
var str = object.toString();

if (str.contains(",")) {
str = "\"" + str + "\"";
}
writer.write(str);
}

public void export(String scope) throws SQLException {
var session = SessionManager.getCurrentSession();

Expand All @@ -144,7 +154,7 @@ public void export(String scope) throws SQLException {

if (scope.equals("current")) {
for (Field field : this.data.getFields()) {
writer.write(field.getName());
writeString(writer, field.getName());
writer.write(",");
}
writer.newLine();
Expand All @@ -155,7 +165,7 @@ public void export(String scope) throws SQLException {
writer.write("NULL");
writer.write(",");
} else {
writer.write(row.get(field.getName()).toString());
writeString(writer, row.get(field.getName()));
writer.write(",");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public List<String> getSchemas() throws SQLException {

@Override
public void changeSchema(String schema) throws SQLException {
this.execute(SQL.of("SET SEARCH_PATH TO ?;", schema));
this.execute(SQL.of("SET SEARCH_PATH TO '?';", schema));
}

@Override
public SQLExecutePlan createPlan(String schema, String table, SQLQueryParams sqlQueryParams) throws SQLException {
var sql = SQL.of("select * from ?.?", schema, table);
var sql = SQL.of("select * from \"?\".\"?\"", schema, table);
return this.createPlan(sql, sqlQueryParams);
}
}

0 comments on commit aeee0fc

Please sign in to comment.