Search in sources :

Example 11 with FakeExecutionContextImpl

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

Example 12 with FakeExecutionContextImpl

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

the class TestOracleTranslator method testSourceHint2.

@Test
public void testSourceHint2() throws Exception {
    ExecutionContextImpl impl = new FakeExecutionContextImpl();
    impl.setHints(Arrays.asList("hello world"));
    helpTestVisitor(getTestVDB(), "with x (y) as /*+ no_inline */ (select part_name from parts) select y from x", impl, null, "WITH x AS (SELECT g_0.PART_NAME AS y FROM PARTS g_0) SELECT /*+ hello world */ g_1.y FROM x g_1", 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 13 with FakeExecutionContextImpl

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

the class TestJDBCDirectQueryExecution method testPrepareUpdateCount.

@Test
public void testPrepareUpdateCount() throws Exception {
    // $NON-NLS-1$
    Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('update source set e1=? where e2 = ?', 2, 'foo')");
    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.getUpdateCount()).toReturn(5);
    Mockito.stub(stmt.execute()).toReturn(false);
    Mockito.stub(rs.getMetaData()).toReturn(rsm);
    Mockito.stub(rsm.getColumnCount()).toReturn(2);
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("update source set e1=? where e2 = ?")).toReturn(stmt);
    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();
    assertArrayEquals(new Object[] { 5 }, (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 14 with FakeExecutionContextImpl

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

the class TestJDBCUpdateExecution method testAutoGeneretionKeysPrepared.

@Test
public void testAutoGeneretionKeysPrepared() 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)");
    ((Literal) ((ExpressionValueSource) command.getValueSource()).getValues().get(0)).setBindEligible(true);
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, 2)", new String[] { "IntKey" })).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory() {

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

        @Override
        public boolean useColumnNamesForGeneratedKeys() {
            return true;
        }
    };
    ResultSet r = Mockito.mock(ResultSet.class);
    Mockito.stub(r.next()).toReturn(true).toReturn(false);
    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();
    config = new JDBCExecutionFactory() {

        @Override
        public boolean supportsGeneratedKeys() {
            return true;
        }
    };
    assertEquals(0, context.getCommandContext().getGeneratedKeys().getKeyIterator().next().get(0));
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ResultSetMetaData(java.sql.ResultSetMetaData) Literal(org.teiid.language.Literal) ResultSet(java.sql.ResultSet) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 15 with FakeExecutionContextImpl

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

the class TestJDBCUpdateExecution method testPreparedInsertWithGeometry.

@Test
public void testPreparedInsertWithGeometry() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into cola_markets(name,shape) values('foo124', ST_GeomFromText('POINT (300 100)', 8307))");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.STRING);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.GEOMETRY);
    param.setValueIndex(1);
    values.set(1, param);
    GeometryType value = new GeometryType();
    value.setSrid(123);
    command.setParameterValues(Arrays.asList(Arrays.asList("a", value)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int[] { 1 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO COLA_MARKETS (NAME, SHAPE) VALUES (?, st_geomfromwkb(?, ?))")).toReturn(p);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
    updateExecution.execute();
    Mockito.verify(p, Mockito.times(1)).addBatch();
    Mockito.verify(p, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
    Mockito.verify(p, Mockito.times(1)).setInt(3, 123);
}
Also used : GeometryType(org.teiid.core.types.GeometryType) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Expression(org.teiid.language.Expression) Connection(java.sql.Connection) Parameter(org.teiid.language.Parameter) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource) 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