Search in sources :

Example 1 with Parameter

use of org.teiid.language.Parameter in project teiid by teiid.

the class TestUpdates method testBulkUpdate.

@Test
public void testBulkUpdate() throws Exception {
    CassandraExecutionFactory cef = new CassandraExecutionFactory();
    String input = "insert into pm1.g1 (e1) values ('a')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    Insert insert = (Insert) command;
    Parameter p = new Parameter();
    p.setType(String.class);
    p.setValueIndex(0);
    ((ExpressionValueSource) insert.getValueSource()).getValues().set(0, p);
    insert.setParameterValues(Arrays.asList(Arrays.asList("a"), Arrays.asList("b")).iterator());
    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(Mockito.eq("INSERT INTO g1 (e1) VALUES (?)"), (List<Object[]>) Mockito.anyObject())).toReturn(rsf);
    UpdateExecution execution = (UpdateExecution) cef.createExecution(command, ec, rm, connection);
    execution.execute();
    assertArrayEquals(new int[] { 2 }, execution.getUpdateCounts());
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) Parameter(org.teiid.language.Parameter) UpdateExecution(org.teiid.translator.UpdateExecution) Insert(org.teiid.language.Insert) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Example 2 with Parameter

use of org.teiid.language.Parameter in project teiid by teiid.

the class InsertExecutionImpl method buildBulkRowPayload.

protected List<com.sforce.async.SObject> buildBulkRowPayload(Insert insert, Iterator<? extends List<?>> it, int rowCount) throws TranslatorException {
    List<com.sforce.async.SObject> rows = new ArrayList<com.sforce.async.SObject>();
    List<ColumnReference> columns = insert.getColumns();
    int boundCount = 0;
    List<Expression> literalValues = ((ExpressionValueSource) insert.getValueSource()).getValues();
    while (it.hasNext()) {
        if (boundCount >= rowCount) {
            break;
        }
        boundCount++;
        List<?> values = it.next();
        com.sforce.async.SObject sobj = new com.sforce.async.SObject();
        for (int i = 0; i < columns.size(); i++) {
            Expression ex = literalValues.get(i);
            ColumnReference element = columns.get(i);
            Column column = element.getMetadataObject();
            Class<?> type = ex.getType();
            Object value = null;
            if (ex instanceof Parameter) {
                value = values.get(((Parameter) ex).getValueIndex());
            } else if (!(ex instanceof Literal)) {
                throw new TranslatorException(SalesForcePlugin.Util.gs(SalesForcePlugin.Event.TEIID13007));
            } else {
                value = ((Literal) ex).getValue();
            }
            sobj.setField(column.getSourceName(), getStringValue(value, type));
        }
        rows.add(sobj);
    }
    return rows;
}
Also used : ArrayList(java.util.ArrayList) Expression(org.teiid.language.Expression) Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) SObject(com.sforce.async.SObject) Parameter(org.teiid.language.Parameter) SObject(com.sforce.async.SObject) TranslatorException(org.teiid.translator.TranslatorException) SObject(com.sforce.async.SObject) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 3 with Parameter

use of org.teiid.language.Parameter in project teiid by teiid.

the class JDBCBaseExecution method bind.

/**
 * Bind the values in the TranslatedCommand to the PreparedStatement
 */
protected void bind(PreparedStatement stmt, List<?> params, List<?> batchValues) throws SQLException {
    for (int i = 0; i < params.size(); i++) {
        Object paramValue = params.get(i);
        Object value = null;
        Class<?> paramType = null;
        if (paramValue instanceof Literal) {
            Literal litParam = (Literal) paramValue;
            value = litParam.getValue();
            paramType = litParam.getType();
        } else if (paramValue instanceof Argument) {
            Argument arg = (Argument) paramValue;
            value = ((Literal) arg.getExpression()).getValue();
            paramType = arg.getType();
        } else {
            Parameter param = (Parameter) paramValue;
            if (batchValues == null) {
                // $NON-NLS-1$
                throw new AssertionError("Expected batchValues when using a Parameter");
            }
            value = batchValues.get(param.getValueIndex());
            paramType = param.getType();
        }
        this.executionFactory.bindValue(stmt, value, paramType, i + 1);
    }
    if (batchValues != null) {
        stmt.addBatch();
    }
}
Also used : Argument(org.teiid.language.Argument) Literal(org.teiid.language.Literal) Parameter(org.teiid.language.Parameter)

Example 4 with Parameter

use of org.teiid.language.Parameter in project teiid by teiid.

the class TestDependentJoins method helpTestPushdown.

private void helpTestPushdown(boolean supportsArrayType) {
    // Create query
    // $NON-NLS-1$
    String sql = "SELECT pm1.g1.e1 FROM /*+ MAKEIND */ pm1.g1, pm2.g1 WHERE pm1.g1.e1 = pm2.g1.e1 AND pm1.g1.e2=pm2.g1.e2 order by pm1.g1.e1";
    // Create expected results
    List[] expected = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "a" }) };
    // Construct data manager with data
    HardcodedDataManager dataManager = new HardcodedDataManager(RealMetadataFactory.example1Cached());
    dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1), Arrays.asList("b", 2) });
    if (supportsArrayType) {
        dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE (g_0.e1, g_0.e2) = (?, ?) ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
    } else {
        dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE g_0.e1 = ? AND g_0.e2 = ? ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
    }
    BasicSourceCapabilities bsc = TestOptimizer.getTypicalCapabilities();
    bsc.setCapabilitySupport(Capability.DEPENDENT_JOIN, true);
    bsc.setCapabilitySupport(Capability.ARRAY_TYPE, supportsArrayType);
    bsc.setSourceProperty(Capability.MAX_DEPENDENT_PREDICATES, 1);
    bsc.setSourceProperty(Capability.MAX_IN_CRITERIA_SIZE, 1);
    DefaultCapabilitiesFinder dcf = new DefaultCapabilitiesFinder(bsc);
    // Plan query
    ProcessorPlan plan = TestProcessor.helpGetPlan(sql, RealMetadataFactory.example1Cached(), dcf);
    TestOptimizer.checkDependentJoinCount(plan, 1);
    // Run query
    TestProcessor.helpProcess(plan, dataManager, expected);
    Select s = (Select) dataManager.getPushdownCommands().get(1);
    assertEquals(1, s.getDependentValues().size());
    List<? extends List<?>> vals = s.getDependentValues().values().iterator().next();
    assertEquals(2, vals.size());
    if (supportsArrayType) {
        Comparison comp = (Comparison) s.getWhere();
        Parameter p = (Parameter) ((Array) comp.getRightExpression()).getExpressions().get(0);
        assertEquals(0, p.getValueIndex());
        assertNotNull(s.getDependentValues().get(p.getDependentValueId()));
    }
}
Also used : Array(org.teiid.language.Array) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) Comparison(org.teiid.language.Comparison) Select(org.teiid.language.Select) Parameter(org.teiid.language.Parameter) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)

Example 5 with Parameter

use of org.teiid.language.Parameter in project teiid by teiid.

the class TestOracleTranslator method testDependentJoin.

@Test
public void testDependentJoin() throws Exception {
    CommandBuilder commandBuilder = new CommandBuilder(getOracleSpecificMetadata());
    Select command = (Select) commandBuilder.getCommand("select id from smalla where description = 'a'");
    Parameter param = new Parameter();
    param.setType(TypeFacility.RUNTIME_TYPES.STRING);
    param.setDependentValueId("x");
    param.setValueIndex(0);
    Map<String, List<? extends List<?>>> dependentValues = new HashMap<String, List<? extends List<?>>>();
    dependentValues.put("x", Arrays.asList(Arrays.asList("a"), Arrays.asList("b")));
    command.setDependentValues(dependentValues);
    ((Comparison) command.getWhere()).setRightExpression(param);
    Connection connection = Mockito.mock(Connection.class);
    Statement statement = Mockito.mock(Statement.class);
    Mockito.stub(connection.createStatement()).toReturn(statement);
    PreparedStatement ps = Mockito.mock(PreparedStatement.class);
    Mockito.stub(ps.executeBatch()).toReturn(new int[] { -2, -2 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO TEIID_DKJ1 (COL1) VALUES (?)")).toReturn(ps);
    // we won't bother to retrieve the results, but we expect the following join query
    PreparedStatement ps1 = Mockito.mock(PreparedStatement.class);
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("SELECT SmallishA.ID FROM TEIID_DKJ1, SmallishA WHERE SmallishA.description = TEIID_DKJ1.COL1")).toReturn(ps1);
    OracleExecutionFactory ef = new OracleExecutionFactory() {

        public String getTemporaryTableName(String prefix) {
            // don't use random for testing
            return prefix;
        }
    };
    ef.setDatabaseVersion(Version.DEFAULT_VERSION);
    ef.start();
    JDBCQueryExecution e = new JDBCQueryExecution(command, connection, new FakeExecutionContextImpl(), ef);
    e.execute();
    Mockito.verify(statement, Mockito.times(1)).execute("DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE 'create global temporary table TEIID_DKJ1 (COL1 varchar2(100 char)) on commit delete rows; END;");
    Mockito.verify(ps, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
    Mockito.verify(ps, Mockito.times(1)).setObject(1, "b", Types.VARCHAR);
    Mockito.verify(ps, Mockito.times(2)).addBatch();
    Mockito.verify(ps, Mockito.times(1)).executeBatch();
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Comparison(org.teiid.language.Comparison) Select(org.teiid.language.Select) Parameter(org.teiid.language.Parameter) SPParameter(org.teiid.query.sql.lang.SPParameter) ProcedureParameter(org.teiid.metadata.ProcedureParameter) List(java.util.List) JDBCQueryExecution(org.teiid.translator.jdbc.JDBCQueryExecution) CommandBuilder(org.teiid.cdk.CommandBuilder) 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