Пример #1
0
        protected virtual ReadOnlyCollection <DbColumnDeclaration> VisitColumnDeclarations(
            ReadOnlyCollection <DbColumnDeclaration> columns)
        {
            List <DbColumnDeclaration> alternate = null;

            for (int i = 0, n = columns.Count; i < n; i++)
            {
                DbColumnDeclaration column = columns[i];
                Expression          e      = Visit(column.Expression);
                if (alternate == null && e != column.Expression)
                {
                    alternate = columns.Take(i).ToList();
                }

                if (alternate != null)
                {
                    alternate.Add(new DbColumnDeclaration(column.Name, e));
                }
            }
            if (alternate != null)
            {
                return(alternate.AsReadOnly());
            }

            return(columns);
        }
        public static DbSelectExpression AddColumn(this DbSelectExpression select, DbColumnDeclaration column)
        {
            List <DbColumnDeclaration> columns = new List <DbColumnDeclaration>(select.Columns);

            columns.Add(column);
            return(select.SetColumns(columns));
        }
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            select = (DbSelectExpression)base.VisitSelect(select);

            // look for redundant column declarations
            List <DbColumnDeclaration> cols = select.Columns.OrderBy(c => c.Name).ToList();
            BitArray removed    = new BitArray(select.Columns.Count);
            bool     anyRemoved = false;

            for (int i = 0, n = cols.Count; i < n - 1; i++)
            {
                DbColumnDeclaration ci  = cols[i];
                DbColumnExpression  cix = ci.Expression as DbColumnExpression;
                DbDataType          qt  = cix != null ? cix.DbType : null;
                DbColumnExpression  cxi = new DbColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name);
                for (int j = i + 1; j < n; j++)
                {
                    if (!removed.Get(j))
                    {
                        DbColumnDeclaration cj = cols[j];
                        if (SameExpression(ci.Expression, cj.Expression))
                        {
                            // any reference to 'j' should now just be a reference to 'i'
                            DbColumnExpression cxj = new DbColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name);
                            this.map.Add(cxj, cxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }
            if (anyRemoved)
            {
                List <DbColumnDeclaration> newDecls = new List <DbColumnDeclaration>();
                for (int i = 0, n = cols.Count; i < n; i++)
                {
                    if (!removed.Get(i))
                    {
                        newDecls.Add(cols[i]);
                    }
                }
                select = select.SetColumns(newDecls);
            }
            return(select);
        }
Пример #4
0
 protected override Expression VisitSelect(DbSelectExpression select)
 {
     select = (DbSelectExpression)base.VisitSelect(select);
     if (lookup.Contains(select.Alias))
     {
         List <DbColumnDeclaration> aggColumns = new List <DbColumnDeclaration>(select.Columns);
         foreach (DbAggregateSubqueryExpression ae in lookup[select.Alias])
         {
             string name            = "agg" + aggColumns.Count;
             DbColumnDeclaration cd = new DbColumnDeclaration(name, ae.AggregateInGroupSelect);
             this.map.Add(ae, new DbColumnExpression(ae.Type, null, ae.GroupByAlias, name));
             aggColumns.Add(cd);
         }
         return(new DbSelectExpression(select.Alias, aggColumns, select.From, select.Where, select.OrderBy,
                                       select.GroupBy, select.IsDistinct, select.Skip, select.Take));
     }
     return(select);
 }
 protected override Expression VisitSelect(DbSelectExpression select)
 {
     select = (DbSelectExpression)base.VisitSelect(select);
     if (lookup.Contains(select.Alias))
     {
         List<DbColumnDeclaration> aggColumns = new List<DbColumnDeclaration>(select.Columns);
         foreach (DbAggregateSubqueryExpression ae in lookup[select.Alias])
         {
             string name = "agg" + aggColumns.Count;
             DbColumnDeclaration cd = new DbColumnDeclaration(name, ae.AggregateInGroupSelect);
             this.map.Add(ae, new DbColumnExpression(ae.Type, null, ae.GroupByAlias, name));
             aggColumns.Add(cd);
         }
         return new DbSelectExpression(select.Alias, aggColumns, select.From, select.Where, select.OrderBy, 
             select.GroupBy, select.IsDistinct, select.Skip, select.Take);
     }
     return select;
 }
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new DbTableAlias();
            var aliases = DeclaredAliasGatherer.Gather(expression);

            var decls = new List<DbColumnDeclaration>();
            foreach (var ta in aliases) 
            {
                foreach (var col in this.columns[ta])
                {
                    string name = decls.GetAvailableColumnName(col.Name);
                    var decl = new DbColumnDeclaration(name, col);
                    decls.Add(decl);
                    var newCol = new DbColumnExpression(col.Type, col.DbType, newAlias, col.Name);
                    this.map.Add(col, newCol);
                }
            }

            return new DbSelectExpression(newAlias, decls, expression, null);
        }
Пример #7
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new DbTableAlias();
            var aliases  = DeclaredAliasGatherer.Gather(expression);

            var decls = new List <DbColumnDeclaration>();

            foreach (var ta in aliases)
            {
                foreach (var col in this.columns[ta])
                {
                    string name = decls.GetAvailableColumnName(col.Name);
                    var    decl = new DbColumnDeclaration(name, col);
                    decls.Add(decl);
                    var newCol = new DbColumnExpression(col.Type, col.DbType, newAlias, col.Name);
                    this.map.Add(col, newCol);
                }
            }

            return(new DbSelectExpression(newAlias, decls, expression, null));
        }
Пример #8
0
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            // visit column projection first
            ReadOnlyCollection <DbColumnDeclaration> columns = select.Columns;

            List <DbColumnDeclaration> alternate = null;

            for (int i = 0, n = select.Columns.Count; i < n; i++)
            {
                DbColumnDeclaration decl = select.Columns[i];
                if (select.IsDistinct || IsColumnUsed(select.Alias, decl.Name))
                {
                    Expression expr = this.Visit(decl.Expression);
                    if (expr != decl.Expression)
                    {
                        decl = new DbColumnDeclaration(decl.Name, expr);
                    }
                }
                else
                {
                    decl = null;  // null means it gets omitted
                }
                if (decl != select.Columns[i] && alternate == null)
                {
                    alternate = new List <DbColumnDeclaration>();
                    for (int j = 0; j < i; j++)
                    {
                        alternate.Add(select.Columns[j]);
                    }
                }
                if (decl != null && alternate != null)
                {
                    alternate.Add(decl);
                }
            }
            if (alternate != null)
            {
                columns = alternate.AsReadOnly();
            }

            Expression take = this.Visit(select.Take);
            Expression skip = this.Visit(select.Skip);
            ReadOnlyCollection <Expression>        groupbys = this.VisitExpressionList(select.GroupBy);
            ReadOnlyCollection <DbOrderExpression> orderbys = this.VisitOrderBy(select.OrderBy);

            Expression where = this.Visit(select.Where);
            Expression from = this.Visit(select.From);

            ClearColumnsUsed(select.Alias);

            if (columns != select.Columns ||
                take != select.Take ||
                skip != select.Skip ||
                orderbys != select.OrderBy ||
                groupbys != select.GroupBy ||
                where != select.Where ||
                from != select.From)
            {
                select = new DbSelectExpression(select.Alias, columns, from, where,
                                                orderbys, groupbys, select.IsDistinct, skip, take);
            }

            return(select);
        }
 protected virtual bool CompareColumnDeclaration(
     DbColumnDeclaration a, DbColumnDeclaration b)
 {
     return(a.Name == b.Name && Compare(a.Expression, b.Expression));
 }
 public static DbSelectExpression RemoveColumn(this DbSelectExpression select, DbColumnDeclaration column)
 {
     List<DbColumnDeclaration> columns = new List<DbColumnDeclaration>(select.Columns);
     columns.Remove(column);
     return select.SetColumns(columns);
 }
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            // visit column projection first
            ReadOnlyCollection<DbColumnDeclaration> columns = select.Columns;

            List<DbColumnDeclaration> alternate = null;
            for (int i = 0, n = select.Columns.Count; i < n; i++)
            {
                DbColumnDeclaration decl = select.Columns[i];
                if (select.IsDistinct || IsColumnUsed(select.Alias, decl.Name))
                {
                    Expression expr = this.Visit(decl.Expression);
                    if (expr != decl.Expression)
                    {
                        decl = new DbColumnDeclaration(decl.Name, expr);
                    }
                }
                else
                {
                    decl = null;  // null means it gets omitted
                }
                if (decl != select.Columns[i] && alternate == null)
                {
                    alternate = new List<DbColumnDeclaration>();
                    for (int j = 0; j < i; j++)
                    {
                        alternate.Add(select.Columns[j]);
                    }
                }
                if (decl != null && alternate != null)
                {
                    alternate.Add(decl);
                }
            }
            if (alternate != null)
            {
                columns = alternate.AsReadOnly();
            }

            Expression take = this.Visit(select.Take);
            Expression skip = this.Visit(select.Skip);
            ReadOnlyCollection<Expression> groupbys = this.VisitExpressionList(select.GroupBy);
            ReadOnlyCollection<DbOrderExpression> orderbys = this.VisitOrderBy(select.OrderBy);
            Expression where = this.Visit(select.Where);
            Expression from = this.Visit(select.From);

            ClearColumnsUsed(select.Alias);

            if (columns != select.Columns 
                || take != select.Take 
                || skip != select.Skip
                || orderbys != select.OrderBy 
                || groupbys != select.GroupBy
                || where != select.Where 
                || from != select.From)
            {
                select = new DbSelectExpression(select.Alias, columns, from, where, 
                    orderbys, groupbys, select.IsDistinct, skip, take);
            }

            return select;
        }
 protected virtual bool CompareColumnDeclaration(
     DbColumnDeclaration a, DbColumnDeclaration b)
 {
     return (a.Name == b.Name && Compare(a.Expression, b.Expression));
 }