protected override Expression VisitSelect(SelectExpression select) { // visit column projection first ReadOnlyCollection <ColumnDeclaration> columns = select.Columns; List <ColumnDeclaration> alternate = null; for (int i = 0, n = select.Columns.Count; i < n; i++) { ColumnDeclaration decl = select.Columns[i]; if (IsColumnUsed(select.Alias, decl.Name)) { Expression expr = this.Visit(decl.Expression); if (expr != decl.Expression) { decl = new ColumnDeclaration(decl.Name, expr); } } 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(); } 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 || orderbys != select.OrderBy || groupbys != select.GroupBy || where != select.Where || from != select.From) { select = new SelectExpression(select.Type, select.Alias, columns, from, where, orderbys, groupbys); } 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; ColumnDeclaration cd = new ColumnDeclaration(name, ae.AggregateInGroupSelect); this.map.Add(ae, new ColumnExpression(ae.Type, ae.GroupByAlias, name)); aggColumns.Add(cd); } return(new SelectExpression(select.Type, select.Alias, aggColumns, select.From, select.Where, select.OrderBy, select.GroupBy)); } return(select); }
protected 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)); } } if (alternate != null) { return(alternate.AsReadOnly()); } return(columns); }
protected override Expression VisitSelect(SelectExpression select) { sb.Append("SELECT "); for (int i = 0, n = select.Columns.Count; i < n; i++) { ColumnDeclaration column = select.Columns[i]; if (i > 0) { sb.Append(", "); } ColumnExpression c = this.Visit(column.Expression) as ColumnExpression; if (!string.IsNullOrEmpty(column.Name) && (c == null || c.Name != column.Name)) { sb.Append(" AS "); sb.Append(column.Name); } } if (select.From != null) { this.AppendNewLine(Indentation.Same); sb.Append("FROM "); this.VisitSource(select.From); } if (select.Where != null) { this.AppendNewLine(Indentation.Same); sb.Append("WHERE "); this.Visit(select.Where); } if (select.OrderBy != null && select.OrderBy.Count > 0) { this.AppendNewLine(Indentation.Same); sb.Append("ORDER BY "); for (int i = 0, n = select.OrderBy.Count; i < n; i++) { OrderExpression exp = select.OrderBy[i]; if (i > 0) { sb.Append(", "); } this.Visit(exp.Expression); if (exp.OrderType != OrderType.Ascending) { sb.Append(" DESC"); } } } if (select.GroupBy != null && select.GroupBy.Count > 0) { this.AppendNewLine(Indentation.Same); sb.Append("GROUP BY "); for (int i = 0, n = select.GroupBy.Count; i < n; i++) { if (i > 0) { sb.Append(", "); } this.Visit(select.GroupBy[i]); } } return(select); }