Search in sources :

Example 6 with GeneratedKeys

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);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) GeneratedKeys(org.teiid.GeneratedKeys) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with GeneratedKeys

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

GeneratedKeys (org.teiid.GeneratedKeys)7 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)2 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)2 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)2 Table (org.teiid.metadata.Table)2 ExecutionContext (org.teiid.translator.ExecutionContext)2 BatchUpdateException (java.sql.BatchUpdateException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Property (org.apache.olingo.commons.api.data.Property)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)1 OEntity (org.odata4j.core.OEntity)1 TeiidException (org.teiid.core.TeiidException)1