Search in sources :

Example 1 with ForeignKey

use of org.teiid.metadata.ForeignKey in project teiid by teiid.

the class ODataSchemaBuilder method buildNavigationProperties.

private static void buildNavigationProperties(org.teiid.metadata.Schema schema, Map<String, CsdlEntityType> entityTypes, Map<String, CsdlEntitySet> entitySets) {
    for (Table table : schema.getTables().values()) {
        // skip if the table does not have the PK or unique
        if (getIdentifier(table) == null) {
            continue;
        }
        // build Associations
        for (ForeignKey fk : table.getForeignKeys()) {
            // check to see if fk is part of this table's pk, then it is 1 to 1 relation
            boolean fkPKSame = sameColumnSet(getIdentifier(table), fk);
            addForwardNavigation(entityTypes, entitySets, table, fk, fkPKSame);
            addReverseNavigation(entityTypes, entitySets, table, fk, fkPKSame);
        }
    }
}
Also used : Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey)

Example 2 with ForeignKey

use of org.teiid.metadata.ForeignKey in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method visit.

@Override
public void visit(UriResourceNavigation info) {
    EdmNavigationProperty property = info.getProperty();
    this.referenceTable = new ScopedTable(DocumentNode.findTable(property.getType(), this.metadata), property.getType(), info.getKeyPredicates());
    if (property.isCollection()) {
        ForeignKey fk = DocumentNode.joinFK(referenceTable, this.updateTable, property);
        referenceTable.setFk(fk);
        ScopedTable temp = this.updateTable;
        this.updateTable = referenceTable;
        this.referenceTable = temp;
        this.collection = true;
    } else {
        ForeignKey fk = DocumentNode.joinFK(this.updateTable, referenceTable, property);
        this.updateTable.setFk(fk);
    }
}
Also used : EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) ForeignKey(org.teiid.metadata.ForeignKey)

Example 3 with ForeignKey

use of org.teiid.metadata.ForeignKey in project teiid by teiid.

the class TestODataMetadataProcessor method testManytoManyAssosiation.

@Test
public void testManytoManyAssosiation() throws Exception {
    ODataMetadataProcessor processor = new ODataMetadataProcessor();
    MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    EdmEntityType.Builder g1Entity = entityType("g1");
    EdmEntityType.Builder g2Entity = entityType("g2");
    EdmAssociationEnd.Builder aend1 = EdmAssociationEnd.newBuilder().setRole("source").setType(g1Entity).setMultiplicity(EdmMultiplicity.MANY);
    EdmAssociationEnd.Builder aend2 = EdmAssociationEnd.newBuilder().setRole("target").setType(g2Entity).setMultiplicity(EdmMultiplicity.MANY);
    EdmAssociation.Builder assocition = EdmAssociation.newBuilder().setNamespace("namspace").setName("m_2_m").setEnds(aend2, aend1);
    EdmNavigationProperty.Builder navigation = EdmNavigationProperty.newBuilder("g1").setFromTo(aend2, aend1).setFromToName("source", "target").setRelationship(assocition);
    g2Entity.addNavigationProperties(navigation);
    EdmEntitySet g1Set = EdmEntitySet.newBuilder().setName("G1").setEntityType(g1Entity).build();
    EdmEntitySet g2Set = EdmEntitySet.newBuilder().setName("G2").setEntityType(g2Entity).build();
    processor.addEntitySetAsTable(mf, g1Set);
    processor.addEntitySetAsTable(mf, g2Set);
    processor.addNavigationRelations(mf, "G2", g2Entity.build());
    Table g1 = mf.getSchema().getTable("G1");
    Table g2 = mf.getSchema().getTable("G2");
    Table linkTable = mf.getSchema().getTable("m_2_m");
    assertEquals(1, linkTable.getColumns().size());
    assertEquals("e1", linkTable.getColumns().get(0).getName());
    assertNotNull(linkTable);
    assertEquals("G2,G1", linkTable.getProperty(ODataMetadataProcessor.LINK_TABLES, false));
    ForeignKey fk1 = linkTable.getForeignKeys().get(0);
    assertEquals("G2_FK", fk1.getName());
    assertNotNull(fk1.getColumnByName("e1"));
    ForeignKey fk2 = linkTable.getForeignKeys().get(1);
    assertEquals("G1_FK", fk2.getName());
    assertNotNull(fk2.getColumnByName("e1"));
}
Also used : EdmAssociationEnd(org.odata4j.edm.EdmAssociationEnd) Table(org.teiid.metadata.Table) EdmEntityType(org.odata4j.edm.EdmEntityType) EdmNavigationProperty(org.odata4j.edm.EdmNavigationProperty) Properties(java.util.Properties) ForeignKey(org.teiid.metadata.ForeignKey) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) EdmAssociation(org.odata4j.edm.EdmAssociation) EdmEntitySet(org.odata4j.edm.EdmEntitySet) Test(org.junit.Test)

Example 4 with ForeignKey

use of org.teiid.metadata.ForeignKey in project teiid by teiid.

the class TestODataMetadataProcessor method testManytoManyAssosiationWithReferntialConstraint.

@Test
public void testManytoManyAssosiationWithReferntialConstraint() throws Exception {
    ODataMetadataProcessor processor = new ODataMetadataProcessor();
    MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    EdmEntityType.Builder g1Entity = entityType("g1");
    g1Entity.addProperties(EdmProperty.newBuilder("g2e2").setType(EdmSimpleType.STRING).setNullable(false));
    EdmEntityType.Builder g2Entity = entityType("g2");
    EdmAssociationEnd.Builder aend1 = EdmAssociationEnd.newBuilder().setRole("source").setType(g1Entity).setMultiplicity(EdmMultiplicity.MANY);
    EdmAssociationEnd.Builder aend2 = EdmAssociationEnd.newBuilder().setRole("target").setType(g2Entity).setMultiplicity(EdmMultiplicity.MANY);
    EdmReferentialConstraint.Builder refContraint = EdmReferentialConstraint.newBuilder().addPrincipalReferences("e1").addDependentReferences("g2e2");
    EdmAssociation.Builder assocition = EdmAssociation.newBuilder().setNamespace("namspace").setName("m_2_m").setEnds(aend2, aend1).setRefConstraint(refContraint);
    EdmNavigationProperty.Builder navigation = EdmNavigationProperty.newBuilder("g1").setFromTo(aend2, aend1).setFromToName("source", "target").setRelationship(assocition);
    g2Entity.addNavigationProperties(navigation);
    EdmEntitySet g1Set = EdmEntitySet.newBuilder().setName("G1").setEntityType(g1Entity).build();
    EdmEntitySet g2Set = EdmEntitySet.newBuilder().setName("G2").setEntityType(g2Entity).build();
    processor.addEntitySetAsTable(mf, g1Set);
    processor.addEntitySetAsTable(mf, g2Set);
    processor.addNavigationRelations(mf, "G2", g2Entity.build());
    Table g1 = mf.getSchema().getTable("G1");
    Table g2 = mf.getSchema().getTable("G2");
    Table linkTable = mf.getSchema().getTable("m_2_m");
    assertEquals(2, linkTable.getColumns().size());
    assertEquals("e1", linkTable.getColumns().get(0).getName());
    assertEquals("g2e2", linkTable.getColumns().get(1).getName());
    assertNotNull(linkTable);
    assertEquals("G2,G1", linkTable.getProperty(ODataMetadataProcessor.LINK_TABLES, false));
    ForeignKey fk = linkTable.getForeignKeys().get(0);
    assertEquals("G2_FK", fk.getName());
    assertNotNull(fk.getColumnByName("e1"));
    ForeignKey fk2 = linkTable.getForeignKeys().get(1);
    assertEquals("G1_FK", fk2.getName());
    assertNotNull(fk2.getColumnByName("g2e2"));
}
Also used : EdmAssociationEnd(org.odata4j.edm.EdmAssociationEnd) Table(org.teiid.metadata.Table) EdmReferentialConstraint(org.odata4j.edm.EdmReferentialConstraint) EdmEntityType(org.odata4j.edm.EdmEntityType) EdmNavigationProperty(org.odata4j.edm.EdmNavigationProperty) Properties(java.util.Properties) ForeignKey(org.teiid.metadata.ForeignKey) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) EdmAssociation(org.odata4j.edm.EdmAssociation) EdmEntitySet(org.odata4j.edm.EdmEntitySet) Test(org.junit.Test)

Example 5 with ForeignKey

use of org.teiid.metadata.ForeignKey in project teiid by teiid.

the class TestODataMetadataProcessor method testOneToOneAssosiation.

@Test
public void testOneToOneAssosiation() throws Exception {
    ODataMetadataProcessor processor = new ODataMetadataProcessor();
    MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    EdmEntityType.Builder g1Entity = entityType("g1");
    EdmEntityType.Builder g2Entity = entityType("g2");
    EdmAssociationEnd.Builder aend1 = EdmAssociationEnd.newBuilder().setRole("source").setType(g1Entity).setMultiplicity(EdmMultiplicity.ONE);
    EdmAssociationEnd.Builder aend2 = EdmAssociationEnd.newBuilder().setRole("target").setType(g2Entity).setMultiplicity(EdmMultiplicity.ONE);
    EdmAssociation.Builder assocition = EdmAssociation.newBuilder().setNamespace("namspace").setName("one_2_one").setEnds(aend2, aend1);
    EdmNavigationProperty.Builder navigation = EdmNavigationProperty.newBuilder("g1").setFromTo(aend2, aend1).setFromToName("source", "target").setRelationship(assocition);
    g2Entity.addNavigationProperties(navigation);
    EdmEntitySet g1Set = EdmEntitySet.newBuilder().setName("G1").setEntityType(g1Entity).build();
    processor.addEntitySetAsTable(mf, g1Set);
    EdmEntitySet g2Set = EdmEntitySet.newBuilder().setName("G2").setEntityType(g2Entity).build();
    processor.addEntitySetAsTable(mf, g2Set);
    processor.addNavigationRelations(mf, "G2", g2Entity.build());
    Table g1 = mf.getSchema().getTable("G1");
    Table g2 = mf.getSchema().getTable("G2");
    ForeignKey fk = g1.getForeignKeys().get(0);
    assertEquals("one_2_one", fk.getName());
    assertNotNull(fk.getColumnByName("e1"));
}
Also used : EdmAssociationEnd(org.odata4j.edm.EdmAssociationEnd) Table(org.teiid.metadata.Table) EdmEntityType(org.odata4j.edm.EdmEntityType) EdmNavigationProperty(org.odata4j.edm.EdmNavigationProperty) Properties(java.util.Properties) ForeignKey(org.teiid.metadata.ForeignKey) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) EdmAssociation(org.odata4j.edm.EdmAssociation) EdmEntitySet(org.odata4j.edm.EdmEntitySet) Test(org.junit.Test)

Aggregations

ForeignKey (org.teiid.metadata.ForeignKey)24 Table (org.teiid.metadata.Table)22 Test (org.junit.Test)12 MetadataFactory (org.teiid.metadata.MetadataFactory)12 Properties (java.util.Properties)9 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)9 EdmAssociation (org.odata4j.edm.EdmAssociation)4 EdmAssociationEnd (org.odata4j.edm.EdmAssociationEnd)4 EdmEntitySet (org.odata4j.edm.EdmEntitySet)4 EdmEntityType (org.odata4j.edm.EdmEntityType)4 EdmNavigationProperty (org.odata4j.edm.EdmNavigationProperty)4 Column (org.teiid.metadata.Column)4 Connection (java.sql.Connection)3 KeyRecord (org.teiid.metadata.KeyRecord)3 TranslatorException (org.teiid.translator.TranslatorException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 XMLMetadata (org.apache.olingo.client.api.edm.xml.XMLMetadata)2 ClientCsdlXMLMetadata (org.apache.olingo.client.core.edm.ClientCsdlXMLMetadata)2 EdmReferentialConstraint (org.odata4j.edm.EdmReferentialConstraint)2