@Override public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { return metaData.getImportedKeys(catalog, schema, table); }
private void initForeignKeys(DatabaseMetaData meta) throws SQLException { ResultSet rs = null; try { rs = meta.getImportedKeys( catalog, schema, name ); while ( rs.next() ) { addForeignKey( rs ); } } finally { if ( rs != null ) { rs.close(); } } }
@Test public void testGetImportedKeysReturnsNonNull() throws SQLException { assertThat( dbmd.getImportedKeys( null, null, "%" ), notNullValue() ); } // TODO: Later, test more (e.g., right columns (even if/though zero rows)).
conn.getMetaData().getImportedKeys(null, null, table.getId());
-> connection.getMetaData().getImportedKeys( jdbcCatalogLookupName(schema), jdbcSchemaLookupName(schema),
ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getImportedKeys( catalogFilter, schemaFilter,
@Override public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { return metaData.getImportedKeys(catalog, schema, table); }
/** * Check that lookup in the metadata have been performed using specified catalog name (that is neither {@code null} * nor correct catalog name), empty result set is returned. * * @param invalidCat catalog name that is not either */ private void checkNoEntitiesFoundForCatalog(String invalidCat) throws SQLException { try (Connection conn = DriverManager.getConnection(BASE_URL)) { DatabaseMetaData meta = conn.getMetaData(); // Intention: we set the other arguments that way, the values to have as many results as possible. assertIsEmpty(meta.getTables(invalidCat, null, "%", new String[] {"TABLE"})); assertIsEmpty(meta.getColumns(invalidCat, null, "%", "%")); assertIsEmpty(meta.getColumnPrivileges(invalidCat, "pers", "PERSON", "%")); assertIsEmpty(meta.getTablePrivileges(invalidCat, null, "%")); assertIsEmpty(meta.getPrimaryKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getImportedKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getExportedKeys(invalidCat, "pers", "PERSON")); // meta.getCrossReference(...) doesn't make sense because we don't have FK constraint. assertIsEmpty(meta.getIndexInfo(invalidCat, null, "%", false, true)); assertIsEmpty(meta.getSuperTables(invalidCat, "%", "%")); assertIsEmpty(meta.getSchemas(invalidCat, null)); assertIsEmpty(meta.getPseudoColumns(invalidCat, null, "%", "")); } }
/** * Check that lookup in the metadata have been performed using specified catalog name (that is neither {@code null} * nor correct catalog name), empty result set is returned. * * @param invalidCat catalog name that is not either */ private void checkNoEntitiesFoundForCatalog(String invalidCat) throws SQLException { try (Connection conn = DriverManager.getConnection(URL)) { DatabaseMetaData meta = conn.getMetaData(); // Intention: we set the other arguments that way, the values to have as many results as possible. assertIsEmpty(meta.getTables(invalidCat, null, "%", new String[] {"TABLE"})); assertIsEmpty(meta.getColumns(invalidCat, null, "%", "%")); assertIsEmpty(meta.getColumnPrivileges(invalidCat, "pers", "PERSON", "%")); assertIsEmpty(meta.getTablePrivileges(invalidCat, null, "%")); assertIsEmpty(meta.getPrimaryKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getImportedKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getExportedKeys(invalidCat, "pers", "PERSON")); // meta.getCrossReference(...) doesn't make sense because we don't have FK constraint. assertIsEmpty(meta.getIndexInfo(invalidCat, null, "%", false, true)); assertIsEmpty(meta.getSuperTables(invalidCat, "%", "%")); assertIsEmpty(meta.getSchemas(invalidCat, null)); assertIsEmpty(meta.getPseudoColumns(invalidCat, null, "%", "")); } }
} else if (isBuiltIn(sql, "@imported_keys")) { String[] p = split(sql); return meta.getImportedKeys(p[1], p[2], p[3]); } else if (isBuiltIn(sql, "@index_info")) { String[] p = split(sql);
md.getImportedKeys(null, dbs.name, tbname); try { if (rs_fks.next()) {
/** * Connect to the PK's referenced by this table that live in the original schema * @throws SQLException */ private void connectForeignKeysRemoteTable(Database db, RemoteTable remoteTable, Map<String, Table> tables) throws SQLException { LOGGER.trace("Connecting foreign keys to {}", remoteTable.getFullName()); try (ResultSet rs = sqlService.getDatabaseMetaData().getImportedKeys(remoteTable.getCatalog(), remoteTable.getSchema(), remoteTable.getName())){ // get remote table's FKs that reference PKs in our schema while (rs.next()) { String otherSchema = rs.getString("PKTABLE_SCHEM"); String otherCatalog = rs.getString("PKTABLE_CAT"); // if it points back to our schema then use it if (remoteTable.getBaseContainer().equals(otherSchema) || remoteTable.getBaseContainer().equals(otherCatalog)) { addForeignKey(db, remoteTable, rs.getString("FK_NAME"), rs.getString("FKCOLUMN_NAME"), otherCatalog, otherSchema, rs.getString("PKTABLE_NAME"), rs.getString("PKCOLUMN_NAME"), rs.getInt("UPDATE_RULE"), rs.getInt("DELETE_RULE"), tables); } } } catch (SQLException sqlExc) { if (!remoteTable.isLogical()) { // if explicitly asking for these details then propagate the exception if (Config.getInstance().isOneOfMultipleSchemas()) throw sqlExc; // otherwise just report the fact that we tried & couldn't LOGGER.warn("Couldn't resolve foreign keys for remote table '{}'", remoteTable.getFullName(), sqlExc); } } }
try (ResultSet rs = sqlService.getDatabaseMetaData().getImportedKeys(table.getCatalog(), table.getSchema(), table.getName())) {
private void retrieveForeignKeysFromMetadataForAllTables() throws SQLException { final NamedObjectList<MutableForeignKey> foreignKeys = new NamedObjectList<>(); final DatabaseMetaData metaData = getMetaData(); // Get imported foreign keys try (final MetadataResultSet results = new MetadataResultSet(metaData .getImportedKeys(null, null, "%"));) { createForeignKeys(results, foreignKeys); } // We need to get exported keys as well, since if only a single // table is selected, we have not retrieved it's keys that are // imported by other tables. try (final MetadataResultSet results = new MetadataResultSet(metaData .getExportedKeys(null, null, "%"));) { createForeignKeys(results, foreignKeys); } }
private void retrieveForeignKeysFromMetadataForAllTables() throws SQLException { final NamedObjectList<MutableForeignKey> foreignKeys = new NamedObjectList<>(); final DatabaseMetaData metaData = getMetaData(); // Get imported foreign keys try (final MetadataResultSet results = new MetadataResultSet(metaData .getImportedKeys(null, null, "%"));) { createForeignKeys(results, foreignKeys); } // We need to get exported keys as well, since if only a single // table is selected, we have not retrieved it's keys that are // imported by other tables. try (final MetadataResultSet results = new MetadataResultSet(metaData .getExportedKeys(null, null, "%"));) { createForeignKeys(results, foreignKeys); } }
private static void printForeignKeys(Connection connection, String tableName) throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); ResultSet foreignKeys = metaData.getImportedKeys(connection.getCatalog(), null, tableName); while (foreignKeys.next()) { String fkTableName = foreignKeys.getString("FKTABLE_NAME"); String fkColumnName = foreignKeys.getString("FKCOLUMN_NAME"); String pkTableName = foreignKeys.getString("PKTABLE_NAME"); String pkColumnName = foreignKeys.getString("PKCOLUMN_NAME"); System.out.println(fkTableName + "." + fkColumnName + " -> " + pkTableName + "." + pkColumnName); } }
.getImportedKeys(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName()));)
.getImportedKeys(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName()));)
@Override public ResultSet getImportedKeys(final String catalog, final String schema, final String table) throws SQLException { connection.checkOpen(); try { return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getImportedKeys(catalog, schema, table)); } catch (final SQLException e) { handleException(e); throw new AssertionError(); } }
@Test public void testGetImportedKeysReturnsNonNull() throws SQLException { assertThat( dbmd.getImportedKeys( null, null, "%" ), notNullValue() ); } // TODO: Later, test more (e.g., right columns (even if/though zero rows)).