Search in sources :

Example 1 with UpdateInfo

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

the class QueryResolver method resolveView.

public static QueryNode resolveView(GroupSymbol virtualGroup, QueryNode qnode, String cacheString, QueryMetadataInterface qmi, boolean logValidation) throws TeiidComponentException, QueryMetadataException, QueryResolverException, QueryValidatorException {
    qmi = qmi.getDesignTimeMetadata();
    // $NON-NLS-1$
    cacheString = "transformation/" + cacheString;
    QueryNode cachedNode = (QueryNode) qmi.getFromMetadataCache(virtualGroup.getMetadataID(), cacheString);
    if (cachedNode == null) {
        Command result = qnode.getCommand();
        List<String> bindings = null;
        if (result == null) {
            try {
                result = QueryParser.getQueryParser().parseCommand(qnode.getQuery());
            } catch (QueryParserException e) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID30065, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30065, virtualGroup));
            }
            bindings = qnode.getBindings();
        } else {
            result = (Command) result.clone();
        }
        if (bindings != null && !bindings.isEmpty()) {
            QueryResolver.resolveWithBindingMetadata(result, qmi, qnode, true);
        } else {
            QueryResolver.resolveCommand(result, qmi, false);
        }
        Request.validateWithVisitor(new ValidationVisitor(), qmi, result);
        validateProjectedSymbols(virtualGroup, qmi, result);
        cachedNode = new QueryNode(qnode.getQuery());
        cachedNode.setCommand(result);
        if (isView(virtualGroup, qmi)) {
            String updatePlan = qmi.getUpdatePlan(virtualGroup.getMetadataID());
            String deletePlan = qmi.getDeletePlan(virtualGroup.getMetadataID());
            String insertPlan = qmi.getInsertPlan(virtualGroup.getMetadataID());
            // the elements must be against the view and not the alias
            if (virtualGroup.getDefinition() != null) {
                GroupSymbol group = new GroupSymbol(virtualGroup.getNonCorrelationName());
                group.setMetadataID(virtualGroup.getMetadataID());
                virtualGroup = group;
            }
            List<ElementSymbol> elements = ResolverUtil.resolveElementsInGroup(virtualGroup, qmi);
            UpdateValidator validator = new UpdateValidator(qmi, determineType(insertPlan), determineType(updatePlan), determineType(deletePlan));
            validator.validate(result, elements);
            UpdateInfo info = validator.getUpdateInfo();
            if (logValidation && qmi.groupSupports(virtualGroup.getMetadataID(), SupportConstants.Group.UPDATE)) {
                if (info.isInherentInsert() && validator.getInsertReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getInsertReport().getFailureMessage(), SQLConstants.Reserved.INSERT, qmi.getFullName(virtualGroup.getMetadataID())));
                }
                if (info.isInherentUpdate() && validator.getUpdateReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getUpdateReport().getFailureMessage(), SQLConstants.Reserved.UPDATE, qmi.getFullName(virtualGroup.getMetadataID())));
                }
                if (info.isInherentDelete() && validator.getDeleteReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getDeleteReport().getFailureMessage(), SQLConstants.Reserved.DELETE, qmi.getFullName(virtualGroup.getMetadataID())));
                }
            }
            cachedNode.setUpdateInfo(info);
        }
        qmi.addToMetadataCache(virtualGroup.getMetadataID(), cacheString, cachedNode);
    }
    return cachedNode;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ValidationVisitor(org.teiid.query.validator.ValidationVisitor) QueryParserException(org.teiid.api.exception.query.QueryParserException) Command(org.teiid.query.sql.lang.Command) QueryNode(org.teiid.query.mapping.relational.QueryNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) UpdateValidator(org.teiid.query.validator.UpdateValidator) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo)

Example 2 with UpdateInfo

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

the class TestUpdateValidator method helpTest.

private UpdateValidator helpTest(String sql, TransformationMetadata md, boolean failInsert, boolean failUpdate, boolean failDelete) {
    try {
        String vGroup = "gx";
        Command command = createView(sql, md, vGroup);
        UpdateValidator uv = new UpdateValidator(md, UpdateType.INHERENT, UpdateType.INHERENT, UpdateType.INHERENT);
        GroupSymbol gs = new GroupSymbol(vGroup);
        ResolverUtil.resolveGroup(gs, md);
        uv.validate(command, ResolverUtil.resolveElementsInGroup(gs, md));
        UpdateInfo info = uv.getUpdateInfo();
        assertEquals(uv.getReport().getFailureMessage(), failInsert, info.getInsertValidationError() != null);
        assertEquals(uv.getReport().getFailureMessage(), failUpdate, info.getUpdateValidationError() != null);
        assertEquals(uv.getReport().getFailureMessage(), failDelete, info.getDeleteValidationError() != null);
        return uv;
    } catch (TeiidException e) {
        throw new RuntimeException(e);
    }
}
Also used : Command(org.teiid.query.sql.lang.Command) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo) TeiidException(org.teiid.core.TeiidException)

Example 3 with UpdateInfo

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

the class QueryRewriter method rewriteUpdate.

private Command rewriteUpdate(Update update) throws TeiidComponentException, TeiidProcessingException {
    if (update.getGroup().getDefinition() != null) {
        removeAlias(update, update.getGroup());
    }
    Command c = rewriteForWriteThrough(update);
    if (c != null) {
        return c;
    }
    UpdateInfo info = update.getUpdateInfo();
    if (info != null && info.isInherentUpdate()) {
        if (!info.getUnionBranches().isEmpty()) {
            List<Command> batchedUpdates = new ArrayList<Command>(info.getUnionBranches().size() + 1);
            for (UpdateInfo branchInfo : info.getUnionBranches()) {
                batchedUpdates.add(rewriteInherentUpdate((Update) update.clone(), branchInfo));
            }
            batchedUpdates.add(0, rewriteInherentUpdate(update, info));
            return new BatchedUpdateCommand(batchedUpdates, true);
        }
        return rewriteInherentUpdate(update, info);
    }
    boolean preserveUnknownOld = preserveUnknown;
    preserveUnknown = true;
    // Evaluate any function on the right side of set clauses
    for (SetClause entry : update.getChangeList().getClauses()) {
        entry.setValue(rewriteExpressionDirect(entry.getValue()));
    }
    preserveUnknown = preserveUnknownOld;
    // Rewrite criteria
    Criteria crit = update.getCriteria();
    if (crit != null) {
        preserveUnknown = false;
        update.setCriteria(rewriteCriteria(crit));
        preserveUnknown = preserveUnknownOld;
    }
    return update;
}
Also used : RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo)

Example 4 with UpdateInfo

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

the class QueryRewriter method rewriteDelete.

private Command rewriteDelete(Delete delete) throws TeiidComponentException, TeiidProcessingException {
    if (delete.getGroup().getDefinition() != null) {
        removeAlias(delete, delete.getGroup());
    }
    Command c = rewriteForWriteThrough(delete);
    if (c != null) {
        return c;
    }
    UpdateInfo info = delete.getUpdateInfo();
    if (info != null && info.isInherentDelete()) {
        if (!info.getUnionBranches().isEmpty()) {
            List<Command> batchedUpdates = new ArrayList<Command>(info.getUnionBranches().size() + 1);
            for (UpdateInfo branchInfo : info.getUnionBranches()) {
                batchedUpdates.add(rewriteInherentDelete((Delete) delete.clone(), branchInfo));
            }
            batchedUpdates.add(0, rewriteInherentDelete(delete, info));
            return new BatchedUpdateCommand(batchedUpdates, true);
        }
        return rewriteInherentDelete(delete, info);
    }
    // Rewrite criteria
    Criteria crit = delete.getCriteria();
    if (crit != null) {
        boolean preserveUnknownOld = preserveUnknown;
        preserveUnknown = false;
        delete.setCriteria(rewriteCriteria(crit));
        preserveUnknown = preserveUnknownOld;
    }
    return delete;
}
Also used : RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo)

Example 5 with UpdateInfo

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

the class ValidationVisitor method validateUpdate.

protected void validateUpdate(Update update) {
    try {
        UpdateInfo info = update.getUpdateInfo();
        // list of elements that are being updated
        for (SetClause entry : update.getChangeList().getClauses()) {
            ElementSymbol elementID = entry.getSymbol();
            // Check that left side element is updatable
            if (!getMetadata().elementSupports(elementID.getMetadataID(), SupportConstants.Element.UPDATE)) {
                // $NON-NLS-1$
                handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0059", elementID), elementID);
            }
            Object metadataID = elementID.getMetadataID();
            if (getMetadata().isMultiSourceElement(metadataID)) {
                // $NON-NLS-1$
                handleValidationError(QueryPlugin.Util.getString("multi_source_update_not_allowed", elementID), elementID);
            }
            // Check that right expression is a constant and is non-null
            Expression value = entry.getValue();
            if (EvaluatableVisitor.isFullyEvaluatable(value, true)) {
                try {
                    value = new Constant(Evaluator.evaluate(value));
                } catch (ExpressionEvaluationException err) {
                }
            }
            if (value instanceof Constant) {
                // If value is null, check that element supports this as a nullable column
                if (((Constant) value).isNull() && !getMetadata().elementSupports(elementID.getMetadataID(), SupportConstants.Element.NULL)) {
                    // $NON-NLS-1$
                    handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0060", SQLStringVisitor.getSQLString(elementID)), elementID);
                }
            // end of if
            }
        }
        if (info != null && info.isInherentUpdate()) {
            validateUpdate(update, Command.TYPE_UPDATE, info);
            Set<ElementSymbol> updateCols = update.getChangeList().getClauseMap().keySet();
            if (!info.hasValidUpdateMapping(updateCols)) {
                handleValidationError(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30376, updateCols), update);
            }
        }
    } catch (TeiidException e) {
        handleException(e, update);
    }
    validateSetClauseList(update.getChangeList());
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) SaxonXQueryExpression(org.teiid.query.xquery.saxon.SaxonXQueryExpression) LanguageObject(org.teiid.query.sql.LanguageObject) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo) TeiidException(org.teiid.core.TeiidException)

Aggregations

UpdateInfo (org.teiid.query.validator.UpdateValidator.UpdateInfo)7 TeiidException (org.teiid.core.TeiidException)2 RuleMergeCriteria (org.teiid.query.optimizer.relational.rules.RuleMergeCriteria)2 LanguageObject (org.teiid.query.sql.LanguageObject)2 Command (org.teiid.query.sql.lang.Command)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)1 QueryParserException (org.teiid.api.exception.query.QueryParserException)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)1 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)1 FunctionLibrary (org.teiid.query.function.FunctionLibrary)1 QueryNode (org.teiid.query.mapping.relational.QueryNode)1 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)1 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)1 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)1 UpdateValidator (org.teiid.query.validator.UpdateValidator)1 UpdateMapping (org.teiid.query.validator.UpdateValidator.UpdateMapping)1 ValidationVisitor (org.teiid.query.validator.ValidationVisitor)1 SaxonXQueryExpression (org.teiid.query.xquery.saxon.SaxonXQueryExpression)1