connection.getMetadata() column info duplicates fields across schemas/catalogs
@@ -149,8 +149,7 @@ public class DBSchemaParser {
List<String> catalogs = null;
try {
catalogs = listCatalogs();
- }
- catch (Exception e) {
+ } catch (Exception e) {
//ok to catch exception here
}
boolean hasCatalogs = catalogs != null && !catalogs.isEmpty();
@@ -182,7 +181,6 @@ public class DBSchemaParser {
} else {
-
try {
if (hasCatalogs) {
try (final Connection conn = KerberosUtil.getConnectionWithOrWithoutKerberos(ds, kerberosTicketConfiguration)) {
@@ -231,8 +229,7 @@ public class DBSchemaParser {
List<String> catalogs = null;
try {
catalogs = listCatalogs();
- }
- catch (Exception e) {
+ } catch (Exception e) {
//ok to catch exception here
}
boolean hasCatalogs = catalogs != null && !catalogs.isEmpty();
@@ -312,8 +309,16 @@ public class DBSchemaParser {
try (ResultSet rs = conn.getMetaData().getPrimaryKeys(null, schema, tableName)) {
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
+ String cat = rs.getString("TABLE_CAT");
+ if (StringUtils.isNotBlank(cat) && StringUtils.isNotBlank(schema)) {
+ //this db supports Catalogs. Ensure the cat matches the supplied schema
+ if (schema.equalsIgnoreCase(cat)) {
+ primaryKeys.add(columnName);
+ }
+ } else {
primaryKeys.add(columnName);
}
+ }
} catch (SQLException e) {
//attempt to use the catalog instead of the schema
try (ResultSet rs = conn.getMetaData().getPrimaryKeys(schema, null, tableName)) {
@@ -332,21 +337,29 @@ public class DBSchemaParser {
List<Field> fields;
Set<String> pkSet = listPrimaryKeys(conn, schema, tableName);
try (ResultSet columns = conn.getMetaData().getColumns(null, schema, tableName, null)) {
- fields = columnsResultSetToField(columns, pkSet);
+ fields = columnsResultSetToField(columns, pkSet, schema);
}
if (fields.isEmpty()) {
//if empty try the schema as the catalog (for MySQL db)
try (ResultSet columns = conn.getMetaData().getColumns(schema, null, tableName, null)) {
- fields = columnsResultSetToField(columns, pkSet);
+ fields = columnsResultSetToField(columns, pkSet, schema);
}
}
return fields;
}
- private List<Field> columnsResultSetToField(ResultSet columns, Set<String> pkSet) throws SQLException {
+ private List<Field> columnsResultSetToField(ResultSet columns, Set<String> pkSet, String schema) throws SQLException {
List<Field> fields = new Vector<>();
if (columns != null) {
while (columns.next()) {
+ String cat = columns.getString("TABLE_CAT");
+ if (StringUtils.isNotBlank(cat) && StringUtils.isNotBlank(schema)) {
+ //this db supports Catalogs. Ensure the cat matches the supplied schema
+ if (!schema.equalsIgnoreCase(cat)) {
+ continue;
+ }
+ }
+
DefaultField field = new DefaultField();
field.setName(columns.getString("COLUMN_NAME"));
Integer dataType = columns.getInt("DATA_TYPE");
@@ -368,5 +381,4 @@ public class DBSchemaParser {
}
-
}
