mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-01 06:16:04 +08:00
Merge pull request #1210 from openai0229/fix-dm-table-ddl-missing-comment
fix-dm-table-ddl-missing-comment
This commit is contained in:
@ -1,11 +1,5 @@
|
|||||||
package ai.chat2db.plugin.dm;
|
package ai.chat2db.plugin.dm;
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import ai.chat2db.plugin.dm.builder.DMSqlBuilder;
|
import ai.chat2db.plugin.dm.builder.DMSqlBuilder;
|
||||||
import ai.chat2db.plugin.dm.type.DMColumnTypeEnum;
|
import ai.chat2db.plugin.dm.type.DMColumnTypeEnum;
|
||||||
import ai.chat2db.plugin.dm.type.DMDefaultValueEnum;
|
import ai.chat2db.plugin.dm.type.DMDefaultValueEnum;
|
||||||
@ -16,11 +10,16 @@ import ai.chat2db.spi.jdbc.DefaultMetaService;
|
|||||||
import ai.chat2db.spi.model.*;
|
import ai.chat2db.spi.model.*;
|
||||||
import ai.chat2db.spi.sql.SQLExecutor;
|
import ai.chat2db.spi.sql.SQLExecutor;
|
||||||
import ai.chat2db.spi.util.SortUtils;
|
import ai.chat2db.spi.util.SortUtils;
|
||||||
import ai.chat2db.spi.util.SqlUtils;
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DMMetaData extends DefaultMetaService implements MetaData {
|
public class DMMetaData extends DefaultMetaService implements MetaData {
|
||||||
|
|
||||||
private List<String> systemSchemas = Arrays.asList("CTISYS", "SYS","SYSDBA","SYSSSO","SYSAUDITOR");
|
private List<String> systemSchemas = Arrays.asList("CTISYS", "SYS","SYSDBA","SYSSSO","SYSAUDITOR");
|
||||||
@ -30,15 +29,28 @@ public class DMMetaData extends DefaultMetaService implements MetaData {
|
|||||||
List<Schema> schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null);
|
List<Schema> schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null);
|
||||||
return SortUtils.sortSchema(schemas, systemSchemas);
|
return SortUtils.sortSchema(schemas, systemSchemas);
|
||||||
}
|
}
|
||||||
|
private String format(String tableName){
|
||||||
|
return "\"" + tableName + "\"";
|
||||||
|
}
|
||||||
|
|
||||||
public String tableDDL(Connection connection, String databaseName, String schemaName, String tableName) {
|
public String tableDDL(Connection connection, String databaseName, String schemaName, String tableName) {
|
||||||
String selectObjectDDLSQL = String.format(
|
String sql = """
|
||||||
"select dbms_metadata.get_ddl(%s, %s, %s) AS \"sql\" from dual",
|
SELECT
|
||||||
SqlUtils.formatSQLString("TABLE"), SqlUtils.formatSQLString(tableName),
|
(SELECT comments FROM user_tab_comments WHERE table_name = '%s') AS comments,
|
||||||
SqlUtils.formatSQLString(schemaName));
|
(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 -> {
|
return SQLExecutor.getInstance().execute(connection, selectObjectDDLSQL, resultSet -> {
|
||||||
try {
|
try {
|
||||||
if (resultSet.next()) {
|
if (resultSet.next()) {
|
||||||
return resultSet.getString("sql");
|
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;
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
Reference in New Issue
Block a user