private void CheckJoinCondition(SqlExpression expr) { switch (expr.NodeType) { case SqlNodeType.And: { SqlBinary b = (SqlBinary)expr; CheckJoinCondition(b.Left); CheckJoinCondition(b.Right); break; } case SqlNodeType.EQ: case SqlNodeType.EQ2V: { SqlBinary b = (SqlBinary)expr; SqlColumnRef crLeft = b.Left as SqlColumnRef; SqlColumnRef crRight = b.Right as SqlColumnRef; if (crLeft != null && crRight != null) { SqlColumn cLeft = crLeft.GetRootColumn(); SqlColumn cRight = crRight.GetRootColumn(); this._map[cLeft] = cRight; this._map[cRight] = cLeft; } break; } } }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlExpression literal = this.GetLiteralValue(cref); if (literal != null) { return literal; } return cref; }
private static SqlColumn UnwrapColumn(SqlExpression expr) { System.Diagnostics.Debug.Assert(expr != null); SqlUnary exprAsUnary = expr as SqlUnary; if (exprAsUnary != null) { expr = exprAsUnary.Operand; } SqlColumn exprAsColumn = expr as SqlColumn; if (exprAsColumn != null) { return(exprAsColumn); } SqlColumnRef exprAsColumnRef = expr as SqlColumnRef; if (exprAsColumnRef != null) { return(exprAsColumnRef.GetRootColumn()); } // // For all other types return null to revert to default behavior for Equals() // and GetHashCode() // return(null); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumn mapped; if (this.map.TryGetValue(cref.Column, out mapped)) { return new SqlColumnRef(mapped); } return cref; }
internal bool RefersToColumn(SqlExpression exp, SqlColumn col) { #if DEBUG try { refersDepth++; System.Diagnostics.Debug.Assert(refersDepth < 20); #endif if (exp != null) { switch (exp.NodeType) { case SqlNodeType.Column: return exp == col || this.RefersToColumn(((SqlColumn)exp).Expression, col); case SqlNodeType.ColumnRef: SqlColumnRef cref = (SqlColumnRef)exp; return cref.Column == col || this.RefersToColumn(cref.Column.Expression, col); case SqlNodeType.ExprSet: SqlExprSet set = (SqlExprSet)exp; for (int i = 0, n = set.Expressions.Count; i < n; i++) { if (this.RefersToColumn(set.Expressions[i], col)) { return true; } } break; case SqlNodeType.OuterJoinedValue: return this.RefersToColumn(((SqlUnary)exp).Operand, col); } } return false; #if DEBUG } finally { refersDepth--; } #endif }
public void Visit(SqlColumnRef node) { if (!node.SqlColumn.IsNullReference()) { Visit(node.SqlColumn); } }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumnRef result = this.BubbleUp(cref); if (result == null) { throw Error.ColumnReferencedIsNotInScope(GetColumnName(cref.Column)); } return result; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.ingoreExternalRefs && !this.nodeMap.ContainsKey(cref.Column)) { return(cref); } return(new SqlColumnRef((SqlColumn)this.Visit(cref.Column))); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (cref.Column.Name == null && this.lastName != null) { cref.Column.Name = this.lastName; } return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (expressionSink != null) { expressionSink.ReferencedExpressions.Add(cref.Column); } return(cref); }
protected override SqlSelect GenerateSkipTake(SqlSelect sequence, SqlExpression skipExp, SqlExpression takeExp) { //return base.GenerateSkipTake(sequence, skipExp, takeExp); SqlSelect node = LockSelect(sequence); var value2 = skipExp as SqlValue; if ((skipExp == null) || ((value2 != null) && (((int)value2.Value) <= 0))) { skipExp = sql.ValueFromObject(0, dominatingExpression); } var alias = new SqlAlias(node); var selection = new SqlAliasRef(alias); if (UseConverterStrategy(ConverterStrategy.SkipWithRowNumber)) { var col = new SqlColumn("ROW_NUMBER", this.sql.RowNumber(new List <SqlOrderExpression>(), this.dominatingExpression)); var expr = new SqlColumnRef(col); node.Row.Columns.Add(col); var select2 = new SqlSelect(selection, alias, this.dominatingExpression); if (takeExp != null) { select2.Where = this.sql.Between(expr, this.sql.Add(skipExp, 1), this.sql.Binary(SqlNodeType.Add, (SqlExpression)SqlDuplicator.Copy(skipExp), takeExp), this.dominatingExpression); return(select2); } select2.Where = this.sql.Binary(SqlNodeType.GT, expr, skipExp); return(select2); } if (!this.CanSkipOnSelection(node.Selection)) { throw SqlClient.Error.SkipNotSupportedForSequenceTypes(); } var visitor = new SingleTableQueryVisitor(); visitor.Visit(node); if (!visitor.IsValid) { throw ALinq.SqlClient.Error.SkipRequiresSingleTableQueryWithPKs(); } var select3 = (SqlSelect)SqlDuplicator.Copy(node); select3.Top = skipExp; var alias2 = new SqlAlias(select3); var ref4 = new SqlAliasRef(alias2); var select = new SqlSelect(ref4, alias2, this.dominatingExpression); select.Where = this.sql.Binary(SqlNodeType.EQ2V, selection, ref4); SqlSubSelect expression = this.sql.SubSelect(SqlNodeType.Exists, select); var select6 = new SqlSelect(selection, alias, this.dominatingExpression); select6.Where = this.sql.Unary(SqlNodeType.Not, expression, this.dominatingExpression); select6.Top = takeExp; return(select6); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlExpression literal = this.GetLiteralValue(cref); if (literal != null) { return(literal); } return(cref); }
internal SqlColumn GetOriginatingColumn(SqlColumn col) { SqlColumnRef cref = col.Expression as SqlColumnRef; if (cref != null) { return(this.GetOriginatingColumn(cref.Column)); } return(col); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumn mapped; if (this.map.TryGetValue(cref.Column, out mapped)) { return(new SqlColumnRef(mapped)); } return(cref); }
internal SqlColumn GetOriginatingColumn(SqlColumn col) { SqlColumnRef expression = col.Expression as SqlColumnRef; if (expression != null) { return(this.GetOriginatingColumn(expression.Column)); } return(col); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumnRef result = this.BubbleUp(cref); if (result == null) { throw Error.ColumnReferencedIsNotInScope(GetColumnName(cref.Column)); } return(result); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlExpression result = base.VisitColumnRef(cref); if (result == cref) { // must be external return(ExtractParameter(result)); } return(result); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (!this.columns.Contains(cref.Column)) { this.columns.Add(cref.Column); if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } } return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.aliasesToCheck.Contains(cref.Column.Alias)) { this.hasDependency = true; } else if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.parent.aliases.Contains(cref.Column.Alias)) { this.parent.referencesAny = true; } else if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumn c = this.FindColumn(this.row.Columns, cref.Column); if (c == null) { return(MakeFlattenedColumn(cref, null)); } else { return(new SqlColumnRef(c)); } }
private SqlColumnRef BubbleUp(SqlColumnRef cref) { for (Scope s = this.CurrentScope; s != null; s = s.ContainingScope) { if (s.Source != null) { SqlColumn found = this.bubbler.BubbleUp(cref.Column, s.Source); if (found != null) { if (found != cref.Column) return new SqlColumnRef(found); return cref; } } } return null; }
private static SqlColumn GetBaseColumn(SqlColumnRef cref) { while ((cref != null) && (cref.Column.Expression != null)) { SqlColumnRef expression = cref.Column.Expression as SqlColumnRef; if (expression == null) { break; } cref = expression; } return(cref.Column); }
private static SqlColumn GetBaseColumn(SqlColumnRef cref) { while (cref != null && cref.Column.Expression != null) { SqlColumnRef cr = cref.Column.Expression as SqlColumnRef; if (cr != null) { cref = cr; continue; } break; } return(cref.Column); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlExpression result = base.VisitColumnRef(cref); if (result != null && result == cref) { // reference to outer scope, don't propogate references to expressions or aliases SqlColumn col = cref.Column; SqlColumn newcol = new SqlColumn(col.ClrType, col.SqlType, col.Name, col.MetaMember, null, col.SourceExpression); newcol.Ordinal = col.Ordinal; result = new SqlColumnRef(newcol); newcol.ClearSourceExpression(); } return(result); }
internal override SqlSelect VisitSelect(SqlSelect select) { base.VisitSelect(@select); // enforce exact ordering of columns in union selects SqlUnion union = this.GetUnion(@select.From); if (union != null) { SqlSelect sleft = union.Left as SqlSelect; SqlSelect sright = union.Right as SqlSelect; if (sleft != null & sright != null) { // preset ordinals to high values (so any unreachable column definition is ordered last) for (int i = 0, n = sleft.Row.Columns.Count; i < n; i++) { sleft.Row.Columns[i].Ordinal = @select.Row.Columns.Count + i; } for (int i = 0, n = sright.Row.Columns.Count; i < n; i++) { sright.Row.Columns[i].Ordinal = @select.Row.Columns.Count + i; } // next assign ordinals to all direct columns in subselects for (int i = 0, n = @select.Row.Columns.Count; i < n; i++) { SqlExprSet es = @select.Row.Columns[i].Expression as SqlExprSet; if (es != null) { for (int e = 0, en = es.Expressions.Count; e < en; e++) { SqlColumnRef cr = es.Expressions[e] as SqlColumnRef; if (cr != null && e >= @select.Row.Columns.Count) { cr.Column.Ordinal = i; } } } } // next sort columns in left & right subselects Comparison <SqlColumn> comp = (x, y) => x.Ordinal - y.Ordinal; sleft.Row.Columns.Sort(comp); sright.Row.Columns.Sort(comp); } } return(@select); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { string str = null; SqlColumn key = cref.Column; SqlAlias alias = key.Alias; if (alias == null) { this.aliasMap.TryGetValue(key, out alias); } if (alias != null && !disableAlias) { if (alias.Name == null) { if (!this.names.TryGetValue(alias, out str)) { str = "A" + this.names.Count; this.names[key.Alias] = str; } } else { str = key.Alias.Name; } } if ((!this.suppressedAliases.Contains(key.Alias) && (str != null)) && (str.Length != 0)) { this.WriteName(str); this.sb.Append("."); } string name = key.Name; string str3 = this.InferName(key.Expression, null); if (name == null) { name = str3; } if ((name == null) && !this.names.TryGetValue(key, out name)) { name = "C" + this.names.Count; this.names[key] = name; } this.WriteName(name); return(cref); }
private SqlColumnRef BubbleUp(SqlColumnRef cref) { for (SqlScope s = this.CurrentScope; s != null; s = s.ContainingScope) { if (s.Source != null) { SqlColumn found = this.bubbler.BubbleUp(cref.Column, s.Source); if (found != null) { if (found != cref.Column) { return(new SqlColumnRef(found)); } return(cref); } } } return(null); }
internal bool AreEquivalent(SqlExpression e1, SqlExpression e2) { if (SqlComparer.AreEqual(e1, e2)) { return(true); } SqlColumnRef cr1 = e1 as SqlColumnRef; SqlColumnRef cr2 = e2 as SqlColumnRef; if (cr1 != null && cr2 != null) { SqlColumn c1 = cr1.GetRootColumn(); SqlColumn c2 = cr2.GetRootColumn(); SqlColumn r; return(this._map.TryGetValue(c1, out r) && r == c2); } return(false); }
private SqlColumnRef BubbleUp(SqlColumnRef cref) { for (SqlResolver.SqlScopedVisitor.Scope scope = base.CurrentScope; scope != null; scope = scope.ContainingScope) { if (scope.Source != null) { SqlColumn col = this.bubbler.BubbleUp(cref.Column, scope.Source); if (col != null) { if (col != cref.Column) { return(new SqlColumnRef(col)); } return(cref); } } } return(null); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (cref.Column.Alias != null && _removedMap.ContainsKey(cref.Column.Alias)) { SqlColumnRef c = cref.Column.Expression as SqlColumnRef; if (c != null) { //The following code checks for cases where there are differences between the type returned //by a ColumnRef and the column that refers to it. This situation can occur when conversions //are optimized out of the SQL node tree. As mentioned in the SetClrType comments this is not //an operation that can have adverse effects and should only be used in limited cases, such as //this one. if (c.ClrType != cref.ClrType) { c.SetClrType(cref.ClrType); return(this.VisitColumnRef(c)); } } return(c); } return(cref); }
internal override SqlSelect VisitSelect(SqlSelect select) { if (IsTrivialSelect(@select)) { SqlSelect aselect = (SqlSelect)((SqlAlias)@select.From).Node; // build up a column map, so we can rewrite the top-level selection expression Dictionary <SqlColumn, SqlColumnRef> map = new Dictionary <SqlColumn, SqlColumnRef>(); foreach (SqlColumn c in @select.Row.Columns) { SqlColumnRef cref = (SqlColumnRef)c.Expression; map.Add(c, cref); // push the interesting column names down (non null) if (!String.IsNullOrEmpty(c.Name)) { cref.Column.Name = c.Name; } } aselect.Selection = new ColumnMapper(map).VisitExpression(@select.Selection); return(aselect); } return(@select); }
internal static string DiscoverName(SqlExpression e) { if (e != null) { switch (e.NodeType) { case SqlNodeType.Column: return(DiscoverName(((SqlColumn)e).Expression)); case SqlNodeType.ColumnRef: SqlColumnRef cref = (SqlColumnRef)e; if (cref.Column.Name != null) { return(cref.Column.Name); } return(DiscoverName(cref.Column)); case SqlNodeType.ExprSet: SqlExprSet eset = (SqlExprSet)e; return(DiscoverName(eset.Expressions[0])); } } return("value"); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumn c = this.FindColumn(this.row.Columns, cref.Column); if (c == null) { return MakeFlattenedColumn(cref, null); } else { return new SqlColumnRef(c); } }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (cref.Column.Alias != null && this.removedMap.ContainsKey(cref.Column.Alias)) { SqlColumnRef c = cref.Column.Expression as SqlColumnRef; if (c != null) { //The following code checks for cases where there are differences between the type returned //by a ColumnRef and the column that refers to it. This situation can occur when conversions //are optimized out of the SQL node tree. As mentioned in the SetClrType comments this is not //an operation that can have adverse effects and should only be used in limited cases, such as //this one. if (c.ClrType != cref.ClrType) { c.SetClrType(cref.ClrType); return this.VisitColumnRef(c); } } return c; } return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (!this.columns.Contains(cref.Column)) { this.columns.Add(cref.Column); if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } } return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumnRef mapped; return(this.map.TryGetValue(cref.Column, out mapped) ? mapped : cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlExpression result = base.VisitColumnRef(cref); if (result == cref) { // must be external return ExtractParameter(result); } return result; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { this.referenceMap[cref.Column] = cref.Column; return cref; }
private static SqlColumn GetBaseColumn(SqlColumnRef cref) { while (cref != null && cref.Column.Expression != null) { SqlColumnRef cr = cref.Column.Expression as SqlColumnRef; if (cr != null) { cref = cr; continue; } else { break; } } return cref.Column; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (cref.Column.Name == null && this.lastName != null) { cref.Column.Name = this.lastName; } return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { Visit(cref.Column); return base.VisitColumnRef(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (expressionSink!=null) { expressionSink.ReferencedExpressions.Add(cref.Column); } return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.aliasesToCheck.Contains(cref.Column.Alias)) { this.hasDependency = true; } else if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { this.VisitColumn(cref.Column); // Travel through column references return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.parent.aliases.Contains(cref.Column.Alias)) { this.parent.referencesAny = true; } else if (cref.Column.Expression != null) { this.Visit(cref.Column.Expression); } return cref; }
private SqlSelect GenerateSkipTake(SqlSelect sequence, SqlExpression skipExp, SqlExpression takeExp) { SqlSelect select = this.LockSelect(sequence); // no skip? if (skipExp == null) { if (takeExp != null) { select.Top = takeExp; } return select; } SqlAlias alias = new SqlAlias(select); SqlAliasRef aref = new SqlAliasRef(alias); if (this.UseConverterStrategy(ConverterStrategy.SkipWithRowNumber)) { // use ROW_NUMBER() (preferred) SqlColumn rowNumber = new SqlColumn("ROW_NUMBER", sql.RowNumber(new List<SqlOrderExpression>(), this.dominatingExpression)); SqlColumnRef rowNumberRef = new SqlColumnRef(rowNumber); select.Row.Columns.Add(rowNumber); SqlSelect final = new SqlSelect(aref, alias, this.dominatingExpression); if (takeExp != null) { // use BETWEEN for skip+take combo (much faster) final.Where = sql.Between( rowNumberRef, sql.Add(skipExp, 1), sql.Binary(SqlNodeType.Add, (SqlExpression)SqlDuplicator.Copy(skipExp), takeExp), this.dominatingExpression ); } else { final.Where = sql.Binary(SqlNodeType.GT, rowNumberRef, skipExp); } return final; } else { // Ensure that the sequence contains elements that can be skipped if (!CanSkipOnSelection(select.Selection)) { throw Error.SkipNotSupportedForSequenceTypes(); } // use NOT EXISTS // Supported cases: // - Entities // - Projections that contain all PK columns // // .. where there sequence can be traced back to a: // - Single-table query // - Distinct // - Except // - Intersect // - Union, where union.All == false // Not supported: joins // Sequence should also be ordered, but we can't test for it at this // point in processing, and we won't know that we need to test it, later. SingleTableQueryVisitor stqv = new SingleTableQueryVisitor(); stqv.Visit(select); if (!stqv.IsValid) { throw Error.SkipRequiresSingleTableQueryWithPKs(); } SqlSelect dupsel = (SqlSelect)SqlDuplicator.Copy(select); dupsel.Top = skipExp; SqlAlias dupAlias = new SqlAlias(dupsel); SqlAliasRef dupRef = new SqlAliasRef(dupAlias); SqlSelect eqsel = new SqlSelect(dupRef, dupAlias, this.dominatingExpression); eqsel.Where = sql.Binary(SqlNodeType.EQ2V, aref, dupRef); SqlSubSelect ss = sql.SubSelect(SqlNodeType.Exists, eqsel); SqlSelect final = new SqlSelect(aref, alias, this.dominatingExpression); final.Where = sql.Unary(SqlNodeType.Not, ss, this.dominatingExpression); final.Top = takeExp; return final; } }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { VisitAliasConsumed(cref.Column.Alias); return cref; }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { if (this.ingoreExternalRefs && !this.nodeMap.ContainsKey(cref.Column)) { return cref; } return new SqlColumnRef((SqlColumn)this.Visit(cref.Column)); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { string aliasName = null; SqlColumn c = cref.Column; SqlAlias alias = c.Alias; if (alias == null) { this.aliasMap.TryGetValue(c, out alias); } if (alias != null) { if (alias.Name == null) { if (!this.names.TryGetValue(alias, out aliasName)) { aliasName = "A" + this.names.Count; this.names[c.Alias] = aliasName; } } else { aliasName = c.Alias.Name; } } if (!this.suppressedAliases.Contains(c.Alias) && aliasName != null && aliasName.Length != 0) { this.WriteName(aliasName); sb.Append("."); } string name = c.Name; string inferredName = this.InferName(c.Expression, null); if (name == null) name = inferredName; if (name == null) { if (!this.names.TryGetValue(c, out name)) { name = "C" + this.names.Count; this.names[c] = name; } } this.WriteName(name); return cref; }
internal virtual SqlExpression VisitColumnRef(SqlColumnRef cref) { return cref; }