Search in sources :

Example 16 with KeyRecord

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

the class TestODataMetadataProcessor method testKeyMaytches.

@Test
public void testKeyMaytches() {
    Column c1 = new Column();
    c1.setName("one");
    Column c2 = new Column();
    c2.setName("two");
    KeyRecord record = new KeyRecord(Type.Primary);
    record.addColumn(c1);
    record.addColumn(c2);
    ODataMetadataProcessor p = new ODataMetadataProcessor();
    assertTrue(p.keyMatches(Arrays.asList("one", "one"), record));
    assertTrue(p.keyMatches(Arrays.asList("two", "one"), record));
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) Test(org.junit.Test)

Example 17 with KeyRecord

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

the class TestODataMetadataProcessor method testAssosiationWithReferentialContriant.

@Test
public void testAssosiationWithReferentialContriant() 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.ONE);
    EdmAssociationEnd.Builder aend2 = EdmAssociationEnd.newBuilder().setRole("target").setType(g2Entity).setMultiplicity(EdmMultiplicity.ONE);
    EdmReferentialConstraint.Builder refContraint = EdmReferentialConstraint.newBuilder().addPrincipalReferences("g2e2").setPrincipalRole("source").addDependentReferences("e1").setDependentRole("target");
    EdmAssociation.Builder assocition = EdmAssociation.newBuilder().setNamespace("namspace").setName("one_2_one").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();
    Table t1 = processor.addEntitySetAsTable(mf, g1Set);
    Table t2 = processor.addEntitySetAsTable(mf, g2Set);
    KeyRecord record = new KeyRecord(Type.Unique);
    record.addColumn(t1.getColumnByName("g2e2"));
    t1.setUniqueKeys(Arrays.asList(record));
    processor.addNavigationRelations(mf, "G2", g2Entity.build());
    Table g1 = mf.getSchema().getTable("G1");
    Table g2 = mf.getSchema().getTable("G2");
    assertNotNull(g1);
    assertNotNull(g2);
    ForeignKey fk = g1.getForeignKeys().get(0);
    assertEquals("one_2_one", fk.getName());
    assertNotNull(fk.getColumnByName("g2e2"));
    assertEquals("e1", fk.getReferenceColumns().get(0));
}
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) KeyRecord(org.teiid.metadata.KeyRecord) 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 18 with KeyRecord

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

the class SalesForceMetadataProcessor method addRelationships.

private void addRelationships() {
    for (Map.Entry<String, ChildRelationship[]> entry : this.relationships.entrySet()) {
        for (ChildRelationship relationship : entry.getValue()) {
            if (relationship.getRelationshipName() == null) {
                // not queryable
                continue;
            }
            if (!isModelAuditFields() && isAuditField(relationship.getField())) {
                continue;
            }
            Table parent = tableMap.get(entry.getKey());
            KeyRecord pk = parent.getPrimaryKey();
            if (null == pk) {
                // $NON-NLS-1$
                throw new RuntimeException("ERROR !!primary key column not found!!");
            }
            Table child = tableMap.get(relationship.getChildSObject());
            if (child == null) {
                // child must have been excluded
                continue;
            }
            Column col = null;
            columns = child.getColumns();
            for (Iterator<Column> colIter = columns.iterator(); colIter.hasNext(); ) {
                Column column = colIter.next();
                if (column.getNameInSource().equals(relationship.getField())) {
                    col = column;
                }
            }
            if (null == col) {
                throw new RuntimeException(// $NON-NLS-1$
                "ERROR !!foreign key column not found!! " + child.getName() + relationship.getField());
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            String name = "FK_" + parent.getName() + "_" + col.getName();
            ArrayList<String> columnNames = new ArrayList<String>();
            columnNames.add(col.getName());
            ForeignKey fk = metadataFactory.addForeignKey(name, columnNames, parent.getName(), child);
            // TODO: only needed for custom relationships
            fk.setNameInSource(relationship.getRelationshipName());
        }
    }
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) ChildRelationship(com.sforce.soap.partner.ChildRelationship) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) ForeignKey(org.teiid.metadata.ForeignKey) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 19 with KeyRecord

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

the class TestCoherenceTranslator method getTradeTranslationUtility.

/**
 * this is used as an alternative to loading the vdb
 * @return
 */
private TranslationUtility getTradeTranslationUtility() {
    MetadataStore metadataStore = new MetadataStore();
    // Create TRADE
    // $NON-NLS-1$
    Schema trading = RealMetadataFactory.createPhysicalModel("Trade_Object", metadataStore);
    // Create physical groups
    // $NON-NLS-1$
    Table trade = RealMetadataFactory.createPhysicalGroup("TRADE", trading);
    trade.setNameInSource("org.teiid.translator.coherence.Trade");
    // Create physical elements
    String[] elemNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    "NAME", // $NON-NLS-1$ //$NON-NLS-2$
    "TRADEID" };
    String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
    List<Column> cols = RealMetadataFactory.createElements(trade, elemNames, elemTypes);
    // Set name in source on each column
    String[] nameInSource = new String[] { "Name", "TradeId" };
    for (int i = 0; i < nameInSource.length; i++) {
        cols.get(i).setNameInSource(nameInSource[i]);
    }
    List<Column> keys = new ArrayList(1);
    keys.add(cols.get(1));
    KeyRecord trade_pk = RealMetadataFactory.createKey(Type.Primary, "TradeID_PK", trade, keys);
    // LEG
    // $NON-NLS-1$
    Table leg = RealMetadataFactory.createPhysicalGroup("LEG", trading);
    leg.setNameInSource("org.teiid.translator.coherence.Leg");
    // Create physical elements
    String[] legNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    "LEGID", // $NON-NLS-1$ //$NON-NLS-2$
    "NOTATIONAL", // $NON-NLS-1$ //$NON-NLS-2$
    "NAME", // $NON-NLS-1$ //$NON-NLS-2$
    "TRADEID" };
    String[] legTypes = new String[] { DataTypeManager.DefaultDataTypes.LONG, DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
    List<Column> legcols = RealMetadataFactory.createElements(leg, legNames, legTypes);
    // Set name in source on each column
    String[] legnameInSource = new String[] { "LegId", "Notational", "Name", "TradeId" };
    for (int i = 0; i < legnameInSource.length; i++) {
        legcols.get(i).setNameInSource(legnameInSource[i]);
    }
    legcols.get(3).setSelectable(false);
    legcols.get(3).setUpdatable(false);
    List<Column> legkeys = new ArrayList(1);
    keys.add(legcols.get(0));
    RealMetadataFactory.createKey(Type.Primary, "Leg_ID_PK", leg, legkeys);
    List<Column> foreignkey = new ArrayList(1);
    foreignkey.add(legcols.get(3));
    ForeignKey fk = RealMetadataFactory.createForeignKey("TRADE_FK", leg, foreignkey, trade_pk);
    fk.setNameInSource("Legs");
    fk.setParent(trade);
    // Set column-specific properties
    // cols.get(0).setSelectable(false);
    // cols.get(0).setSearchType(SearchType.Unsearchable);
    Schema tradeview = RealMetadataFactory.createVirtualModel("Trade_View", metadataStore);
    QueryNode qn = new QueryNode("SELECT Trade_Object.Trade.TradeID, Trade_Object.Trade.Name, Trade_Object.Leg.LegID, Trade_Object.Leg.Notational, Trade_Object.Leg.Name AS LegName " + " FROM Trade_Object.Trade, Trade_Object.Leg " + "WHERE	Trade_Object.Trade.TradeID = Trade_Object.Leg.TradeID " + "SELECT * FROM Trade.Trad " + " WHERE Trade.Trade.TradeID = Trade.Trade.TradeID");
    Table trades = RealMetadataFactory.createVirtualGroup("Trades", tradeview, qn);
    return new TranslationUtility(RealMetadataFactory.createTransformationMetadata(metadataStore, "Trade"));
}
Also used : Table(org.teiid.metadata.Table) Schema(org.teiid.metadata.Schema) ArrayList(java.util.ArrayList) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ForeignKey(org.teiid.metadata.ForeignKey) MetadataStore(org.teiid.metadata.MetadataStore) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) QueryNode(org.teiid.query.mapping.relational.QueryNode)

Example 20 with KeyRecord

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

the class SolrUpdateExecution method buildSelectQuery.

private Select buildSelectQuery(Insert insert) throws TranslatorException {
    Table table = insert.getTable().getMetadataObject();
    KeyRecord pk = table.getPrimaryKey();
    final String id = getRecordName(pk.getColumns().get(0));
    NamedTable g = insert.getTable();
    List<DerivedColumn> symbols = new ArrayList<DerivedColumn>();
    for (Column column : table.getColumns()) {
        String columnName = getRecordName(column);
        symbols.add(new DerivedColumn(columnName, new ColumnReference(g, columnName, column, column.getJavaType())));
    }
    List groups = new ArrayList();
    groups.add(g);
    ColumnReference idCol = new ColumnReference(g, id, table.getColumnByName(id), table.getColumnByName(id).getJavaType());
    Comparison cc = new Comparison(idCol, getPKValue(id, insert), Operator.EQ);
    Select q = new Select(symbols, false, groups, cc, null, null, null);
    return q;
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

KeyRecord (org.teiid.metadata.KeyRecord)22 Column (org.teiid.metadata.Column)18 Table (org.teiid.metadata.Table)14 ArrayList (java.util.ArrayList)11 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 ForeignKey (org.teiid.metadata.ForeignKey)3 MetadataStore (org.teiid.metadata.MetadataStore)3 Schema (org.teiid.metadata.Schema)3 QueryNode (org.teiid.query.mapping.relational.QueryNode)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 TranslatorException (org.teiid.translator.TranslatorException)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 EntityType (javax.persistence.metamodel.EntityType)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ChildRelationship (com.sforce.soap.partner.ChildRelationship)1 BatchUpdateException (java.sql.BatchUpdateException)1