Search in sources :

Example 6 with Insert

use of org.teiid.query.sql.lang.Insert in project teiid by teiid.

the class TestGroupCollectorVisitor method testBatchedUpdateCommand.

public void testBatchedUpdateCommand() {
    GroupSymbol g1 = exampleGroupSymbol(1);
    GroupSymbol g2 = exampleGroupSymbol(2);
    GroupSymbol g3 = exampleGroupSymbol(3);
    Insert insert = new Insert();
    insert.setGroup(g1);
    Update update = new Update();
    update.setGroup(g2);
    Delete delete = new Delete();
    delete.setGroup(g3);
    List updates = new ArrayList(3);
    updates.add(insert);
    updates.add(update);
    updates.add(delete);
    Set groups = new HashSet();
    groups.add(g1);
    groups.add(g2);
    groups.add(g3);
    helpTestGroups(new BatchedUpdateCommand(updates), true, groups);
}
Also used : Delete(org.teiid.query.sql.lang.Delete) Set(java.util.Set) HashSet(java.util.HashSet) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Insert(org.teiid.query.sql.lang.Insert) Update(org.teiid.query.sql.lang.Update) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) HashSet(java.util.HashSet)

Example 7 with Insert

use of org.teiid.query.sql.lang.Insert in project teiid by teiid.

the class TestStaticSymbolMappingVisitor method testVisitInsert2.

public void testVisitInsert2() {
    Insert insert = new Insert();
    insert.setGroup(exampleGroup(true, 0));
    List values = new ArrayList();
    // $NON-NLS-1$
    values.add(new Constant("abc"));
    // $NON-NLS-1$
    values.add(new Constant("abc"));
    insert.setValues(values);
    helpTest(insert, getSymbolMap());
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Insert(org.teiid.query.sql.lang.Insert)

Example 8 with Insert

use of org.teiid.query.sql.lang.Insert in project teiid by teiid.

the class Request method validateAccess.

protected boolean validateAccess(String[] commandStr, Command command, CommandType type) throws QueryValidatorException, TeiidComponentException {
    boolean returnsResultSet = command.returnsResultSet();
    this.returnsUpdateCount = !(command instanceof StoredProcedure) && !returnsResultSet;
    if ((this.requestMsg.getResultsMode() == ResultsMode.UPDATECOUNT && returnsResultSet) || (this.requestMsg.getResultsMode() == ResultsMode.RESULTSET && !returnsResultSet)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new QueryValidatorException(QueryPlugin.Event.TEIID30490, QueryPlugin.Util.getString(this.requestMsg.getResultsMode() == ResultsMode.RESULTSET ? "Request.no_result_set" : "Request.result_set"));
    }
    createCommandContext();
    if (this.requestMsg.isReturnAutoGeneratedKeys() && command instanceof Insert) {
        Insert insert = (Insert) command;
        List<ElementSymbol> variables = ResolverUtil.resolveElementsInGroup(insert.getGroup(), metadata);
        variables.removeAll(insert.getVariables());
        Object pk = metadata.getPrimaryKey(insert.getGroup().getMetadataID());
        if (pk != null) {
            List<?> cols = metadata.getElementIDsInKey(pk);
            int colCount = 0;
            for (Iterator<ElementSymbol> iter = variables.iterator(); iter.hasNext(); ) {
                ElementSymbol variable = iter.next();
                if (!(metadata.elementSupports(variable.getMetadataID(), SupportConstants.Element.NULL) || metadata.elementSupports(variable.getMetadataID(), SupportConstants.Element.AUTO_INCREMENT)) || !cols.contains(variable.getMetadataID())) {
                    iter.remove();
                }
                colCount++;
            }
            if (colCount == cols.size()) {
                context.setReturnAutoGeneratedKeys(variables);
            }
        }
    }
    if (!this.workContext.isAdmin() && this.authorizationValidator != null) {
        return this.authorizationValidator.validate(commandStr, command, metadata, context, type);
    }
    return false;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) Insert(org.teiid.query.sql.lang.Insert)

Example 9 with Insert

use of org.teiid.query.sql.lang.Insert 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 10 with Insert

use of org.teiid.query.sql.lang.Insert in project teiid by teiid.

the class LanguageBridgeFactory method translate.

public org.teiid.language.Command translate(Command command) {
    try {
        if (command == null) {
            return null;
        }
        if (command instanceof Query) {
            Select result = translate((Query) command);
            result.setDependentValues(this.dependentSets);
            setProjected(result);
            return result;
        } else if (command instanceof SetQuery) {
            org.teiid.language.SetQuery result = translate((SetQuery) command);
            setProjected(result);
            return result;
        } else if (command instanceof Insert) {
            return translate((Insert) command);
        } else if (command instanceof Update) {
            return translate((Update) command);
        } else if (command instanceof Delete) {
            return translate((Delete) command);
        } else if (command instanceof StoredProcedure) {
            return translate((StoredProcedure) command);
        } else if (command instanceof BatchedUpdateCommand) {
            return translate((BatchedUpdateCommand) command);
        }
        // $NON-NLS-1$
        throw new AssertionError(command.getClass().getName() + " " + command);
    } finally {
        this.allValues.clear();
        this.dependentSets = null;
        this.valueIndex = 0;
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SetQuery(org.teiid.query.sql.lang.SetQuery) SetQuery(org.teiid.query.sql.lang.SetQuery) Select(org.teiid.language.Select) Insert(org.teiid.query.sql.lang.Insert) Update(org.teiid.query.sql.lang.Update)

Aggregations

Insert (org.teiid.query.sql.lang.Insert)29 List (java.util.List)13 Command (org.teiid.query.sql.lang.Command)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Constant (org.teiid.query.sql.symbol.Constant)7 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)6 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)6 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)5 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)4 TupleSource (org.teiid.common.buffer.TupleSource)4 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)4 Delete (org.teiid.query.sql.lang.Delete)4 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)4 Update (org.teiid.query.sql.lang.Update)4 Expression (org.teiid.query.sql.symbol.Expression)4 CommandContext (org.teiid.query.util.CommandContext)4