Search in sources :

Example 6 with BatchedUpdates

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

the class CassandraUpdateExecution method internalExecute.

private void internalExecute() throws TranslatorException {
    if (this.command instanceof BatchedUpdates) {
        handleBatchedUpdates();
        return;
    }
    CassandraSQLVisitor visitor = new CassandraSQLVisitor();
    visitor.translateSQL(this.command);
    String cql = visitor.getTranslatedSQL();
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Source-Query:", cql);
    this.executionContext.logCommand(cql);
    if (this.command instanceof BulkCommand) {
        BulkCommand bc = (BulkCommand) this.command;
        if (bc.getParameterValues() != null) {
            int count = 0;
            List<Object[]> newValues = new ArrayList<Object[]>();
            Iterator<? extends List<?>> values = bc.getParameterValues();
            while (values.hasNext()) {
                Object[] bindValues = values.next().toArray();
                for (int i = 0; i < bindValues.length; i++) {
                    if (bindValues[i] instanceof Blob) {
                        Blob blob = (Blob) bindValues[i];
                        try {
                            if (blob.length() > Integer.MAX_VALUE) {
                                // $NON-NLS-1$
                                throw new AssertionError("Blob is too large");
                            }
                            byte[] bytes = ((Blob) bindValues[i]).getBytes(0, (int) blob.length());
                            bindValues[i] = ByteBuffer.wrap(bytes);
                        } catch (SQLException e) {
                            throw new TranslatorException(e);
                        }
                    } else if (bindValues[i] instanceof BinaryType) {
                        bindValues[i] = ByteBuffer.wrap(((BinaryType) bindValues[i]).getBytesDirect());
                    }
                }
                newValues.add(bindValues);
                count++;
            }
            updateCount = count;
            resultSetFuture = connection.executeBatch(cql, newValues);
            return;
        }
    }
    resultSetFuture = connection.executeQuery(cql);
}
Also used : Blob(java.sql.Blob) BinaryType(org.teiid.core.types.BinaryType) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) BulkCommand(org.teiid.language.BulkCommand) TranslatorException(org.teiid.translator.TranslatorException) BatchedUpdates(org.teiid.language.BatchedUpdates)

Example 7 with BatchedUpdates

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

the class TestUpdates method testBatchedUpdate.

@Test
public void testBatchedUpdate() throws TranslatorException {
    CassandraExecutionFactory cef = new CassandraExecutionFactory();
    String input = "insert into pm1.g1 (e1) values ('a')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    Command command1 = util.parseCommand("update pm1.g1 set e1 = 'b'");
    command = new BatchedUpdates(Arrays.asList(command, command1));
    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(Arrays.asList("INSERT INTO g1 (e1) VALUES ('a')", "UPDATE g1 SET e1 = 'b'"))).toReturn(rsf);
    UpdateExecution execution = (UpdateExecution) cef.createExecution(command, ec, rm, connection);
    execution.execute();
    assertArrayEquals(new int[] { 2 }, execution.getUpdateCounts());
    Mockito.verify(connection).executeBatch(Arrays.asList("INSERT INTO g1 (e1) VALUES ('a')", "UPDATE g1 SET e1 = 'b'"));
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) UpdateExecution(org.teiid.translator.UpdateExecution) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) BatchedUpdates(org.teiid.language.BatchedUpdates) Test(org.junit.Test)

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