Skip to content

Commit

Permalink
Merge pull request CodePhiliaX#1245 from openai0229/dm-schema-export
Browse files Browse the repository at this point in the history
fix  dm table ddl query and dm-schema-export
  • Loading branch information
Chat2DB-Pro authored Mar 22, 2024
2 parents 90db345 + fd344c8 commit 7df622d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public String exportDatabase(Connection connection, String databaseName, String
}

private void exportTables(Connection connection, StringBuilder sqlBuilder, String schemaName, boolean containData) throws SQLException {
try (Statement statement = connection.createStatement(); ResultSet tables = statement.executeQuery("SELECT TABLE_NAME FROM ALL_TABLES where OWNER='" + schemaName + "' and TABLESPACE_NAME='MAIN'")) {
while (tables.next()) {
String tableName = tables.getString(1);
String sql =String.format("SELECT TABLE_NAME FROM ALL_TABLES where OWNER='%s' and TABLESPACE_NAME='MAIN'", schemaName);
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
exportTable(connection, tableName, schemaName, sqlBuilder, containData);
}
}
Expand All @@ -54,49 +55,61 @@ private void exportTable(Connection connection, String tableName, String schemaN
String formatSchemaName = format(schemaName);
String formatTableName = format(tableName);
if (resultSet.next()) {
sqlBuilder.append("DROP TABLE IF EXISTS ").append(formatSchemaName).append(".").append(formatTableName).append(";\n")
.append(resultSet.getString(2)).append("\n");
String comment = resultSet.getString(1);
sqlBuilder.append("DROP TABLE IF EXISTS ").append(formatSchemaName).append(".").append(formatTableName)
.append(";").append("\n")
.append(resultSet.getString("ddl")).append("\n");
String comment = resultSet.getString("comments");
if (StringUtils.isNotBlank(comment)) {
sqlBuilder.append("COMMENT ON TABLE ").append(formatSchemaName).append(".").append(formatTableName)
.append(" IS ").append("'").append(comment).append("';");
}
exportTableColumnComment(connection, schemaName, tableName, sqlBuilder);
}
if (containData) {
exportTableData(connection, tableName, sqlBuilder);
exportTableData(connection, schemaName, tableName, sqlBuilder);
}
}
}

private void exportTableColumnComment(Connection connection, String schemaName, String tableName, StringBuilder sqlBuilder) throws SQLException {
String sql =String.format("select COLNAME,COMMENT$ from SYS.SYSCOLUMNCOMMENTS\n" +
"where SCHNAME = '%s' and TVNAME = '%s'and TABLE_TYPE = 'TABLE';", schemaName,tableName);
try(ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
while (resultSet.next()) {
String columnName = resultSet.getString("COLNAME");
String comment = resultSet.getString("COMMENT$");
sqlBuilder.append("COMMENT ON COLUMN ").append(format(schemaName)).append(".").append(format(tableName))
.append(".").append(format(columnName)).append(" IS ").append("'").append(comment).append("';").append("\n");
}
}
}


private void exportTableData(Connection connection, String tableName, StringBuilder sqlBuilder) throws SQLException {
StringBuilder insertSql = new StringBuilder();
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from " + tableName)) {
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()) {
insertSql.append("INSERT INTO ").append(tableName).append(" VALUES (");
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)) {
insertSql.append("NULL");
sqlBuilder.append("NULL");
} else {

insertSql.append("'").append(value).append("'");
sqlBuilder.append("'").append(value).append("'");
}
if (i < metaData.getColumnCount()) {
insertSql.append(", ");
sqlBuilder.append(", ");
}
}
insertSql.append(");\n");
sqlBuilder.append(");\n");
}
insertSql.append("\n");
sqlBuilder.append("\n");
}
sqlBuilder.append(insertSql);
}

private void exportViews(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
DatabaseMetaData metadata = connection.getMetaData();
try (ResultSet resultSet = metadata.getTables(null, schemaName, null, new String[]{"VIEW"})) {
try (ResultSet resultSet = connection.getMetaData().getTables(null, schemaName, null, new String[]{"VIEW"})) {
while (resultSet.next()) {
String viewName = resultSet.getString("TABLE_NAME");
exportView(connection, viewName, schemaName, sqlBuilder);
Expand All @@ -105,16 +118,16 @@ private void exportViews(Connection connection, String schemaName, StringBuilder
}

private void exportView(Connection connection, String viewName, String schemaName, StringBuilder sqlBuilder) throws SQLException {
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT DBMS_METADATA.GET_DDL('VIEW','" + viewName + "','" + schemaName + "') FROM DUAL;")) {
String sql = String.format("SELECT DBMS_METADATA.GET_DDL('VIEW','%s','%s') as ddl FROM DUAL;", viewName, schemaName);
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
if (resultSet.next()) {
sqlBuilder.append(resultSet.getString(1)).append("\n");
sqlBuilder.append(resultSet.getString("ddl")).append("\n");
}
}
}

private void exportProcedures(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet resultSet = metaData.getProcedures(null, schemaName, null)) {
try (ResultSet resultSet = connection.getMetaData().getProcedures(null, schemaName, null)) {
while (resultSet.next()) {
String procedureName = resultSet.getString("PROCEDURE_NAME");
exportProcedure(connection, schemaName,procedureName, sqlBuilder);
Expand All @@ -133,7 +146,7 @@ private void exportProcedure(Connection connection, String schemaName, String pr

private void exportTriggers(Connection connection, String schemaName, StringBuilder sqlBuilder) throws SQLException {
String sql =String.format(TRIGGER_SQL_LIST, schemaName);
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
while (resultSet.next()) {
String triggerName = resultSet.getString("TRIGGER_NAME");
exportTrigger(connection,schemaName, triggerName, sqlBuilder);
Expand All @@ -143,7 +156,7 @@ private void exportTriggers(Connection connection, String schemaName, StringBuil

private void exportTrigger(Connection connection, String schemaName, String triggerName, StringBuilder sqlBuilder) throws SQLException {
String sql = String.format(TRIGGER_SQL, schemaName,triggerName);
try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
if (resultSet.next()) {
sqlBuilder.append(resultSet.getString("TRIGGER_BODY")).append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,29 @@ public String tableDDL(Connection connection, String databaseName, String schema
(SELECT dbms_metadata.get_ddl('TABLE', '%s', '%s') FROM dual) AS ddl
FROM dual;
""";
String selectObjectDDLSQL = String.format(sql, tableName, tableName, schemaName);
return SQLExecutor.getInstance().execute(connection, selectObjectDDLSQL, resultSet -> {
try {
if (resultSet.next()) {
String ddl = resultSet.getString("ddl");
String comment = resultSet.getString("comments");
if (StringUtils.isNotBlank(comment)) {
return ddl +"\n"+ "COMMENT ON TABLE " + format(schemaName) + "." + format(tableName) +
" IS " + "'" + comment + "';";
}
return ddl;
StringBuilder ddlBuilder = new StringBuilder();
String tableDDLSql = String.format(sql, tableName, tableName, schemaName);
SQLExecutor.getInstance().execute(connection, tableDDLSql, resultSet -> {
if (resultSet.next()) {
String ddl = resultSet.getString("ddl");
String comment = resultSet.getString("comments");
if (StringUtils.isNotBlank(comment)) {
ddlBuilder.append(ddl).append("\n").append("COMMENT ON TABLE ").append(format(schemaName))
.append(".").append(format(tableName)).append(" IS ").append("'").append(comment).append("';");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return null;
});
String columnCommentsSql =String.format("select COLNAME,COMMENT$ from SYS.SYSCOLUMNCOMMENTS\n" +
"where SCHNAME = '%s' and TVNAME = '%s'and TABLE_TYPE = 'TABLE';", schemaName,tableName);
SQLExecutor.getInstance().execute(connection, columnCommentsSql, resultSet->{
while (resultSet.next()) {
String columnName = resultSet.getString("COLNAME");
String comment = resultSet.getString("COMMENT$");
ddlBuilder.append("COMMENT ON COLUMN ").append(format(schemaName)).append(".").append(format(tableName))
.append(".").append(format(columnName)).append(" IS ").append("'").append(comment).append("';").append("\n");
}
});
return ddlBuilder.toString();
}

private static String ROUTINES_SQL
Expand Down

0 comments on commit 7df622d

Please sign in to comment.