Search in sources :

Example 1 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class LocalClient method executeUpdate.

@Override
public UpdateResponse executeUpdate(Command query, List<SQLParameter> parameters) throws SQLException {
    String sql = query.toString();
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql);
    final PreparedStatementImpl stmt = getConnection().prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, Statement.RETURN_GENERATED_KEYS);
    if (!parameters.isEmpty()) {
        for (int i = 0; i < parameters.size(); i++) {
            stmt.setObject(i + 1, parameters.get(i).getValue(), parameters.get(i).getSqlType());
        }
    }
    final int count = stmt.executeUpdate();
    final Map<String, Object> keys = getGeneratedKeys(stmt.getGeneratedKeys());
    stmt.close();
    return new UpdateResponse() {

        @Override
        public Map<String, Object> getGeneratedKeys() {
            return keys;
        }

        @Override
        public int getUpdateCount() {
            return count;
        }
    };
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) CacheHint(org.teiid.query.sql.lang.CacheHint)

Example 2 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method performDeepInsert.

private UpdateResponse performDeepInsert(String rawURI, UriInfo uriInfo, EdmEntityType entityType, Entity entity, List<ExpandNode> expandNodes) throws SQLException, TeiidException {
    UpdateResponse response = performInsert(rawURI, uriInfo, entityType, entity);
    for (String navigationName : entityType.getNavigationPropertyNames()) {
        EdmNavigationProperty navProperty = entityType.getNavigationProperty(navigationName);
        Link navLink = entity.getNavigationLink(navigationName);
        if (navLink != null && navLink.getInlineEntity() != null) {
            ExpandNode node = new ExpandNode();
            node.navigationProperty = navProperty;
            expandNodes.add(node);
            performDeepInsert(rawURI, uriInfo, navProperty.getType(), navLink.getInlineEntity(), node.children);
        } else if (navLink != null && navLink.getInlineEntitySet() != null && !navLink.getInlineEntitySet().getEntities().isEmpty()) {
            ExpandNode node = new ExpandNode();
            node.navigationProperty = navProperty;
            expandNodes.add(node);
            for (Entity inlineEntity : navLink.getInlineEntitySet().getEntities()) {
                performDeepInsert(rawURI, uriInfo, navProperty.getType(), inlineEntity, node.children);
            }
        }
    }
    return response;
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) Entity(org.apache.olingo.commons.api.data.Entity) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) Link(org.apache.olingo.commons.api.data.Link)

Example 3 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method deleteEntity.

@Override
public void deleteEntity(DataRequest request, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Delete delete = visitor.delete();
        updateResponse = getClient().executeUpdate(delete, 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.writeDeletedEntityOrReference();
    } else {
        // since DELETE is idempotent same response as otherwise success operation.
        response.writeDeletedEntityOrReference();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Example 4 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method upsertStreamProperty.

@Override
public void upsertStreamProperty(DataRequest request, String entityETag, InputStream streamContent, NoContentResponse response) throws ODataLibraryException, ODataApplicationException {
    UpdateResponse updateResponse = null;
    EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateStreamProperty(edmProperty, streamContent);
        updateResponse = getClient().executeUpdate(update, visitor.getParameters());
    } catch (SQLException | TeiidException 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) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException)

Example 5 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method updateProperty.

/**
 * since Teiid only deals with primitive types, merge does not apply
 */
@Override
public void updateProperty(DataRequest request, Property property, boolean rawValue, boolean merge, String entityETag, PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateProperty(edmProperty, property, this.prepared, rawValue);
        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);
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writePropertyUpdated();
    } else {
        response.writeNotModified();
    }
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException)

Aggregations

UpdateResponse (org.teiid.odata.api.UpdateResponse)8 SQLException (java.sql.SQLException)6 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)6 TeiidException (org.teiid.core.TeiidException)4 Update (org.teiid.query.sql.lang.Update)4 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)2 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)2 Delete (org.teiid.query.sql.lang.Delete)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Entity (org.apache.olingo.commons.api.data.Entity)1 Link (org.apache.olingo.commons.api.data.Link)1 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)1 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)1 UriParameter (org.apache.olingo.server.api.uri.UriParameter)1 PreparedStatementImpl (org.teiid.jdbc.PreparedStatementImpl)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1 Insert (org.teiid.query.sql.lang.Insert)1 Query (org.teiid.query.sql.lang.Query)1