Search in sources :

Example 41 with ResultSetExecution

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

the class TestSimpleDBExecution method testDirectExecution.

@Test
public void testDirectExecution() throws Exception {
    SelectResult result = new SelectResult();
    result.setItems(mockResult());
    String query = "select * from item where attribute > 'name'";
    Mockito.stub(connection.performSelect(Mockito.anyString(), Mockito.anyString())).toReturn(result);
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    List<Argument> arguments = new ArrayList<Argument>();
    Argument arg = new Argument(Direction.IN, String.class, Mockito.mock(ProcedureParameter.class));
    arg.setArgumentValue(LanguageFactory.INSTANCE.createLiteral(query, String.class));
    arguments.add(arg);
    ResultSetExecution exec = translator.createDirectExecution(arguments, cmd, context, Mockito.mock(RuntimeMetadata.class), connection);
    exec.execute();
    List row = exec.next();
    Mockito.verify(connection).performSelect("select * from item where attribute > 'name'", null);
    Object[] results = (Object[]) row.get(0);
    assertEquals("a1", results[0]);
    assertEquals("[a2, a22]", results[1]);
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) Argument(org.teiid.language.Argument) ArrayList(java.util.ArrayList) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 42 with ResultSetExecution

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

the class TestHotrodExecution method testServer_Teiid_5165.

// TEIID-5165 - test large cache delete
@Test
public void testServer_Teiid_5165() throws Exception {
    EF.setSupportsUpsert(false);
    ResultSetExecution exec = null;
    Command command = null;
    UpdateExecution update = null;
    InfinispanConnection connection = getConnection("foo");
    // the below also test one-2-one relation.
    command = UTILITY.parseCommand("DELETE FROM G2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    int rows = 12000;
    for (int i = 0; i < rows; i++) {
        command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (" + i + ", 'row" + i + "', 1, 'one')");
        update = EF.createUpdateExecution(command, EC, METADATA, connection);
        update.execute();
    }
    Thread.sleep(5000);
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    int cnt = 0;
    while (true) {
        List<?> results = exec.next();
        if (results == null)
            break;
        cnt++;
    }
    assertEquals(new Integer(rows), new Integer(cnt));
    command = UTILITY.parseCommand("DELETE FROM G2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT count(*) as cnt FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertNull(exec.next());
}
Also used : BigInteger(java.math.BigInteger) ResultSetExecution(org.teiid.translator.ResultSetExecution) Command(org.teiid.language.Command) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) UpdateExecution(org.teiid.translator.UpdateExecution) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 43 with ResultSetExecution

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

the class TestODataIntegration method testErrorCodes.

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

        @Override
        public ResultSetExecution createResultSetExecution(final QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<? extends List<?>> list = getData(command);
            if (list == null) {
                throw new RuntimeException(command.toString());
            }
            final Iterator<? extends List<?>> result = list.iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                    throw new TranslatorException(ODataPlugin.Event.TEIID16001, "execution failed");
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (result.hasNext()) {
                        return result.next();
                    }
                    return null;
                }
            };
        }
    };
    hc.addData("SELECT x.a, x.b FROM x", Arrays.asList(Arrays.asList("a", 1)));
    teiid.addTranslator("x1", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (a string, b integer, primary key (a));");
        mmd.addSourceMapping("x1", "x1", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = http.newRequest(baseURL + "/northwind/m/x").method("GET").send();
        assertEquals(400, response.getStatus());
        assertEquals("{\"error\":{\"code\":\"TEIID30504\"," + "\"message\":\"TEIID30504 x1: TEIID16001 execution failed\"}}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$format=xml").method("GET").send();
        assertEquals(400, response.getStatus());
        assertEquals("<?xml version='1.0' encoding='UTF-8'?>" + "<error xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\">" + "<code>TEIID30504</code>" + "<message>TEIID30504 x1: TEIID16001 execution failed</message>" + "</error>", response.getContentAsString());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 44 with ResultSetExecution

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

the class TestEmbeddedServer method testQueryTimeout.

@Test
public void testQueryTimeout() throws Exception {
    es.start(new EmbeddedConfiguration());
    es.addTranslator("foo", new ExecutionFactory() {

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

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            return super.createResultSetExecution(command, executionContext, metadata, connection);
        }
    });
    es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\"><source name=\"foo\" translator-name=\"foo\"/><metadata type=\"DDL\"><![CDATA[CREATE foreign table x (y xml);]]> </metadata></model></vdb>".getBytes()));
    Connection c = es.getDriver().connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    s.setQueryTimeout(1);
    try {
        s.execute("select * from x");
        fail();
    } catch (SQLException e) {
        assertEquals(SQLStates.QUERY_CANCELED, e.getSQLState());
    }
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ExecutionFactory(org.teiid.translator.ExecutionFactory) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 45 with ResultSetExecution

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

the class TestEmbeddedServer method testSourceLobUnderTxn.

@Test
public void testSourceLobUnderTxn() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setMaxResultSetCacheStaleness(0);
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    final AtomicBoolean closed = new AtomicBoolean();
    es.addTranslator("foo", new ExecutionFactory() {

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

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ResultSetExecution() {

                private boolean returned;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                    closed.set(true);
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    ArrayList<Object> result = new ArrayList<Object>(1);
                    result.add(new SQLXMLImpl(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            // need to make it of a sufficient size to not be inlined
                            return new ByteArrayInputStream(new byte[DataTypeManager.MAX_LOB_MEMORY_BYTES + 1]);
                        }
                    }));
                    return result;
                }
            };
        }
    });
    es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\"><source name=\"foo\" translator-name=\"foo\"/><metadata type=\"DDL\"><![CDATA[CREATE foreign table x (y xml);]]> </metadata></model></vdb>".getBytes()));
    Connection c = es.getDriver().connect("jdbc:teiid:test", null);
    c.setAutoCommit(false);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("select * from x");
    rs.next();
    assertFalse(closed.get());
    s.close();
    assertTrue(closed.get());
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) IOException(java.io.IOException) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) QueryExpression(org.teiid.language.QueryExpression) 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