use of org.teiid.language.Parameter in project teiid by teiid.
the class TestJDBCUpdateExecution method testPreparedBatchedUpdateFailed.
@Test
public void testPreparedBatchedUpdateFailed() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
Parameter param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
param.setValueIndex(0);
List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
values.set(0, param);
command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
Connection connection = Mockito.mock(Connection.class);
PreparedStatement s = Mockito.mock(PreparedStatement.class);
Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED }));
Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
JDBCExecutionFactory config = new JDBCExecutionFactory();
ResultSet r = Mockito.mock(ResultSet.class);
ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
Mockito.stub(r.getMetaData()).toReturn(rs);
FakeExecutionContextImpl context = new FakeExecutionContextImpl();
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
try {
updateExecution.execute();
fail();
} catch (TranslatorBatchException e) {
int[] counts = e.getUpdateCounts();
assertArrayEquals(new int[] { 1, -3 }, counts);
}
// test multiple batches
connection = Mockito.mock(Connection.class);
updateExecution = new JDBCUpdateExecution(command, connection, context, config);
command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
s = Mockito.mock(PreparedStatement.class);
Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
Mockito.stub(s.executeBatch()).toReturn(new int[] { 1 }).toThrow(new BatchUpdateException(new int[] { Statement.EXECUTE_FAILED }));
updateExecution.setMaxPreparedInsertBatchSize(1);
try {
updateExecution.execute();
fail();
} catch (TranslatorBatchException e) {
int[] counts = e.getUpdateCounts();
assertArrayEquals(new int[] { 1, -3 }, counts);
}
// test only a single update count
connection = Mockito.mock(Connection.class);
updateExecution = new JDBCUpdateExecution(command, connection, context, config);
command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
s = Mockito.mock(PreparedStatement.class);
Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1 }));
try {
updateExecution.execute();
fail();
} catch (TranslatorBatchException e) {
int[] counts = e.getUpdateCounts();
assertArrayEquals(new int[] { 1 }, counts);
}
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestJDBCUpdateExecution method testInsertIteratorUpdate.
@Test
public void testInsertIteratorUpdate() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey, IntNum) values (1, 2)");
Parameter param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
param.setValueIndex(0);
List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
values.set(0, param);
param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
param.setValueIndex(1);
values.set(1, param);
command.setParameterValues(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 2)).iterator());
Connection connection = Mockito.mock(Connection.class);
PreparedStatement p = Mockito.mock(PreparedStatement.class);
Mockito.stub(p.executeBatch()).toReturn(new int[] { 1, 1 });
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, ?)")).toReturn(p);
JDBCExecutionFactory config = new JDBCExecutionFactory();
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
updateExecution.execute();
Mockito.verify(p, Mockito.times(2)).addBatch();
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestJDBCUpdateExecution method testPreparedInsertWithGeometry.
@Test
public void testPreparedInsertWithGeometry() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into cola_markets(name,shape) values('foo124', ST_GeomFromText('POINT (300 100)', 8307))");
Parameter param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.STRING);
param.setValueIndex(0);
List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
values.set(0, param);
param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.GEOMETRY);
param.setValueIndex(1);
values.set(1, param);
GeometryType value = new GeometryType();
value.setSrid(123);
command.setParameterValues(Arrays.asList(Arrays.asList("a", value)).iterator());
Connection connection = Mockito.mock(Connection.class);
PreparedStatement p = Mockito.mock(PreparedStatement.class);
Mockito.stub(p.executeBatch()).toReturn(new int[] { 1 });
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("INSERT INTO COLA_MARKETS (NAME, SHAPE) VALUES (?, st_geomfromwkb(?, ?))")).toReturn(p);
JDBCExecutionFactory config = new JDBCExecutionFactory();
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
updateExecution.execute();
Mockito.verify(p, Mockito.times(1)).addBatch();
Mockito.verify(p, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
Mockito.verify(p, Mockito.times(1)).setInt(3, 123);
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestJDBCUpdateExecution method testBulkUpdate.
@Test
public void testBulkUpdate() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
Parameter param = new Parameter();
param.setType(Integer.class);
param.setValueIndex(0);
ExpressionValueSource evs = new ExpressionValueSource(Arrays.asList((Expression) param));
command.setValueSource(evs);
List<List<?>> vals = new ArrayList<List<?>>();
for (int i = 0; i < 8; i++) {
vals.add(Arrays.asList(i));
}
command.setParameterValues(vals.iterator());
Connection connection = Mockito.mock(Connection.class);
PreparedStatement p = Mockito.mock(PreparedStatement.class);
Mockito.stub(p.executeBatch()).toReturn(new int[] { 1, 1 });
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(p);
JDBCExecutionFactory config = new JDBCExecutionFactory();
config.setMaxPreparedInsertBatchSize(2);
ResultSet r = Mockito.mock(ResultSet.class);
ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
Mockito.stub(r.getMetaData()).toReturn(rs);
Mockito.stub(p.getGeneratedKeys()).toReturn(r);
FakeExecutionContextImpl context = new FakeExecutionContextImpl();
((org.teiid.query.util.CommandContext) context.getCommandContext()).setReturnAutoGeneratedKeys(Collections.EMPTY_LIST);
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
updateExecution.execute();
assertArrayEquals(new int[] { 1, 1, 1, 1, 1, 1, 1, 1 }, updateExecution.getUpdateCounts());
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestBulkInsertExecution method testFlowAndErrorReturn.
@Test
public void testFlowAndErrorReturn() throws Exception {
NamedTable table = new NamedTable("temp", null, Mockito.mock(Table.class));
ArrayList<ColumnReference> elements = new ArrayList<ColumnReference>();
elements.add(new ColumnReference(table, "one", Mockito.mock(Column.class), Integer.class));
elements.add(new ColumnReference(table, "two", Mockito.mock(Column.class), String.class));
List<Expression> values = new ArrayList<Expression>();
Parameter param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
param.setValueIndex(0);
values.add(param);
param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.STRING);
param.setValueIndex(1);
values.add(param);
ExpressionValueSource valueSource = new ExpressionValueSource(values);
Insert insert = new Insert(table, elements, valueSource);
insert.setParameterValues(Arrays.asList(Arrays.asList(2, '2'), Arrays.asList(2, '2'), Arrays.asList(3, '3')).iterator());
Result r1 = Mockito.mock(Result.class);
Result r2 = Mockito.mock(Result.class);
Result r3 = Mockito.mock(Result.class);
Mockito.when(r1.isSuccess()).thenReturn(true);
Mockito.when(r1.isCreated()).thenReturn(true);
Mockito.when(r2.isSuccess()).thenReturn(true);
Mockito.when(r2.isCreated()).thenReturn(true);
Mockito.when(r3.isSuccess()).thenReturn(false);
Mockito.when(r3.isCreated()).thenReturn(false);
com.sforce.async.Error error = new com.sforce.async.Error();
error.setMessage("failed, check your data");
error.setStatusCode(StatusCode.CANNOT_DISABLE_LAST_ADMIN);
Mockito.when(r3.getErrors()).thenReturn(new Error[] { error });
BatchResult batchResult = Mockito.mock(BatchResult.class);
Mockito.when(batchResult.getResult()).thenReturn(new Result[] { r1 }).thenReturn((new Result[] { r2 })).thenReturn(new Result[] { r3 });
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
JobInfo jobInfo = Mockito.mock(JobInfo.class);
Mockito.when(connection.createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.insert), Mockito.eq(false))).thenReturn(jobInfo);
Mockito.when(connection.getBulkResults(Mockito.any(JobInfo.class), Mockito.anyList())).thenReturn(new BatchResult[] { batchResult, batchResult, batchResult });
SalesForceExecutionFactory config = new SalesForceExecutionFactory();
config.setMaxBulkInsertBatchSize(1);
InsertExecutionImpl updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
while (true) {
try {
updateExecution.execute();
org.junit.Assert.assertArrayEquals(new int[] { 1, 1, -3 }, updateExecution.getUpdateCounts());
break;
} catch (DataNotAvailableException e) {
continue;
}
}
Mockito.verify(connection, Mockito.times(1)).createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.insert), Mockito.eq(false));
Mockito.verify(connection, Mockito.times(1)).getBulkResults(Mockito.any(JobInfo.class), Mockito.anyList());
}
Aggregations