Search in sources :

Example 11 with Update

use of org.teiid.query.sql.lang.Update in project teiid by teiid.

the class UpdateResolver method getVariableValues.

/**
 * @see org.teiid.query.resolver.VariableResolver#getVariableValues(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.QueryMetadataInterface)
 */
public Map<ElementSymbol, Expression> getVariableValues(Command command, boolean changingOnly, QueryMetadataInterface metadata) throws QueryMetadataException, TeiidComponentException {
    Map<ElementSymbol, Expression> result = new HashMap<ElementSymbol, Expression>();
    Update update = (Update) command;
    Map<ElementSymbol, Expression> changing = update.getChangeList().getClauseMap();
    for (Entry<ElementSymbol, Expression> entry : changing.entrySet()) {
        ElementSymbol leftSymbol = entry.getKey().clone();
        leftSymbol.getGroupSymbol().setName(ProcedureReservedWords.CHANGING);
        leftSymbol.setType(DataTypeManager.DefaultDataClasses.BOOLEAN);
        result.put(leftSymbol, new Constant(Boolean.TRUE));
        if (!changingOnly) {
            leftSymbol = entry.getKey().clone();
            leftSymbol.getGroupSymbol().setName(SQLConstants.Reserved.NEW);
            result.put(leftSymbol, entry.getValue());
        }
    }
    Collection<ElementSymbol> insertElmnts = ResolverUtil.resolveElementsInGroup(update.getGroup(), metadata);
    insertElmnts.removeAll(changing.keySet());
    Iterator<ElementSymbol> defaultIter = insertElmnts.iterator();
    while (defaultIter.hasNext()) {
        ElementSymbol varSymbol = defaultIter.next().clone();
        varSymbol.getGroupSymbol().setName(ProcedureReservedWords.CHANGING);
        varSymbol.setType(DataTypeManager.DefaultDataClasses.BOOLEAN);
        result.put(varSymbol, new Constant(Boolean.FALSE));
    }
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Expression(org.teiid.query.sql.symbol.Expression) HashMap(java.util.HashMap) Constant(org.teiid.query.sql.symbol.Constant) Update(org.teiid.query.sql.lang.Update)

Example 12 with Update

use of org.teiid.query.sql.lang.Update in project teiid by teiid.

the class TestStaticSymbolMappingVisitor method testVisitUpdate2.

public void testVisitUpdate2() {
    Update update = new Update();
    update.setGroup(exampleGroup(true, 0));
    // $NON-NLS-1$
    update.addChange(exampleElement(true, 0), new Constant("abc"));
    // $NON-NLS-1$
    update.addChange(exampleElement(true, 1), new Constant("abc"));
    // $NON-NLS-1$
    update.setCriteria(new CompareCriteria(exampleElement(true, 2), CompareCriteria.LT, new Constant("xyz")));
    helpTest(update, getSymbolMap());
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) Update(org.teiid.query.sql.lang.Update) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 13 with Update

use of org.teiid.query.sql.lang.Update 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)

Example 14 with Update

use of org.teiid.query.sql.lang.Update in project teiid by teiid.

the class TeiidServiceHandler method updateEntity.

@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    if (merge) {
        try {
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            Update update = visitor.update(entityType, entity, this.prepared);
            updateResponse = getClient().executeUpdate(update, visitor.getParameters());
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        }
    } else {
        // delete, then insert
        String txn = startTransaction();
        boolean success = false;
        try {
            // build insert first as it could fail to validate
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            List<UriParameter> keys = request.getKeyPredicates();
            Insert command = visitor.insert(entityType, entity, keys, this.prepared);
            // run delete
            ODataSQLBuilder deleteVisitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            deleteVisitor.visit(request.getUriInfo());
            Delete delete = deleteVisitor.delete();
            updateResponse = getClient().executeUpdate(delete, deleteVisitor.getParameters());
            // run insert
            updateResponse = getClient().executeUpdate(command, visitor.getParameters());
            commit(txn);
            success = true;
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } finally {
            if (!success) {
                rollback(txn);
            }
        }
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeUpdatedEntity();
    } else {
        response.writeNotModified();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SQLException(java.sql.SQLException) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) Update(org.teiid.query.sql.lang.Update) Insert(org.teiid.query.sql.lang.Insert) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException) UpdateResponse(org.teiid.odata.api.UpdateResponse) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 15 with Update

use of org.teiid.query.sql.lang.Update in project teiid by teiid.

the class TeiidServiceHandler method manageReference.

private void manageReference(DataRequest request, URI referenceId, NoContentResponse response, boolean delete) throws ODataApplicationException {
    UpdateResponse updateResponse = null;
    try {
        ReferenceUpdateSQLBuilder visitor = new ReferenceUpdateSQLBuilder(getClient().getMetadataStore(), request.getODataRequest().getRawBaseUri(), this.serviceMetadata, this.odata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateReference(referenceId, this.prepared, delete);
        updateResponse = getClient().executeUpdate(update, visitor.getParameters());
    } catch (SQLException e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeNoContent();
    } else {
        response.writeNotModified();
    }
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Aggregations

Update (org.teiid.query.sql.lang.Update)15 SQLException (java.sql.SQLException)5 Constant (org.teiid.query.sql.symbol.Constant)5 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)4 TeiidException (org.teiid.core.TeiidException)4 UpdateResponse (org.teiid.odata.api.UpdateResponse)4 Delete (org.teiid.query.sql.lang.Delete)4 Insert (org.teiid.query.sql.lang.Insert)4 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 HashSet (java.util.HashSet)3 List (java.util.List)3 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)2 UriParameter (org.apache.olingo.server.api.uri.UriParameter)2 Test (org.junit.Test)2 Criteria (org.teiid.query.sql.lang.Criteria)2 URISyntaxException (java.net.URISyntaxException)1