mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-30 19:22:58 +08:00
oracle column Type error fix
This commit is contained in:
@ -24,6 +24,7 @@ public class OracleDBManage extends DefaultDBManage implements DBManage {
|
|||||||
"FROM user_col_comments " +
|
"FROM user_col_comments " +
|
||||||
"WHERE table_name = '%s' " +
|
"WHERE table_name = '%s' " +
|
||||||
"AND comments IS NOT NULL";
|
"AND comments IS NOT NULL";
|
||||||
|
|
||||||
public void exportDatabase(Connection connection, String databaseName, String schemaName, AsyncContext asyncContext) throws SQLException {
|
public void exportDatabase(Connection connection, String databaseName, String schemaName, AsyncContext asyncContext) throws SQLException {
|
||||||
exportTables(connection, databaseName, schemaName, asyncContext);
|
exportTables(connection, databaseName, schemaName, asyncContext);
|
||||||
exportViews(connection, asyncContext, schemaName);
|
exportViews(connection, asyncContext, schemaName);
|
||||||
@ -159,11 +160,17 @@ public class OracleDBManage extends DefaultDBManage implements DBManage {
|
|||||||
public void copyTable(Connection connection, String databaseName, String schemaName, String tableName, String newTableName, boolean copyData) throws SQLException {
|
public void copyTable(Connection connection, String databaseName, String schemaName, String tableName, String newTableName, boolean copyData) throws SQLException {
|
||||||
String sql = "";
|
String sql = "";
|
||||||
if (copyData) {
|
if (copyData) {
|
||||||
sql = "CREATE TABLE " + newTableName + " AS SELECT * FROM " + tableName;
|
sql = "CREATE TABLE " + SqlUtils.quoteObjectName(newTableName) + " AS SELECT * FROM " + SqlUtils.quoteObjectName(tableName);
|
||||||
} else {
|
} else {
|
||||||
sql = "CREATE TABLE " + newTableName + " AS SELECT * FROM " + tableName + " WHERE 1=0";
|
sql = "CREATE TABLE " + SqlUtils.quoteObjectName(newTableName) + " AS SELECT * FROM " + SqlUtils.quoteObjectName(tableName) + " WHERE 1=0";
|
||||||
}
|
}
|
||||||
SQLExecutor.getInstance().execute(connection, sql, resultSet -> null);
|
SQLExecutor.getInstance().execute(connection, sql, resultSet -> null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dropTable(Connection connection, String databaseName, String schemaName, String tableName) {
|
||||||
|
String sql = "DROP TABLE " + SqlUtils.quoteObjectName(tableName);
|
||||||
|
SQLExecutor.getInstance().execute(connection, sql, (resultSet) -> null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import ai.chat2db.spi.util.SortUtils;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
@ -32,10 +33,10 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
|
|||||||
private List<String> systemSchemas = Arrays.asList("ANONYMOUS", "APEX_030200", "APEX_PUBLIC_USER", "APPQOSSYS", "BI", "CTXSYS", "DBSNMP", "DIP", "EXFSYS", "FLOWS_FILES", "HR", "IX", "MDDATA", "MDSYS", "MGMT_VIEW", "OE", "OLAPSYS", "ORACLE_OCM", "ORDDATA", "ORDPLUGINS", "ORDSYS", "OUTLN", "OWBSYS", "OWBSYS_AUDIT", "PM", "SCOTT", "SH", "SI_INFORMTN_SCHEMA", "SPATIAL_CSW_ADMIN_USR", "SPATIAL_WFS_ADMIN_USR", "SYS", "SYSMAN", "SYSTEM", "WMSYS", "XDB", "XS$NULL");
|
private List<String> systemSchemas = Arrays.asList("ANONYMOUS", "APEX_030200", "APEX_PUBLIC_USER", "APPQOSSYS", "BI", "CTXSYS", "DBSNMP", "DIP", "EXFSYS", "FLOWS_FILES", "HR", "IX", "MDDATA", "MDSYS", "MGMT_VIEW", "OE", "OLAPSYS", "ORACLE_OCM", "ORDDATA", "ORDPLUGINS", "ORDSYS", "OUTLN", "OWBSYS", "OWBSYS_AUDIT", "PM", "SCOTT", "SH", "SI_INFORMTN_SCHEMA", "SPATIAL_CSW_ADMIN_USR", "SPATIAL_WFS_ADMIN_USR", "SYS", "SYSMAN", "SYSTEM", "WMSYS", "XDB", "XS$NULL");
|
||||||
|
|
||||||
private String PROCEDURE_LIST_DDL = """
|
private String PROCEDURE_LIST_DDL = """
|
||||||
SELECT OBJECT_NAME, OBJECT_TYPE
|
SELECT OBJECT_NAME, OBJECT_TYPE
|
||||||
FROM ALL_OBJECTS
|
FROM ALL_OBJECTS
|
||||||
WHERE OBJECT_TYPE IN ('PROCEDURE')
|
WHERE OBJECT_TYPE IN ('PROCEDURE')
|
||||||
AND OWNER = '%s'""";
|
AND OWNER = '%s'""";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Procedure> procedures(Connection connection, String databaseName, String schemaName) {
|
public List<Procedure> procedures(Connection connection, String databaseName, String schemaName) {
|
||||||
@ -101,9 +102,25 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TableColumn> columns(Connection connection, String databaseName, String schemaName, String tableName) {
|
public List<TableColumn> columns(Connection connection, String databaseName, String schemaName, String tableName) {
|
||||||
|
List<TableColumn> tableColumns = super.columns(connection, databaseName, schemaName, tableName);
|
||||||
|
if (CollectionUtils.isNotEmpty(tableColumns)) {
|
||||||
|
Map<String, TableColumn> tableColumnMap = getTableColumns(connection, databaseName, schemaName, tableName);
|
||||||
|
for (TableColumn tableColumn : tableColumns) {
|
||||||
|
TableColumn column = tableColumnMap.get(tableColumn.getName());
|
||||||
|
tableColumn.setUnit(column.getUnit());
|
||||||
|
tableColumn.setComment(column.getComment());
|
||||||
|
tableColumn.setDefaultValue(column.getDefaultValue());
|
||||||
|
tableColumn.setOrdinalPosition(column.getOrdinalPosition());
|
||||||
|
tableColumn.setNullable(column.getNullable());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tableColumns;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, TableColumn> getTableColumns(Connection connection, String databaseName, String schemaName, String tableName) {
|
||||||
|
Map<String, TableColumn> tableColumns = new HashMap<>();
|
||||||
String sql = String.format(SELECT_TAB_COLS, schemaName, tableName);
|
String sql = String.format(SELECT_TAB_COLS, schemaName, tableName);
|
||||||
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
|
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
|
||||||
List<TableColumn> tableColumns = new ArrayList<>();
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
TableColumn tableColumn = new TableColumn();
|
TableColumn tableColumn = new TableColumn();
|
||||||
tableColumn.setTableName(tableName);
|
tableColumn.setTableName(tableName);
|
||||||
@ -151,10 +168,11 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
|
|||||||
} else if ("C".equalsIgnoreCase(charUsed)) {
|
} else if ("C".equalsIgnoreCase(charUsed)) {
|
||||||
tableColumn.setUnit("CHAR");
|
tableColumn.setUnit("CHAR");
|
||||||
}
|
}
|
||||||
tableColumns.add(tableColumn);
|
tableColumns.put(tableColumn.getName(), tableColumn);
|
||||||
}
|
}
|
||||||
return tableColumns;
|
return tableColumns;
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String ROUTINES_SQL
|
private static String ROUTINES_SQL
|
||||||
@ -197,11 +215,11 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
|
|||||||
String pkSql = String.format(SELECT_PK_SQL, schemaName, tableName);
|
String pkSql = String.format(SELECT_PK_SQL, schemaName, tableName);
|
||||||
Set<String> pkSet = new HashSet<>();
|
Set<String> pkSet = new HashSet<>();
|
||||||
SQLExecutor.getInstance().execute(connection, pkSql, resultSet -> {
|
SQLExecutor.getInstance().execute(connection, pkSql, resultSet -> {
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
pkSet.add(resultSet.getString("CONSTRAINT_NAME"));
|
pkSet.add(resultSet.getString("CONSTRAINT_NAME"));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
String sql = String.format(SELECT_TABLE_INDEX, schemaName, tableName);
|
String sql = String.format(SELECT_TABLE_INDEX, schemaName, tableName);
|
||||||
@ -264,17 +282,17 @@ public class OracleMetaData extends DefaultMetaService implements MetaData {
|
|||||||
public List<Trigger> triggers(Connection connection, String databaseName, String schemaName) {
|
public List<Trigger> triggers(Connection connection, String databaseName, String schemaName) {
|
||||||
List<Trigger> triggers = new ArrayList<>();
|
List<Trigger> triggers = new ArrayList<>();
|
||||||
return SQLExecutor.getInstance().execute(connection, String.format(TRIGGER_SQL_LIST, schemaName),
|
return SQLExecutor.getInstance().execute(connection, String.format(TRIGGER_SQL_LIST, schemaName),
|
||||||
resultSet -> {
|
resultSet -> {
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
String triggerName = resultSet.getString("TRIGGER_NAME");
|
String triggerName = resultSet.getString("TRIGGER_NAME");
|
||||||
Trigger trigger = new Trigger();
|
Trigger trigger = new Trigger();
|
||||||
trigger.setTriggerName(triggerName == null ? "" : triggerName.trim());
|
trigger.setTriggerName(triggerName == null ? "" : triggerName.trim());
|
||||||
trigger.setSchemaName(schemaName);
|
trigger.setSchemaName(schemaName);
|
||||||
trigger.setDatabaseName(databaseName);
|
trigger.setDatabaseName(databaseName);
|
||||||
triggers.add(trigger);
|
triggers.add(trigger);
|
||||||
}
|
}
|
||||||
return triggers;
|
return triggers;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String TRIGGER_DDL_SQL = "SELECT DBMS_METADATA.GET_DDL('TRIGGER', '%s', '%s') as ddl FROM DUAL";
|
private static final String TRIGGER_DDL_SQL = "SELECT DBMS_METADATA.GET_DDL('TRIGGER', '%s', '%s') as ddl FROM DUAL";
|
||||||
|
@ -241,4 +241,15 @@ public class EasyStringUtils {
|
|||||||
// .replace("\r", "\\r");
|
// .replace("\r", "\\r");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String sqlEscape(String str) {
|
||||||
|
if (StringUtils.isBlank(str)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
str = str.trim();
|
||||||
|
if (str.endsWith(";")) {
|
||||||
|
str = str.substring(0, str.length() - 1);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user