@Test public void testGetDbForeignKeyMetaList() throws Exception { final MockResultSet resultSet = new MockResultSet(); ArrayMap rowData = new ArrayMap(); rowData.put("PKTABLE_CAT", "dept_catalog"); rowData.put("PKTABLE_SCHEM", "dept_schema"); rowData.put("PKTABLE_NAME", "dept"); rowData.put("PKCOLUMN_NAME", "dept_no"); rowData.put("FKCOLUMN_NAME", "dept_no_fk"); rowData.put("FK_NAME", "emp_fk1"); resultSet.addRowData(rowData); rowData = new ArrayMap(); rowData.put("PKTABLE_CAT", "dept_catalog"); rowData.put("PKTABLE_SCHEM", "dept_schema"); rowData.put("PKTABLE_NAME", "dept"); rowData.put("PKCOLUMN_NAME", "dept_name"); rowData.put("FKCOLUMN_NAME", "dept_name_fk"); rowData.put("FK_NAME", "emp_fk1"); resultSet.addRowData(rowData); rowData = new ArrayMap(); rowData.put("PKTABLE_CAT", "address_catalog"); rowData.put("PKTABLE_SCHEM", "address_schema"); rowData.put("PKTABLE_NAME", "address"); rowData.put("PKCOLUMN_NAME", "address_name"); rowData.put("FKCOLUMN_NAME", "address_name_fk"); rowData.put("FK_NAME", "emp_fk2"); resultSet.addRowData(rowData); GenMockDatabaseMetaData metaData = new GenMockDatabaseMetaData() { @Override public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { return resultSet; } }; DbTableMetaReaderImpl reader = new DbTableMetaReaderImpl( new MockDataSource(), new StandardGenDialect(), null, ".*", "", false); List<DbForeignKeyMeta> list = reader.getDbForeignKeyMetaList(metaData, new DbTableMeta()); assertEquals(2, list.size()); DbForeignKeyMeta fkMeta = list.get(0); assertEquals("emp_fk1", fkMeta.getName()); assertEquals("dept_catalog", fkMeta.getPrimaryKeyCatalogName()); assertEquals("dept_schema", fkMeta.getPrimaryKeySchemaName()); assertEquals("dept", fkMeta.getPrimaryKeyTableName()); assertEquals(2, fkMeta.getPrimaryKeyColumnNameList().size()); assertEquals(Arrays.asList("dept_no", "dept_name"), fkMeta .getPrimaryKeyColumnNameList()); assertEquals(2, fkMeta.getForeignKeyColumnNameList().size()); assertEquals(Arrays.asList("dept_no_fk", "dept_name_fk"), fkMeta .getForeignKeyColumnNameList()); fkMeta = list.get(1); assertEquals("emp_fk2", fkMeta.getName()); assertEquals("address_catalog", fkMeta.getPrimaryKeyCatalogName()); assertEquals("address_schema", fkMeta.getPrimaryKeySchemaName()); assertEquals("address", fkMeta.getPrimaryKeyTableName()); assertEquals(1, fkMeta.getPrimaryKeyColumnNameList().size()); assertEquals(Arrays.asList("address_name"), fkMeta .getPrimaryKeyColumnNameList()); assertEquals(1, fkMeta.getForeignKeyColumnNameList().size()); assertEquals(Arrays.asList("address_name_fk"), fkMeta .getForeignKeyColumnNameList()); }
protected List<DbForeignKeyMeta> getDbForeignKeyMetaList( DatabaseMetaData metaData, DbTableMeta tableMeta) { @SuppressWarnings("unchecked") Map<String, DbForeignKeyMeta> map = new ArrayMap(); try { ResultSet rs = metaData.getImportedKeys(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName()); try { while (rs.next()) { String name = rs.getString("FK_NAME"); if (!map.containsKey(name)) { DbForeignKeyMeta fkMeta = new DbForeignKeyMeta(); fkMeta.setName(name); fkMeta.setPrimaryKeyCatalogName(rs .getString("PKTABLE_CAT")); fkMeta.setPrimaryKeySchemaName(rs .getString("PKTABLE_SCHEM")); fkMeta.setPrimaryKeyTableName(rs .getString("PKTABLE_NAME")); map.put(name, fkMeta); } DbForeignKeyMeta fkMeta = map.get(name); fkMeta.addPrimaryKeyColumnName(rs .getString("PKCOLUMN_NAME")); fkMeta.addForeignKeyColumnName(rs .getString("FKCOLUMN_NAME")); } } finally { ResultSetUtil.close(rs); } DbForeignKeyMeta[] array = map.values().toArray( new DbForeignKeyMeta[map.size()]); return Arrays.asList(array); } catch (SQLException ex) { throw new SQLRuntimeException(ex); } }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbForeignKeyMeta> getDbForeignKeyMetaList( DatabaseMetaData metaData, DbTableMeta tableMeta) { @SuppressWarnings("unchecked") Map<String, DbForeignKeyMeta> map = new ArrayMap(); try { ResultSet rs = metaData.getImportedKeys(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName()); try { while (rs.next()) { String name = rs.getString("FK_NAME"); if (!map.containsKey(name)) { DbForeignKeyMeta fkMeta = new DbForeignKeyMeta(); fkMeta.setName(name); fkMeta.setPrimaryKeyCatalogName(rs .getString("PKTABLE_CAT")); fkMeta.setPrimaryKeySchemaName(rs .getString("PKTABLE_SCHEM")); fkMeta.setPrimaryKeyTableName(rs .getString("PKTABLE_NAME")); map.put(name, fkMeta); } DbForeignKeyMeta fkMeta = map.get(name); fkMeta.addPrimaryKeyColumnName(rs .getString("PKCOLUMN_NAME")); fkMeta.addForeignKeyColumnName(rs .getString("FKCOLUMN_NAME")); } } finally { ResultSetUtil.close(rs); } DbForeignKeyMeta[] array = map.values().toArray( new DbForeignKeyMeta[map.size()]); return Arrays.asList(array); } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbForeignKeyMeta> getDbForeignKeyMetaList( DatabaseMetaData metaData, DbTableMeta tableMeta) { @SuppressWarnings("unchecked") Map<String, DbForeignKeyMeta> map = new ArrayMap(); try { ResultSet rs = metaData.getImportedKeys(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName()); try { while (rs.next()) { String name = rs.getString("FK_NAME"); if (!map.containsKey(name)) { DbForeignKeyMeta fkMeta = new DbForeignKeyMeta(); fkMeta.setName(name); fkMeta.setPrimaryKeyCatalogName(rs .getString("PKTABLE_CAT")); fkMeta.setPrimaryKeySchemaName(rs .getString("PKTABLE_SCHEM")); fkMeta.setPrimaryKeyTableName(rs .getString("PKTABLE_NAME")); map.put(name, fkMeta); } DbForeignKeyMeta fkMeta = map.get(name); fkMeta.addPrimaryKeyColumnName(rs .getString("PKCOLUMN_NAME")); fkMeta.addForeignKeyColumnName(rs .getString("FKCOLUMN_NAME")); } } finally { ResultSetUtil.close(rs); } DbForeignKeyMeta[] array = map.values().toArray( new DbForeignKeyMeta[map.size()]); return Arrays.asList(array); } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbForeignKeyMeta> getDbForeignKeyMetaList( DatabaseMetaData metaData, DbTableMeta tableMeta) { @SuppressWarnings("unchecked") Map<String, DbForeignKeyMeta> map = new ArrayMap(); try { ResultSet rs = metaData.getImportedKeys(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName()); try { while (rs.next()) { String name = rs.getString("FK_NAME"); if (!map.containsKey(name)) { DbForeignKeyMeta fkMeta = new DbForeignKeyMeta(); fkMeta.setName(name); fkMeta.setPrimaryKeyCatalogName(rs .getString("PKTABLE_CAT")); fkMeta.setPrimaryKeySchemaName(rs .getString("PKTABLE_SCHEM")); fkMeta.setPrimaryKeyTableName(rs .getString("PKTABLE_NAME")); map.put(name, fkMeta); } DbForeignKeyMeta fkMeta = map.get(name); fkMeta.addPrimaryKeyColumnName(rs .getString("PKCOLUMN_NAME")); fkMeta.addForeignKeyColumnName(rs .getString("FKCOLUMN_NAME")); } } finally { ResultSetUtil.close(rs); } DbForeignKeyMeta[] array = map.values().toArray( new DbForeignKeyMeta[map.size()]); return Arrays.asList(array); } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); List<DbTableMeta> read(); }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbForeignKeyMeta> getDbForeignKeyMetaList( DatabaseMetaData metaData, DbTableMeta tableMeta) { @SuppressWarnings("unchecked") Map<String, DbForeignKeyMeta> map = new ArrayMap(); try { ResultSet rs = metaData.getImportedKeys(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName()); try { while (rs.next()) { String name = rs.getString("FK_NAME"); if (!map.containsKey(name)) { DbForeignKeyMeta fkMeta = new DbForeignKeyMeta(); fkMeta.setName(name); fkMeta.setPrimaryKeyCatalogName(rs .getString("PKTABLE_CAT")); fkMeta.setPrimaryKeySchemaName(rs .getString("PKTABLE_SCHEM")); fkMeta.setPrimaryKeyTableName(rs .getString("PKTABLE_NAME")); map.put(name, fkMeta); } DbForeignKeyMeta fkMeta = map.get(name); fkMeta.addPrimaryKeyColumnName(rs .getString("PKCOLUMN_NAME")); fkMeta.addForeignKeyColumnName(rs .getString("FKCOLUMN_NAME")); } } finally { ResultSetUtil.close(rs); } DbForeignKeyMeta[] array = map.values().toArray( new DbForeignKeyMeta[map.size()]); return Arrays.asList(array); } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); List<DbTableMeta> read();  }