Search in sources :

Example 1 with JDBCQueryExecution

use of org.teiid.translator.jdbc.JDBCQueryExecution 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)

Example 2 with JDBCQueryExecution

use of org.teiid.translator.jdbc.JDBCQueryExecution in project teiid by teiid.

the class TestOracleTranslator method testCharType.

@Test
public void testCharType() throws Exception {
    CommandBuilder commandBuilder = new CommandBuilder(getOracleSpecificMetadata());
    Command command = commandBuilder.getCommand("select id from smalla where description = 'a' and ndescription in ('b', 'c')");
    for (Literal l : CollectorVisitor.collectObjects(Literal.class, command)) {
        l.setBindEligible(true);
    }
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement ps = Mockito.mock(PreparedStatement.class);
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("SELECT SmallishA.ID FROM SmallishA WHERE SmallishA.description = ? AND SmallishA.ndescription IN (?, ?)")).toReturn(ps);
    OracleExecutionFactory ef = new OracleExecutionFactory();
    ef.start();
    JDBCQueryExecution e = new JDBCQueryExecution(command, connection, new FakeExecutionContextImpl(), ef);
    e.execute();
    Mockito.verify(ps, Mockito.times(1)).setObject(1, "a", OracleExecutionFactory.FIXED_CHAR_TYPE);
    Mockito.verify(ps, Mockito.times(1)).setObject(2, "b", OracleExecutionFactory.FIXED_CHAR_TYPE);
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Command(org.teiid.language.Command) TranslatedCommand(org.teiid.translator.jdbc.TranslatedCommand) Literal(org.teiid.language.Literal) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JDBCQueryExecution(org.teiid.translator.jdbc.JDBCQueryExecution) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Test (org.junit.Test)2 CommandBuilder (org.teiid.cdk.CommandBuilder)2 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)2 JDBCQueryExecution (org.teiid.translator.jdbc.JDBCQueryExecution)2 CallableStatement (java.sql.CallableStatement)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Command (org.teiid.language.Command)1 Comparison (org.teiid.language.Comparison)1 Literal (org.teiid.language.Literal)1 Parameter (org.teiid.language.Parameter)1 Select (org.teiid.language.Select)1 ProcedureParameter (org.teiid.metadata.ProcedureParameter)1 SPParameter (org.teiid.query.sql.lang.SPParameter)1 TranslatedCommand (org.teiid.translator.jdbc.TranslatedCommand)1