Search in sources :

Example 26 with Insert

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

the class TestGroupCollectorVisitor method testInsert.

public void testInsert() {
    GroupSymbol gs1 = exampleGroupSymbol(1);
    Insert insert = new Insert();
    insert.setGroup(gs1);
    Set groups = new HashSet();
    groups.add(gs1);
    helpTestGroups(insert, true, groups);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Insert(org.teiid.query.sql.lang.Insert) HashSet(java.util.HashSet)

Example 27 with Insert

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

the class TestStaticSymbolMappingVisitor method testVisitInsert1.

public void testVisitInsert1() {
    Insert insert = new Insert();
    insert.setGroup(exampleGroup(true, 0));
    List vars = new ArrayList();
    vars.add(exampleElement(true, 0));
    vars.add(exampleElement(true, 1));
    insert.setVariables(vars);
    List values = new ArrayList();
    // $NON-NLS-1$
    values.add(new Constant("abc"));
    // $NON-NLS-1$
    values.add(new Constant("abc"));
    insert.setValues(values);
    helpTest(insert, getSymbolMap());
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Insert(org.teiid.query.sql.lang.Insert)

Example 28 with Insert

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

the class TeiidServiceHandler method performInsert.

private UpdateResponse performInsert(String rawURI, UriInfo uriInfo, EdmEntityType entityType, Entity entity) throws SQLException, TeiidException {
    ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, rawURI, this.serviceMetadata);
    visitor.visit(uriInfo);
    Insert command = visitor.insert(entityType, entity, null, this.prepared);
    return getClient().executeUpdate(command, visitor.getParameters());
}
Also used : Insert(org.teiid.query.sql.lang.Insert)

Example 29 with Insert

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

Aggregations

Insert (org.teiid.query.sql.lang.Insert)29 List (java.util.List)13 Command (org.teiid.query.sql.lang.Command)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Constant (org.teiid.query.sql.symbol.Constant)7 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)6 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)6 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)5 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)4 TupleSource (org.teiid.common.buffer.TupleSource)4 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)4 Delete (org.teiid.query.sql.lang.Delete)4 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)4 Update (org.teiid.query.sql.lang.Update)4 Expression (org.teiid.query.sql.symbol.Expression)4 CommandContext (org.teiid.query.util.CommandContext)4