Exemple #1
0
        public static SelectExpression RemoveColumn(this SelectExpression select, ColumnDeclaration column)
        {
            var columns = new List <ColumnDeclaration>(select.Columns);

            columns.Remove(column);
            return(select.SetColumns(columns));
        }
Exemple #2
0
 protected virtual void WriteColumns(ReadOnlyCollection <ColumnDeclaration> columns)
 {
     if (columns.Count > 0)
     {
         for (int i = 0, n = columns.Count; i < n; i++)
         {
             ColumnDeclaration column = columns[i];
             if (i > 0)
             {
                 Write(", ");
             }
             var c = VisitValue(column.Expression) as ColumnExpression;
             if (!string.IsNullOrEmpty(column.Name) && (c == null || c.Name != column.Name))
             {
                 Write(" ");
                 WriteAsColumnName(column.Name);
             }
         }
     }
     else
     {
         Write("NULL ");
         if (isNested)
         {
             WriteAsColumnName("tmp");
             Write(" ");
         }
     }
 }
 private static bool IsColumnProjection(SelectExpression select)
 {
     for (int i = 0, n = select.Columns.Count; i < n; i++)
     {
         ColumnDeclaration cd = select.Columns[i];
         if (cd.Expression.NodeType != (ExpressionType)DbExpressionType.Column && cd.Expression.NodeType != ExpressionType.Constant)
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #4
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = (SelectExpression)base.VisitSelect(select);

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

            for (int i = 0, n = cols.Count; i < n - 1; i++)
            {
                ColumnDeclaration ci = cols[i];
                var       cix        = ci.Expression as ColumnExpression;
                QueryType qt         = cix != null ? cix.QueryType : ci.QueryType;
                var       cxi        = new ColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name);
                for (int j = i + 1; j < n; j++)
                {
                    if (!removed.Get(j))
                    {
                        ColumnDeclaration cj = cols[j];
                        if (SameExpression(ci.Expression, cj.Expression))
                        {
                            // any reference to 'j' should now just be a reference to 'i'
                            var cxj = new ColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name);
                            map.Add(cxj, cxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }
            if (anyRemoved)
            {
                var newDecls = new List <ColumnDeclaration>();
                for (int i = 0, n = cols.Count; i < n; i++)
                {
                    if (!removed.Get(i))
                    {
                        newDecls.Add(cols[i]);
                    }
                }
                select = select.SetColumns(newDecls);
            }
            return(select);
        }
Exemple #5
0
 protected override Expression VisitSelect(SelectExpression select)
 {
     select = (SelectExpression)base.VisitSelect(select);
     if (lookup.Contains(select.Alias))
     {
         var aggColumns = new List <ColumnDeclaration>(select.Columns);
         foreach (AggregateSubqueryExpression ae in lookup[select.Alias])
         {
             string    name    = "agg" + aggColumns.Count;
             QueryType colType = language.TypeSystem.GetColumnType(ae.Type);
             var       cd      = new ColumnDeclaration(name, ae.AggregateInGroupSelect, colType);
             map.Add(ae, new ColumnExpression(ae.Type, colType, ae.GroupByAlias, name));
             aggColumns.Add(cd);
         }
         return(new SelectExpression(select.Alias, aggColumns, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take, select.IsReverse));
     }
     return(select);
 }
Exemple #6
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new TableAlias();
            HashSet <TableAlias> aliases = DeclaredAliasGatherer.Gather(expression);

            var decls = new List <ColumnDeclaration>();

            foreach (TableAlias ta in aliases)
            {
                foreach (ColumnExpression col in columns[ta])
                {
                    string name = decls.GetAvailableColumnName(col.Name);
                    var    decl = new ColumnDeclaration(name, col, col.QueryType);
                    decls.Add(decl);
                    var newCol = new ColumnExpression(col.Type, col.QueryType, newAlias, col.Name);
                    map.Add(col, newCol);
                }
            }

            return(new SelectExpression(newAlias, decls, expression, null));
        }
        protected virtual ReadOnlyCollection <ColumnDeclaration> VisitColumnDeclarations(ReadOnlyCollection <ColumnDeclaration> columns)
        {
            List <ColumnDeclaration> alternate = null;

            for (int i = 0, n = columns.Count; i < n; i++)
            {
                ColumnDeclaration 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 ColumnDeclaration(column.Name, e, column.QueryType));
                }
            }
            if (alternate != null)
            {
                return(alternate.AsReadOnly());
            }
            return(columns);
        }
 protected virtual bool CompareColumnDeclaration(ColumnDeclaration a, ColumnDeclaration b)
 {
     return(a.Name == b.Name && Compare(a.Expression, b.Expression));
 }
Exemple #9
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            // visit column projection first
            ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;

            bool wasRetained = retainAllColumns;

            retainAllColumns = false;

            List <ColumnDeclaration> alternate = null;

            for (int i = 0, n = select.Columns.Count; i < n; i++)
            {
                ColumnDeclaration decl = select.Columns[i];
                if (wasRetained || select.IsDistinct || IsColumnUsed(select.Alias, decl.Name))
                {
                    Expression expr = Visit(decl.Expression);
                    if (expr != decl.Expression)
                    {
                        decl = new ColumnDeclaration(decl.Name, expr, decl.QueryType);
                    }
                }
                else
                {
                    decl = null; // null means it gets omitted
                }
                if (decl != select.Columns[i] && alternate == null)
                {
                    alternate = new List <ColumnDeclaration>();
                    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 = Visit(select.Take);
            Expression skip = Visit(select.Skip);
            ReadOnlyCollection <Expression>      groupbys = VisitExpressionList(select.GroupBy);
            ReadOnlyCollection <OrderExpression> orderbys = VisitOrderBy(select.OrderBy);

            Expression where = Visit(select.Where);

            Expression from = 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 SelectExpression(select.Alias, columns, from, where, orderbys, groupbys, select.IsDistinct, skip, take, select.IsReverse);
            }

            retainAllColumns = wasRetained;

            return(select);
        }