use of org.teiid.language.Parameter in project teiid by teiid.
the class TestBulkInsertExecution method testFlowAndInvocationStack.
@Test
public void testFlowAndInvocationStack() 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(true);
Mockito.when(r3.isCreated()).thenReturn(true);
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, 1 }, 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());
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestPreparedStatementBatchedUpdate method testBatchedUpdatePushdown1.
@Test
public void testBatchedUpdatePushdown1() throws Exception {
// TODO: just use straight ddl
TransformationMetadata metadata = TestUpdateValidator.example1();
TestUpdateValidator.createView("select 1 as x, 2 as y", metadata, "GX");
Table t = metadata.getMetadataStore().getSchemas().get("VM1").getTables().get("GX");
t.setDeletePlan("");
t.setUpdatePlan("");
t.setInsertPlan("FOR EACH ROW BEGIN insert into pm1.g1 (e1) values (new.x); END");
// $NON-NLS-1$
String preparedSql = "insert into gx (x, y) values (?,?)";
// Create a testable prepared plan cache
SessionAwareCache<PreparedPlan> prepPlanCache = new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0);
// Construct data manager with data
HardcodedDataManager dataManager = new HardcodedDataManager(metadata);
// $NON-NLS-1$
dataManager.addData("INSERT INTO g1 (e1) VALUES (convert(?, string))", new List[] { Arrays.asList(2) });
// Source capabilities must support batched updates
FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
caps.setFunctionSupport(SourceSystemFunctions.CONVERT, true);
caps.setCapabilitySupport(Capability.BULK_UPDATE, true);
// $NON-NLS-1$
capFinder.addCapabilities("pm1", caps);
// batch with two commands
ArrayList<ArrayList<Object>> values = new ArrayList<ArrayList<Object>>(2);
// $NON-NLS-1$
values.add(new ArrayList<Object>(Arrays.asList(3, 4)));
values.add(new ArrayList<Object>(Arrays.asList(5, 6)));
List<?>[] expected = new List[] { Arrays.asList(2) };
// Create the plan and process the query
TestPreparedStatement.helpTestProcessing(preparedSql, values, expected, dataManager, capFinder, metadata, prepPlanCache, false, false, false, RealMetadataFactory.example1VDB());
org.teiid.language.Insert insert = (org.teiid.language.Insert) dataManager.getPushdownCommands().iterator().next();
Parameter p = CollectorVisitor.collectObjects(Parameter.class, insert).iterator().next();
assertEquals(0, p.getValueIndex());
assertEquals(Arrays.asList(3), insert.getParameterValues().next());
assertTrue(insert.getParameterValues().hasNext());
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestN1QLUpdateVisitor method testInsertBulk.
@Test
public void testInsertBulk() {
String sql = "INSERT INTO Customer (documentID) VALUES ('customer-1')";
Insert insert = (Insert) translationUtility.parseCommand(sql);
Parameter p = new Parameter();
p.setType(String.class);
p.setValueIndex(0);
((ExpressionValueSource) insert.getValueSource()).getValues().set(0, p);
insert.setParameterValues(Arrays.asList(Arrays.asList("customer-1"), Arrays.asList("customer-2")).iterator());
N1QLUpdateVisitor visitor = TRANSLATOR.getN1QLUpdateVisitor();
visitor.append(insert);
String actual = visitor.getBulkCommands()[0];
assertEquals("INSERT INTO `test` (KEY, VALUE) VALUES ('customer-1', {\"type\":\"Customer\"}), VALUES ('customer-2', {\"type\":\"Customer\"}) RETURNING META(`test`).id AS PK", actual);
}
Aggregations