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));
}
Aggregations