Search in sources :

Example 1 with UpdateMapping

use of org.teiid.query.validator.UpdateValidator.UpdateMapping in project teiid by teiid.

the class QueryRewriter method rewriteInherentDelete.

private Command rewriteInherentDelete(Delete delete, UpdateInfo info) throws QueryMetadataException, TeiidComponentException, QueryResolverException, TeiidProcessingException {
    UpdateMapping mapping = info.getDeleteTarget();
    if (info.isSimple()) {
        Collection<ElementSymbol> elements = getAllElementsUsed(delete, delete.getGroup());
        UpdateMapping fullMapping = info.findUpdateMapping(elements, false);
        if (fullMapping != null) {
            delete.setGroup(mapping.getGroup().clone());
            // TODO: properly handle correlated references
            DeepPostOrderNavigator.doVisit(delete, new ExpressionMappingVisitor(mapping.getUpdatableViewSymbols(), true));
            delete.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(delete.getGroup(), metadata, Command.TYPE_DELETE, true));
            if (info.getViewDefinition().getCriteria() != null) {
                delete.setCriteria(Criteria.combineCriteria(delete.getCriteria(), (Criteria) info.getViewDefinition().getCriteria().clone()));
            }
            return rewriteDelete(delete);
        }
    }
    Query query = (Query) info.getViewDefinition().clone();
    query.setOrderBy(null);
    SymbolMap expressionMapping = SymbolMap.createSymbolMap(delete.getGroup(), query.getProjectedSymbols(), metadata);
    query.setSelect(new Select());
    ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
    Criteria crit = delete.getCriteria();
    if (crit != null) {
        PostOrderNavigator.doVisit(crit, emv);
        query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
    }
    GroupSymbol group = mapping.getGroup();
    String correlationName = mapping.getCorrelatedName().getName();
    return createDeleteProcedure(delete, query, group, correlationName);
}
Also used : UpdateMapping(org.teiid.query.validator.UpdateValidator.UpdateMapping) SymbolMap(org.teiid.query.sql.util.SymbolMap) RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor)

Example 2 with UpdateMapping

use of org.teiid.query.validator.UpdateValidator.UpdateMapping in project teiid by teiid.

the class QueryRewriter method rewriteInsert.

private Command rewriteInsert(Insert insert) throws TeiidComponentException, TeiidProcessingException {
    Command c = rewriteInsertForWriteThrough(insert);
    if (c != null) {
        return c;
    }
    UpdateInfo info = insert.getUpdateInfo();
    if (info != null && info.isInherentInsert()) {
        // TODO: update error messages
        UpdateMapping mapping = info.findInsertUpdateMapping(insert, true);
        if (mapping == null) {
            throw new QueryValidatorException(QueryPlugin.Event.TEIID30375, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30375, insert.getVariables()));
        }
        Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
        List<ElementSymbol> mappedSymbols = new ArrayList<ElementSymbol>(insert.getVariables().size());
        for (ElementSymbol symbol : insert.getVariables()) {
            mappedSymbols.add(symbolMap.get(symbol));
        }
        insert.setVariables(mappedSymbols);
        insert.setGroup(mapping.getGroup().clone());
        insert.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(insert.getGroup(), metadata, Command.TYPE_INSERT, true));
        return rewriteInsert(insert);
    }
    if (insert.getQueryExpression() != null) {
        insert.setQueryExpression((QueryCommand) rewriteCommand(insert.getQueryExpression(), true));
        return correctDatatypes(insert);
    }
    // Evaluate any function / constant trees in the insert values
    List expressions = insert.getValues();
    List evalExpressions = new ArrayList(expressions.size());
    Iterator expIter = expressions.iterator();
    boolean preserveUnknownOld = preserveUnknown;
    preserveUnknown = true;
    while (expIter.hasNext()) {
        Expression exp = (Expression) expIter.next();
        if (processing && exp instanceof ExpressionSymbol) {
            // expression symbols that were created in the PlanToProcessesConverter
            evalExpressions.add(evaluate(exp, true));
        } else {
            evalExpressions.add(rewriteExpressionDirect(exp));
        }
    }
    preserveUnknown = preserveUnknownOld;
    insert.setValues(evalExpressions);
    return insert;
}
Also used : UpdateMapping(org.teiid.query.validator.UpdateValidator.UpdateMapping) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo)

Example 3 with UpdateMapping

use of org.teiid.query.validator.UpdateValidator.UpdateMapping in project teiid by teiid.

the class QueryRewriter method rewriteInherentUpdate.

private Command rewriteInherentUpdate(Update update, UpdateInfo info) throws QueryValidatorException, QueryMetadataException, TeiidComponentException, QueryResolverException, TeiidProcessingException {
    UpdateMapping mapping = info.findUpdateMapping(update.getChangeList().getClauseMap().keySet(), false);
    if (mapping == null) {
        throw new QueryValidatorException(QueryPlugin.Event.TEIID30376, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30376, update.getChangeList().getClauseMap().keySet()));
    }
    Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
    if (info.isSimple()) {
        Collection<ElementSymbol> elements = getAllElementsUsed(update, update.getGroup());
        UpdateMapping fullMapping = info.findUpdateMapping(elements, false);
        if (fullMapping != null) {
            update.setGroup(mapping.getGroup().clone());
            for (SetClause clause : update.getChangeList().getClauses()) {
                clause.setSymbol(symbolMap.get(clause.getSymbol()));
            }
            // TODO: properly handle correlated references
            DeepPostOrderNavigator.doVisit(update, new ExpressionMappingVisitor(symbolMap, true));
            if (info.getViewDefinition().getCriteria() != null) {
                update.setCriteria(Criteria.combineCriteria(update.getCriteria(), (Criteria) info.getViewDefinition().getCriteria().clone()));
            }
            // resolve
            update.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(update.getGroup(), metadata, Command.TYPE_UPDATE, true));
            return rewriteUpdate(update);
        }
    }
    Query query = (Query) info.getViewDefinition().clone();
    query.setOrderBy(null);
    SymbolMap expressionMapping = SymbolMap.createSymbolMap(update.getGroup(), query.getProjectedSymbols(), metadata);
    SetClauseList setClauseList = (SetClauseList) update.getChangeList().clone();
    GroupSymbol varGroup = getVarGroup(update);
    ArrayList<Expression> selectSymbols = mapChangeList(setClauseList, symbolMap, varGroup);
    query.setSelect(new Select(selectSymbols));
    ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
    PostOrderNavigator.doVisit(query.getSelect(), emv);
    Criteria crit = update.getCriteria();
    if (crit != null) {
        PostOrderNavigator.doVisit(crit, emv);
        query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
    }
    GroupSymbol group = mapping.getGroup();
    String correlationName = mapping.getCorrelatedName().getName();
    return createUpdateProcedure(update, query, group, correlationName, setClauseList, varGroup, null);
}
Also used : UpdateMapping(org.teiid.query.validator.UpdateValidator.UpdateMapping) SymbolMap(org.teiid.query.sql.util.SymbolMap) RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException)

Aggregations

UpdateMapping (org.teiid.query.validator.UpdateValidator.UpdateMapping)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)2 RuleMergeCriteria (org.teiid.query.optimizer.relational.rules.RuleMergeCriteria)2 SymbolMap (org.teiid.query.sql.util.SymbolMap)2 ExpressionMappingVisitor (org.teiid.query.sql.visitor.ExpressionMappingVisitor)2 UpdateInfo (org.teiid.query.validator.UpdateValidator.UpdateInfo)1