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