Search in sources :

Example 1 with GeneratedKeysImpl

use of org.teiid.query.util.GeneratedKeysImpl in project teiid by teiid.

the class RequestWorkItem method handleGeneratedKeys.

private boolean handleGeneratedKeys(ResultsMessage response) throws QueryMetadataException, TeiidComponentException {
    GeneratedKeysImpl keys = this.processor.getContext().getGeneratedKeys();
    if (keys.getKeys().isEmpty()) {
        return false;
    }
    List<ElementSymbol> keyCols = this.processor.getContext().getReturnAutoGeneratedKeys();
    // match the key cols with the result
    ElementSymbol col = keyCols.get(0);
    String[] columnNames = keys.getColumnNames();
    if (keyCols.size() != columnNames.length) {
        return false;
    }
    if (!col.getGroupSymbol().isTempTable() && this.processor.getContext().getMetadata().isVirtualGroup(col.getGroupSymbol().getMetadataID())) {
        if (keyCols.size() != 1 && ((Insert) originalCommand).getUpdateInfo().isInherentInsert()) {
            // TODO: we need to ensure the column names line up correctly
            return false;
        }
        columnNames = new String[columnNames.length];
        columnNames[0] = col.getShortName();
    }
    response.setColumnNames(columnNames);
    String[] dataTypes = new String[columnNames.length];
    for (int i = 0; i < dataTypes.length; i++) {
        dataTypes[i] = DataTypeManager.getDataTypeName(keys.getColumnTypes()[i]);
    }
    response.setUpdateCount((Integer) response.getResultsList().get(0).get(0));
    response.setDataTypes(dataTypes);
    response.setResults(keys.getKeys());
    response.setLastRow(keys.getKeys().size());
    return true;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) GeneratedKeysImpl(org.teiid.query.util.GeneratedKeysImpl) Insert(org.teiid.query.sql.lang.Insert) CacheHint(org.teiid.query.sql.lang.CacheHint)

Example 2 with GeneratedKeysImpl

use of org.teiid.query.util.GeneratedKeysImpl in project teiid by teiid.

the class TempTable method insert.

public TupleSource insert(TupleSource tuples, final List<ElementSymbol> variables, boolean canUndo, boolean upsert, CommandContext context) throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException {
    List<ElementSymbol> cols = getColumns();
    final int[] indexes = new int[cols.size()];
    boolean shouldProject = false;
    for (int i = 0; i < cols.size(); i++) {
        indexes[i] = variables.indexOf(cols.get(i));
        shouldProject |= (indexes[i] != i);
    }
    InsertUpdateProcessor up = new InsertUpdateProcessor(tuples, rowId != null, shouldProject ? indexes : null, canUndo, upsert);
    if (context != null && context.isReturnAutoGeneratedKeys() && rowId == null) {
        List<String> colNames = null;
        List<Class<?>> colTypes = null;
        for (int i = 0; i < tree.getKeyLength(); i++) {
            TempMetadataID col = tid.getElements().get(i);
            if (col.isAutoIncrement() && indexes[i] == -1) {
                if (colNames == null) {
                    colNames = new ArrayList<String>();
                    colTypes = new ArrayList<Class<?>>();
                }
                colNames.add(col.getName());
                colTypes.add(col.getType());
                break;
            }
        }
        if (colNames != null) {
            GeneratedKeysImpl keys = context.returnGeneratedKeys(colNames.toArray(new String[colNames.size()]), colTypes.toArray(new Class<?>[colTypes.size()]));
            up.setGeneratedKeys(keys);
        }
    }
    long updateCount = up.process();
    tid.setCardinality(tree.getRowCount());
    tid.getTableData().dataModified(updateCount);
    return CollectionTupleSource.createUpdateCountArrayTupleSource(updateCount);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GeneratedKeysImpl(org.teiid.query.util.GeneratedKeysImpl) CacheHint(org.teiid.query.sql.lang.CacheHint)

Aggregations

CacheHint (org.teiid.query.sql.lang.CacheHint)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2 GeneratedKeysImpl (org.teiid.query.util.GeneratedKeysImpl)2 TempMetadataID (org.teiid.query.metadata.TempMetadataID)1 Insert (org.teiid.query.sql.lang.Insert)1