Search in sources :

Example 1 with FakeExecutionContextImpl

use of org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl in project teiid by teiid.

the class TestOracleTranslator method testSourceHint1.

@Test
public void testSourceHint1() throws Exception {
    ExecutionContextImpl impl = new FakeExecutionContextImpl();
    impl.setHints(Arrays.asList("hello world"));
    helpTestVisitor(getTestVDB(), "select part_name from parts union select part_id from parts", impl, null, "SELECT /*+ hello world */ g_1.PART_NAME AS c_0 FROM PARTS g_1 UNION SELECT g_0.PART_ID AS c_0 FROM PARTS g_0", true);
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) ExecutionContextImpl(org.teiid.dqp.internal.datamgr.ExecutionContextImpl) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Test(org.junit.Test)

Example 2 with FakeExecutionContextImpl

use of org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl 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 3 with FakeExecutionContextImpl

use of org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl in project teiid by teiid.

the class TestJDBCDirectQueryExecution method testPrepareExecution.

@Test
public void testPrepareExecution() throws Exception {
    // $NON-NLS-1$
    Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('select * from Source where e1 = ?', 2)");
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement stmt = Mockito.mock(PreparedStatement.class);
    ResultSet rs = Mockito.mock(ResultSet.class);
    ResultSetMetaData rsm = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(stmt.getUpdateCount()).toReturn(-1);
    Mockito.stub(stmt.getResultSet()).toReturn(rs);
    Mockito.stub(stmt.execute()).toReturn(true);
    Mockito.stub(rs.getMetaData()).toReturn(rsm);
    Mockito.stub(rsm.getColumnCount()).toReturn(2);
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("select * from Source where e1 = ?")).toReturn(stmt);
    Mockito.stub(rs.next()).toReturn(true);
    Mockito.stub(rs.getObject(1)).toReturn(5);
    Mockito.stub(rs.getObject(2)).toReturn("five");
    DatabaseMetaData dbmd = Mockito.mock(DatabaseMetaData.class);
    Mockito.stub(connection.getMetaData()).toReturn(dbmd);
    JDBCExecutionFactory ef = new JDBCExecutionFactory();
    ef.setSupportsDirectQueryProcedure(true);
    ResultSetExecution execution = (ResultSetExecution) ef.createExecution(command, new FakeExecutionContextImpl(), Mockito.mock(RuntimeMetadata.class), connection);
    execution.execute();
    Mockito.verify(stmt).setObject(1, 2);
    assertArrayEquals(new Object[] { 5, "five" }, (Object[]) execution.next().get(0));
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSetExecution(org.teiid.translator.ResultSetExecution) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Command(org.teiid.language.Command) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 4 with FakeExecutionContextImpl

use of org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl 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);
    }
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) ResultSetMetaData(java.sql.ResultSetMetaData) Expression(org.teiid.language.Expression) ResultSet(java.sql.ResultSet) Parameter(org.teiid.language.Parameter) ExpressionValueSource(org.teiid.language.ExpressionValueSource) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 5 with FakeExecutionContextImpl

use of org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl in project teiid by teiid.

the class TestJDBCUpdateExecution method testAutoGeneretionKeys.

@Test
public void testAutoGeneretionKeys() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate("create foreign table SmallA (IntKey integer primary key, IntNum integer)", "insert into SmallA (IntKey, IntNum) values (1, 2)");
    Connection connection = Mockito.mock(Connection.class);
    Statement s = Mockito.mock(Statement.class);
    Mockito.stub(connection.createStatement()).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory() {

        @Override
        public boolean supportsGeneratedKeys() {
            return true;
        }

        @Override
        public boolean useColumnNamesForGeneratedKeys() {
            return true;
        }
    };
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    Mockito.stub(s.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();
    Mockito.verify(s, Mockito.times(1)).getGeneratedKeys();
    Mockito.verify(s, Mockito.times(1)).executeUpdate("INSERT INTO SmallA (IntKey, IntNum) VALUES (1, 2)", new String[] { "IntKey" });
    config = new JDBCExecutionFactory() {

        @Override
        public boolean supportsGeneratedKeys() {
            return true;
        }
    };
    s = Mockito.mock(Statement.class);
    Mockito.stub(connection.createStatement()).toReturn(s);
    Mockito.stub(s.getGeneratedKeys()).toReturn(r);
    updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    updateExecution.execute();
    Mockito.verify(s, Mockito.times(1)).getGeneratedKeys();
    Mockito.verify(s, Mockito.times(1)).executeUpdate("INSERT INTO SmallA (IntKey, IntNum) VALUES (1, 2)", Statement.RETURN_GENERATED_KEYS);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Insert(org.teiid.language.Insert) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)16 Connection (java.sql.Connection)12 PreparedStatement (java.sql.PreparedStatement)12 ResultSet (java.sql.ResultSet)8 ResultSetMetaData (java.sql.ResultSetMetaData)8 Insert (org.teiid.language.Insert)8 ExpressionValueSource (org.teiid.language.ExpressionValueSource)5 Parameter (org.teiid.language.Parameter)5 Statement (java.sql.Statement)4 ExecutionContextImpl (org.teiid.dqp.internal.datamgr.ExecutionContextImpl)4 Expression (org.teiid.language.Expression)4 Command (org.teiid.language.Command)3 BatchUpdateException (java.sql.BatchUpdateException)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 List (java.util.List)2 CommandBuilder (org.teiid.cdk.CommandBuilder)2 BatchedUpdates (org.teiid.language.BatchedUpdates)2 Literal (org.teiid.language.Literal)2 ResultSetExecution (org.teiid.translator.ResultSetExecution)2