@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()); }
@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()); }
private static void parseVDB(XMLStreamReader reader, VDBMetaData vdb) throws XMLStreamException { while (reader.hasNext() && (reader.nextTag() != XMLStreamConstants.END_ELEMENT)) { Element element = Element.forName(reader.getLocalName()); switch (element) { case DESCRIPTION: vdb.setDescription(reader.getElementText()); break; case PROPERTY: parseProperty(reader, vdb); break; case MODEL: ModelMetaData model = new ModelMetaData(); parseModel(reader, model); vdb.addModel(model); break; case TRANSLATOR: VDBTranslatorMetaData translator = new VDBTranslatorMetaData(); parseTranslator(reader, translator); vdb.addOverideTranslator(translator); break; case DATA_ROLE: DataPolicyMetadata policy = new DataPolicyMetadata(); parseDataRole(reader, policy); vdb.addDataPolicy(policy); break; case IMPORT_VDB: VDBImportMetadata vdbImport = new VDBImportMetadata(); Properties props = getAttributes(reader); vdbImport.setName(props.getProperty(Element.NAME.getLocalName())); vdbImport.setVersion(Integer.parseInt(props.getProperty(Element.VERSION.getLocalName()))); vdbImport.setImportDataPolicies( Boolean.parseBoolean( props.getProperty(Element.IMPORT_POLICIES.getLocalName(), "true"))); vdb.getVDBImports().add(vdbImport); ignoreTillEnd(reader); break; case ENTRY: EntryMetaData entry = new EntryMetaData(); parseEntry(reader, entry); vdb.getEntries().add(entry); break; default: throw new XMLStreamException( AdminPlugin.Util.gs( "unexpected_element5", reader.getName(), Element.DESCRIPTION.getLocalName(), Element.PROPERTY.getLocalName(), Element.MODEL.getLocalName(), Element.TRANSLATOR.getLocalName(), Element.DATA_ROLE.getLocalName()), reader.getLocation()); } } }
private void buildCompositeState(VDBRepository vdbRepository) throws VirtualDatabaseException { if (vdb.getVDBImports().isEmpty()) { this.vdb.addAttchment(ConnectorManagerRepository.class, this.cmr); return; } VDBMetaData newMergedVDB = this.vdb.clone(); ConnectorManagerRepository mergedRepo = this.cmr; if (!this.cmr.isShared()) { mergedRepo = new ConnectorManagerRepository(); mergedRepo.getConnectorManagers().putAll(this.cmr.getConnectorManagers()); } newMergedVDB.addAttchment(ConnectorManagerRepository.class, mergedRepo); ClassLoader[] toSearch = new ClassLoader[vdb.getVDBImports().size() + 1]; toSearch[0] = this.vdb.getAttachment(ClassLoader.class); this.children = new LinkedHashMap<VDBKey, CompositeVDB>(); newMergedVDB.setImportedModels(new TreeSet<String>(String.CASE_INSENSITIVE_ORDER)); int i = 1; for (VDBImport vdbImport : vdb.getVDBImports()) { CompositeVDB importedVDB = vdbRepository.getCompositeVDB(new VDBKey(vdbImport.getName(), vdbImport.getVersion())); if (importedVDB == null) { throw new VirtualDatabaseException( RuntimePlugin.Event.TEIID40083, RuntimePlugin.Util.gs( RuntimePlugin.Event.TEIID40083, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion())); } VDBMetaData childVDB = importedVDB.getVDB(); newMergedVDB.getVisibilityOverrides().putAll(childVDB.getVisibilityOverrides()); toSearch[i++] = childVDB.getAttachment(ClassLoader.class); this.children.put(new VDBKey(childVDB.getName(), childVDB.getVersion()), importedVDB); if (vdbImport.isImportDataPolicies()) { for (DataPolicy dp : importedVDB.getVDB().getDataPolicies()) { DataPolicyMetadata role = (DataPolicyMetadata) dp; if (newMergedVDB.addDataPolicy(role) != null) { throw new VirtualDatabaseException( RuntimePlugin.Event.TEIID40084, RuntimePlugin.Util.gs( RuntimePlugin.Event.TEIID40084, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), role.getName())); } if (role.isGrantAll()) { role.setSchemas(childVDB.getModelMetaDatas().keySet()); } } } // add models for (ModelMetaData m : childVDB.getModelMetaDatas().values()) { if (newMergedVDB.addModel(m) != null) { throw new VirtualDatabaseException( RuntimePlugin.Event.TEIID40085, RuntimePlugin.Util.gs( RuntimePlugin.Event.TEIID40085, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), m.getName())); } newMergedVDB.getImportedModels().add(m.getName()); String visibilityOverride = newMergedVDB.getPropertyValue(m.getName() + ".visible"); // $NON-NLS-1$ if (visibilityOverride != null) { boolean visible = Boolean.valueOf(visibilityOverride); newMergedVDB.setVisibilityOverride(m.getName(), visible); } } ConnectorManagerRepository childCmr = childVDB.getAttachment(ConnectorManagerRepository.class); if (childCmr == null) { throw new AssertionError("childVdb had not connector manager repository"); // $NON-NLS-1$ } if (!this.cmr.isShared()) { for (Map.Entry<String, ConnectorManager> entry : childCmr.getConnectorManagers().entrySet()) { if (mergedRepo.getConnectorManagers().put(entry.getKey(), entry.getValue()) != null) { throw new VirtualDatabaseException( RuntimePlugin.Event.TEIID40086, RuntimePlugin.Util.gs( RuntimePlugin.Event.TEIID40086, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), entry.getKey())); } } } } if (toSearch[0] != null) { CombinedClassLoader ccl = new CombinedClassLoader(toSearch[0].getParent(), toSearch); this.mergedVDB.addAttchment(ClassLoader.class, ccl); } this.mergedVDB = newMergedVDB; }
/** @param models */ public void setModels(Collection<ModelMetaData> models) { this.models.clear(); for (ModelMetaData obj : models) { addModel(obj); } }