Search in sources :

Example 1 with Update

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

the class TestPreparedStatementBatchedUpdate method testBatchedUpdatePushdown.

@Test
public void testBatchedUpdatePushdown() throws Exception {
    // Create query
    // $NON-NLS-1$
    String preparedSql = "UPDATE pm1.g1 SET pm1.g1.e1=?, pm1.g1.e3=? WHERE pm1.g1.e2=?";
    // Create a testable prepared plan cache
    SessionAwareCache<PreparedPlan> prepPlanCache = new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0);
    // Construct data manager with data
    HardcodedDataManager dataManager = new HardcodedDataManager();
    // $NON-NLS-1$
    dataManager.addData("UPDATE pm1.g1 SET e1 = ?, e3 = ? WHERE pm1.g1.e2 = ?", new List[] { Arrays.asList(4) });
    // Source capabilities must support batched updates
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setCapabilitySupport(Capability.BULK_UPDATE, true);
    // $NON-NLS-1$
    capFinder.addCapabilities("pm1", caps);
    // batch with two commands
    ArrayList<ArrayList<Object>> values = new ArrayList<ArrayList<Object>>(2);
    // $NON-NLS-1$
    values.add(new ArrayList<Object>(Arrays.asList(new Object[] { "a", Boolean.FALSE, new Integer(0) })));
    values.add(new ArrayList<Object>(Arrays.asList(new Object[] { null, Boolean.FALSE, new Integer(1) })));
    List<?>[] expected = new List[] { Arrays.asList(4) };
    // Create the plan and process the query
    TestPreparedStatement.helpTestProcessing(preparedSql, values, expected, dataManager, capFinder, RealMetadataFactory.example1Cached(), prepPlanCache, false, false, false, RealMetadataFactory.example1VDB());
    Update update = (Update) dataManager.getCommandHistory().iterator().next();
    assertTrue(((Constant) update.getChangeList().getClauses().get(0).getValue()).isMultiValued());
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) ArrayList(java.util.ArrayList) Update(org.teiid.query.sql.lang.Update) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 2 with Update

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

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

Example 4 with Update

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

the class UpdateResolver method resolveProceduralCommand.

/**
 * @see org.teiid.query.resolver.ProcedureContainerResolver#resolveProceduralCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter)
 */
public void resolveProceduralCommand(Command command, TempMetadataAdapter metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    // Cast to known type
    Update update = (Update) command;
    // Resolve elements and functions
    Set<GroupSymbol> groups = new HashSet<GroupSymbol>();
    groups.add(update.getGroup());
    for (SetClause clause : update.getChangeList().getClauses()) {
        ResolverVisitor.resolveLanguageObject(clause.getSymbol(), groups, null, metadata);
    }
    QueryResolver.resolveSubqueries(command, metadata, groups);
    ResolverVisitor.resolveLanguageObject(update, groups, update.getExternalGroupContexts(), metadata);
}
Also used : GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Update(org.teiid.query.sql.lang.Update) HashSet(java.util.HashSet) SetClause(org.teiid.query.sql.lang.SetClause)

Example 5 with Update

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

the class TestGroupCollectorVisitor method testUpdate.

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

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