Search in sources :

Example 1 with BatchedUpdates

use of org.teiid.language.BatchedUpdates in project teiid by teiid.

the class CassandraUpdateExecution method handleBatchedUpdates.

private void handleBatchedUpdates() {
    BatchedUpdates updates = (BatchedUpdates) this.command;
    List<String> cqlUpdates = new ArrayList<String>();
    for (Command update : updates.getUpdateCommands()) {
        CassandraSQLVisitor visitor = new CassandraSQLVisitor();
        visitor.translateSQL(update);
        String cql = visitor.getTranslatedSQL();
        cqlUpdates.add(cql);
    }
    this.updateCount = cqlUpdates.size();
    resultSetFuture = connection.executeBatch(cqlUpdates);
}
Also used : Command(org.teiid.language.Command) BulkCommand(org.teiid.language.BulkCommand) ArrayList(java.util.ArrayList) BatchedUpdates(org.teiid.language.BatchedUpdates)

Example 2 with BatchedUpdates

use of org.teiid.language.BatchedUpdates in project teiid by teiid.

the class ConnectorHost method executeBatchedUpdates.

public int[] executeBatchedUpdates(Command[] commands, RuntimeMetadata runtimeMetadata) throws TranslatorException {
    List<List> result = executeCommand(new BatchedUpdates(Arrays.asList(commands)), runtimeMetadata, true);
    int[] counts = new int[result.size()];
    for (int i = 0; i < counts.length; i++) {
        counts[i] = ((Integer) result.get(i).get(0)).intValue();
    }
    return counts;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BatchedUpdates(org.teiid.language.BatchedUpdates)

Example 3 with BatchedUpdates

use of org.teiid.language.BatchedUpdates in project teiid by teiid.

the class TestJDBCUpdateExecution method testBatchedUpdateFailed.

@Test
public void testBatchedUpdateFailed() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
    // $NON-NLS-1$
    Insert command1 = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (StringKey) values ('1')");
    Connection connection = Mockito.mock(Connection.class);
    Statement s = Mockito.mock(Statement.class);
    Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { Statement.EXECUTE_FAILED }));
    Mockito.stub(connection.createStatement()).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    Mockito.stub(s.getGeneratedKeys()).toReturn(r);
    FakeExecutionContextImpl context = new FakeExecutionContextImpl();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(new BatchedUpdates(Arrays.asList((Command) command, command1)), connection, context, config);
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { -3 }, counts);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Insert(org.teiid.language.Insert) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) BatchUpdateException(java.sql.BatchUpdateException) BatchedUpdates(org.teiid.language.BatchedUpdates) Test(org.junit.Test)

Example 4 with BatchedUpdates

use of org.teiid.language.BatchedUpdates in project teiid by teiid.

the class TestJDBCUpdateExecution method testBatchedUpdate.

@Test
public void testBatchedUpdate() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
    // $NON-NLS-1$
    Insert command1 = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (StringKey) values ('1')");
    Connection connection = Mockito.mock(Connection.class);
    Statement s = Mockito.mock(Statement.class);
    Mockito.stub(s.executeBatch()).toReturn(new int[] { 1, 1 });
    Mockito.stub(connection.createStatement()).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    Mockito.stub(s.getGeneratedKeys()).toReturn(r);
    FakeExecutionContextImpl context = new FakeExecutionContextImpl();
    ((org.teiid.query.util.CommandContext) context.getCommandContext()).setReturnAutoGeneratedKeys(Collections.EMPTY_LIST);
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(new BatchedUpdates(Arrays.asList((Command) command, command1)), connection, context, config);
    updateExecution.execute();
    assertArrayEquals(new int[] { 1, 1 }, updateExecution.getUpdateCounts());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Insert(org.teiid.language.Insert) BatchedUpdates(org.teiid.language.BatchedUpdates) Test(org.junit.Test)

Example 5 with BatchedUpdates

use of org.teiid.language.BatchedUpdates in project teiid by teiid.

the class ConnectorWorkItem method setExecution.

private void setExecution(Command command, org.teiid.language.Command translatedCommand, final Execution exec) {
    if (translatedCommand instanceof Call) {
        // $NON-NLS-1$
        this.execution = Assertion.isInstanceOf(exec, ProcedureExecution.class, "Call Executions are expected to be ProcedureExecutions");
        StoredProcedure proc = (StoredProcedure) command;
        if (proc.returnParameters()) {
            this.procedureBatchHandler = new ProcedureBatchHandler((Call) translatedCommand, (ProcedureExecution) exec);
        }
    } else if (command instanceof QueryCommand) {
        // $NON-NLS-1$
        this.execution = Assertion.isInstanceOf(exec, ResultSetExecution.class, "QueryExpression Executions are expected to be ResultSetExecutions");
    } else {
        final boolean singleUpdateCount = connector.returnsSingleUpdateCount() && (translatedCommand instanceof BatchedUpdates || (translatedCommand instanceof BulkCommand && ((BulkCommand) translatedCommand).getParameterValues() != null));
        // $NON-NLS-1$
        Assertion.isInstanceOf(exec, UpdateExecution.class, "Update Executions are expected to be UpdateExecutions");
        this.execution = new ResultSetExecution() {

            private int[] results;

            private int index;

            @Override
            public void cancel() throws TranslatorException {
                exec.cancel();
            }

            @Override
            public void close() {
                exec.close();
            }

            @Override
            public void execute() throws TranslatorException {
                exec.execute();
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                if (results == null) {
                    results = ((UpdateExecution) exec).getUpdateCounts();
                }
                if (singleUpdateCount) {
                    if (index++ < results[0]) {
                        return CollectionTupleSource.UPDATE_ROW;
                    }
                    return null;
                }
                if (index < results.length) {
                    return Arrays.asList(results[index++]);
                }
                return null;
            }
        };
    }
}
Also used : Call(org.teiid.language.Call) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) BulkCommand(org.teiid.language.BulkCommand) QueryCommand(org.teiid.query.sql.lang.QueryCommand) BatchedUpdates(org.teiid.language.BatchedUpdates)

Aggregations

BatchedUpdates (org.teiid.language.BatchedUpdates)7 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 BulkCommand (org.teiid.language.BulkCommand)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)2 Command (org.teiid.language.Command)2 Insert (org.teiid.language.Insert)2 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 BatchUpdateException (java.sql.BatchUpdateException)1 Blob (java.sql.Blob)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 TranslationUtility (org.teiid.cdk.api.TranslationUtility)1 BinaryType (org.teiid.core.types.BinaryType)1 Call (org.teiid.language.Call)1