Search in sources :

Example 1 with KeyRecord

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

the class TestODataIntegration method testBasicTypes.

@Test
public void testBasicTypes() throws Exception {
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMapping("x3", "x3", null);
        MetadataStore ms = RealMetadataFactory.exampleBQTStore();
        Schema s = ms.getSchema("BQT1");
        KeyRecord pk = new KeyRecord(KeyRecord.Type.Primary);
        Table smalla = s.getTable("SmallA");
        pk.setName("pk");
        pk.addColumn(smalla.getColumnByName("IntKey"));
        smalla.setPrimaryKey(pk);
        String ddl = DDLStringVisitor.getDDLString(s, EnumSet.allOf(SchemaObjectType.class), "SmallA");
        mmd.addSourceMetadata("DDL", ddl);
        HardCodedExecutionFactory hc = buildHardCodedExecutionFactory();
        teiid.addTranslator("x3", hc);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = http.GET(baseURL + "/northwind/m/SmallA?$format=json&$select=TimeValue");
        assertEquals(200, response.getStatus());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : MetadataStore(org.teiid.metadata.MetadataStore) KeyRecord(org.teiid.metadata.KeyRecord) Table(org.teiid.metadata.Table) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Schema(org.teiid.metadata.Schema) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) Properties(java.util.Properties) SchemaObjectType(org.teiid.adminapi.Admin.SchemaObjectType) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 2 with KeyRecord

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

the class ODataSQLBuilder method selectWithEntityKey.

// TODO: allow the generated key building.
public Query selectWithEntityKey(EdmEntityType entityType, Entity entity, Map<String, Object> generatedKeys, List<ExpandNode> expand) throws TeiidException {
    Table table = findTable(entityType.getName(), this.metadata);
    DocumentNode resource = new DocumentNode(table, new GroupSymbol(table.getFullName()), entityType);
    resource.setFromClause(new UnaryFromClause(new GroupSymbol(table.getFullName())));
    resource.addAllColumns(false);
    this.context = resource;
    Query query = this.context.buildQuery();
    processExpand(expand, resource, query, 1);
    Criteria criteria = null;
    KeyRecord pk = ODataSchemaBuilder.getIdentifier(table);
    for (Column c : pk.getColumns()) {
        Property prop = entity.getProperty(c.getName());
        Constant right = null;
        if (prop != null) {
            right = new Constant(ODataTypeManager.convertToTeiidRuntimeType(c.getJavaType(), prop.getValue(), null));
        } else {
            Object value = generatedKeys.get(c.getName());
            if (value == null) {
                throw new TeiidProcessingException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16016, entityType.getName()));
            }
            right = new Constant(value);
        }
        ElementSymbol left = new ElementSymbol(c.getName(), this.context.getGroupSymbol());
        if (criteria == null) {
            criteria = new CompareCriteria(left, AbstractCompareCriteria.EQ, right);
        } else {
            CompareCriteria rightCC = new CompareCriteria(left, AbstractCompareCriteria.EQ, right);
            criteria = new CompoundCriteria(CompoundCriteria.AND, criteria, rightCC);
        }
    }
    query.setCriteria(criteria);
    return query;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Table(org.teiid.metadata.Table) Constant(org.teiid.query.sql.symbol.Constant) TeiidProcessingException(org.teiid.core.TeiidProcessingException) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 3 with KeyRecord

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

the class ODataSchemaBuilder method addForwardNavigation.

private static void addForwardNavigation(Map<String, CsdlEntityType> entityTypes, Map<String, CsdlEntitySet> entitySets, Table table, ForeignKey fk, boolean onetoone) {
    CsdlNavigationProperty navigaton = null;
    CsdlNavigationPropertyBinding navigationBinding = null;
    String entityTypeName = table.getName();
    navigaton = buildNavigation(fk);
    navigationBinding = buildNavigationBinding(fk);
    if (onetoone) {
        navigaton.setNullable(false);
    } else {
        for (Column c : fk.getColumns()) {
            if (c.getNullType() == NullType.No_Nulls) {
                navigaton.setNullable(false);
                break;
            }
        }
    }
    List<CsdlReferentialConstraint> constraints = new ArrayList<CsdlReferentialConstraint>();
    KeyRecord key = fk.getReferenceKey();
    for (int i = 0; i < key.getColumns().size(); i++) {
        constraints.add(new CsdlReferentialConstraint().setReferencedProperty(key.getColumns().get(i).getName()).setProperty(fk.getColumns().get(i).getName()));
    }
    navigaton.setReferentialConstraints(constraints);
    CsdlEntityType entityType = entityTypes.get(entityTypeName);
    entityType.getNavigationProperties().add(navigaton);
    CsdlEntitySet entitySet = entitySets.get(entityTypeName);
    entitySet.getNavigationPropertyBindings().add(navigationBinding);
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList)

Example 4 with KeyRecord

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

the class ODataSchemaBuilder method buildEntityTypes.

static void buildEntityTypes(String namespace, org.teiid.metadata.Schema schema, CsdlSchema csdlSchema) {
    Map<String, CsdlEntitySet> entitySets = new LinkedHashMap<String, CsdlEntitySet>();
    Map<String, CsdlEntityType> entityTypes = new LinkedHashMap<String, CsdlEntityType>();
    String fullSchemaName = namespace + "." + schema.getName();
    for (Table table : schema.getTables().values()) {
        // skip if the table does not have the PK or unique
        KeyRecord primaryKey = getIdentifier(table);
        if (primaryKey == null) {
            LogManager.logDetail(LogConstants.CTX_ODATA, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16017, table.getFullName()));
            continue;
        }
        String entityTypeName = table.getName();
        CsdlEntityType entityType = new CsdlEntityType().setName(entityTypeName);
        // adding properties
        List<CsdlProperty> properties = new ArrayList<CsdlProperty>();
        for (Column c : table.getColumns()) {
            boolean nullable = c.getNullType() == NullType.Nullable;
            CsdlProperty property = buildProperty(c, isPartOfPrimaryKey(table, c.getName()) ? false : nullable);
            addColumnAnnotations(c, property, csdlSchema);
            properties.add(property);
        }
        entityType.setProperties(properties);
        if (hasStream(properties)) {
            entityType.setHasStream(true);
        }
        // set keys
        ArrayList<CsdlPropertyRef> keyProps = new ArrayList<CsdlPropertyRef>();
        for (Column c : primaryKey.getColumns()) {
            keyProps.add(new CsdlPropertyRef().setName(c.getName()));
        }
        entityType.setKey(keyProps);
        addTableAnnotations(table, entityType, csdlSchema);
        // entity set one for one entity type
        CsdlEntitySet entitySet = new CsdlEntitySet().setName(table.getName()).setType(new FullQualifiedName(fullSchemaName, table.getName())).setIncludeInServiceDocument(true);
        // add entity types for entity schema
        entityTypes.put(entityTypeName, entityType);
        entitySets.put(entityTypeName, entitySet);
    }
    buildNavigationProperties(schema, entityTypes, entitySets);
    // entity container is holder entity sets, association sets, function
    // imports
    CsdlEntityContainer entityContainer = new CsdlEntityContainer().setName(schema.getName()).setEntitySets(new ArrayList<CsdlEntitySet>(entitySets.values()));
    // build entity schema
    csdlSchema.setEntityTypes(new ArrayList<CsdlEntityType>(entityTypes.values())).setEntityContainer(entityContainer);
}
Also used : Table(org.teiid.metadata.Table) FullQualifiedName(org.apache.olingo.commons.api.edm.FullQualifiedName) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column)

Example 5 with KeyRecord

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

the class SAPMetadataProcessor method buildColumn.

@Override
protected Column buildColumn(MetadataFactory mf, Table table, EdmProperty ep, EdmEntitySet entitySet, String prefix) {
    boolean creatable = true;
    boolean updatable = true;
    boolean filterable = true;
    boolean required_in_filter = false;
    String columnName = ep.getName();
    if (prefix != null) {
        // $NON-NLS-1$
        columnName = prefix + "_" + columnName;
    }
    Column c = mf.addColumn(columnName, ODataTypeManager.teiidType(ep.getType().getFullyQualifiedTypeName()), table);
    c.setNameInSource(ep.getName());
    Iterable<? extends NamespacedAnnotation> annotations = ep.getAnnotations();
    for (NamespacedAnnotation annotation : annotations) {
        PrefixedNamespace namespace = annotation.getNamespace();
        if (namespace.getUri().equals(SAPURI)) {
            String name = annotation.getName();
            if (name.equalsIgnoreCase("label")) {
                // $NON-NLS-1$
                c.setAnnotation((String) annotation.getValue());
            } else if (name.equalsIgnoreCase("creatable")) {
                // $NON-NLS-1$
                creatable = Boolean.parseBoolean((String) annotation.getValue());
            }
            if (name.equalsIgnoreCase("visible")) {
                // $NON-NLS-1$
                c.setSelectable(Boolean.parseBoolean((String) annotation.getValue()));
            }
            if (name.equalsIgnoreCase("updatable")) {
                // $NON-NLS-1$
                updatable = Boolean.parseBoolean((String) annotation.getValue());
            }
            if (name.equalsIgnoreCase("sortable")) {
                // $NON-NLS-1$
                if (!Boolean.parseBoolean((String) annotation.getValue())) {
                    c.setSearchType(SearchType.Unsearchable);
                }
            }
            if (name.equalsIgnoreCase("filterable")) {
                // $NON-NLS-1$
                filterable = Boolean.parseBoolean((String) annotation.getValue());
            }
            if (name.equalsIgnoreCase("required-in-filter")) {
                // $NON-NLS-1$
                required_in_filter = Boolean.parseBoolean((String) annotation.getValue());
            }
            if (name.equalsIgnoreCase("filter-restriction")) {
            // $NON-NLS-1$
            // TODO:
            }
        }
    }
    c.setUpdatable(creatable && updatable);
    if (!filterable) {
        c.setSearchType(SearchType.Unsearchable);
    }
    if (required_in_filter) {
        if (this.accessPatterns.get(table) == null) {
            KeyRecord record = new KeyRecord(Type.AccessPattern);
            record.addColumn(c);
            this.accessPatterns.put(table, record);
        } else {
            this.accessPatterns.get(table).addColumn(c);
        }
    }
    // entity set defined to as must have filter
    // $NON-NLS-1$
    boolean requiresFilter = Boolean.parseBoolean(getProperty(entitySet, "requires-filter"));
    if (requiresFilter && filterable && !required_in_filter) {
        KeyRecord record = new KeyRecord(Type.AccessPattern);
        record.addColumn(c);
        table.getAccessPatterns().add(record);
    }
    return c;
}
Also used : NamespacedAnnotation(org.odata4j.core.NamespacedAnnotation) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) PrefixedNamespace(org.odata4j.core.PrefixedNamespace)

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