Search in sources :

Example 1 with TeiidDriver

use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.

the class TestEmbeddedServer method testWithPushdownChangeName.

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

        @Override
        public boolean supportsCommonTableExpressions() {
            return true;
        }

        @Override
        public boolean supportsSelfJoins() {
            return true;
        }

        @Override
        public boolean supportsCompareCriteriaEquals() {
            return true;
        }

        @Override
        public boolean supportsAliasedTable() {
            return true;
        }

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

        @Override
        public String getExcludedCommonTableExpressionName() {
            return "a";
        }

        @Override
        public boolean supportsInnerJoins() {
            return true;
        }
    };
    es.addTranslator("y", hcef);
    hcef.addData("WITH a__2 (x) AS (SELECT g_0.e1 FROM pm1.g1 AS g_0) SELECT g_0.x FROM a__2 AS g_0, a__2 AS g_1", Arrays.asList(Arrays.asList("a")));
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("my-schema");
    mmd.addSourceMapping("x", "y", null);
    mmd.addSourceMetadata("ddl", "create foreign table \"pm1.g1\" (e1 string)");
    es.deployVDB("test", mmd);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    // see the correct pushdown in hcef.addData above
    // $NON-NLS-1$
    s.execute("with a (x) as (select e1 from pm1.g1) SELECT a.x from a, a z");
}
Also used : TeiidDriver(org.teiid.jdbc.TeiidDriver) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 2 with TeiidDriver

use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.

the class TestEmbeddedServer method testTransactions.

@Test
public void testTransactions() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    ModelMetaData mmd1 = new ModelMetaData();
    mmd1.setName("b");
    mmd1.setModelType(Type.VIRTUAL);
    mmd1.setSchemaSourceType("ddl");
    mmd1.setSchemaText("create view v as select 1; " + "create virtual procedure proc () options (updatecount 2) as begin select * from v; end; " + "create virtual procedure proc0 () as begin atomic select * from v; end; " + "create virtual procedure proc1 () as begin atomic insert into #temp values (1); insert into #temp values (1); end; " + "create virtual procedure proc2 (x integer) as begin atomic insert into #temp values (1); insert into #temp values (1); select 1; begin select 1/x; end exception e end; " + "create virtual procedure proc3 (x integer) as begin begin atomic call proc (); select 1; end create local temporary table x (y string); begin atomic call proc (); select 1; end end;");
    es.deployVDB("test", mmd1);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:test", null);
    // local txn
    c.setAutoCommit(false);
    Statement s = c.createStatement();
    s.execute("select 1");
    c.setAutoCommit(true);
    assertEquals(1, tm.txnHistory.size());
    Transaction txn = tm.txnHistory.remove(0);
    Mockito.verify(txn).commit();
    // should be an auto-commit txn (could also force with autoCommitTxn=true)
    s.execute("call proc ()");
    assertEquals(1, tm.txnHistory.size());
    txn = tm.txnHistory.remove(0);
    Mockito.verify(txn).commit();
    // no txn needed
    s.execute("call proc0()");
    assertEquals(0, tm.txnHistory.size());
    // block txn
    s.execute("call proc1()");
    assertEquals(1, tm.txnHistory.size());
    txn = tm.txnHistory.remove(0);
    Mockito.verify(txn).commit();
    s.execute("set autoCommitTxn on");
    s.execute("set noexec on");
    s.execute("select 1");
    assertFalse(s.getResultSet().next());
    s.execute("set autoCommitTxn off");
    s.execute("set noexec off");
    s.execute("call proc2(0)");
    // verify that the block txn was committed because the exception was caught
    assertEquals(1, tm.txnHistory.size());
    txn = tm.txnHistory.remove(0);
    Mockito.verify(txn).rollback();
    // test detection
    tm.txnHistory.clear();
    tm.begin();
    try {
        c.setAutoCommit(false);
        // needed since we lazily start the transaction
        s.execute("select 1");
        fail("should fail since we aren't allowing a nested transaction");
    } catch (TeiidSQLException e) {
    }
    txn = tm.txnHistory.remove(0);
    Mockito.verify(txn, Mockito.times(0)).commit();
    tm.commit();
    c.setAutoCommit(true);
    tm.txnHistory.clear();
    // ensure that we properly reset the txn context
    s.execute("call proc3(0)");
    assertEquals(2, tm.txnHistory.size());
    txn = tm.txnHistory.remove(0);
    Mockito.verify(txn, Mockito.times(0)).registerSynchronization((Synchronization) Mockito.any());
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) TeiidDriver(org.teiid.jdbc.TeiidDriver) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 3 with TeiidDriver

use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.

the class TestDDLMetadataStore method testRoles.

@Test
public void testRoles() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setUseDisk(false);
    ec.setSecurityHelper(new ThreadLocalSecurityHelper());
    es.addTranslator("y", new TestEmbeddedServer.FakeTranslator(false));
    es.addTranslator("y2", new TestEmbeddedServer.FakeTranslator(false));
    final AtomicInteger counter = new AtomicInteger();
    ConnectionFactoryProvider<AtomicInteger> cfp = new EmbeddedServer.SimpleConnectionFactoryProvider<AtomicInteger>(counter);
    es.addConnectionFactoryProvider("z", cfp);
    es.start(ec);
    es.addMetadataRepository("myrepo", Mockito.mock(MetadataRepository.class));
    es.deployVDB(new FileInputStream(UnitTestUtil.getTestDataPath() + "/first-db.ddl"), true);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:empty", null);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("select * from mytable");
    assertFalse(rs.next());
    assertEquals("my-column", rs.getMetaData().getColumnLabel(1));
    s.execute("update mytable set \"my-column\" = 'a'");
    assertEquals(2, s.getUpdateCount());
    try {
        s.execute("delete from mytable where \"my-column\" = 'a'");
        fail("should have stopped by roles");
    } catch (Exception e) {
    // pass
    }
}
Also used : TestEmbeddedServer(org.teiid.runtime.TestEmbeddedServer) Statement(java.sql.Statement) Connection(java.sql.Connection) EmbeddedConfiguration(org.teiid.runtime.EmbeddedConfiguration) FileInputStream(java.io.FileInputStream) LoginException(javax.security.auth.login.LoginException) AdminProcessingException(org.teiid.adminapi.AdminProcessingException) AdminException(org.teiid.adminapi.AdminException) MetadataRepository(org.teiid.metadata.MetadataRepository) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) TeiidDriver(org.teiid.jdbc.TeiidDriver) Test(org.junit.Test)

Example 4 with TeiidDriver

use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.

the class TestEmbeddedServer method testDeploy.

@Test
public void testDeploy() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setUseDisk(false);
    es.start(ec);
    es.addTranslator("y", new FakeTranslator(false));
    final AtomicInteger counter = new AtomicInteger();
    ConnectionFactoryProvider<AtomicInteger> cfp = new EmbeddedServer.SimpleConnectionFactoryProvider<AtomicInteger>(counter);
    es.addConnectionFactoryProvider("z", cfp);
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("my-schema");
    mmd.addSourceMapping("x", "y", "z");
    ModelMetaData mmd1 = new ModelMetaData();
    mmd1.setName("virt");
    mmd1.setModelType(Type.VIRTUAL);
    mmd1.setSchemaSourceType("ddl");
    mmd1.setSchemaText("create view \"my-view\" OPTIONS (UPDATABLE 'true') as select * from \"my-table\"");
    es.deployVDB("test", mmd, mmd1);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("select * from \"my-view\"");
    assertFalse(rs.next());
    assertEquals("my-column", rs.getMetaData().getColumnLabel(1));
    s.execute("update \"my-view\" set \"my-column\" = 'a'");
    assertEquals(2, s.getUpdateCount());
    es.deployVDB("empty");
    c = es.getDriver().connect("jdbc:teiid:empty", null);
    s = c.createStatement();
    s.execute("select * from sys.tables");
    assertNotNull(es.getSchemaDdl("empty", "SYS"));
    assertNull(es.getSchemaDdl("empty", "xxx"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TeiidDriver(org.teiid.jdbc.TeiidDriver) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 5 with TeiidDriver

use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.

the class TestEmbeddedServer method testTranslatorCreation.

@Test
public void testTranslatorCreation() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setUseDisk(false);
    es.start(ec);
    es.addTranslator(DummyExecutionFactory.class);
    Map<String, String> props = new HashMap<String, String>();
    props.put("supportsOrderBy", "true");
    es.addTranslator("dummy-override", "dummy", props);
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("test-one");
    mmd.addSourceMapping("one", "dummy", null);
    ModelMetaData mmd2 = new ModelMetaData();
    mmd2.setName("test-two");
    mmd2.addSourceMapping("two", "dummy", null);
    ModelMetaData mmd3 = new ModelMetaData();
    mmd3.setName("test-three");
    mmd3.addSourceMapping("three", "dummy-override", null);
    es.deployVDB("test", mmd, mmd2, mmd3);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    s.execute("select count(distinct name) from sys.tables where name like 'test%'");
    s.getResultSet().next();
    assertEquals(3, s.getResultSet().getInt(1));
    s.execute("select count(distinct name) from sys.columns where tablename like 'test%'");
    s.getResultSet().next();
    assertEquals(2, s.getResultSet().getInt(1));
}
Also used : HashMap(java.util.HashMap) TeiidDriver(org.teiid.jdbc.TeiidDriver) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Aggregations

TeiidDriver (org.teiid.jdbc.TeiidDriver)10 Test (org.junit.Test)9 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)6 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Statement (java.sql.Statement)2 Properties (java.util.Properties)2 ConnectionImpl (org.teiid.jdbc.ConnectionImpl)2 ConnectionProfile (org.teiid.jdbc.ConnectionProfile)2 CommunicationException (org.teiid.net.CommunicationException)2 ConnectionException (org.teiid.net.ConnectionException)2 TranslatorException (org.teiid.translator.TranslatorException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1