Search in sources :

Example 61 with ExecutionContext

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

the class TestEmbeddedServer method testGeneratedKeysVirtual.

@Test
public void testGeneratedKeysVirtual() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    HardCodedExecutionFactory hcef = new HardCodedExecutionFactory() {

        @Override
        public UpdateExecution createUpdateExecution(Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            UpdateExecution ue = super.createUpdateExecution(command, executionContext, metadata, connection);
            GeneratedKeys keys = executionContext.getCommandContext().returnGeneratedKeys(new String[] { "y" }, new Class<?>[] { Integer.class });
            keys.addKey(Arrays.asList(1));
            return ue;
        }
    };
    hcef.addUpdate("INSERT INTO tbl (x) VALUES (1)", new int[] { 1 });
    es.addTranslator("t", hcef);
    ModelMetaData mmd1 = new ModelMetaData();
    mmd1.setName("b");
    mmd1.addSourceMapping("b", "t", null);
    mmd1.addSourceMetadata("ddl", "create foreign table tbl (x integer, y integer auto_increment primary key) OPTIONS (UPDATABLE true);" + "create view v (i integer, k integer auto_increment primary key) OPTIONS (UPDATABLE true) as select x, y from tbl;" + "create view v1 (i integer, k integer not null auto_increment primary key) OPTIONS (UPDATABLE true) as select x, y from tbl;" + "create trigger on v1 instead of insert as for each row begin atomic " + "insert into tbl (x) values (new.i); key.k = cast(generated_key('y') as integer); end;");
    es.deployVDB("vdb", mmd1);
    Connection c = es.getDriver().connect("jdbc:teiid:vdb", null);
    PreparedStatement ps = c.prepareStatement("insert into v (i) values (1)", Statement.RETURN_GENERATED_KEYS);
    assertEquals(1, ps.executeUpdate());
    ResultSet rs = ps.getGeneratedKeys();
    assertTrue(rs.next());
    assertEquals("k", rs.getMetaData().getColumnLabel(1));
    ps = c.prepareStatement("insert into v1 (i) values (1)", Statement.RETURN_GENERATED_KEYS);
    assertEquals(1, ps.executeUpdate());
    rs = ps.getGeneratedKeys();
    assertTrue(rs.next());
    assertEquals("k", rs.getMetaData().getColumnLabel(1));
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) GeneratedKeys(org.teiid.GeneratedKeys) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) Test(org.junit.Test)

Example 62 with ExecutionContext

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

the class TestLocalConnections method oneTimeSetup.

@SuppressWarnings("serial")
@BeforeClass
public static void oneTimeSetup() throws Exception {
    server.setUseCallingThread(true);
    server.setConnectorManagerRepository(new ConnectorManagerRepository() {

        @Override
        public ConnectorManager getConnectorManager(String connectorName) {
            return new ConnectorManager(connectorName, connectorName) {

                @Override
                public ExecutionFactory<Object, Object> getExecutionFactory() {
                    return new ExecutionFactory<Object, Object>() {

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

                        @Override
                        public Execution createExecution(Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
                            return new ResultSetExecution() {

                                boolean returnedRow = false;

                                @Override
                                public void execute() throws TranslatorException {
                                    lock.lock();
                                    try {
                                        sourceCounter.release();
                                        if (!wait.await(5, TimeUnit.SECONDS)) {
                                            throw new RuntimeException();
                                        }
                                    } catch (InterruptedException e) {
                                        throw new RuntimeException(e);
                                    } finally {
                                        lock.unlock();
                                    }
                                }

                                @Override
                                public void close() {
                                }

                                @Override
                                public void cancel() throws TranslatorException {
                                }

                                @Override
                                public List<?> next() throws TranslatorException, DataNotAvailableException {
                                    if (returnedRow) {
                                        return null;
                                    }
                                    returnedRow = true;
                                    return new ArrayList<Object>(Collections.singleton(null));
                                }
                            };
                        }
                    };
                }

                @Override
                public Object getConnectionFactory() throws TranslatorException {
                    return null;
                }
            };
        }
    });
    FunctionMethod function = new FunctionMethod("foo", null, FunctionCategoryConstants.MISCELLANEOUS, PushDown.CANNOT_PUSHDOWN, TestLocalConnections.class.getName(), "blocking", null, new FunctionParameter("result", DataTypeManager.DefaultDataTypes.INTEGER), false, FunctionMethod.Determinism.NONDETERMINISTIC);
    HashMap<String, Collection<FunctionMethod>> udfs = new HashMap<String, Collection<FunctionMethod>>();
    udfs.put("test", Arrays.asList(function));
    server.deployVDB("PartsSupplier", UnitTestUtil.getTestDataPath() + "/PartsSupplier.vdb", new DeployVDBParameter(udfs, null));
}
Also used : HashMap(java.util.HashMap) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Execution(org.teiid.translator.Execution) ResultSetExecution(org.teiid.translator.ResultSetExecution) FunctionMethod(org.teiid.metadata.FunctionMethod) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) FunctionParameter(org.teiid.metadata.FunctionParameter) ConnectorManagerRepository(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository) ExecutionFactory(org.teiid.translator.ExecutionFactory) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) DeployVDBParameter(org.teiid.jdbc.FakeServer.DeployVDBParameter) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) Collection(java.util.Collection) TranslatorException(org.teiid.translator.TranslatorException) BeforeClass(org.junit.BeforeClass)

Example 63 with ExecutionContext

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

the class TestExecutionReuse method oneTimeSetUp.

@BeforeClass
public static void oneTimeSetUp() throws Exception {
    EmbeddedConfiguration config = new EmbeddedConfiguration();
    config.setUserRequestSourceConcurrency(1);
    server = new FakeServer(false);
    server.setConnectorManagerRepository(new ConnectorManagerRepository() {

        private ConnectorManager cm = new ConnectorManager("x", "y") {

            private ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

                @Override
                public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
                    ec = executionContext;
                    return execution;
                }

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

            @Override
            public ExecutionFactory<Object, Object> getExecutionFactory() {
                return ef;
            }

            @Override
            public Object getConnectionFactory() throws TranslatorException {
                return null;
            }
        };

        @Override
        public ConnectorManager getConnectorManager(String connectorName) {
            return cm;
        }
    });
    server.start(config, false);
    server.deployVDB("PartsSupplier", UnitTestUtil.getTestDataPath() + "/PartsSupplier.vdb");
}
Also used : FakeServer(org.teiid.jdbc.FakeServer) ConnectorManagerRepository(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository) EmbeddedConfiguration(org.teiid.runtime.EmbeddedConfiguration) ExecutionFactory(org.teiid.translator.ExecutionFactory) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) BeforeClass(org.junit.BeforeClass)

Example 64 with ExecutionContext

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

the class TestResultsCache method testScope.

@Test
public void testScope() throws Exception {
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("x");
    mmd.addProperty("teiid_rel:determinism", "USER_DETERMINISTIC");
    mmd.addSourceMapping("x", "x", null);
    mmd.addSourceMetadata("ddl", "create foreign table t (c string); create foreign procedure p () returns table (c string);");
    final AtomicBoolean setScope = new AtomicBoolean();
    server.addTranslator("x", new ExecutionFactory() {

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

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, final ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return createProcedureExecution(null, executionContext, metadata, connection);
        }

        @Override
        public ProcedureExecution createProcedureExecution(Call command, final ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ProcedureExecution() {

                boolean returned = false;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (setScope.get()) {
                        // prevent caching altogether
                        executionContext.setScope(Scope.SESSION);
                    }
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    return Arrays.asList(executionContext.getSession().getSessionId());
                }

                @Override
                public List<?> getOutputParameterValues() throws TranslatorException {
                    return null;
                }
            };
        }
    });
    server.deployVDB("x", mmd);
    Connection c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    String sessionid = rs.getString(1);
    // should be the same with same user/session
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    assertEquals(sessionid, rs.getString(1));
    c.close();
    c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    assertEquals(sessionid, rs.getString(1));
    c.close();
    // for the final test
    setScope.set(true);
    // should be different with another user
    c = server.getDriver().connect("jdbc:teiid:x;user=bill", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    String sessionid1 = rs.getString(1);
    c.close();
    assertNotEquals(sessionid, sessionid1);
    c = server.getDriver().connect("jdbc:teiid:x;user=bill", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    // scope session should prevent reuse
    assertNotEquals(sessionid1, rs.getString(1));
    setScope.set(false);
    rs = s.executeQuery("/* cache */ call p()");
    assertTrue(rs.next());
    sessionid = rs.getString(1);
    c.close();
    c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ call p()");
    assertTrue(rs.next());
    assertNotEquals(sessionid, rs.getString(1));
    c.close();
}
Also used : Call(org.teiid.language.Call) Statement(java.sql.Statement) Connection(java.sql.Connection) ExecutionFactory(org.teiid.translator.ExecutionFactory) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ProcedureExecution(org.teiid.translator.ProcedureExecution) ResultSet(java.sql.ResultSet) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 65 with ExecutionContext

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

the class TestAsynch method oneTimeSetup.

@BeforeClass
public static void oneTimeSetup() throws Exception {
    server = new FakeServer(true);
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("v");
    mmd.setModelType(Type.PHYSICAL);
    mmd.setSchemaSourceType("ddl");
    mmd.addSourceMapping("z", "z", null);
    mmd.setSchemaText("create view test (col integer) as select 1; create foreign table someTable (col integer);");
    ef = new HardCodedExecutionFactory() {

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            partIds.add(executionContext.getPartIdentifier());
            return super.createResultSetExecution(command, executionContext, metadata, connection);
        }
    };
    server.addTranslator("z", ef);
    server.deployVDB("x", mmd);
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) FakeServer(org.teiid.jdbc.FakeServer) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) BeforeClass(org.junit.BeforeClass)

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