Search in sources :

Example 26 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution 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 27 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestJDBCDirectQueryExecution method testSelectExecution.

@Test
public void testSelectExecution() throws Exception {
    // $NON-NLS-1$
    Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('select * from Source')");
    Connection connection = Mockito.mock(Connection.class);
    Statement stmt = Mockito.mock(Statement.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(rs.getMetaData()).toReturn(rsm);
    Mockito.stub(rsm.getColumnCount()).toReturn(2);
    // $NON-NLS-1$
    Mockito.stub(connection.createStatement()).toReturn(stmt);
    Mockito.stub(stmt.execute("select * from Source")).toReturn(true);
    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, Mockito.mock(ExecutionContext.class), Mockito.mock(RuntimeMetadata.class), connection);
    execution.execute();
    assertArrayEquals(new Object[] { 5, "five" }, (Object[]) execution.next().get(0));
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSetExecution(org.teiid.translator.ResultSetExecution) Command(org.teiid.language.Command) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 28 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution 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 29 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestMongoDBDirectQueryExecution method testShellDirect.

@Test
public void testShellDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("$ShellCmd;MyTable;remove;{ qty: { $gt: 20 }}", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    Mockito.verify(dbCollection).remove(QueryBuilder.start("qty").greaterThan(20).get());
}
Also used : DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Argument(org.teiid.language.Argument) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 30 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestODataQueryExecution method testExpandBasedInnerJoin.

@Test
public void testExpandBasedInnerJoin() throws Exception {
    String query = "select p.UserName, pf.UserName from People p JOIN People_Friends pf " + "ON p.UserName = pf.People_UserName WHERE p.UserName= 'russellwhyte'";
    String expectedURL = "People?$select=UserName&$filter=UserName eq 'russellwhyte'" + "&$expand=Friends($select=UserName)";
    FileReader reader = new FileReader(UnitTestUtil.getTestDataFile("russel-friends.json"));
    ResultSetExecution excution = helpExecute(TestODataMetadataProcessor.tripPinMetadata(), query, ObjectConverterUtil.convertToString(reader), expectedURL);
    reader.close();
    assertArrayEquals(new Object[] { "russellwhyte", "scottketchum" }, excution.next().toArray(new Object[2]));
    assertArrayEquals(new Object[] { "russellwhyte", "ronaldmundy" }, excution.next().toArray(new Object[2]));
    assertArrayEquals(new Object[] { "russellwhyte", "javieralfred" }, excution.next().toArray(new Object[2]));
    assertArrayEquals(new Object[] { "russellwhyte", "angelhuffman" }, excution.next().toArray(new Object[2]));
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) FileReader(java.io.FileReader) Test(org.junit.Test)

Aggregations

ResultSetExecution (org.teiid.translator.ResultSetExecution)50 Test (org.junit.Test)41 ExecutionContext (org.teiid.translator.ExecutionContext)22 FileReader (java.io.FileReader)19 Command (org.teiid.language.Command)17 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)15 QueryExpression (org.teiid.language.QueryExpression)12 List (java.util.List)11 ArrayList (java.util.ArrayList)10 TranslatorException (org.teiid.translator.TranslatorException)10 ExecutionFactory (org.teiid.translator.ExecutionFactory)8 TranslationUtility (org.teiid.cdk.api.TranslationUtility)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)4 MongoDBConnection (org.teiid.mongodb.MongoDBConnection)4 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)4 UpdateExecution (org.teiid.translator.UpdateExecution)4 DatabaseMetaData (java.sql.DatabaseMetaData)3