Search in sources :

Example 21 with UpdateExecution

use of org.teiid.translator.UpdateExecution 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)

Aggregations

UpdateExecution (org.teiid.translator.UpdateExecution)21 Test (org.junit.Test)16 Command (org.teiid.language.Command)16 ExecutionContext (org.teiid.translator.ExecutionContext)14 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10 List (java.util.List)5 TranslationUtility (org.teiid.cdk.api.TranslationUtility)4 ResultSetExecution (org.teiid.translator.ResultSetExecution)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 QueryExpression (org.teiid.language.QueryExpression)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 TranslatorException (org.teiid.translator.TranslatorException)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2 DataSource (javax.activation.DataSource)2 CommandContext (org.teiid.CommandContext)2