Inheritance: Shaolinq.Persistence.Linq.Expressions.SqlExpressionVisitor
Exemplo n.º 1
0
        internal static List <SqlSelectExpression> Find(Expression source)
        {
            var gatherer = new SqlRedundantSubqueryFinder();

            gatherer.Visit(source);

            return(gatherer.redundant);
        }
Exemplo n.º 2
0
		internal static List<SqlSelectExpression> Find(Expression source)
		{
			var gatherer = new SqlRedundantSubqueryFinder();

			gatherer.Visit(source);

			return gatherer.redundant;
		}
Exemplo n.º 3
0
        protected override Expression VisitSelect(SqlSelectExpression select)
        {
            select = (SqlSelectExpression)base.VisitSelect(select);

            // Expand all purely redundant subqueries

            var redundantQueries = SqlRedundantSubqueryFinder.Find(select.From);

            if (redundantQueries != null)
            {
                select = SubqueryRemover.Remove(select, redundantQueries);
            }

            return(select);
        }
Exemplo n.º 4
0
        protected override Expression VisitProjection(SqlProjectionExpression projection)
        {
            projection = (SqlProjectionExpression)base.VisitProjection(projection);

            if (projection.Select.From is SqlSelectExpression)
            {
                var redundantQueries = SqlRedundantSubqueryFinder.Find(projection.Select);

                if (redundantQueries != null)
                {
                    projection = SubqueryRemover.Remove(projection, redundantQueries);
                }
            }

            return(projection);
        }
Exemplo n.º 5
0
            private static bool CanMergeWithFrom(SqlSelectExpression select)
            {
                var fromSelect = select.From.GetLeftMostSelect();

                if (fromSelect == null)
                {
                    return(false);
                }

                if (fromSelect.Columns.Any(c => c.NoOptimise))
                {
                    return(false);
                }

                if (!IsColumnProjection(fromSelect))
                {
                    return(false);
                }

                var selHasNameMapProjection = SqlRedundantSubqueryFinder.IsNameMapProjection(select);
                var selHasSkip       = select.Skip != null;
                var selHasWhere      = select.Where != null;
                var selHasOrderBy    = select.OrderBy != null && select.OrderBy.Count > 0;
                var selHasGroupBy    = select.GroupBy != null && select.GroupBy.Count > 0;
                var selHasAggregates = SqlAggregateChecker.HasAggregates(select);
                var frmHasOrderBy    = fromSelect.OrderBy != null && fromSelect.OrderBy.Count > 0;
                var frmHasGroupBy    = fromSelect.GroupBy != null && fromSelect.GroupBy.Count > 0;

                // Both cannot have OrderBy
                if (selHasOrderBy && frmHasOrderBy)
                {
                    return(false);
                }

                // Both cannot have GroupBy
                if (selHasGroupBy && frmHasGroupBy)
                {
                    return(false);
                }

                // Cannot move forward OrderBy if outer has GroupBy
                if (frmHasOrderBy && (selHasGroupBy || selHasAggregates || select.Distinct))
                {
                    return(false);
                }

                // Cannot move forward GroupBy
                if (frmHasGroupBy)
                {
                    return(false);
                }

                // Cannot move forward a take if outer has take or skip or distinct
                if (fromSelect.Take != null && (select.Take != null || selHasSkip || select.Distinct || selHasAggregates || selHasGroupBy))
                {
                    return(false);
                }

                // Cannot move forward a skip if outer has skip or distinct or aggregates with accompanying groupby or where
                if (fromSelect.Skip != null && ((selHasWhere || selHasGroupBy) && (select.Skip != null || select.Distinct || selHasAggregates)))
                {
                    return(false);
                }

                // Cannot merge a distinct if the outer has a distinct or aggregates with accompanying groupby or where
                if (fromSelect.Distinct && (!selHasNameMapProjection || (((selHasWhere || selHasGroupBy) && (selHasAggregates || selHasOrderBy)))))
                {
                    return(false);
                }

                return(true);
            }