mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 02:32:33 +08:00
remove redundant code and optimize exception throwing
This commit is contained in:
@ -15,12 +15,6 @@ import static ai.chat2db.plugin.postgresql.consts.SQLConst.*;
|
||||
|
||||
public class PostgreSQLDBManage extends DefaultDBManage implements DBManage {
|
||||
|
||||
@Override
|
||||
public String exportDatabaseData(Connection connection, String databaseName, String schemaName, String tableName) throws SQLException {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
exportTableData(connection,schemaName, tableName, sqlBuilder);
|
||||
return sqlBuilder.toString();
|
||||
}
|
||||
public String exportDatabase(Connection connection, String databaseName, String schemaName, boolean containData) throws SQLException {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
exportTypes(connection, sqlBuilder);
|
||||
@ -67,28 +61,6 @@ public class PostgreSQLDBManage extends DefaultDBManage implements DBManage {
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTableData(Connection connection, String schemaName, String tableName, StringBuilder sqlBuilder) throws SQLException {
|
||||
String sql =String.format("select * from %s.%s", schemaName,tableName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
while (resultSet.next()) {
|
||||
sqlBuilder.append("INSERT INTO ").append(tableName).append(" VALUES (");
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String value = resultSet.getString(i);
|
||||
if (Objects.isNull(value)) {
|
||||
sqlBuilder.append("NULL");
|
||||
} else {
|
||||
sqlBuilder.append("'").append(value).append("'");
|
||||
}
|
||||
if (i < metaData.getColumnCount()) {
|
||||
sqlBuilder.append(", ");
|
||||
}
|
||||
}
|
||||
sqlBuilder.append(");\n");
|
||||
}
|
||||
sqlBuilder.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void exportViews(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
|
||||
|
Reference in New Issue
Block a user