Search in sources :

Example 36 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class TestCorrelatedReferenceCollectorVisitor method testDeepNesting.

@Test
public void testDeepNesting() throws Exception {
    String sql = "select * from bqt1.smalla where exists (select intnum from bqt1.smalla x where smalla.intnum = x.intnum and exists (select intnum from bqt1.smalla where exists (select intnum from bqt1.smalla x where smalla.intnum = x.intnum)))";
    Command command = TestResolver.helpResolve(sql, RealMetadataFactory.exampleBQTCached());
    command = QueryRewriter.rewrite(command, RealMetadataFactory.exampleBQTCached(), null);
    command = ((ExistsCriteria) ((Query) command).getCriteria()).getCommand();
    LinkedList<Reference> correlatedReferences = new LinkedList<Reference>();
    GroupSymbol gs = new GroupSymbol("bqt1.smalla");
    ResolverUtil.resolveGroup(gs, RealMetadataFactory.exampleBQTCached());
    CorrelatedReferenceCollectorVisitor.collectReferences(command, Arrays.asList(gs), correlatedReferences, RealMetadataFactory.exampleBQTCached());
    assertEquals(1, correlatedReferences.size());
}
Also used : Query(org.teiid.query.sql.lang.Query) Command(org.teiid.query.sql.lang.Command) Reference(org.teiid.query.sql.symbol.Reference) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 37 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class ODataSQLBuilder method updateProperty.

public Update updateProperty(EdmProperty edmProperty, Property property, boolean prepared, boolean rawValue) throws TeiidException {
    Update update = new Update();
    update.setGroup(this.context.getGroupSymbol());
    Column column = this.context.getColumnByName(edmProperty.getName());
    ElementSymbol symbol = new ElementSymbol(column.getName(), this.context.getGroupSymbol());
    if (prepared) {
        update.addChange(symbol, new Reference(0));
        this.params.add(asParam(edmProperty, property.getValue(), rawValue));
    } else {
        update.addChange(symbol, new Constant(asParam(edmProperty, property.getValue()).getValue()));
    }
    update.setCriteria(this.context.getCriteria());
    return update;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Column(org.teiid.metadata.Column) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant)

Example 38 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class ODataSQLBuilder method insert.

public Insert insert(EdmEntityType entityType, Entity entity, List<UriParameter> keys, boolean prepared) throws TeiidException {
    Table entityTable = findTable(entityType.getName(), this.metadata);
    DocumentNode resource = new DocumentNode(entityTable, new GroupSymbol(entityTable.getFullName()), entityType);
    List<Reference> referenceValues = new ArrayList<Reference>();
    List<Constant> constantValues = new ArrayList<Constant>();
    Insert insert = new Insert();
    insert.setGroup(resource.getGroupSymbol());
    if (keys != null) {
        for (UriParameter key : keys) {
            EdmProperty edmProperty = (EdmProperty) entityType.getProperty(key.getName());
            Column column = entityTable.getColumnByName(edmProperty.getName());
            Object propertyValue = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), key.getText());
            Property existing = entity.getProperty(edmProperty.getName());
            if (existing == null || (existing.getValue() == null && propertyValue != null) || (existing.getValue() != null && propertyValue == null) || (existing.getValue() != null && !existing.getValue().equals(propertyValue))) {
                throw new TeiidProcessingException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16048, edmProperty.getName()));
            }
        }
    }
    int i = 0;
    for (Property prop : entity.getProperties()) {
        EdmProperty edmProp = (EdmProperty) entityType.getProperty(prop.getName());
        Column column = entityTable.getColumnByName(edmProp.getName());
        insert.addVariable(new ElementSymbol(column.getName(), resource.getGroupSymbol()));
        if (prepared) {
            referenceValues.add(new Reference(i++));
            this.params.add(asParam(edmProp, prop.getValue()));
        } else {
            constantValues.add(new Constant(asParam(edmProp, prop.getValue()).getValue()));
        }
    }
    if (prepared) {
        insert.setValues(referenceValues);
    } else {
        insert.setValues(constantValues);
    }
    return insert;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Table(org.teiid.metadata.Table) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Column(org.teiid.metadata.Column) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) 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 39 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class ODataSQLBuilder method updateStreamProperty.

public Update updateStreamProperty(EdmProperty edmProperty, final InputStream content) throws TeiidException {
    Update update = new Update();
    update.setGroup(this.context.getGroupSymbol());
    Column column = this.context.getColumnByName(edmProperty.getName());
    ElementSymbol symbol = new ElementSymbol(column.getName(), this.context.getGroupSymbol());
    update.addChange(symbol, new Reference(0));
    Class<?> lobType = DataTypeManager.getDataTypeClass(column.getRuntimeType());
    int sqlType = JDBCSQLTypeInfo.getSQLType(column.getRuntimeType());
    if (content == null) {
        this.params.add(new SQLParameter(null, sqlType));
    } else {
        Object value = null;
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return content;
            }
        };
        if (lobType.isAssignableFrom(SQLXML.class)) {
            value = new SQLXMLImpl(isf);
        } else if (lobType.isAssignableFrom(ClobType.class)) {
            value = new ClobImpl(isf, -1);
        } else if (lobType.isAssignableFrom(BlobType.class)) {
            value = new BlobImpl(isf);
        } else {
            throw new TeiidException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16031, column.getName()));
        }
        this.params.add(new SQLParameter(value, sqlType));
    }
    update.setCriteria(this.context.getCriteria());
    return update;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) Reference(org.teiid.query.sql.symbol.Reference) SQLParameter(org.teiid.odata.api.SQLParameter) InputStreamFactory(org.teiid.core.types.InputStreamFactory) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidException(org.teiid.core.TeiidException) ClobType(org.teiid.core.types.ClobType) Column(org.teiid.metadata.Column) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

Example 40 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method updateReference.

public Update updateReference(URI referenceId, boolean prepared, boolean delete) throws SQLException {
    try {
        if (referenceId != null) {
            UriInfo uriInfo = ODataSQLBuilder.buildUriInfo(referenceId, this.baseURI, this.serviceMetadata, this.odata);
            UriResourceEntitySet uriEnitytSet = (UriResourceEntitySet) uriInfo.asUriInfoResource().getUriResourceParts().get(0);
            if (this.collection) {
                this.updateTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            } else {
                this.referenceTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            }
        }
    } catch (UriParserException e) {
        throw new SQLException(e);
    } catch (URISyntaxException e) {
        throw new SQLException(e);
    } catch (UriValidationException e) {
        throw new SQLException(e);
    }
    try {
        Update update = new Update();
        update.setGroup(this.updateTable.getGroupSymbol());
        List<String> columnNames = DocumentNode.getColumnNames(this.updateTable.getFk().getColumns());
        for (int i = 0; i < columnNames.size(); i++) {
            Column column = this.updateTable.getFk().getColumns().get(i);
            String columnName = columnNames.get(i);
            ElementSymbol symbol = new ElementSymbol(columnName, this.updateTable.getGroupSymbol());
            EdmEntityType entityType = this.updateTable.getEdmEntityType();
            EdmProperty edmProperty = (EdmProperty) entityType.getProperty(columnName);
            // reference table keys will be null for delete scenario
            Object value = null;
            if (!delete) {
                UriParameter parameter = getParameter(this.updateTable.getFk().getReferenceColumns().get(i), this.referenceTable.getKeyPredicates());
                value = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), parameter.getText());
            }
            if (prepared) {
                update.addChange(symbol, new Reference(i++));
                this.params.add(ODataSQLBuilder.asParam(edmProperty, value));
            } else {
                update.addChange(symbol, new Constant(ODataSQLBuilder.asParam(edmProperty, value).getValue()));
            }
        }
        Criteria criteria = DocumentNode.buildEntityKeyCriteria(this.updateTable, null, this.metadata, this.odata, null, null);
        update.setCriteria(criteria);
        return update;
    } catch (TeiidException e) {
        throw new SQLException(e);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SQLException(java.sql.SQLException) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) URISyntaxException(java.net.URISyntaxException) Criteria(org.teiid.query.sql.lang.Criteria) Update(org.teiid.query.sql.lang.Update) TeiidException(org.teiid.core.TeiidException) UriValidationException(org.apache.olingo.server.core.uri.validator.UriValidationException) Column(org.teiid.metadata.Column) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) UriResourceEntitySet(org.apache.olingo.server.api.uri.UriResourceEntitySet) UriInfo(org.apache.olingo.server.api.uri.UriInfo) UriParserException(org.apache.olingo.server.core.uri.parser.UriParserException) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Aggregations

Reference (org.teiid.query.sql.symbol.Reference)42 Test (org.junit.Test)15 Constant (org.teiid.query.sql.symbol.Constant)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)14 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)12 Expression (org.teiid.query.sql.symbol.Expression)11 Limit (org.teiid.query.sql.lang.Limit)7 Query (org.teiid.query.sql.lang.Query)7 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)7 ArrayList (java.util.ArrayList)6 Criteria (org.teiid.query.sql.lang.Criteria)6 LinkedList (java.util.LinkedList)5 From (org.teiid.query.sql.lang.From)5 Select (org.teiid.query.sql.lang.Select)5 SetQuery (org.teiid.query.sql.lang.SetQuery)5 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)5 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)5 Column (org.teiid.metadata.Column)4 Command (org.teiid.query.sql.lang.Command)4 SPParameter (org.teiid.query.sql.lang.SPParameter)4