public void VisitInstrs <T>(VisitFunc <IRInstrList, IRInstruction, T> visitFunc, T arg)
 {
     for (int i = 0; i < this.Count; i++)
     {
         visitFunc(this, this[i], ref i, arg);
     }
 }
Exemple #2
0
 public void VisitInstrs <T>(VisitFunc <ILInstrList, ILInstruction, T> visitFunc, T arg)
 {
     for (var i = 0; i < Count; i++)
     {
         visitFunc(this, this[i], ref i, arg);
     }
 }
Exemple #3
0
        void Visit(IQueryElement element, bool all, bool parentFirst, VisitFunc action)
        {
            if (element == null || !all && _visitedElements.ContainsKey(element))
            {
                return;
            }

            if (parentFirst)
            {
                var @continue = action(element);

                if (!all)
                {
                    _visitedElements.Add(element, element);
                }

                if (!@continue)
                {
                    return;
                }
            }

            switch (element.ElementType)
            {
            case QueryElementType.SqlFunction:
            {
                foreach (var p in ((SqlFunction)element).Parameters)
                {
                    Visit(p, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.SqlExpression:
            {
                foreach (var v in ((SqlExpression)element).Parameters)
                {
                    Visit(v, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.SqlBinaryExpression:
            {
                var bexpr = (SqlBinaryExpression)element;
                Visit(bexpr.Expr1, all, parentFirst, action);
                Visit(bexpr.Expr2, all, parentFirst, action);
                break;
            }

            case QueryElementType.SqlTable:
            {
                var table = (SqlTable)element;

                Visit(table.All, all, parentFirst, action);
                foreach (var field in table.Fields.Values)
                {
                    Visit(field, all, parentFirst, action);
                }
                foreach (var join  in table.Joins)
                {
                    Visit(join, all, parentFirst, action);
                }

                if (table.TableArguments != null)
                {
                    foreach (var a in table.TableArguments)
                    {
                        Visit(a, all, parentFirst, action);
                    }
                }

                break;
            }

            case QueryElementType.Join:
            {
                foreach (var j in ((Join)element).JoinOns)
                {
                    Visit(j, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.Column:
            {
                Visit(((SqlQuery.Column)element).Expression, all, parentFirst, action);
                break;
            }

            case QueryElementType.TableSource:
            {
                var table = (SqlQuery.TableSource)element;

                Visit(table.Source, all, parentFirst, action);
                foreach (var j in table.Joins)
                {
                    Visit(j, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.JoinedTable:
            {
                var join = (SqlQuery.JoinedTable)element;
                Visit(join.Table, all, parentFirst, action);
                Visit(join.Condition, all, parentFirst, action);
                break;
            }

            case QueryElementType.SearchCondition:
            {
                foreach (var c in ((SqlQuery.SearchCondition)element).Conditions)
                {
                    Visit(c, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.Condition:
            {
                Visit(((SqlQuery.Condition)element).Predicate, all, parentFirst, action);
                break;
            }

            case QueryElementType.ExprPredicate:
            {
                Visit(((SqlQuery.Predicate.Expr)element).Expr1, all, parentFirst, action);
                break;
            }

            case QueryElementType.NotExprPredicate:
            {
                Visit(((SqlQuery.Predicate.NotExpr)element).Expr1, all, parentFirst, action);
                break;
            }

            case QueryElementType.ExprExprPredicate:
            {
                var p = (SqlQuery.Predicate.ExprExpr)element;
                Visit(p.Expr1, all, parentFirst, action);
                Visit(p.Expr2, all, parentFirst, action);
                break;
            }

            case QueryElementType.LikePredicate:
            {
                var p = (SqlQuery.Predicate.Like)element;
                Visit(p.Expr1, all, parentFirst, action);
                Visit(p.Expr2, all, parentFirst, action);
                Visit(p.Escape, all, parentFirst, action);
                break;
            }

            case QueryElementType.BetweenPredicate:
            {
                var p = (SqlQuery.Predicate.Between)element;
                Visit(p.Expr1, all, parentFirst, action);
                Visit(p.Expr2, all, parentFirst, action);
                Visit(p.Expr3, all, parentFirst, action);
                break;
            }

            case QueryElementType.IsNullPredicate:
            {
                Visit(((SqlQuery.Predicate.IsNull)element).Expr1, all, parentFirst, action);
                break;
            }

            case QueryElementType.InSubQueryPredicate:
            {
                var p = (SqlQuery.Predicate.InSubQuery)element;
                Visit(p.Expr1, all, parentFirst, action);
                Visit(p.SubQuery, all, parentFirst, action);
                break;
            }

            case QueryElementType.InListPredicate:
            {
                var p = (SqlQuery.Predicate.InList)element;
                Visit(p.Expr1, all, parentFirst, action);
                foreach (var value in p.Values)
                {
                    Visit(value, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.FuncLikePredicate:
            {
                var p = (SqlQuery.Predicate.FuncLike)element;
                Visit(p.Function, all, parentFirst, action);
                break;
            }

            case QueryElementType.SetExpression:
            {
                var s = (SqlQuery.SetExpression)element;
                Visit(s.Column, all, parentFirst, action);
                Visit(s.Expression, all, parentFirst, action);
                break;
            }

            case QueryElementType.SetClause:
            {
                var sc = (SqlQuery.SetClause)element;

                if (sc.Into != null)
                {
                    Visit(sc.Into, all, parentFirst, action);
                }

                foreach (var c in sc.Items.ToArray())
                {
                    Visit(c, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.SelectClause:
            {
                var sc = (SqlQuery.SelectClause)element;
                Visit(sc.TakeValue, all, parentFirst, action);
                Visit(sc.SkipValue, all, parentFirst, action);

                foreach (var c in sc.Columns.ToArray())
                {
                    Visit(c, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.FromClause:
            {
                foreach (var t in ((SqlQuery.FromClause)element).Tables)
                {
                    Visit(t, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.WhereClause:
            {
                Visit(((SqlQuery.WhereClause)element).SearchCondition, all, parentFirst, action);
                break;
            }

            case QueryElementType.GroupByClause:
            {
                foreach (var i in ((SqlQuery.GroupByClause)element).Items)
                {
                    Visit(i, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.OrderByClause:
            {
                foreach (var i in ((SqlQuery.OrderByClause)element).Items)
                {
                    Visit(i, all, parentFirst, action);
                }
                break;
            }

            case QueryElementType.OrderByItem:
            {
                Visit(((SqlQuery.OrderByItem)element).Expression, all, parentFirst, action);
                break;
            }

            case QueryElementType.Union:
                Visit(((SqlQuery.Union)element).SqlQuery, all, parentFirst, action);
                break;

            case QueryElementType.SqlQuery:
            {
                if (all)
                {
                    if (_visitedElements.ContainsKey(element))
                    {
                        return;
                    }
                    _visitedElements.Add(element, element);
                }

                var q = (SqlQuery)element;

                switch (q.QueryType)
                {
                case QueryType.Update:
                    Visit(q.Set, all, parentFirst, action);
                    break;

                case QueryType.Insert:
                    Visit(q.Set, all, parentFirst, action);

                    if (q.From.Tables.Count == 0)
                    {
                        break;
                    }

                    goto default;

                default:
                    Visit(q.Select, all, parentFirst, action);
                    break;
                }

                Visit(q.From, all, parentFirst, action);
                Visit(q.Where, all, parentFirst, action);
                Visit(q.GroupBy, all, parentFirst, action);
                Visit(q.Having, all, parentFirst, action);
                Visit(q.OrderBy, all, parentFirst, action);

                if (q.HasUnion)
                {
                    foreach (var i in q.Unions)
                    {
                        if (i.SqlQuery == q)
                        {
                            throw new InvalidOperationException();
                        }

                        Visit(i, all, parentFirst, action);
                    }
                }

                break;
            }
            }

            if (!parentFirst)
            {
                action(element);

                if (!all)
                {
                    _visitedElements.Add(element, element);
                }
            }
        }
Exemple #4
0
 public void Visit(IQueryElement element, bool parentFirst, VisitFunc action)
 {
     _visitedElements.Clear();
     Visit(element, false, parentFirst, action);
 }
Exemple #5
0
 public TestParser()
 {
     OnFunc += new VisitFunc(_Traveler_OnFunc);
 }
Exemple #6
0
        void Visit(IQueryElement element, bool all, VisitFunc action)
        {
            if (element == null || !all && _visitedElements.ContainsKey(element))
            {
                return;
            }

            switch (element.ElementType)
            {
            case QueryElementType.SqlFunction:
            {
                foreach (ISqlExpression p in ((SqlFunction)element).Parameters)
                {
                    Visit(p, all, action);
                }
                break;
            }

            case QueryElementType.SqlExpression:
            {
                foreach (ISqlExpression v in ((SqlExpression)element).Parameters)
                {
                    Visit(v, all, action);
                }
                break;
            }

            case QueryElementType.SqlBinaryExpression:
            {
                SqlBinaryExpression bexpr = (SqlBinaryExpression)element;
                Visit(bexpr.Expr1, all, action);
                Visit(bexpr.Expr2, all, action);
                break;
            }

            case QueryElementType.SqlTable:
            {
                SqlTable table = (SqlTable)element;

                Visit(table.All, all, action);
                foreach (SqlField field in table.Fields.Values)
                {
                    Visit(field, all, action);
                }
                foreach (Join join  in table.Joins)
                {
                    Visit(join, all, action);
                }
                break;
            }

            case QueryElementType.Join:
            {
                foreach (JoinOn j in ((Join)element).JoinOns)
                {
                    Visit(j, all, action);
                }
                break;
            }

            case QueryElementType.Column:
            {
                Visit(((SqlQuery.Column)element).Expression, all, action);
                break;
            }

            case QueryElementType.TableSource:
            {
                SqlQuery.TableSource table = (SqlQuery.TableSource)element;

                Visit(table.Source, all, action);
                foreach (SqlQuery.JoinedTable j in table.Joins)
                {
                    Visit(j, all, action);
                }
                break;
            }

            case QueryElementType.JoinedTable:
            {
                SqlQuery.JoinedTable join = (SqlQuery.JoinedTable)element;
                Visit(join.Table, all, action);
                Visit(join.Condition, all, action);
                break;
            }

            case QueryElementType.SearchCondition:
            {
                foreach (SqlQuery.Condition c in ((SqlQuery.SearchCondition)element).Conditions)
                {
                    Visit(c, all, action);
                }
                break;
            }

            case QueryElementType.Condition:
            {
                Visit(((SqlQuery.Condition)element).Predicate, all, action);
                break;
            }

            case QueryElementType.ExprPredicate:
            {
                Visit(((SqlQuery.Predicate.Expr)element).Expr1, all, action);
                break;
            }

            case QueryElementType.NotExprPredicate:
            {
                Visit(((SqlQuery.Predicate.NotExpr)element).Expr1, all, action);
                break;
            }

            case QueryElementType.ExprExprPredicate:
            {
                SqlQuery.Predicate.ExprExpr p = (SqlQuery.Predicate.ExprExpr)element;
                Visit(p.Expr1, all, action);
                Visit(p.Expr2, all, action);
                break;
            }

            case QueryElementType.LikePredicate:
            {
                SqlQuery.Predicate.Like p = (SqlQuery.Predicate.Like)element;
                Visit(p.Expr1, all, action);
                Visit(p.Expr2, all, action);
                Visit(p.Escape, all, action);
                break;
            }

            case QueryElementType.BetweenPredicate:
            {
                SqlQuery.Predicate.Between p = (SqlQuery.Predicate.Between)element;
                Visit(p.Expr1, all, action);
                Visit(p.Expr2, all, action);
                Visit(p.Expr3, all, action);
                break;
            }

            case QueryElementType.IsNullPredicate:
            {
                Visit(((SqlQuery.Predicate.IsNull)element).Expr1, all, action);
                break;
            }

            case QueryElementType.InSubqueryPredicate:
            {
                SqlQuery.Predicate.InSubQuery p = (SqlQuery.Predicate.InSubQuery)element;
                Visit(p.Expr1, all, action);
                Visit(p.SubQuery, all, action);
                break;
            }

            case QueryElementType.InListPredicate:
            {
                SqlQuery.Predicate.InList p = (SqlQuery.Predicate.InList)element;
                Visit(p.Expr1, all, action);
                foreach (ISqlExpression value in p.Values)
                {
                    Visit(value, all, action);
                }
                break;
            }

            case QueryElementType.FuncLikePredicate:
            {
                SqlQuery.Predicate.FuncLike p = (SqlQuery.Predicate.FuncLike)element;
                Visit(p.Function, all, action);
                break;
            }

            case QueryElementType.SetExpression:
            {
                SqlQuery.SetExpression s = (SqlQuery.SetExpression)element;
                Visit(s.Column, all, action);
                Visit(s.Expression, all, action);
                break;
            }

            case QueryElementType.SetClause:
            {
                SqlQuery.SetClause sc = (SqlQuery.SetClause)element;

                if (sc.Into != null)
                {
                    Visit(sc.Into, all, action);
                }

                foreach (SqlQuery.SetExpression c in sc.Items.ToArray())
                {
                    Visit(c, all, action);
                }
                break;
            }

            case QueryElementType.SelectClause:
            {
                SqlQuery.SelectClause sc = (SqlQuery.SelectClause)element;
                Visit(sc.TakeValue, all, action);
                Visit(sc.SkipValue, all, action);

                foreach (SqlQuery.Column c in sc.Columns.ToArray())
                {
                    Visit(c, all, action);
                }
                break;
            }

            case QueryElementType.FromClause:
            {
                foreach (SqlQuery.TableSource t in ((SqlQuery.FromClause)element).Tables)
                {
                    Visit(t, all, action);
                }
                break;
            }

            case QueryElementType.WhereClause:
            {
                Visit(((SqlQuery.WhereClause)element).SearchCondition, all, action);
                break;
            }

            case QueryElementType.GroupByClause:
            {
                foreach (ISqlExpression i in ((SqlQuery.GroupByClause)element).Items)
                {
                    Visit(i, all, action);
                }
                break;
            }

            case QueryElementType.OrderByClause:
            {
                foreach (SqlQuery.OrderByItem i in ((SqlQuery.OrderByClause)element).Items)
                {
                    Visit(i, all, action);
                }
                break;
            }

            case QueryElementType.OrderByItem:
            {
                Visit(((SqlQuery.OrderByItem)element).Expression, all, action);
                break;
            }

            case QueryElementType.Union:
                Visit(((SqlQuery.Union)element).SqlQuery, all, action);
                break;

            case QueryElementType.SqlQuery:
            {
                if (all)
                {
                    if (_visitedElements.ContainsKey(element))
                    {
                        return;
                    }
                    _visitedElements.Add(element, element);
                }

                SqlQuery q = (SqlQuery)element;

                if (q.QueryType == QueryType.Update || q.QueryType == QueryType.Insert)
                {
                    Visit(q.Set, all, action);
                }
                else
                {
                    Visit(q.Select, all, action);
                }

                Visit(q.From, all, action);
                Visit(q.Where, all, action);
                Visit(q.GroupBy, all, action);
                Visit(q.Having, all, action);
                Visit(q.OrderBy, all, action);

                if (q.HasUnion)
                {
                    foreach (SqlQuery.Union i in q.Unions)
                    {
                        if (i.SqlQuery == q)
                        {
                            throw new InvalidOperationException();
                        }

                        Visit(i, all, action);
                    }
                }

                break;
            }
            }

            action(element);

            if (!all)
            {
                _visitedElements.Add(element, element);
            }
        }
Exemple #7
0
 public void VisitAll(IQueryElement element, VisitFunc action)
 {
     _visitedElements.Clear();
     Visit(element, true, action);
 }
Exemple #8
0
 public BinaryTree(VisitFunc visit)
 {
     Visit = visit;
 }