@Test public void testFKAccrossSchemas() throws Exception { String ddl = "CREATE FOREIGN TABLE G1(g1e1 integer, g1e2 varchar, PRIMARY KEY(g1e1, g1e2));\n"; String ddl2 = "CREATE FOREIGN TABLE G2( g2e1 integer, g2e2 varchar, PRIMARY KEY(g2e1, g2e2)," + "FOREIGN KEY (g2e1, g2e2) REFERENCES model.G1)"; MetadataFactory f1 = helpParse(ddl, "model"); MetadataFactory f2 = helpParse(ddl2, "model2"); VDBMetaData vdb = new VDBMetaData(); vdb.setName("myVDB"); // $NON-NLS-1$ ModelMetaData modelOne = new ModelMetaData(); modelOne.setName("model"); // $NON-NLS-1$ vdb.addModel(modelOne); ModelMetaData modelTwo = new ModelMetaData(); modelTwo.setName("model2"); // $NON-NLS-1$ vdb.addModel(modelTwo); MetadataStore s = f1.asMetadataStore(); f2.mergeInto(s); ValidatorReport report = new MetadataValidator().validate(vdb, s); assertFalse(report.hasItems()); Table table = s.getSchema("model2").getTable("G2"); ForeignKey fk = table.getForeignKeys().get(0); assertEquals(fk.getColumns(), table.getColumns()); assertEquals("G1", fk.getReferenceTableName()); assertEquals(fk.getPrimaryKey().getColumns(), s.getSchema("model").getTable("G1").getColumns()); }
public static VDBMetaData unmarshell(InputStream content) throws XMLStreamException { XMLInputFactory inputFactory = XMLType.getXmlInputFactory(); XMLStreamReader reader = inputFactory.createXMLStreamReader(content); try { // elements while (reader.hasNext() && (reader.nextTag() != XMLStreamConstants.END_ELEMENT)) { Element element = Element.forName(reader.getLocalName()); switch (element) { case VDB: VDBMetaData vdb = new VDBMetaData(); Properties props = getAttributes(reader); vdb.setName(props.getProperty(Element.NAME.getLocalName())); vdb.setVersion(Integer.parseInt(props.getProperty(Element.VERSION.getLocalName()))); parseVDB(reader, vdb); return vdb; default: throw new XMLStreamException( AdminPlugin.Util.gs( "unexpected_element1", reader.getName(), Element.VDB.getLocalName()), reader.getLocation()); } } } finally { try { content.close(); } catch (IOException e) { Logger.getLogger(VDBMetadataParser.class.getName()) .log(Level.FINE, "Exception closing vdb stream", e); } } return null; }
@Test public void testOptionalFKFail() throws Exception { String ddl = "CREATE FOREIGN TABLE G1(g1e1 integer, g1e2 varchar);\n" + "CREATE FOREIGN TABLE G2( g2e1 integer, g2e2 varchar, PRIMARY KEY(g2e1, g2e2)," + "FOREIGN KEY (g2e1, g2e2) REFERENCES G1)"; MetadataFactory s = helpParse(ddl, "model"); Map<String, Table> tableMap = s.getSchema().getTables(); assertEquals(2, tableMap.size()); assertTrue("Table not found", tableMap.containsKey("G1")); assertTrue("Table not found", tableMap.containsKey("G2")); Table table = tableMap.get("G2"); ForeignKey fk = table.getForeignKeys().get(0); assertEquals(fk.getColumns(), table.getColumns()); assertEquals("G1", fk.getReferenceTableName()); VDBMetaData vdb = new VDBMetaData(); vdb.setName("myVDB"); // $NON-NLS-1$ ModelMetaData modelOne = new ModelMetaData(); modelOne.setName("model"); // $NON-NLS-1$ vdb.addModel(modelOne); ValidatorReport report = new MetadataValidator().validate(vdb, s.asMetadataStore()); assertTrue(report.hasItems()); }