예제 #1
0
        public static HashSet <TableAlias> Gather(Expression expression)
        {
            var gatherer = new ReferencedAliasGatherer();

            gatherer.Visit(expression);
            return(gatherer.aliases);
        }
예제 #2
0
        private bool CanBeJoinCondition(Expression expression, HashSet <TableAlias> left, HashSet <TableAlias> right, HashSet <TableAlias> all)
        {
            // an expression is good if it has at least one reference to an alias from both left & right sets and does
            // not have any additional references that are not in both left & right sets
            var referenced = ReferencedAliasGatherer.Gather(expression);
            var leftOkay   = referenced.Intersect(left).Any();
            var rightOkay  = referenced.Intersect(right).Any();
            var subset     = referenced.IsSubsetOf(all);

            return(leftOkay && rightOkay && subset);
        }
예제 #3
0
        /// <summary>
        /// 访问 <see cref="JoinExpression"/>。
        /// </summary>
        /// <param name="join">要访问的表达式。</param>
        /// <returns></returns>
        protected override Expression VisitJoin(JoinExpression join)
        {
            join = (JoinExpression)base.VisitJoin(join);

            if (join.JoinType == JoinType.CrossApply || join.JoinType == JoinType.OuterApply)
            {
                if (join.Right is TableExpression)
                {
                    return(new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null));
                }
                else
                {
                    var select = join.Right as SelectExpression;
                    // Only consider rewriting cross apply if
                    //   1) right side is a select
                    //   2) other than in the where clause in the right-side select, no left-side declared aliases are referenced
                    //   3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc).
                    // Note: it is best to attempt this after redundant subqueries have been removed.
                    if (select != null &&
                        select.Take == null &&
                        select.Skip == null &&
                        !AggregateChecker.HasAggregates(select) &&
                        (select.GroupBy == null || select.GroupBy.Count == 0))
                    {
                        var selectWithoutWhere = select.SetWhere(null);
                        var referencedAliases  = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        var declaredAliases    = DeclaredAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            var where = select.Where;
                            select    = selectWithoutWhere;
                            var pc = ColumnProjector.ProjectColumns(this.CanBeColumn, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From));
                            select = select.SetColumns(pc.Columns);
                            where  = pc.Projector;
                            var jt = (where == null) ? JoinType.CrossJoin : (join.JoinType == JoinType.CrossApply ? JoinType.InnerJoin : JoinType.LeftOuter);
                            return(new JoinExpression(jt, join.Left, select, where));
                        }
                    }
                }
            }

            return(join);
        }