mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-07 04:37:48 +00:00
fix bug: oracle主键与PostgreSQL表的DDL
This commit is contained in:
@@ -19,7 +19,10 @@ import com.gitee.dbswitch.core.model.ColumnMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 支持Greenplum数据库的元信息实现
|
||||
@@ -28,10 +31,18 @@ import java.util.List;
|
||||
*/
|
||||
public class DatabaseGreenplumImpl extends AbstractDatabase implements IDatabaseInterface {
|
||||
|
||||
private static Set<String> systemSchemas = new HashSet<>();
|
||||
|
||||
private static final String SHOW_CREATE_VIEW_SQL =
|
||||
"SELECT pg_get_viewdef((select pg_class.relfilenode from pg_catalog.pg_class \n"
|
||||
+ "join pg_catalog.pg_namespace on pg_class.relnamespace = pg_namespace.oid \n"
|
||||
+ "where pg_namespace.nspname='%s' and pg_class.relname ='%s'),true) ";
|
||||
"select pg_get_viewdef('\"%s\".\"%s\"', true)";
|
||||
|
||||
static {
|
||||
systemSchemas.add("gp_toolkit");
|
||||
systemSchemas.add("pg_aoseg");
|
||||
systemSchemas.add("information_schema");
|
||||
systemSchemas.add("pg_catalog");
|
||||
systemSchemas.add("pg_bitmapindex");
|
||||
}
|
||||
|
||||
public DatabaseGreenplumImpl() {
|
||||
super("com.pivotal.jdbc.GreenplumDriver");
|
||||
@@ -42,6 +53,14 @@ public class DatabaseGreenplumImpl extends AbstractDatabase implements IDatabase
|
||||
return DatabaseTypeEnum.GREENPLUM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> querySchemaList() {
|
||||
List<String> schemas = super.querySchemaList();
|
||||
return schemas.stream()
|
||||
.filter(s -> !systemSchemas.contains(s))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableDDL(String schemaName, String tableName) {
|
||||
String sql = PostgresqlConst.CREATE_TABLE_SQL_TPL
|
||||
@@ -56,7 +75,9 @@ public class DatabaseGreenplumImpl extends AbstractDatabase implements IDatabase
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
//throw new RuntimeException(e);
|
||||
// TODO: Greenplum的表的DDL获取方法
|
||||
return e.getMessage();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@@ -128,10 +128,10 @@ public class DatabaseOracleImpl extends AbstractDatabase implements IDatabaseInt
|
||||
// 使主键失效:alter table tableName disable primary key;
|
||||
// 使主键恢复:alter table tableName enable primary key;
|
||||
Set<String> ret = new HashSet<>();
|
||||
String sql = String.format("SELECT COLUMN_NAME FROM user_cons_columns "
|
||||
+ "WHERE owner='%s' and constraint_name = "
|
||||
+ "(SELECT constraint_name FROM user_constraints WHERE table_name = '%s' "
|
||||
+ "AND constraint_type = 'P' and STATUS='ENABLED') ",
|
||||
String sql = String.format(
|
||||
"SELECT col.COLUMN_NAME FROM all_cons_columns col INNER JOIN all_constraints con \n"
|
||||
+ "ON col.constraint_name=con.constraint_name AND col.OWNER =con.OWNER AND col.TABLE_NAME =con.TABLE_NAME \n"
|
||||
+ "WHERE con.constraint_type = 'P' and con.STATUS='ENABLED' and con.owner='%s' AND con.table_name='%s'",
|
||||
schemaName, tableName);
|
||||
try (PreparedStatement ps = this.connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
@@ -20,7 +20,10 @@ import com.gitee.dbswitch.core.util.DDLFormatterUtils;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 支持PostgreSQL数据库的元信息实现
|
||||
@@ -29,10 +32,21 @@ import java.util.List;
|
||||
*/
|
||||
public class DatabasePostgresImpl extends AbstractDatabase implements IDatabaseInterface {
|
||||
|
||||
private static final String SHOW_CREATE_VIEW_SQL =
|
||||
private static Set<String> systemSchemas = new HashSet<>();
|
||||
|
||||
private static final String SHOW_CREATE_VIEW_SQL_1 =
|
||||
"SELECT pg_get_viewdef((select pg_class.relfilenode from pg_catalog.pg_class \n"
|
||||
+ "join pg_catalog.pg_namespace on pg_class.relnamespace = pg_namespace.oid \n"
|
||||
+ "where pg_namespace.nspname='%s' and pg_class.relname ='%s'),true) ";
|
||||
private static final String SHOW_CREATE_VIEW_SQL_2 =
|
||||
"select pg_get_viewdef('\"%s\".\"%s\"', true)";
|
||||
|
||||
static {
|
||||
systemSchemas.add("pg_aoseg");
|
||||
systemSchemas.add("information_schema");
|
||||
systemSchemas.add("pg_catalog");
|
||||
systemSchemas.add("pg_bitmapindex");
|
||||
}
|
||||
|
||||
public DatabasePostgresImpl() {
|
||||
super("org.postgresql.Driver");
|
||||
@@ -43,6 +57,14 @@ public class DatabasePostgresImpl extends AbstractDatabase implements IDatabaseI
|
||||
return DatabaseTypeEnum.POSTGRESQL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> querySchemaList() {
|
||||
List<String> schemas = super.querySchemaList();
|
||||
return schemas.stream()
|
||||
.filter(s -> !systemSchemas.contains(s))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableDDL(String schemaName, String tableName) {
|
||||
String sql = PostgresqlConst.CREATE_TABLE_SQL_TPL
|
||||
@@ -57,7 +79,9 @@ public class DatabasePostgresImpl extends AbstractDatabase implements IDatabaseI
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
//throw new RuntimeException(e);
|
||||
// 低版本的PostgreSQL的表的DDL获取方法
|
||||
return e.getMessage();
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -65,12 +89,23 @@ public class DatabasePostgresImpl extends AbstractDatabase implements IDatabaseI
|
||||
|
||||
@Override
|
||||
public String getViewDDL(String schemaName, String tableName) {
|
||||
String sql = String.format(SHOW_CREATE_VIEW_SQL, schemaName, tableName);
|
||||
String sql = String.format(SHOW_CREATE_VIEW_SQL_1, schemaName, tableName);
|
||||
try (Statement st = connection.createStatement()) {
|
||||
if (st.execute(sql)) {
|
||||
try (ResultSet rs = st.getResultSet()) {
|
||||
if (rs != null && rs.next()) {
|
||||
return rs.getString(1);
|
||||
try {
|
||||
if (st.execute(sql)) {
|
||||
try (ResultSet rs = st.getResultSet()) {
|
||||
if (rs != null && rs.next()) {
|
||||
return rs.getString(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException se) {
|
||||
String.format(SHOW_CREATE_VIEW_SQL_2, schemaName, tableName);
|
||||
if (st.execute(sql)) {
|
||||
try (ResultSet rs = st.getResultSet()) {
|
||||
if (rs != null && rs.next()) {
|
||||
return rs.getString(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user