@ParameterizedTest @ArgumentsSource(TestCaseArgumentsProvider.class) @NullValue("NULL") @TestCase({"NULL", "NULL", "FRED,BOB,JOE", "FRED.REFA,BOB.REFB,BOB.REFC", "FRED.REFA,BOB.REFB,BOB.REFC"}) @TestCase({"NULL", "NULL", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC"}) @TestCase({"NULL", "NULL", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC"}) @TestCase({"NULL", "NULL", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"A", "NULL", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", "A..FRED.REFA"}) @TestCase({"B", "NULL", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", "B..BOB.REFB,B..BOB.REFC"}) @TestCase({"C", "NULL", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", ""}) @TestCase({"A", "", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", "A..FRED.REFA"}) @TestCase({"B", "", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", "B..BOB.REFB,B..BOB.REFC"}) @TestCase({"C", "", "A..FRED,B..BOB,C..JOE", "A..FRED.REFA,B..BOB.REFB,B..BOB.REFC", ""}) @TestCase({"A", "NULL", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA"}) @TestCase({"B", "NULL", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"C", "NULL", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", ""}) @TestCase({"A", "", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA"}) @TestCase({"B", "", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"C", "", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", ""}) @TestCase({"NULL", "X", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", "X.FRED.REFA"}) @TestCase({"NULL", "Y", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", "Y.BOB.REFB,Y.BOB.REFC"}) @TestCase({"NULL", "Z", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", ""}) @TestCase({"", "X", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", "X.FRED.REFA"}) @TestCase({"", "Y", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", "Y.BOB.REFB,Y.BOB.REFC"}) @TestCase({"", "Z", "X.FRED,Y.BOB,Z.JOE", "X.FRED.REFA,Y.BOB.REFB,Y.BOB.REFC", ""}) @TestCase({"NULL", "X", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA"}) @TestCase({"NULL", "Y", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"NULL", "Z", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", ""}) @TestCase({"", "X", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA"}) @TestCase({"", "Y", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"", "Z", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", ""}) @TestCase({"A", "X", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "A.X.FRED.REFA"}) @TestCase({"B", "Y", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", "B.Y.BOB.REFB,B.Y.BOB.REFC"}) @TestCase({"C", "Z", "A.X.FRED,B.Y.BOB,C.Z.JOE", "A.X.FRED.REFA,B.Y.BOB.REFB,B.Y.BOB.REFC", ""}) void foreignKeyNames(String catalog, String schema, String[] actualTableNames, String[] actualFks, String[] expectedFks) throws SQLException { whenGetTablesThen(actualTableNames); whenGetImportedKeysThen(actualTableNames, actualFks); Stream<ForeignKeyName> result = DatabaseMetaDataUtil.foreignKeyNames(metaData, catalog, schema); ForeignKeyName[] rStream = Arrays.stream(expectedFks) .filter(StringUtils::isNotBlank) .map(ForeignKeyName::parseString) .toArray(ForeignKeyName[]::new); assertThat(result, contains(rStream)); }
public static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema) { List<QualifiedName> tableNames = tableNames(metaData, catalog, schema).collect(toList()); return tableNames .stream() .flatMap(table -> ResultSetUtil.stream( () -> metaData.getImportedKeys(table.catalog().orElse(null), table.schema().orElse(null), table.name().orElse(null)), rs -> new ForeignKeyName( new QualifiedName(getString(rs, "FKTABLE_CAT"), getString(rs, "FKTABLE_SCHEM"), getString(rs, "FKTABLE_NAME")), getString(rs, "FK_NAME"))) .distinct() ); }
DatabaseMetaDataUtil extends UtilityClass { public static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema) { List<QualifiedName> tableNames = tableNames(metaData, catalog, schema).collect(toList()); return tableNames .stream() .flatMap(table -> ResultSetUtil.stream( () -> metaData.getImportedKeys(table.catalog().orElse(null), table.schema().orElse(null), table.name().orElse(null)), rs -> new ForeignKeyName( new QualifiedName(getString(rs, "FKTABLE_CAT"), getString(rs, "FKTABLE_SCHEM"), getString(rs, "FKTABLE_NAME")), getString(rs, "FK_NAME"))) .distinct() ); } }
DatabaseMetaDataUtil extends UtilityClass { public static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema) { List<QualifiedName> tableNames = tableNames(metaData, catalog, schema).collect(toList()); return tableNames .stream() .flatMap(table -> ResultSetUtil.stream( () -> metaData.getImportedKeys(table.catalog().orElse(null), table.schema().orElse(null), table.name().orElse(null)), rs -> new ForeignKeyName( new QualifiedName(getString(rs, "FKTABLE_CAT"), getString(rs, "FKTABLE_SCHEM"), getString(rs, "FKTABLE_NAME")), getString(rs, "FK_NAME"))) .distinct() ); }  }
DatabaseMetaDataUtil extends UtilityClass { public static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema) { List<QualifiedName> tableNames = tableNames(metaData, catalog, schema).collect(toList()); return tableNames .stream() .flatMap(table -> ResultSetUtil.stream( () -> metaData.getImportedKeys(table.catalog().orElse(null), table.schema().orElse(null), table.name().orElse(null)), rs -> new ForeignKeyName( new QualifiedName(getString(rs, "FKTABLE_CAT"), getString(rs, "FKTABLE_SCHEM"), getString(rs, "FKTABLE_NAME")), getString(rs, "FK_NAME"))) .distinct() ); }  static boolean tableExists(DatabaseMetaData metaData, String catalog, String schema, String tableName); static Stream<QualifiedName> tableNames(DatabaseMetaData metaData, String catalog, String schema); static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema); }
DatabaseMetaDataUtil extends UtilityClass { public static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema) { List<QualifiedName> tableNames = tableNames(metaData, catalog, schema).collect(toList()); return tableNames .stream() .flatMap(table -> ResultSetUtil.stream( () -> metaData.getImportedKeys(table.catalog().orElse(null), table.schema().orElse(null), table.name().orElse(null)), rs -> new ForeignKeyName( new QualifiedName(getString(rs, "FKTABLE_CAT"), getString(rs, "FKTABLE_SCHEM"), getString(rs, "FKTABLE_NAME")), getString(rs, "FK_NAME"))) .distinct() ); }  static boolean tableExists(DatabaseMetaData metaData, String catalog, String schema, String tableName); static Stream<QualifiedName> tableNames(DatabaseMetaData metaData, String catalog, String schema); static Stream<ForeignKeyName> foreignKeyNames(DatabaseMetaData metaData, String catalog, String schema);  }