Search in sources :

Example 1 with UpdateExecution

use of org.teiid.translator.UpdateExecution in project teiid by teiid.

the class TestUpdates method testBulkUpdate.

@Test
public void testBulkUpdate() throws Exception {
    CassandraExecutionFactory cef = new CassandraExecutionFactory();
    String input = "insert into pm1.g1 (e1) values ('a')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    Insert insert = (Insert) command;
    Parameter p = new Parameter();
    p.setType(String.class);
    p.setValueIndex(0);
    ((ExpressionValueSource) insert.getValueSource()).getValues().set(0, p);
    insert.setParameterValues(Arrays.asList(Arrays.asList("a"), Arrays.asList("b")).iterator());
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    CassandraConnection connection = Mockito.mock(CassandraConnection.class);
    ResultSetFuture rsf = Mockito.mock(ResultSetFuture.class);
    Mockito.stub(rsf.isDone()).toReturn(true);
    Mockito.stub(connection.executeBatch(Mockito.eq("INSERT INTO g1 (e1) VALUES (?)"), (List<Object[]>) Mockito.anyObject())).toReturn(rsf);
    UpdateExecution execution = (UpdateExecution) cef.createExecution(command, ec, rm, connection);
    execution.execute();
    assertArrayEquals(new int[] { 2 }, execution.getUpdateCounts());
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) Parameter(org.teiid.language.Parameter) UpdateExecution(org.teiid.translator.UpdateExecution) Insert(org.teiid.language.Insert) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Example 2 with UpdateExecution

use of org.teiid.translator.UpdateExecution in project teiid by teiid.

the class TestSimpleDBExecution method testDeleteAll.

@Test
public void testDeleteAll() throws Exception {
    String query = "delete from item";
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    UpdateExecution exec = translator.createUpdateExecution(cmd, context, Mockito.mock(RuntimeMetadata.class), connection);
    exec.execute();
    Mockito.verify(connection).deleteDomain("item");
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) UpdateExecution(org.teiid.translator.UpdateExecution) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Example 3 with UpdateExecution

use of org.teiid.translator.UpdateExecution in project teiid by teiid.

the class TestSimpleDBExecution method testInsert.

@Test
public void testInsert() throws Exception {
    String query = "insert into item (\"itemName()\", attribute, somedate, strarray) values ('one', 'value', {ts '2014-04-04 10:50:45'}, ('1', '2'))";
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    UpdateExecution exec = translator.createUpdateExecution(cmd, context, Mockito.mock(RuntimeMetadata.class), connection);
    exec.execute();
    SimpleDBInsertVisitor visitor = new SimpleDBInsertVisitor();
    visitor.visitNode(cmd);
    ArgumentCaptor<Iterator> argument = ArgumentCaptor.forClass(Iterator.class);
    ArgumentCaptor<String> itemName = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<ArrayList> columns = ArgumentCaptor.forClass(ArrayList.class);
    Mockito.verify(connection).performInsert(itemName.capture(), columns.capture(), argument.capture());
    assertEquals("item", itemName.getValue());
    List<?> values = (List<?>) argument.getValue().next();
    assertEquals("value", values.get(1));
    assertEquals("2014-04-04 10:50:45.0", values.get(2).toString());
    assertEquals("1,2,", arrayToStr((String[]) values.get(3)));
}
Also used : ArrayList(java.util.ArrayList) UpdateExecution(org.teiid.translator.UpdateExecution) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SimpleDBInsertVisitor(org.teiid.translator.simpledb.SimpleDBInsertVisitor) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with UpdateExecution

use of org.teiid.translator.UpdateExecution in project teiid by teiid.

the class ConnectorHost method readResultsFromExecution.

private List<List> readResultsFromExecution(Execution execution) throws TranslatorException {
    List<List> results = new ArrayList<List>();
    while (true) {
        try {
            if (execution instanceof ResultSetExecution) {
                ResultSetExecution rs = (ResultSetExecution) execution;
                List result = null;
                while ((result = rs.next()) != null) {
                    results.add(result);
                }
                break;
            }
            UpdateExecution rs = (UpdateExecution) execution;
            int[] result = rs.getUpdateCounts();
            for (int i = 0; i < result.length; i++) {
                results.add(Arrays.asList(result[i]));
            }
            break;
        } catch (DataNotAvailableException e) {
            if (e.getRetryDelay() > 0) {
                try {
                    Thread.sleep(e.getRetryDelay());
                } catch (InterruptedException e1) {
                    throw new TranslatorException(e1);
                }
            }
        }
    }
    return results;
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) ArrayList(java.util.ArrayList) UpdateExecution(org.teiid.translator.UpdateExecution) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException)

Example 5 with UpdateExecution

use of org.teiid.translator.UpdateExecution in project teiid by teiid.

the class TestMongoDBUpdateExecution method helpUpdate.

private DBCollection helpUpdate(String query, String[] expectedCollection, DBObject match_result, ArrayList<DBObject> results) throws TranslatorException {
    Command cmd = this.utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    CommandContext cc = Mockito.mock(CommandContext.class);
    Mockito.stub(context.getCommandContext()).toReturn(cc);
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    DB db = Mockito.mock(DB.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    for (String collection : expectedCollection) {
        Mockito.stub(db.getCollection(collection)).toReturn(dbCollection);
    }
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Mockito.stub(db.getCollectionFromString(Mockito.anyString())).toReturn(dbCollection);
    Mockito.stub(dbCollection.findOne(Mockito.any(BasicDBObject.class))).toReturn(match_result);
    WriteResult result = Mockito.mock(WriteResult.class);
    Mockito.stub(result.getN()).toReturn(1);
    Mockito.stub(dbCollection.insert(Mockito.any(BasicDBObject.class), Mockito.any(WriteConcern.class))).toReturn(result);
    Mockito.stub(dbCollection.update(Mockito.any(BasicDBObject.class), Mockito.any(BasicDBObject.class), Mockito.eq(false), Mockito.eq(true), Mockito.any(WriteConcern.class))).toReturn(result);
    if (results != null) {
        Cursor out = new ResultsCursor(results);
        for (DBObject obj : results) {
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
        }
    }
    UpdateExecution execution = this.translator.createUpdateExecution(cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    return dbCollection;
}
Also used : CommandContext(org.teiid.CommandContext) UpdateExecution(org.teiid.translator.UpdateExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection)

Aggregations

UpdateExecution (org.teiid.translator.UpdateExecution)21 Test (org.junit.Test)16 Command (org.teiid.language.Command)16 ExecutionContext (org.teiid.translator.ExecutionContext)14 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10 List (java.util.List)5 TranslationUtility (org.teiid.cdk.api.TranslationUtility)4 ResultSetExecution (org.teiid.translator.ResultSetExecution)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 QueryExpression (org.teiid.language.QueryExpression)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 TranslatorException (org.teiid.translator.TranslatorException)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2 DataSource (javax.activation.DataSource)2 CommandContext (org.teiid.CommandContext)2