Search in sources :

Example 11 with ForeignKey

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

the class TestDynamicImportedMetaData method testMultipleFK.

@Test
public void testMultipleFK() throws Exception {
    ModelMetaData mmd = new ModelMetaData();
    mmd.addSourceMetadata("ddl", "create foreign table x (y integer, z integer, primary key (y, z));" + "create foreign table z (y integer, z integer, y1 integer, z1 integer, foreign key (y, z) references x (y, z), foreign key (y1, z1) references x (y, z))");
    mmd.setName("foo");
    mmd.addSourceMapping("x", "x", "x");
    server.addTranslator("x", new ExecutionFactory());
    server.deployVDB("vdb", mmd);
    // $NON-NLS-1$
    Connection conn = server.createConnection("jdbc:teiid:vdb");
    Properties importProperties = new Properties();
    importProperties.setProperty("importer.importKeys", "true");
    MetadataFactory mf = getMetadata(importProperties, conn);
    Table t = mf.asMetadataStore().getSchemas().get("test").getTables().get("vdb.foo.z");
    List<ForeignKey> fks = t.getForeignKeys();
    assertEquals(2, fks.size());
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) Connection(java.sql.Connection) OracleExecutionFactory(org.teiid.translator.jdbc.oracle.OracleExecutionFactory) ExecutionFactory(org.teiid.translator.ExecutionFactory) TeiidExecutionFactory(org.teiid.translator.jdbc.teiid.TeiidExecutionFactory) Properties(java.util.Properties) ForeignKey(org.teiid.metadata.ForeignKey) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 12 with ForeignKey

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

the class MongoDBUpdateExecution method removeParentKey.

private void removeParentKey(MongoDocument document, BasicDBObject row) throws TranslatorException {
    Table source = document.getTable();
    Table target = document.getMergeTable();
    for (ForeignKey fk : source.getForeignKeys()) {
        if (fk.getReferenceTableName().equals(target.getName())) {
            for (int i = 0; i < fk.getColumns().size(); i++) {
                if (row != null) {
                    row.remove(fk.getColumns().get(i).getName());
                }
            }
        }
    }
}
Also used : Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey)

Example 13 with ForeignKey

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

the class CoherenceUpdateExecution method updateChildObject.

private void updateChildObject(Table t) throws TranslatorException {
    List<ForeignKey> fks = t.getForeignKeys();
    ForeignKey fk = fks.get(0);
    Table parentTable = fk.getParent();
    // the name of the method to obtain the collection is the nameInSource of the foreginKey
    String parentToChildMethod = fk.getNameInSource();
    if (parentToChildMethod == null) {
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noNameInSourceForForeingKey", new Object[] { fk.getName() });
        throw new TranslatorException(msg);
    }
    // there must only be 1 column in the primary key
    String parentColName = visitor.getNameFromElement(fk.getPrimaryKey().getColumns().get(0));
    List<SetClause> updateList = ((Update) command).getChanges();
    Condition criteria = ((Update) command).getWhere();
    ColumnReference leftElement;
    Expression rightExpr;
    String nameLeftElement;
    Object valueRightExpr;
    // API).
    for (int i = 0; i < updateList.size(); i++) {
        SetClause setClause = updateList.get(i);
        // trust that connector API is right and left side
        // will always be an IElement
        leftElement = setClause.getSymbol();
        // call utility method to get NameInSource/Name for element
        nameLeftElement = visitor.getNameFromElement(leftElement.getMetadataObject());
        // get right expression - if it is not a literal we
        // can't handle that so throw an exception
        rightExpr = setClause.getValue();
        // if (!(rightExpr instanceof Literal)) {
        // final String msg = CoherencePlugin.Util.getString("LDAPUpdateExecution.valueNotLiteralError",nameLeftElement); //$NON-NLS-1$
        // throw new TranslatorException(msg);
        // }
        valueRightExpr = ((Literal) rightExpr).getValue();
    // add in the modification as a replacement - meaning
    // any existing value(s) for this attribute will
    // be replaced by the new value.  If the attribute
    // didn't exist, it will automatically be created
    // TODO - since null is a valid attribute
    // value, we don't do any special handling of it right
    // now.  But maybe null should mean to delete an
    // attribute?
    }
}
Also used : Condition(org.teiid.language.Condition) Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey) Update(org.teiid.language.Update) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) SetClause(org.teiid.language.SetClause) ColumnReference(org.teiid.language.ColumnReference)

Example 14 with ForeignKey

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

the class JPAMetadataProcessor method addForeignKey.

private void addForeignKey(MetadataFactory mf, String name, List<String> columnNames, String referenceTable, Table table) throws TranslatorException {
    ForeignKey fk = mf.addForeignKey("FK_" + name, columnNames, referenceTable, table);
    for (String column : columnNames) {
        Column c = table.getColumnByName(column);
        c.setProperty(KEY_ASSOSIATED_WITH_FOREIGN_TABLE, mf.getName() + Tokens.DOT + referenceTable);
    }
    fk.setNameInSource(name);
}
Also used : Column(org.teiid.metadata.Column) ForeignKey(org.teiid.metadata.ForeignKey)

Example 15 with ForeignKey

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

the class TestODataMetadataProcessor method testOneToOneAssosiation.

@Test
public void testOneToOneAssosiation() throws Exception {
    MetadataFactory mf = oneToOneRelationMetadata(true);
    Table g1 = mf.getSchema().getTable("G1");
    Table g2 = mf.getSchema().getTable("G2");
    ForeignKey fk = g1.getForeignKeys().get(0);
    assertEquals("G2_one_2_one", fk.getName());
    assertNotNull(fk.getColumnByName("e1"));
    fk = g2.getForeignKeys().get(0);
    assertEquals("G1_one_2_one", fk.getName());
    assertNotNull(fk.getColumnByName("e1"));
    mf = oneToOneRelationMetadata(false);
    g1 = mf.getSchema().getTable("G1");
    g2 = mf.getSchema().getTable("G2");
    fk = g1.getForeignKeys().get(0);
    assertEquals("G2_one_2_one", fk.getName());
    assertNotNull(fk.getColumnByName("e1"));
    // TODO: could infer this, but for now it's not in the metadata
    assertTrue(g2.getForeignKeys().isEmpty());
}
Also used : Table(org.teiid.metadata.Table) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) ForeignKey(org.teiid.metadata.ForeignKey) 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