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);
}
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'"));
}
Aggregations