mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 02:32:33 +08:00
pgsql-schema-export
This commit is contained in:
@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
import static ai.chat2db.plugin.postgresql.consts.SQLConst.*;
|
||||
|
||||
@ -21,7 +22,7 @@ public class PostgreSQLDBManage extends DefaultDBManage implements DBManage {
|
||||
exportTables(connection, databaseName, schemaName, sqlBuilder, containData);
|
||||
exportViews(connection, schemaName, sqlBuilder);
|
||||
exportFunctions(connection, schemaName, sqlBuilder);
|
||||
exportTriggers(connection, schemaName, sqlBuilder);
|
||||
exportTriggers(connection, sqlBuilder);
|
||||
return sqlBuilder.toString();
|
||||
}
|
||||
|
||||
@ -52,57 +53,55 @@ public class PostgreSQLDBManage extends DefaultDBManage implements DBManage {
|
||||
}
|
||||
|
||||
private void exportTable(Connection connection, String schemaName, String tableName, StringBuilder sqlBuilder) throws SQLException {
|
||||
String tableQuery = "select pg_get_tabledef" + "(" + "'" + schemaName + "'" + "," + "'" + tableName + "'" + "," + "true" + "," + "'" + "COMMENTS" + "'" + ")" + ";";
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(tableQuery)) {
|
||||
sqlBuilder.append("\n").append("DROP TABLE IF EXISTS ").append(schemaName).append(".").append(tableName).append(";\n");
|
||||
String sql =String.format( "select pg_get_tabledef('%s','%s',true,'COMMENTS') as ddl;", schemaName,tableName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
if (resultSet.next()) {
|
||||
sqlBuilder.append(resultSet.getString(1)).append("\n");
|
||||
sqlBuilder.append("\n").append("DROP TABLE IF EXISTS ").append(tableName).append(";").append("\n")
|
||||
.append(resultSet.getString("ddl")).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTableData(Connection connection, String schemaName, String tableName, StringBuilder sqlBuilder) throws SQLException {
|
||||
StringBuilder insertSql = new StringBuilder();
|
||||
String dataQuery = "SELECT * FROM " + schemaName + "." + tableName;
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(dataQuery)) {
|
||||
String sql =String.format("select * from %s.%s", schemaName,tableName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
while (resultSet.next()) {
|
||||
insertSql.append("INSERT INTO ").append(tableName).append(" VALUES (");
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
sqlBuilder.append("INSERT INTO ").append(tableName).append(" VALUES (");
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String value = resultSet.getString(i);
|
||||
if (value != null) {
|
||||
insertSql.append("'").append(value).append("'");
|
||||
if (Objects.isNull(value)) {
|
||||
sqlBuilder.append("NULL");
|
||||
} else {
|
||||
insertSql.append("NULL");
|
||||
sqlBuilder.append("'").append(value).append("'");
|
||||
}
|
||||
if (i < columnCount) {
|
||||
insertSql.append(", ");
|
||||
if (i < metaData.getColumnCount()) {
|
||||
sqlBuilder.append(", ");
|
||||
}
|
||||
}
|
||||
insertSql.append(");\n");
|
||||
sqlBuilder.append(");\n");
|
||||
}
|
||||
insertSql.append("\n");
|
||||
sqlBuilder.append(insertSql);
|
||||
sqlBuilder.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void exportViews(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
|
||||
String viewsQuery = "SELECT table_name, view_definition FROM information_schema.views WHERE table_schema = '" + schemaName + "'";
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(viewsQuery)) {
|
||||
|
||||
String sql = String.format("SELECT table_name, view_definition FROM information_schema.views WHERE table_schema = '%s'",schemaName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
while (resultSet.next()) {
|
||||
String viewName = resultSet.getString("table_name");
|
||||
String viewDefinition = resultSet.getString("view_definition");
|
||||
sqlBuilder.append("DROP VIEW IF EXISTS ").append(schemaName).append(".").append(viewName).append(";\n");
|
||||
sqlBuilder.append("CREATE VIEW ").append(schemaName).append(".").append(viewName).append(" AS ").append(viewDefinition).append("\n\n");
|
||||
sqlBuilder.append("CREATE OR REPLACE VIEW ").append(viewName).append(" AS ").append(viewDefinition).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportFunctions(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
|
||||
String functionsQuery = "SELECT proname, pg_get_functiondef(oid) AS function_definition FROM pg_proc WHERE pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = '" + schemaName + "')";
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(functionsQuery)) {
|
||||
String sql = String.format("SELECT proname, pg_get_functiondef(oid) AS function_definition FROM pg_proc " +
|
||||
"WHERE pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = '%s')", schemaName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
while (resultSet.next()) {
|
||||
String functionName = resultSet.getString("proname");
|
||||
String functionDefinition = resultSet.getString("function_definition");
|
||||
@ -112,13 +111,11 @@ public class PostgreSQLDBManage extends DefaultDBManage implements DBManage {
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTriggers(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
|
||||
String triggersQuery = "SELECT tgname, pg_get_triggerdef(oid) AS trigger_definition FROM pg_trigger";
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(triggersQuery)) {
|
||||
private void exportTriggers(Connection connection, StringBuilder sqlBuilder) throws SQLException {
|
||||
String sql = "SELECT pg_get_triggerdef(oid) AS trigger_definition FROM pg_trigger";
|
||||
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
|
||||
while (resultSet.next()) {
|
||||
String triggerName = resultSet.getString("tgname");
|
||||
String triggerDefinition = resultSet.getString("trigger_definition");
|
||||
sqlBuilder.append(triggerDefinition).append(";\n\n");
|
||||
sqlBuilder.append(resultSet.getString("trigger_definition")).append(";").append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user