internal static Expression Parameterize(DbTableAlias outerAlias, Expression expr) { OuterParameterizer op = new OuterParameterizer(); op.outerAlias = outerAlias; return(op.Visit(expr)); }
public static ProjectedColumns ProjectColumns(Func<Expression, bool> fnCanBeColumn, Expression expression, IEnumerable<DbColumnDeclaration> existingColumns, DbTableAlias newAlias, IEnumerable<DbTableAlias> existingAliases) { ColumnProjector projector = new ColumnProjector(fnCanBeColumn, expression, existingColumns, newAlias, existingAliases); Expression expr = projector.Visit(expression); return new ProjectedColumns(expr, projector.columns.AsReadOnly()); }
public DbTableExpression(DbTableAlias alias, MetaType type, string name) : base(DbExpressionType.Table, typeof(void), alias) { _type = type; _name = name; }
protected override Expression VisitTable(DbTableExpression table) { DbTableAlias newAlias = new DbTableAlias(); this.map[table.Alias] = newAlias; return(new DbTableExpression(newAlias, table.Type, table.Name)); }
public DbTableExpression(DbTableAlias alias, MetaType type, string name) : base(DbExpressionType.Table, typeof(void), alias) { _type = type; _name = name; }
protected override Expression VisitSelect(DbSelectExpression select) { DbTableAlias newAlias = new DbTableAlias(); this.map[select.Alias] = newAlias; select = (DbSelectExpression)base.VisitSelect(select); return new DbSelectExpression(newAlias, select.Columns, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take); }
public DbAggregateSubqueryExpression(DbTableAlias groupByAlias, Expression aggregateInGroupSelect, DbScalarExpression aggregateAsSubquery) : base(DbExpressionType.AggregateSubquery, aggregateAsSubquery.Type) { _aggregateInGroupSelect = aggregateInGroupSelect; _groupByAlias = groupByAlias; _aggregateAsSubquery = aggregateAsSubquery; }
public DbColumnExpression(Type type, DbDataType dbType, DbTableAlias alias, string name) : base(DbExpressionType.Column, type) { _alias = alias; _name = name; _dbType = dbType; }
public DbAggregateSubqueryExpression(DbTableAlias groupByAlias, Expression aggregateInGroupSelect, DbScalarExpression aggregateAsSubquery) : base(DbExpressionType.AggregateSubquery, aggregateAsSubquery.Type) { _aggregateInGroupSelect = aggregateInGroupSelect; _groupByAlias = groupByAlias; _aggregateAsSubquery = aggregateAsSubquery; }
internal Scope(Scope outer, ParameterExpression dbDataReaderParam, DbTableAlias alias, IEnumerable <DbColumnDeclaration> columns) { this.outer = outer; this.dbDataReader = dbDataReaderParam; this.Alias = alias; this.nameMap = columns.Select((c, i) => new { c, i }).ToDictionary(x => x.c.Name, x => x.i); }
public DbSelectExpression(DbTableAlias alias, IEnumerable<DbColumnDeclaration> columns, Expression from, Expression where, IEnumerable<DbOrderExpression> orderBy, IEnumerable<Expression> groupBy) : this(alias, columns, from, where, orderBy, groupBy, false, null, null) { }
protected override Expression VisitSelect(DbSelectExpression select) { DbTableAlias newAlias = new DbTableAlias(); this.map[select.Alias] = newAlias; select = (DbSelectExpression)base.VisitSelect(select); return(new DbSelectExpression(newAlias, select.Columns, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take)); }
protected override Expression VisitProjection(DbProjectionExpression proj) { DbSelectExpression save = this.currentSelect; this.currentSelect = proj.Select; try { if (!this.isTopLevel) { if (this.CanJoinOnClient(this.currentSelect)) { // make a query that combines all the constraints from the outer queries into a single select DbSelectExpression newOuterSelect = (DbSelectExpression)QueryDuplicator.Duplicate(save); // remap any references to the outer select to the new alias; DbSelectExpression newInnerSelect = (DbSelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias); // add outer-join test DbProjectionExpression newInnerProjection = new DbProjectionExpression(newInnerSelect, proj.Projector).AddOuterJoinTest(); newInnerSelect = newInnerProjection.Select; Expression newProjector = newInnerProjection.Projector; DbTableAlias newAlias = new DbTableAlias(); var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, newProjector, newOuterSelect.Columns, newAlias, newOuterSelect.Alias, newInnerSelect.Alias); DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, newOuterSelect, newInnerSelect, null); DbSelectExpression joinedSelect = new DbSelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null); // apply client-join treatment recursively this.currentSelect = joinedSelect; newProjector = this.Visit(pc.Projector); // compute keys (this only works if join condition was a single column comparison) List <Expression> outerKeys = new List <Expression>(); List <Expression> innerKeys = new List <Expression>(); if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys)) { // outerKey needs to refer to the outer-scope's alias var outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias)); // innerKey needs to refer to the new alias for the select with the new join var innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((DbColumnExpression)k).Alias)); DbProjectionExpression newProjection = new DbProjectionExpression(joinedSelect, newProjector, proj.Aggregator); return(new DbClientJoinExpression(newProjection, outerKey, innerKey)); } } } else { this.isTopLevel = false; } return(base.VisitProjection(proj)); } finally { this.currentSelect = save; } }
public DbSelectExpression(DbTableAlias alias, IEnumerable <DbColumnDeclaration> columns, Expression from, Expression where, IEnumerable <DbOrderExpression> orderBy, IEnumerable <Expression> groupBy) : this(alias, columns, from, where, orderBy, groupBy, false, null, null) { }
protected override Expression VisitProjection(DbProjectionExpression proj) { DbSelectExpression save = this.currentSelect; this.currentSelect = proj.Select; try { if (!this.isTopLevel) { if (this.CanJoinOnClient(this.currentSelect)) { // make a query that combines all the constraints from the outer queries into a single select DbSelectExpression newOuterSelect = (DbSelectExpression)QueryDuplicator.Duplicate(save); // remap any references to the outer select to the new alias; DbSelectExpression newInnerSelect = (DbSelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias); // add outer-join test DbProjectionExpression newInnerProjection = new DbProjectionExpression(newInnerSelect, proj.Projector).AddOuterJoinTest(); newInnerSelect = newInnerProjection.Select; Expression newProjector = newInnerProjection.Projector; DbTableAlias newAlias = new DbTableAlias(); var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, newProjector, newOuterSelect.Columns, newAlias, newOuterSelect.Alias, newInnerSelect.Alias); DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, newOuterSelect, newInnerSelect, null); DbSelectExpression joinedSelect = new DbSelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null); // apply client-join treatment recursively this.currentSelect = joinedSelect; newProjector = this.Visit(pc.Projector); // compute keys (this only works if join condition was a single column comparison) List<Expression> outerKeys = new List<Expression>(); List<Expression> innerKeys = new List<Expression>(); if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys)) { // outerKey needs to refer to the outer-scope's alias var outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias)); // innerKey needs to refer to the new alias for the select with the new join var innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((DbColumnExpression)k).Alias)); DbProjectionExpression newProjection = new DbProjectionExpression(joinedSelect, newProjector, proj.Aggregator); return new DbClientJoinExpression(newProjection, outerKey, innerKey); } } } else { this.isTopLevel = false; } return base.VisitProjection(proj); } finally { this.currentSelect = save; } }
private void MarkColumnAsUsed(DbTableAlias alias, string name) { HashSet<string> columns; if (!this.allColumnsUsed.TryGetValue(alias, out columns)) { columns = new HashSet<string>(); this.allColumnsUsed.Add(alias, columns); } columns.Add(name); }
private void MarkColumnAsUsed(DbTableAlias alias, string name) { HashSet <string> columns; if (!this.allColumnsUsed.TryGetValue(alias, out columns)) { columns = new HashSet <string>(); this.allColumnsUsed.Add(alias, columns); } columns.Add(name); }
protected virtual bool CompareAlias(DbTableAlias a, DbTableAlias b) { if (_aliasScope != null) { DbTableAlias mapped; if (_aliasScope.TryGetValue(a, out mapped)) { return(mapped == b); } } return(a == b); }
private bool IsColumnUsed(DbTableAlias alias, string name) { HashSet<string> columnsUsed; if (this.allColumnsUsed.TryGetValue(alias, out columnsUsed)) { if (columnsUsed != null) { return columnsUsed.Contains(name); } } return false; }
private bool IsColumnUsed(DbTableAlias alias, string name) { HashSet <string> columnsUsed; if (this.allColumnsUsed.TryGetValue(alias, out columnsUsed)) { if (columnsUsed != null) { return(columnsUsed.Contains(name)); } } return(false); }
private bool GetEquiJoinKeyExpressions(Expression predicate, DbTableAlias outerAlias, List <Expression> outerExpressions, List <Expression> innerExpressions) { if (predicate.NodeType == ExpressionType.Equal) { var b = (BinaryExpression)predicate; DbColumnExpression leftCol = this.GetColumnExpression(b.Left); DbColumnExpression rightCol = this.GetColumnExpression(b.Right); if (leftCol != null && rightCol != null) { if (leftCol.Alias == outerAlias) { outerExpressions.Add(b.Left); innerExpressions.Add(b.Right); return(true); } else if (rightCol.Alias == outerAlias) { innerExpressions.Add(b.Left); outerExpressions.Add(b.Right); return(true); } } } bool hadKey = false; var parts = predicate.Split(ExpressionType.And, ExpressionType.AndAlso); if (parts.Length > 1) { foreach (var part in parts) { bool hasOuterAliasReference = ReferencedAliasGatherer.Gather(part).Contains(outerAlias); if (hasOuterAliasReference) { if (!GetEquiJoinKeyExpressions(part, outerAlias, outerExpressions, innerExpressions)) { return(false); } hadKey = true; } } } return(hadKey); }
public DbSelectExpression(DbTableAlias alias, IEnumerable<DbColumnDeclaration> columns, Expression from, Expression where, IEnumerable<DbOrderExpression> orderBy, IEnumerable<Expression> groupBy, bool isDistinct, Expression skip, Expression take) : base(DbExpressionType.Select, typeof(void), alias) { _isDistinct = isDistinct; _columns = columns.AsReadOnly(); _from = from; _where = where; _orderBy = orderBy.AsReadOnly(); _groupBy = groupBy.AsReadOnly(); _take = take; _skip = skip; }
public DbSelectExpression(DbTableAlias alias, IEnumerable <DbColumnDeclaration> columns, Expression from, Expression where, IEnumerable <DbOrderExpression> orderBy, IEnumerable <Expression> groupBy, bool isDistinct, Expression skip, Expression take) : base(DbExpressionType.Select, typeof(void), alias) { _isDistinct = isDistinct; _columns = columns.AsReadOnly(); _from = from; _where = where; _orderBy = orderBy.AsReadOnly(); _groupBy = groupBy.AsReadOnly(); _take = take; _skip = skip; }
protected override Expression VisitProjection(DbProjectionExpression proj) { if (isTopLevel) { isTopLevel = false; this.currentSelect = proj.Select; Expression projector = this.Visit(proj.Projector); if (projector != proj.Projector || this.currentSelect != proj.Select) { return(new DbProjectionExpression(this.currentSelect, projector, proj.Aggregator)); } return(proj); } if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect)) { DbTableAlias newAlias = new DbTableAlias(); this.currentSelect = this.currentSelect.AddRedundantSelect(newAlias); // remap any references to the outer select to the new alias; DbSelectExpression source = (DbSelectExpression)ColumnMapper.Map(proj.Select, newAlias, this.currentSelect.Alias); // add outer-join test DbProjectionExpression pex = new DbProjectionExpression(source, proj.Projector).AddOuterJoinTest(); var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, pex.Projector, this.currentSelect.Columns, this.currentSelect.Alias, newAlias, proj.Select.Alias); DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, this.currentSelect.From, pex.Select, null); this.currentSelect = new DbSelectExpression(this.currentSelect.Alias, pc.Columns, join, null); return(this.Visit(pc.Projector)); } var saveTop = this.isTopLevel; var saveSelect = this.currentSelect; this.isTopLevel = true; this.currentSelect = null; Expression result = base.VisitProjection(proj); this.isTopLevel = saveTop; this.currentSelect = saveSelect; return(result); }
private ColumnProjector(Func <Expression, bool> fnCanBeColumn, Expression expression, IEnumerable <DbColumnDeclaration> existingColumns, DbTableAlias newAlias, IEnumerable <DbTableAlias> existingAliases) { this.newAlias = newAlias; this.existingAliases = new HashSet <DbTableAlias>(existingAliases); this.map = new Dictionary <DbColumnExpression, DbColumnExpression>(); if (existingColumns != null) { this.columns = new List <DbColumnDeclaration>(existingColumns); this.columnNames = new HashSet <string>(existingColumns.Select(c => c.Name)); } else { this.columns = new List <DbColumnDeclaration>(); this.columnNames = new HashSet <string>(); } this.candidates = Nominator.Nominate(fnCanBeColumn, expression); }
protected override Expression VisitProjection(DbProjectionExpression proj) { if (isTopLevel) { isTopLevel = false; this.currentSelect = proj.Select; Expression projector = this.Visit(proj.Projector); if (projector != proj.Projector || this.currentSelect != proj.Select) { return new DbProjectionExpression(this.currentSelect, projector, proj.Aggregator); } return proj; } if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect)) { DbTableAlias newAlias = new DbTableAlias(); this.currentSelect = this.currentSelect.AddRedundantSelect(newAlias); // remap any references to the outer select to the new alias; DbSelectExpression source = (DbSelectExpression)ColumnMapper.Map(proj.Select, newAlias, this.currentSelect.Alias); // add outer-join test DbProjectionExpression pex = new DbProjectionExpression(source, proj.Projector).AddOuterJoinTest(); var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, pex.Projector, this.currentSelect.Columns, this.currentSelect.Alias, newAlias, proj.Select.Alias); DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, this.currentSelect.From, pex.Select, null); this.currentSelect = new DbSelectExpression(this.currentSelect.Alias, pc.Columns, join, null); return this.Visit(pc.Projector); } var saveTop = this.isTopLevel; var saveSelect = this.currentSelect; this.isTopLevel = true; this.currentSelect = null; Expression result = base.VisitProjection(proj); this.isTopLevel = saveTop; this.currentSelect = saveSelect; return result; }
private ColumnProjector(Func<Expression, bool> fnCanBeColumn, Expression expression, IEnumerable<DbColumnDeclaration> existingColumns, DbTableAlias newAlias, IEnumerable<DbTableAlias> existingAliases) { this.newAlias = newAlias; this.existingAliases = new HashSet<DbTableAlias>(existingAliases); this.map = new Dictionary<DbColumnExpression, DbColumnExpression>(); if (existingColumns != null) { this.columns = new List<DbColumnDeclaration>(existingColumns); this.columnNames = new HashSet<string>(existingColumns.Select(c => c.Name)); } else { this.columns = new List<DbColumnDeclaration>(); this.columnNames = new HashSet<string>(); } this.candidates = Nominator.Nominate(fnCanBeColumn, expression); }
private Expression MakeSubquery(Expression expression) { var newAlias = new DbTableAlias(); var aliases = DeclaredAliasGatherer.Gather(expression); var decls = new List<DbColumnDeclaration>(); foreach (var ta in aliases) { foreach (var col in this.columns[ta]) { string name = decls.GetAvailableColumnName(col.Name); var decl = new DbColumnDeclaration(name, col); decls.Add(decl); var newCol = new DbColumnExpression(col.Type, col.DbType, newAlias, col.Name); this.map.Add(col, newCol); } } return new DbSelectExpression(newAlias, decls, expression, null); }
private Expression MakeSubquery(Expression expression) { var newAlias = new DbTableAlias(); var aliases = DeclaredAliasGatherer.Gather(expression); var decls = new List <DbColumnDeclaration>(); foreach (var ta in aliases) { foreach (var col in this.columns[ta]) { string name = decls.GetAvailableColumnName(col.Name); var decl = new DbColumnDeclaration(name, col); decls.Add(decl); var newCol = new DbColumnExpression(col.Type, col.DbType, newAlias, col.Name); this.map.Add(col, newCol); } } return(new DbSelectExpression(newAlias, decls, expression, null)); }
protected DbAliasedExpression(DbExpressionType nodeType, Type type, DbTableAlias alias) : base(nodeType, type) { _alias = alias; }
private void ClearColumnsUsed(DbTableAlias alias) { this.allColumnsUsed[alias] = new HashSet <string>(); }
protected DbAliasedExpression(DbExpressionType nodeType, Type type, DbTableAlias alias) : base(nodeType, type) { _alias = alias; }
protected virtual string GetAliasName(DbTableAlias alias) { string name; if (!this.aliases.TryGetValue(alias, out name)) { name = "t" + this.aliases.Count; this.aliases.Add(alias, name); } return name; }
public static ProjectedColumns ProjectColumns(Func <Expression, bool> fnCanBeColumn, Expression expression, IEnumerable <DbColumnDeclaration> existingColumns, DbTableAlias newAlias, params DbTableAlias[] existingAliases) { return(ProjectColumns(fnCanBeColumn, expression, existingColumns, newAlias, (IEnumerable <DbTableAlias>)existingAliases)); }
public DbSelectExpression(DbTableAlias alias, IEnumerable<DbColumnDeclaration> columns, Expression from, Expression where) : this(alias, columns, from, where, null, null) { }
public static Expression Map(Expression expression, DbTableAlias newAlias, IEnumerable<DbTableAlias> oldAliases) { return new ColumnMapper(oldAliases, newAlias).Visit(expression); }
public static Expression Map(Expression expression, DbTableAlias newAlias, params DbTableAlias[] oldAliases) { return Map(expression, newAlias, (IEnumerable<DbTableAlias>)oldAliases); }
private ColumnMapper(IEnumerable<DbTableAlias> oldAliases, DbTableAlias newAlias) { this.oldAliases = new HashSet<DbTableAlias>(oldAliases); this.newAlias = newAlias; }
internal GroupByInfo(DbTableAlias alias, Expression element) { this.Alias = alias; this.Element = element; }
private ProjectedColumns ProjectColumns(Expression expression, DbTableAlias newAlias, params DbTableAlias[] existingAliases) { return(ColumnProjector.ProjectColumns(this.mapping.Language.CanBeColumn, expression, null, newAlias, existingAliases)); }
/// <summary> /// Rebind order expressions to reference a new alias and add to column declarations if necessary /// </summary> protected virtual BindResult RebindOrderings(IEnumerable <DbOrderExpression> orderings, DbTableAlias alias, HashSet <DbTableAlias> existingAliases, IEnumerable <DbColumnDeclaration> existingColumns) { List <DbColumnDeclaration> newColumns = null; List <DbOrderExpression> newOrderings = new List <DbOrderExpression>(); foreach (DbOrderExpression ordering in orderings) { Expression expr = ordering.Expression; DbColumnExpression column = expr as DbColumnExpression; if (column == null || (existingAliases != null && existingAliases.Contains(column.Alias))) { // check to see if a declared column already contains a similar expression int iOrdinal = 0; foreach (DbColumnDeclaration decl in existingColumns) { DbColumnExpression declColumn = decl.Expression as DbColumnExpression; if (decl.Expression == ordering.Expression || (column != null && declColumn != null && column.Alias == declColumn.Alias && column.Name == declColumn.Name)) { // found it, so make a reference to this column expr = new DbColumnExpression(column.Type, column.DbType, alias, decl.Name); break; } iOrdinal++; } // if not already projected, add a new column declaration for it if (expr == ordering.Expression) { if (newColumns == null) { newColumns = new List <DbColumnDeclaration>(existingColumns); existingColumns = newColumns; } string colName = column != null ? column.Name : "c" + iOrdinal; newColumns.Add(new DbColumnDeclaration(colName, ordering.Expression)); expr = new DbColumnExpression(expr.Type, null, alias, colName); } newOrderings.Add(new DbOrderExpression(ordering.OrderType, expr)); } } return(new BindResult(existingColumns, newOrderings)); }
/// <summary> /// Rebind order expressions to reference a new alias and add to column declarations if necessary /// </summary> protected virtual BindResult RebindOrderings(IEnumerable<DbOrderExpression> orderings, DbTableAlias alias, HashSet<DbTableAlias> existingAliases, IEnumerable<DbColumnDeclaration> existingColumns) { List<DbColumnDeclaration> newColumns = null; List<DbOrderExpression> newOrderings = new List<DbOrderExpression>(); foreach (DbOrderExpression ordering in orderings) { Expression expr = ordering.Expression; DbColumnExpression column = expr as DbColumnExpression; if (column == null || (existingAliases != null && existingAliases.Contains(column.Alias))) { // check to see if a declared column already contains a similar expression int iOrdinal = 0; foreach (DbColumnDeclaration decl in existingColumns) { DbColumnExpression declColumn = decl.Expression as DbColumnExpression; if (decl.Expression == ordering.Expression || (column != null && declColumn != null && column.Alias == declColumn.Alias && column.Name == declColumn.Name)) { // found it, so make a reference to this column expr = new DbColumnExpression(column.Type, column.DbType, alias, decl.Name); break; } iOrdinal++; } // if not already projected, add a new column declaration for it if (expr == ordering.Expression) { if (newColumns == null) { newColumns = new List<DbColumnDeclaration>(existingColumns); existingColumns = newColumns; } string colName = column != null ? column.Name : "c" + iOrdinal; newColumns.Add(new DbColumnDeclaration(colName, ordering.Expression)); expr = new DbColumnExpression(expr.Type, null, alias, colName); } newOrderings.Add(new DbOrderExpression(ordering.OrderType, expr)); } } return new BindResult(existingColumns, newOrderings); }
protected override Expression VisitTable(DbTableExpression table) { DbTableAlias newAlias = new DbTableAlias(); this.map[table.Alias] = newAlias; return new DbTableExpression(newAlias, table.Type, table.Name); }
private ProjectedColumns ProjectColumns(Expression expression, DbTableAlias newAlias, params DbTableAlias[] existingAliases) { return ColumnProjector.ProjectColumns(this.mapping.Language.CanBeColumn, expression, null, newAlias, existingAliases); }
private void ClearColumnsUsed(DbTableAlias alias) { this.allColumnsUsed[alias] = new HashSet<string>(); }
public static DbSelectExpression AddRedundantSelect(this DbSelectExpression select, DbTableAlias newAlias) { var newColumns = select.Columns.Select(d => new DbColumnDeclaration(d.Name, new DbColumnExpression( d.Expression.Type, (d.Expression is DbColumnExpression) ? ((DbColumnExpression)d.Expression).DbType : null, newAlias, d.Name ))); var newFrom = new DbSelectExpression(newAlias, select.Columns, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take); return new DbSelectExpression(select.Alias, newColumns, newFrom, null, null, null, false, null, null); }
public static ProjectedColumns ProjectColumns(Func <Expression, bool> fnCanBeColumn, Expression expression, IEnumerable <DbColumnDeclaration> existingColumns, DbTableAlias newAlias, IEnumerable <DbTableAlias> existingAliases) { ColumnProjector projector = new ColumnProjector(fnCanBeColumn, expression, existingColumns, newAlias, existingAliases); Expression expr = projector.Visit(expression); return(new ProjectedColumns(expr, projector.columns.AsReadOnly())); }
public static DbSelectExpression AddRedundantSelect(this DbSelectExpression select, DbTableAlias newAlias) { var newColumns = select.Columns.Select(d => new DbColumnDeclaration(d.Name, new DbColumnExpression( d.Expression.Type, (d.Expression is DbColumnExpression) ? ((DbColumnExpression)d.Expression).DbType : null, newAlias, d.Name ))); var newFrom = new DbSelectExpression(newAlias, select.Columns, select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take); return(new DbSelectExpression(select.Alias, newColumns, newFrom, null, null, null, false, null, null)); }
protected virtual bool CompareAlias(DbTableAlias a, DbTableAlias b) { if (_aliasScope != null) { DbTableAlias mapped; if (_aliasScope.TryGetValue(a, out mapped)) return (mapped == b); } return (a == b); }
internal GroupByInfo(DbTableAlias alias, Expression element) { this.Alias = alias; this.Element = element; }
public static ProjectedColumns ProjectColumns(Func<Expression, bool> fnCanBeColumn, Expression expression, IEnumerable<DbColumnDeclaration> existingColumns, DbTableAlias newAlias, params DbTableAlias[] existingAliases) { return ProjectColumns(fnCanBeColumn, expression, existingColumns, newAlias, (IEnumerable<DbTableAlias>)existingAliases); }
private bool GetEquiJoinKeyExpressions(Expression predicate, DbTableAlias outerAlias, List<Expression> outerExpressions, List<Expression> innerExpressions) { if (predicate.NodeType == ExpressionType.Equal) { var b = (BinaryExpression)predicate; DbColumnExpression leftCol = this.GetColumnExpression(b.Left); DbColumnExpression rightCol = this.GetColumnExpression(b.Right); if (leftCol != null && rightCol != null) { if (leftCol.Alias == outerAlias) { outerExpressions.Add(b.Left); innerExpressions.Add(b.Right); return true; } else if (rightCol.Alias == outerAlias) { innerExpressions.Add(b.Left); outerExpressions.Add(b.Right); return true; } } } bool hadKey = false; var parts = predicate.Split(ExpressionType.And, ExpressionType.AndAlso); if (parts.Length > 1) { foreach (var part in parts) { bool hasOuterAliasReference = ReferencedAliasGatherer.Gather(part).Contains(outerAlias); if (hasOuterAliasReference) { if (!GetEquiJoinKeyExpressions(part, outerAlias, outerExpressions, innerExpressions)) return false; hadKey = true; } } } return hadKey; }
private ColumnMapper(IEnumerable <DbTableAlias> oldAliases, DbTableAlias newAlias) { this.oldAliases = new HashSet <DbTableAlias>(oldAliases); this.newAlias = newAlias; }
public static Expression Map(Expression expression, DbTableAlias newAlias, IEnumerable <DbTableAlias> oldAliases) { return(new ColumnMapper(oldAliases, newAlias).Visit(expression)); }
public static Expression Map(Expression expression, DbTableAlias newAlias, params DbTableAlias[] oldAliases) { return(Map(expression, newAlias, (IEnumerable <DbTableAlias>)oldAliases)); }
public DbSelectExpression(DbTableAlias alias, IEnumerable <DbColumnDeclaration> columns, Expression from, Expression where) : this(alias, columns, from, where, null, null) { }