use of org.teiid.GeneratedKeys in project teiid by teiid.
the class ForEachRowPlan method assignGeneratedKey.
private void assignGeneratedKey(VariableContext vc) {
LinkedHashMap<ElementSymbol, Object> key = null;
for (Map.Entry<Object, Object> entry : vc.getVariableMap().entrySet()) {
if (!(entry.getKey() instanceof ElementSymbol)) {
continue;
}
ElementSymbol es = (ElementSymbol) entry.getKey();
if (!es.getGroupSymbol().getName().equalsIgnoreCase(SQLConstants.NonReserved.KEY)) {
continue;
}
Object value = entry.getValue();
if (value == null) {
return;
}
if (key == null) {
key = new LinkedHashMap<ElementSymbol, Object>();
}
key.put(es, value);
}
if (key != null) {
String[] names = new String[key.size()];
Class<?>[] types = new Class<?>[key.size()];
List<Object> values = new ArrayList<Object>(key.size());
for (Map.Entry<ElementSymbol, Object> entry : key.entrySet()) {
names[values.size()] = entry.getKey().getShortName();
types[values.size()] = entry.getKey().getType();
values.add(entry.getValue());
}
GeneratedKeys generatedKeys = getContext().returnGeneratedKeys(names, types);
generatedKeys.addKey(values);
}
}
use of org.teiid.GeneratedKeys 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