|
@@ -371,44 +371,71 @@ public class DataBase {
|
|
|
return names;
|
|
|
}
|
|
|
|
|
|
- public List<Map<String, String>> getColumnsByConnection(String connection, String catalog, String tableName) {
|
|
|
+ public List<Map<String, String>> getColumnsByConnection(String connection, String tableName) throws Exception {
|
|
|
|
|
|
ResultSet resultSet;
|
|
|
DatabaseMetaData metaData;
|
|
|
ArrayList<Map<String, String>> result = new ArrayList<>();
|
|
|
- try (Connection conn = getDataSourcePool(connection).getConnection()) {
|
|
|
- metaData = conn.getMetaData();
|
|
|
- resultSet = metaData.getColumns(catalog, null, tableName, null);
|
|
|
+ Connection conn = getDataSourcePool(connection).getConnection();
|
|
|
+ metaData = conn.getMetaData();
|
|
|
+
|
|
|
+ if(connection.contains("jdbc:kingbase") || connection.contains("jdbc:postgres")) {
|
|
|
+ resultSet = metaData.getColumns(null, "public", tableName, null);
|
|
|
+ }else if(connection.contains("jdbc:oracle") ) {
|
|
|
+ resultSet = metaData.getColumns(null, conn.getMetaData().getUserName(), tableName, null);
|
|
|
+ }else{
|
|
|
+ resultSet = metaData.getColumns(conn.getCatalog(), null, tableName, null);
|
|
|
+ }
|
|
|
|
|
|
- while (resultSet.next()) {
|
|
|
- HashMap<String, String> value = new HashMap<>();
|
|
|
- String columnName = resultSet.getString("COLUMN_NAME"); // 获取字段名
|
|
|
- value.put("name", columnName);
|
|
|
- String columnType = resultSet.getString("TYPE_NAME"); // 获取字段类型
|
|
|
- value.put("typeName", columnType);
|
|
|
- String remarks = resultSet.getString("REMARKS"); // 获取字段注释
|
|
|
- value.put("remarks", remarks);
|
|
|
- value.put("isPrimaryKey", resultSet.getString("IS_AUTOINCREMENT"));// 判断是否为主键
|
|
|
- result.add(value);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+// resultSet = metaData.getColumns(catalog, null, tableName, null);
|
|
|
+ ResultSet primaryKeys = metaData.getPrimaryKeys(conn.getCatalog(), null, tableName);
|
|
|
+ List<String> primaryKeyColumns = new ArrayList<>();
|
|
|
+ while (primaryKeys.next()) {
|
|
|
+ String name = primaryKeys.getString("COLUMN_NAME");
|
|
|
+ primaryKeyColumns.add(name);
|
|
|
}
|
|
|
+
|
|
|
+ ResultSet uniqueKeys = metaData.getIndexInfo(conn.getCatalog(), null, tableName, true, false);
|
|
|
+ List<String> uniqueKeyColumns = new ArrayList<>();
|
|
|
+ while (uniqueKeys.next()) {
|
|
|
+ String name = uniqueKeys.getString("COLUMN_NAME");
|
|
|
+ uniqueKeyColumns.add(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ while (resultSet.next()) {
|
|
|
+ HashMap<String, String> value = new HashMap<>();
|
|
|
+ String columnName = resultSet.getString("COLUMN_NAME"); // 获取字段名
|
|
|
+ value.put("name", columnName);
|
|
|
+ String columnType = resultSet.getString("TYPE_NAME"); // 获取字段类型
|
|
|
+ value.put("typeName", columnType);
|
|
|
+ String remarks = resultSet.getString("REMARKS"); // 获取字段注释
|
|
|
+ value.put("remarks", remarks);
|
|
|
+
|
|
|
+ value.put("isPrimaryKey", primaryKeyColumns.contains(columnName) ? "YES" : "NO");// 判断是否为主键
|
|
|
+ value.put("isUniqueKey", uniqueKeyColumns.contains(columnName) ? "YES" : "NO");// 判断是否为唯一键
|
|
|
+ result.add(value);
|
|
|
+ }
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public List<Map<String, String>> getColumnsByDataSourceId(String dataSourceId, String catalog, String tableName) throws Exception {
|
|
|
- return getColumnsByConnection(queryConnectionStr(dataSourceId), catalog, tableName);
|
|
|
+ public List<Map<String, String>> getColumnsByDataSourceId(String dataSourceId, String tableName) throws Exception {
|
|
|
+ return getColumnsByConnection(queryConnectionStr(dataSourceId), tableName);
|
|
|
}
|
|
|
|
|
|
- public List<String> getTablesByConnection(String connection, String catalog) {
|
|
|
+ public List<String> getTablesByConnection(String connection) {
|
|
|
ResultSet resultSet;
|
|
|
DatabaseMetaData metaData;
|
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
try (Connection conn = getDataSourcePool(connection).getConnection()) {
|
|
|
metaData = conn.getMetaData();
|
|
|
- resultSet = metaData.getTables(catalog, null, null, new String[]{"TABLE"});
|
|
|
-
|
|
|
+ if(connection.contains("jdbc:kingbase") || connection.contains("jdbc:postgres")) {
|
|
|
+ resultSet = metaData.getTables(null, "public", null, new String[]{"TABLE"});
|
|
|
+ }else if(connection.contains("jdbc:oracle") ) {
|
|
|
+ resultSet = metaData.getTables(null, conn.getMetaData().getUserName(), "%", new String[]{"TABLE"});
|
|
|
+ }else{
|
|
|
+ resultSet = metaData.getTables(conn.getCatalog(), null, null, new String[]{"TABLE"});
|
|
|
+ }
|
|
|
while (resultSet.next()) {
|
|
|
String tableName = resultSet.getString("TABLE_NAME"); // 获取字段名
|
|
|
|
|
@@ -420,8 +447,8 @@ public class DataBase {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public List<String> getTablesByDataSourceId(String dataSourceId, String catalog) throws Exception {
|
|
|
- return getTablesByConnection(queryConnectionStr(dataSourceId), catalog);
|
|
|
+ public List<String> getTablesByDataSourceId(String dataSourceId) throws Exception {
|
|
|
+ return getTablesByConnection(queryConnectionStr(dataSourceId));
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getResult(String connection, String sql, ResultSet resultSet) throws SQLException {
|