Beispiel #1
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)
             {
                 this.Write(", ");
             }
             ColumnExpression c = this.VisitValue(column.Expression) as ColumnExpression;
             if (!string.IsNullOrEmpty(column.Name) && (c == null || c.Name != column.Name))
             {
                 this.Write(" ");
                 this.WriteAsColumnName(column.Name);
             }
         }
     }
     else
     {
         this.Write("NULL ");
         if (this.isNested)
         {
             this.WriteAsColumnName("tmp");
             this.Write(" ");
         }
     }
 }
        public static SelectExpression RemoveColumn(this SelectExpression select, ColumnDeclaration column)
        {
            List <ColumnDeclaration> columns = new List <ColumnDeclaration>(select.Columns);

            columns.Remove(column);
            return(select.SetColumns(columns));
        }
Beispiel #3
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();
            BitArray removed    = new BitArray(select.Columns.Count);
            bool     anyRemoved = false;

            for (int i = 0, n = cols.Count; i < n - 1; i++)
            {
                ColumnDeclaration ci  = cols[i];
                ColumnExpression  cix = ci.Expression as ColumnExpression;
                QueryType         qt  = cix != null ? cix.QueryType : ci.QueryType;
                ColumnExpression  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'
                            ColumnExpression cxj = new ColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name);
                            this.map.Add(cxj, cxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }
            if (anyRemoved)
            {
                List <ColumnDeclaration> 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);
        }
 protected override Expression VisitSelect(SelectExpression select)
 {
     select = (SelectExpression)base.VisitSelect(select);
     if (lookup.Contains(select.Alias))
     {
         List <ColumnDeclaration> aggColumns = new List <ColumnDeclaration>(select.Columns);
         foreach (AggregateSubqueryExpression ae in lookup[select.Alias])
         {
             string            name    = "agg" + aggColumns.Count;
             var               colType = this.language.TypeSystem.GetColumnType(ae.Type);
             ColumnDeclaration cd      = new ColumnDeclaration(name, ae.AggregateInGroupSelect, colType);
             this.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);
 }
Beispiel #5
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new TableAlias();
            var aliases  = DeclaredAliasGatherer.Gather(expression);

            var decls = new List <ColumnDeclaration>();

            foreach (var ta in aliases)
            {
                foreach (var col in this.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);
                    this.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      = this.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);
        }
Beispiel #7
0
 private void GetColumns(MappingEntity entity, Dictionary <string, TableAlias> aliases, List <ColumnDeclaration> columns)
 {
     foreach (MemberInfo mi in this.mapping.GetMappedMembers(entity))
     {
         if (!this.mapping.IsAssociationRelationship(entity, mi))
         {
             if (this.mapping.IsNestedEntity(entity, mi))
             {
                 this.GetColumns(this.mapping.GetRelatedEntity(entity, mi), aliases, columns);
             }
             else if (this.mapping.IsColumn(entity, mi))
             {
                 string     name      = this.mapping.GetColumnName(entity, mi);
                 string     aliasName = this.mapping.GetAlias(entity, mi);
                 TableAlias alias;
                 aliases.TryGetValue(aliasName, out alias);
                 var colType          = this.GetColumnType(entity, mi);
                 ColumnExpression  ce = new ColumnExpression(TypeHelper.GetMemberType(mi), colType, alias, name);
                 ColumnDeclaration cd = new ColumnDeclaration(name, ce, colType);
                 columns.Add(cd);
             }
         }
     }
 }
Beispiel #8
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            // visit column projection first
            ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;

            var wasRetained = this.retainAllColumns;

            this.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 = this.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 = this.Visit(select.Take);
            Expression skip = this.Visit(select.Skip);
            ReadOnlyCollection <Expression>      groupbys = this.VisitExpressionList(select.GroupBy);
            ReadOnlyCollection <OrderExpression> 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 SelectExpression(select.Alias, columns, from, where, orderbys, groupbys, select.IsDistinct, skip, take, select.IsReverse);
            }

            this.retainAllColumns = wasRetained;

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