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);
}
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();
}
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));
}
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);
}
}
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);
}
Aggregations