Search in sources :

Example 11 with Parameter

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());
}
Also used : NamedTable(org.teiid.language.NamedTable) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) ArrayList(java.util.ArrayList) Insert(org.teiid.language.Insert) BatchResult(com.sforce.async.BatchResult) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Result(com.sforce.async.Result) BatchResult(com.sforce.async.BatchResult) ExecutionContext(org.teiid.translator.ExecutionContext) Expression(org.teiid.language.Expression) JobInfo(com.sforce.async.JobInfo) Parameter(org.teiid.language.Parameter) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 12 with Parameter

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());
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Table(org.teiid.metadata.Table) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) ArrayList(java.util.ArrayList) Insert(org.teiid.query.sql.lang.Insert) Parameter(org.teiid.language.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 13 with Parameter

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);
}
Also used : Parameter(org.teiid.language.Parameter) Insert(org.teiid.language.Insert) Test(org.junit.Test)

Aggregations

Parameter (org.teiid.language.Parameter)13 Test (org.junit.Test)10 Insert (org.teiid.language.Insert)8 Expression (org.teiid.language.Expression)7 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ArrayList (java.util.ArrayList)5 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)5 List (java.util.List)4 ColumnReference (org.teiid.language.ColumnReference)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3 Table (org.teiid.metadata.Table)3 ExecutionContext (org.teiid.translator.ExecutionContext)3 BatchResult (com.sforce.async.BatchResult)2 JobInfo (com.sforce.async.JobInfo)2 Result (com.sforce.async.Result)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Comparison (org.teiid.language.Comparison)2