public static SelectExpression RemoveColumn(this SelectExpression select, ColumnDeclaration column) { List <ColumnDeclaration> columns = new List <ColumnDeclaration>(select.Columns); columns.Remove(column); return(select.SetColumns(columns)); }
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; SqlType qt = cix != null ? cix.SqlType : ci.SqlType; 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 = SqlType.Get(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); }
private Expression MakeSubquery(Expression expression) { var newAlias = new TableAlias(); var aliases = TableAliasGatherer.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.SqlType); decls.Add(decl); var newCol = new ColumnExpression(col.Type, col.SqlType, newAlias, 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.SqlType)); } } if (alternate != null) { return(alternate.AsReadOnly()); } return(columns); }
protected virtual bool CompareColumnDeclaration(ColumnDeclaration a, ColumnDeclaration b) { return(a.Name == b.Name && this.Compare(a.Expression, b.Expression)); }
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.SqlType); } } 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); }