private void GetColumns(MappingEntity entity, Dictionary <string, TableAlias> aliases, List <DbColumnDeclaration> columns) { foreach (var mi in this.mapping.GetMappedMembers(entity)) { if (this.mapping.IsAssociationRelationship(entity, mi) == false) { if (this.mapping.IsNestedEntity(entity, mi)) { this.GetColumns(this.mapping.GetRelatedEntity(entity, mi), aliases, columns); } else if (this.mapping.IsColumn(entity, mi)) { var name = this.mapping.GetColumnName(entity, mi); var aliasName = this.mapping.GetAlias(entity, mi); aliases.TryGetValue(aliasName, out TableAlias alias); var colType = this.GetColumnType(entity, mi); var ce = new DbColumnExpression(TypeHelper.GetMemberType(mi), colType, alias, name); var cd = new DbColumnDeclaration(name, ce, colType); columns.Add(cd); } } } }
public static DbSelectExpression AddColumn(this DbSelectExpression select, DbColumnDeclaration column) { var columns = new List <DbColumnDeclaration>(select.Columns) { column }; return(select.SetColumns(columns)); }
protected override Expression VisitSelect(DbSelectExpression select) { select = base.VisitSelect(select) as DbSelectExpression; if (lookup.Contains(select.Alias)) { var aggColumns = new List <DbColumnDeclaration>(select.Columns); foreach (var ae in lookup[select.Alias]) { var name = "agg" + aggColumns.Count; var colType = this.language.TypeSystem.GetColumnType(ae.Type); var cd = new DbColumnDeclaration(name, ae.AggregateInGroupSelect, colType); this.map.Add(ae, new DbColumnExpression(ae.Type, colType, ae.GroupByAlias, name)); aggColumns.Add(cd); } return(new DbSelectExpression(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 = DbDeclaredAliasGatherer.Gather(expression); var decls = new List <DbColumnDeclaration>(); foreach (var ta in aliases) { foreach (var col in this.columns[ta]) { var name = decls.GetAvailableColumnName(col.Name); var decl = new DbColumnDeclaration(name, col, col.QueryType); decls.Add(decl); var newCol = new DbColumnExpression(col.Type, col.QueryType, newAlias, col.Name); this.map.Add(col, newCol); } } return(new DbSelectExpression(newAlias, decls, expression, null)); }
protected virtual bool CompareColumnDeclaration(DbColumnDeclaration a, DbColumnDeclaration b) { return(a.Name == b.Name && this.Compare(a.Expression, b.Expression)); }
public static DbSelectExpression RemoveColumn(this DbSelectExpression select, DbColumnDeclaration column) { var columns = new List <DbColumnDeclaration>(select.Columns); if (columns != null) { columns.Remove(column); } return(select.SetColumns(columns)); }
protected override Expression VisitSelect(DbSelectExpression select) { var columns = select.Columns; var wasRetained = this.retainAllColumns; this.retainAllColumns = false; var alternate = null as List <DbColumnDeclaration>; for (int i = 0, n = select.Columns.Count; i < n; i++) { var decl = select.Columns[i]; if (wasRetained || select.IsDistinct || IsColumnUsed(select.Alias, decl.Name)) { var expr = this.Visit(decl.Expression); if (expr != decl.Expression) { decl = new DbColumnDeclaration(decl.Name, expr, decl.QueryType); } } else { decl = null; } if (decl != select.Columns[i] && alternate == null) { alternate = new List <DbColumnDeclaration>(); for (var j = 0; j < i; j++) { alternate.Add(select.Columns[j]); } } if (decl != null && alternate != null) { alternate.Add(decl); } } if (alternate != null) { columns = alternate.AsReadOnly(); } var take = this.Visit(select.Take); var skip = this.Visit(select.Skip); var groupbys = this.VisitExpressionList(select.GroupBy); var orderbys = this.VisitOrderBy(select.OrderBy); var where = this.Visit(select.Where); var 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 DbSelectExpression(select.Alias, columns, from, where, orderbys, groupbys, select.IsDistinct, skip, take, select.IsReverse); } this.retainAllColumns = wasRetained; return(select); }