Search in sources :

Example 6 with ExecutionContext

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

the class TestConnectorWorkItem method testConversionError.

@Test
public void testConversionError() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<String> list1 = new ArrayList<String>();
            list1.add("1");
            List<String> list2 = new ArrayList<String>();
            list2.add("a");
            final Iterator<List<String>> iter = Arrays.asList(list1, list2).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
    ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector") {

        public ExecutionFactory getExecutionFactory() {
            return ef;
        }

        public Object getConnectionFactory() {
            return null;
        }
    };
    cm.start();
    ef.setCopyLobs(true);
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(-1, message.getFinalRow());
    try {
        cwi.more();
        fail();
    } catch (TranslatorException e) {
    // should throw the conversion error
    }
}
Also used : ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 7 with ExecutionContext

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

the class TestConnectorWorkItem method testUnmodifibleList.

@Test
public void testUnmodifibleList() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<String> list1 = Collections.singletonList("1");
            final Iterator<List<String>> iter = Arrays.asList(list1).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
    ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector") {

        public ExecutionFactory getExecutionFactory() {
            return ef;
        }

        public Object getConnectionFactory() {
            return null;
        }
    };
    cm.start();
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(1, message.getFinalRow());
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 8 with ExecutionContext

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

the class TestODataIntegration method testCheckGeneratedColumns.

@Test
public void testCheckGeneratedColumns() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory() {

        @Override
        public UpdateExecution createUpdateExecution(org.teiid.language.Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            GeneratedKeys keys = executionContext.getCommandContext().returnGeneratedKeys(new String[] { "a" }, new Class[] { String.class });
            keys.addKey(Arrays.asList("ax"));
            return super.createUpdateExecution(command, executionContext, metadata, connection);
        }

        @Override
        public boolean supportsCompareCriteriaEquals() {
            return true;
        }
    };
    hc.addUpdate("INSERT INTO x (b, c) VALUES ('b', 5)", new int[] { 1 });
    // this gets called right after insert.
    hc.addData("SELECT x.a, x.b, x.c FROM x WHERE x.a = 'ax'", Arrays.asList(Arrays.asList("a", "b", 2)));
    teiid.addTranslator("x", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (a string, b string, c integer, " + "primary key (a)) options (updatable true);");
        mmd.addSourceMapping("x", "x", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = http.newRequest(baseURL + "/northwind/m/x").method("POST").content(new StringContentProvider("{\"b\":\"b\", \"c\":5}"), "application/json").send();
        assertEquals(201, response.getStatus());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.query.sql.lang.Command) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) GeneratedKeys(org.teiid.GeneratedKeys) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 9 with ExecutionContext

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

the class TestMongoDBQueryExecution method testNextWithGroupAndOrder.

@Test
public void testNextWithGroupAndOrder() throws Exception {
    String query = "select \"FirstName\" from \"TeiidArray\" group by \"FirstName\" order by \"FirstName\" limit 1000";
    String[] expectedCollection = new String[] { "TeiidArray" };
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("CREATE FOREIGN TABLE TeiidArray (ID String PRIMARY KEY, FirstName varchar(25), LastName varchar(25), Score object[]) OPTIONS(UPDATABLE 'TRUE');", "x", "y");
    TranslationUtility util = new TranslationUtility(metadata);
    Command cmd = util.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    Mockito.stub(context.getBatchSize()).toReturn(256);
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    DB db = Mockito.mock(DB.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    for (String collection : expectedCollection) {
        Mockito.stub(db.getCollection(collection)).toReturn(dbCollection);
    }
    Cursor c = Mockito.mock(Cursor.class);
    Mockito.stub(c.hasNext()).toAnswer(new Answer<Boolean>() {

        boolean next = true;

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            if (next) {
                next = false;
                return true;
            }
            return false;
        }
    });
    DBObject dbo = Mockito.mock(DBObject.class);
    Mockito.stub(c.next()).toReturn(dbo);
    Mockito.stub(dbCollection.aggregate((List<DBObject>) Mockito.anyList(), (AggregationOptions) Mockito.anyObject())).toReturn(c);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Mockito.stub(db.getCollectionFromString(Mockito.anyString())).toReturn(dbCollection);
    ResultSetExecution execution = this.translator.createResultSetExecution((QueryExpression) cmd, context, util.createRuntimeMetadata(), connection);
    execution.execute();
    execution.next();
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 10 with ExecutionContext

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

the class TestMongoDBUpdateExecution method helpUpdate.

private DBCollection helpUpdate(String query, String[] expectedCollection, DBObject match_result, ArrayList<DBObject> results) throws TranslatorException {
    Command cmd = this.utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    CommandContext cc = Mockito.mock(CommandContext.class);
    Mockito.stub(context.getCommandContext()).toReturn(cc);
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    DB db = Mockito.mock(DB.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    for (String collection : expectedCollection) {
        Mockito.stub(db.getCollection(collection)).toReturn(dbCollection);
    }
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Mockito.stub(db.getCollectionFromString(Mockito.anyString())).toReturn(dbCollection);
    Mockito.stub(dbCollection.findOne(Mockito.any(BasicDBObject.class))).toReturn(match_result);
    WriteResult result = Mockito.mock(WriteResult.class);
    Mockito.stub(result.getN()).toReturn(1);
    Mockito.stub(dbCollection.insert(Mockito.any(BasicDBObject.class), Mockito.any(WriteConcern.class))).toReturn(result);
    Mockito.stub(dbCollection.update(Mockito.any(BasicDBObject.class), Mockito.any(BasicDBObject.class), Mockito.eq(false), Mockito.eq(true), Mockito.any(WriteConcern.class))).toReturn(result);
    if (results != null) {
        Cursor out = new ResultsCursor(results);
        for (DBObject obj : results) {
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
        }
    }
    UpdateExecution execution = this.translator.createUpdateExecution(cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    return dbCollection;
}
Also used : CommandContext(org.teiid.CommandContext) UpdateExecution(org.teiid.translator.UpdateExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection)

Aggregations

ExecutionContext (org.teiid.translator.ExecutionContext)65 Test (org.junit.Test)50 Command (org.teiid.language.Command)49 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)48 TranslationUtility (org.teiid.cdk.api.TranslationUtility)32 ResultSetExecution (org.teiid.translator.ResultSetExecution)23 List (java.util.List)17 UpdateExecution (org.teiid.translator.UpdateExecution)14 TranslatorException (org.teiid.translator.TranslatorException)11 ArrayList (java.util.ArrayList)10 QueryExpression (org.teiid.language.QueryExpression)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 LdapContext (javax.naming.ldap.LdapContext)9 ExecutionFactory (org.teiid.translator.ExecutionFactory)9 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 HashMap (java.util.HashMap)6 DataSource (javax.activation.DataSource)6 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)6 WSConnection (org.teiid.translator.WSConnection)6