private BindResult RebindOrderings(IList<OrderByDeclaration> orderings, TableAlias alias, HashSet<TableAlias> existingAliases, IList<ColumnDeclaration> existingColumns) { List<ColumnDeclaration> newColumns = null; List<OrderByDeclaration> newOrderings = new List<OrderByDeclaration>(); foreach(OrderByDeclaration ordering in orderings) { SqlExpression expr = ordering.Expression; ColumnExpression column = expr as ColumnExpression; if(column == null || existingAliases != null && existingAliases.Contains(column.TableAlias)) { int ordinal = 0; foreach(ColumnDeclaration decl in existingColumns) { ColumnExpression declColumn = decl.Expression as ColumnExpression; if(decl.Expression == ordering.Expression || column != null && declColumn != null && column.TableAlias == declColumn.TableAlias && column.ColumnName == declColumn.ColumnName) { expr = new ColumnExpression(alias, decl.DeclaredName); break; } ordinal++; } if(expr == ordering.Expression) { if(newColumns == null) { newColumns = new List<ColumnDeclaration>(existingColumns); existingColumns = newColumns; } string colName = column != null ? column.ColumnName : "c" + ordinal; colName = GetAvailableColumnName(newColumns, colName); newColumns.Add(new ColumnDeclaration(colName, ordering.Expression)); expr = new ColumnExpression(alias, colName); } newOrderings.Add(new OrderByDeclaration(ordering.OrderType, expr)); } } return new BindResult() { Columns = existingColumns, Orderings = newOrderings }; }
protected override Expression VisitInjected(InjectedExpression node) { SqlExpression sqlExpr = node.TranslateResult.SqlExpression; if (sqlExpr.NodeType == SqlExpressionType.Column) { ColumnExpression column = (ColumnExpression)sqlExpr; ColumnExpression mapped; if (this.map.TryGetValue(column, out mapped)) { return new InjectedExpression(new TranslateResult(mapped), node.Type); } foreach (ColumnDeclaration existingColumn in this.columns) { ColumnExpression c = existingColumn.Expression as ColumnExpression; if (c != null && c.TableAlias == column.TableAlias && c.ColumnName == column.ColumnName) { return new InjectedExpression(new TranslateResult(new ColumnExpression(this.newAlias, existingColumn.DeclaredName)), node.Type); } } if (this.existingAliases.Contains(column.TableAlias)) { string declaredName = this.GetUniqueColumnName(column.ColumnName); this.columns.Add(new ColumnDeclaration(declaredName, column)); mapped = new ColumnExpression(this.newAlias, declaredName); this.map.Add(column, mapped); return new InjectedExpression(new TranslateResult(mapped), node.Type); } return node; } else { string declaredName = this.GetNextColumnName(); this.columns.Add(new ColumnDeclaration(declaredName, sqlExpr)); return new InjectedExpression(new TranslateResult(new ColumnExpression(this.newAlias, declaredName)), node.Type); } }
protected override SqlExpression VisitColumn(ColumnExpression expr) { this.Write(GetAliasName(expr.TableAlias) + "." + expr.ColumnName + " "); return expr; }
public ColumnAssignment(ColumnExpression column, SqlExpression expression) { this.Column = column; this.Expression = expression; }