示例#1
0
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingSchemaTypes);

            // only visit the function calls inside the where clauses
            var visitor = new WhereClauseVisitor();

            fragment.Accept(visitor);

            foreach (var clause in visitor.Statements)
            {
                var functionVisitor = new FunctionCallVisitor("charindex");
                clause.Accept(functionVisitor);

                problems.AddRange(functionVisitor.NotIgnoredStatements(RuleId).Select(f => new SqlRuleProblem(Message, sqlObj, f)));
            }

            return(problems);
        }
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            var whereClauseVisitor = new WhereClauseVisitor();

            fragment.Accept(whereClauseVisitor);

            foreach (var whereClause in whereClauseVisitor.Statements)
            {
                var functionVisitor = new FunctionCallVisitor();
                whereClause.Accept(functionVisitor);

                var offenders = from FunctionCall f in functionVisitor.NotIgnoredStatements(RuleId)
                                where CheckFunction(f)
                                select f;

                problems.AddRange(offenders.Select(f => new SqlRuleProblem(Message, sqlObj, f)));
            }

            return(problems);
        }
示例#3
0
            protected override Expression VisitUnary(UnaryExpression node)
            {
                switch (node.NodeType)
                {
                case ExpressionType.Not:
                    if (node.Operand is SubQueryExpression)
                    {
                        var nested = new WhereClauseVisitor(_parent, _mapping);
                        nested.Visit(node.Operand);

                        var @where = new NotWhereFragment(nested.ToWhereFragment());
                        _register.Peek()(@where);
                    }
                    else
                    {
                        var visitor = new NotVisitor(this, _mapping, _register.Peek(), _parent._serializer);
                        visitor.Visit(node);
                    }

                    return(null);
                }


                return(base.VisitUnary(node));
            }
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var sqlObjName = ruleExecutionContext.GetObjectName(sqlObj);
            var fragment   = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            var whereClauseVisitor = new WhereClauseVisitor();

            fragment.Accept(whereClauseVisitor);

            foreach (var whereClause in whereClauseVisitor.Statements)
            {
                var inPredicateVisitor = new InPredicateVisitor();
                whereClause.Accept(inPredicateVisitor);

                var offenders = inPredicateVisitor.NotIgnoredStatements(RuleId).Where(i => i.Subquery != null);
                problems.AddRange(offenders.Select(t => new SqlRuleProblem(Message, sqlObj, t)));
            }

            return(problems);
        }
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            var whereClauseVisitor = new WhereClauseVisitor();

            fragment.Accept(whereClauseVisitor);

            foreach (var whereClause in whereClauseVisitor.Statements)
            {
                var binaryExpressionVisitor = new BinaryExpressionVisitor();
                whereClause.Accept(binaryExpressionVisitor);

                foreach (var comparison in binaryExpressionVisitor.NotIgnoredStatements(RuleId))
                {
                    if (CheckBinaryExpression(comparison).GetValueOrDefault(false))
                    {
                        problems.Add(new SqlRuleProblem(Message, sqlObj, comparison));
                    }
                }
            }

            return(problems);
        }
示例#6
0
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            var whereClauseVisitor = new WhereClauseVisitor();

            fragment.Accept(whereClauseVisitor);

            foreach (var whereClause in whereClauseVisitor.Statements)
            {
                var booleanComparisonVisitor = new BooleanComparisonVisitor();
                whereClause.Accept(booleanComparisonVisitor);

                var offenders = booleanComparisonVisitor.NotIgnoredStatements(RuleId)
                                .Where(c =>
                                       c.ComparisonType == BooleanComparisonType.NotEqualToBrackets ||
                                       c.ComparisonType == BooleanComparisonType.NotEqualToExclamation);

                var sqlObjName = ruleExecutionContext.GetObjectName(sqlObj);
                problems.AddRange(offenders.Select(t => new SqlRuleProblem(Message, sqlObj, t)));
            }

            return(problems);
        }
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            // only visit the operators in the where clause
            var visitor = new WhereClauseVisitor();

            fragment.Accept(visitor);

            foreach (var clause in visitor.Statements)
            {
                var beVisitor = new BooleanParenthesesExpressionVisitor();
                clause.Accept(beVisitor);

                var offenders = (from BooleanParenthesisExpression be in beVisitor.NotIgnoredStatements(RuleId)
                                 where TestClause(be)
                                 select be).ToList();

                problems.AddRange(offenders.Select(o => new SqlRuleProblem(Message, sqlObj, o)));
            }

            return(problems);
        }
 internal static string BuildWhereClause(Expression exp)
 {
     var lambda = exp as LambdaExpression;
     if (lambda == null)
     {
         throw new InvalidCypherWhereExpressionException();
     }
     var visitor = new WhereClauseVisitor();
     return visitor.Translate(lambda.Body);
 }
示例#9
0
        public override void Visit(DeleteStatement node)
        {
            var whereClauseVisitor = new WhereClauseVisitor();

            node.Accept(whereClauseVisitor);

            if (whereClauseVisitor.whereClausefound)
            {
                errorCallback(RULE_NAME, "Delete " + RULE_TEXT, node.StartLine, GetColumnNumber(node));
            }
        }
示例#10
0
        public void Test_can_parse_single_condition_as_expression()
        {
            CmsqlParser cmsqlParser = CmsqlParserFactory.CreateParserForQuery("where foo != 'bar'");

            CmsqlParser.WhereClauseContext parseTree = cmsqlParser.whereClause();

            WhereClauseVisitor    visitor    = new WhereClauseVisitor();
            ICmsqlQueryExpression expression = visitor.VisitWhereClause(parseTree);

            expression.Should().BeOfType <CmsqlQueryCondition>();
        }
        internal static string BuildWhereClause(Expression exp)
        {
            exp = ExpressionEvaluator.PartialEval(exp);
            var lambda = exp as LambdaExpression;

            if (lambda == null)
            {
                throw new InvalidCypherWhereExpressionException();
            }
            var visitor = new WhereClauseVisitor();

            return(visitor.Translate(lambda.Body));
        }
示例#12
0
        public void Test_can_parse_grouped_expression_and_condition()
        {
            CmsqlParser cmsqlParser = CmsqlParserFactory.CreateParserForQuery("where (bar = 'foo' and foo = 'bar') or foo != 'bar'");

            CmsqlParser.WhereClauseContext parseTree = cmsqlParser.whereClause();

            WhereClauseVisitor         visitor          = new WhereClauseVisitor();
            CmsqlQueryBinaryExpression binaryExpression = visitor.VisitWhereClause(parseTree) as CmsqlQueryBinaryExpression;

            binaryExpression.Operator.ShouldBeEquivalentTo(ConditionalOperator.Or);
            binaryExpression.LeftExpression.Should().BeOfType <CmsqlQueryBinaryExpression>();
            binaryExpression.RightExpression.Should().BeOfType <CmsqlQueryCondition>();
        }
示例#13
0
        public IWhereFragment ParseWhereFragment(IQueryableDocument mapping, Expression expression)
        {
            if (expression is LambdaExpression)
            {
                expression = expression.As<LambdaExpression>().Body;
            }

            var visitor = new WhereClauseVisitor(this, mapping);
            visitor.Visit(expression);
            var whereFragment = visitor.ToWhereFragment();

            if (whereFragment == null)
            {
                throw new NotSupportedException("Marten does not (yet) support this Linq query type");
            }

            return whereFragment;
        }
示例#14
0
        public IWhereFragment ParseWhereFragment(IQueryableDocument mapping, Expression expression)
        {
            if (expression is LambdaExpression)
            {
                expression = expression.As <LambdaExpression>().Body;
            }

            var visitor = new WhereClauseVisitor(this, mapping);

            visitor.Visit(expression);
            var whereFragment = visitor.ToWhereFragment();

            if (whereFragment == null)
            {
                throw new NotSupportedException("Marten does not (yet) support this Linq query type");
            }

            return(whereFragment);
        }
示例#15
0
        public IWhereFragment ParseWhereFragment(IFieldMapping mapping, Expression expression)
        {
            if (expression is LambdaExpression l)
            {
                expression = l.Body;
            }

            var visitor = new WhereClauseVisitor(this, mapping);

            visitor.Visit(expression);
            var whereFragment = visitor.ToWhereFragment();

            if (whereFragment == null)
            {
                throw new NotSupportedException($"Marten does not (yet) support this Linq query type ({expression})");
            }

            return(whereFragment);
        }
示例#16
0
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }

            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(ProgrammingAndViewSchemaTypes);

            var whereClauseVisitor = new WhereClauseVisitor();

            fragment.Accept(whereClauseVisitor);

            foreach (var whereClause in whereClauseVisitor.Statements)
            {
                var likeVisitor = new LikePredicateVisitor();
                whereClause.Accept(likeVisitor);

                foreach (var like in likeVisitor.NotIgnoredStatements(RuleId))
                {
                    var stringLiteralVisitor = new StringLiteralVisitor();
                    like.Accept(stringLiteralVisitor);

                    var literal = stringLiteralVisitor.NotIgnoredStatements(RuleId)
                                  .FirstOrDefault(l => l.Value.StartsWith("%") && l.Value.Length > 1);

                    if (literal != null)
                    {
                        problems.Add(new SqlRuleProblem(Message, sqlObj, like));
                        break;
                    }
                }
            }


            return(problems);
        }
示例#17
0
 public NotVisitor(WhereClauseVisitor parent, Action<IWhereFragment> callback)
 {
     _parent = parent;
     _callback = callback;
 }
示例#18
0
            protected override Expression VisitUnary(UnaryExpression node)
            {
                switch (node.NodeType)
                {
                    case ExpressionType.Not:
                        if (node.Operand is SubQueryExpression)
                        {
                            var nested = new WhereClauseVisitor(_parent, _mapping);
                            nested.Visit(node.Operand);

                            var @where = new NotWhereFragment(nested.ToWhereFragment());
                            _register.Peek()(@where);
                        }
                        else
                        {
                            var visitor = new NotVisitor(this, _mapping, _register.Peek());
                            visitor.Visit(node);
                        }

                        return null;
                }


                return base.VisitUnary(node);
            }
示例#19
0
 public NotVisitor(WhereClauseVisitor parent, Action <IWhereFragment> callback)
 {
     _parent   = parent;
     _callback = callback;
 }
        internal void Validate()
        {
            // turn rewritings into cell trees
            // plain: according to rewritings for case statements
            var plainMemberValueTrees = CreateMemberValueTrees(false);
            // complement: uses complement rewriting for the last WHEN ... THEN
            // This is how the final case statement will be generated in update views
            var complementMemberValueTrees = CreateMemberValueTrees(true);

            var plainWhereClauseVisitor      = new WhereClauseVisitor(_basicView, plainMemberValueTrees);
            var complementWhereClauseVisitor = new WhereClauseVisitor(_basicView, complementMemberValueTrees);

            // produce CellTree for each SQuery
            foreach (var wrapper in _viewgenContext.AllWrappersForExtent)
            {
                var cell = wrapper.OnlyInputCell;
                // construct cell tree for CQuery
                CellTreeNode cQueryTree = new LeafCellTreeNode(_viewgenContext, wrapper);
                // sQueryTree: unfolded update view inside S-side of the cell
                CellTreeNode sQueryTree;
                // construct cell tree for SQuery (will be used for domain constraint checking)
                var complementSQueryTreeForCondition = complementWhereClauseVisitor.GetCellTreeNode(cell.SQuery.WhereClause);
                Debug.Assert(complementSQueryTreeForCondition != null, "Rewriting for S-side query is unsatisfiable");
                if (complementSQueryTreeForCondition == null)
                {
                    continue; // situation should never happen
                }
                if (complementSQueryTreeForCondition != _basicView)
                {
                    // intersect with basic expression
                    sQueryTree = new OpCellTreeNode(_viewgenContext, CellTreeOpType.IJ, complementSQueryTreeForCondition, _basicView);
                }
                else
                {
                    sQueryTree = _basicView;
                }

                // Append in-set or in-end condition to both queries to produce more concise errors
                // Otherwise, the errors are of the form "if there exists an entity in extent, then violation". We don't care about empty extents
                var inExtentCondition = BoolExpression.CreateLiteral(wrapper.CreateRoleBoolean(), _viewgenContext.MemberMaps.QueryDomainMap);

                BoolExpression unsatisfiedConstraint;
                if (!CheckEquivalence(
                        cQueryTree.RightFragmentQuery, sQueryTree.RightFragmentQuery, inExtentCondition,
                        out unsatisfiedConstraint))
                {
                    var extentName = StringUtil.FormatInvariant("{0}", _viewgenContext.Extent);

                    // Simplify to produce more readable error messages
                    cQueryTree.RightFragmentQuery.Condition.ExpensiveSimplify();
                    sQueryTree.RightFragmentQuery.Condition.ExpensiveSimplify();

                    var message = Strings.ViewGen_CQ_PartitionConstraint(extentName);

                    ReportConstraintViolation(
                        message, unsatisfiedConstraint, ViewGenErrorCode.PartitionConstraintViolation,
                        cQueryTree.GetLeaves().Concat(sQueryTree.GetLeaves()));
                }

                var plainSQueryTreeForCondition = plainWhereClauseVisitor.GetCellTreeNode(cell.SQuery.WhereClause);
                Debug.Assert(plainSQueryTreeForCondition != null, "Rewriting for S-side query is unsatisfiable");
                if (plainSQueryTreeForCondition != null)
                {
                    // Query is non-empty. Check domain constraints on:
                    // (a) swapped members
                    DomainConstraintVisitor.CheckConstraints(plainSQueryTreeForCondition, wrapper, _viewgenContext, _errorLog);
                    //If you have already found errors, just continue on to the next wrapper instead of                    //collecting more errors for the same
                    if (_errorLog.Count > 0)
                    {
                        continue;
                    }
                    // (b) projected members
                    CheckConstraintsOnProjectedConditionMembers(plainMemberValueTrees, wrapper, sQueryTree, inExtentCondition);
                    if (_errorLog.Count > 0)
                    {
                        continue;
                    }
                }
                CheckConstraintsOnNonNullableMembers(wrapper);
            }

            if (_errorLog.Count > 0)
            {
                ExceptionHelpers.ThrowMappingException(_errorLog, _viewgenContext.Config);
            }
        }