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) { if (expressionSink != null) { expressionSink.ReferencedExpressions.Add(cref.Column); } 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) { if (cref.Column.Name == null && this.lastName != null) { cref.Column.Name = this.lastName; } 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) { 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) { SqlExpression literal = this.GetLiteralValue(cref); if (literal != null) { return(literal); } return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { SqlColumn mapped; if (this.map.TryGetValue(cref.Column, out mapped)) { return(new SqlColumnRef(mapped)); } 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) { 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) { SqlColumn c = this.FindColumn(this.row.Columns, cref.Column); if (c == null) { return(MakeFlattenedColumn(cref, null)); } else { return(new SqlColumnRef(c)); } }
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); }
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 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 }
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); }
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); }
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 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 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 virtual SqlExpression VisitColumnRef(SqlColumnRef cref) { return(cref); }
internal static bool?CanBeNull(SqlExpression expr) { switch (expr.NodeType) { case SqlNodeType.ExprSet: SqlExprSet exprSet = (SqlExprSet)expr; return(CanBeNull(exprSet.Expressions)); case SqlNodeType.SimpleCase: SqlSimpleCase sc = (SqlSimpleCase)expr; return(CanBeNull(sc.Whens.Select(w => w.Value))); case SqlNodeType.Column: SqlColumn col = (SqlColumn)expr; if (col.MetaMember != null) { return(col.MetaMember.CanBeNull); } else if (col.Expression != null) { return(CanBeNull(col.Expression)); } return(null); // Don't know. case SqlNodeType.ColumnRef: SqlColumnRef cref = (SqlColumnRef)expr; return(CanBeNull(cref.Column)); case SqlNodeType.Value: return(((SqlValue)expr).Value == null); case SqlNodeType.New: case SqlNodeType.Multiset: case SqlNodeType.Grouping: case SqlNodeType.DiscriminatedType: case SqlNodeType.IsNotNull: // IsNull\IsNotNull always return true or false and can never return NULL. case SqlNodeType.IsNull: case SqlNodeType.Exists: return(false); case SqlNodeType.Add: case SqlNodeType.Sub: case SqlNodeType.Mul: case SqlNodeType.Div: case SqlNodeType.Mod: case SqlNodeType.BitAnd: case SqlNodeType.BitOr: case SqlNodeType.BitXor: case SqlNodeType.Concat: { SqlBinary bop = (SqlBinary)expr; bool? left = CanBeNull(bop.Left); bool? right = CanBeNull(bop.Right); return((left != false) || (right != false)); } case SqlNodeType.Negate: case SqlNodeType.BitNot: { SqlUnary uop = (SqlUnary)expr; return(CanBeNull(uop.Operand)); } case SqlNodeType.Lift: { SqlLift lift = (SqlLift)expr; return(CanBeNull(lift.Expression)); } case SqlNodeType.OuterJoinedValue: return(true); default: return(null); // Don't know. } }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { this.referenceMap[cref.Column] = cref.Column; return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { Visit(cref.Column); return(base.VisitColumnRef(cref)); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { this.VisitColumn(cref.Column); // Travel through column references return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { VisitAliasConsumed(cref.Column.Alias); return(cref); }
internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { return(cref); }
internal static bool AreEqual(SqlNode node1, SqlNode node2) { if (node1 == node2) { return(true); } if (node1 == null || node2 == null) { return(false); } if (node1.NodeType == SqlNodeType.SimpleCase) { node1 = UnwrapTrivialCaseExpression((SqlSimpleCase)node1); } if (node2.NodeType == SqlNodeType.SimpleCase) { node2 = UnwrapTrivialCaseExpression((SqlSimpleCase)node2); } if (node1.NodeType != node2.NodeType) { // allow expression sets to compare against single expressions if (node1.NodeType == SqlNodeType.ExprSet) { SqlExprSet eset = (SqlExprSet)node1; for (int i = 0, n = eset.Expressions.Count; i < n; i++) { if (AreEqual(eset.Expressions[i], node2)) { return(true); } } } else if (node2.NodeType == SqlNodeType.ExprSet) { SqlExprSet eset = (SqlExprSet)node2; for (int i = 0, n = eset.Expressions.Count; i < n; i++) { if (AreEqual(node1, eset.Expressions[i])) { return(true); } } } return(false); } if (node1.Equals(node2)) { return(true); } switch (node1.NodeType) { case SqlNodeType.Not: case SqlNodeType.Not2V: case SqlNodeType.Negate: case SqlNodeType.BitNot: case SqlNodeType.IsNull: case SqlNodeType.IsNotNull: case SqlNodeType.Count: case SqlNodeType.Max: case SqlNodeType.Min: case SqlNodeType.Sum: case SqlNodeType.Avg: case SqlNodeType.Stddev: case SqlNodeType.ValueOf: case SqlNodeType.OuterJoinedValue: case SqlNodeType.ClrLength: return(AreEqual(((SqlUnary)node1).Operand, ((SqlUnary)node2).Operand)); case SqlNodeType.Add: case SqlNodeType.Sub: case SqlNodeType.Mul: case SqlNodeType.Div: case SqlNodeType.Mod: case SqlNodeType.BitAnd: case SqlNodeType.BitOr: case SqlNodeType.BitXor: case SqlNodeType.And: case SqlNodeType.Or: case SqlNodeType.GE: case SqlNodeType.GT: case SqlNodeType.LE: case SqlNodeType.LT: case SqlNodeType.EQ: case SqlNodeType.NE: case SqlNodeType.EQ2V: case SqlNodeType.NE2V: case SqlNodeType.Concat: SqlBinary firstNode = (SqlBinary)node1; SqlBinary secondNode = (SqlBinary)node2; return(AreEqual(firstNode.Left, secondNode.Left) && AreEqual(firstNode.Right, secondNode.Right)); case SqlNodeType.Convert: case SqlNodeType.Treat: { SqlUnary sun1 = (SqlUnary)node1; SqlUnary sun2 = (SqlUnary)node2; return(sun1.ClrType == sun2.ClrType && sun1.SqlType == sun2.SqlType && AreEqual(sun1.Operand, sun2.Operand)); } case SqlNodeType.Between: { SqlBetween b1 = (SqlBetween)node1; SqlBetween b2 = (SqlBetween)node1; return(AreEqual(b1.Expression, b2.Expression) && AreEqual(b1.Start, b2.Start) && AreEqual(b1.End, b2.End)); } case SqlNodeType.Parameter: return(node1 == node2); case SqlNodeType.Alias: return(AreEqual(((SqlAlias)node1).Node, ((SqlAlias)node2).Node)); case SqlNodeType.AliasRef: return(AreEqual(((SqlAliasRef)node1).Alias, ((SqlAliasRef)node2).Alias)); case SqlNodeType.Column: SqlColumn col1 = (SqlColumn)node1; SqlColumn col2 = (SqlColumn)node2; return(col1 == col2 || (col1.Expression != null && col2.Expression != null && AreEqual(col1.Expression, col2.Expression))); case SqlNodeType.Table: return(((SqlTable)node1).MetaTable == ((SqlTable)node2).MetaTable); case SqlNodeType.Member: return((((SqlMember)node1).Member == ((SqlMember)node2).Member) && AreEqual(((SqlMember)node1).Expression, ((SqlMember)node2).Expression)); case SqlNodeType.ColumnRef: SqlColumnRef cref1 = (SqlColumnRef)node1; SqlColumnRef cref2 = (SqlColumnRef)node2; return(GetBaseColumn(cref1) == GetBaseColumn(cref2)); case SqlNodeType.Value: return(Object.Equals(((SqlValue)node1).Value, ((SqlValue)node2).Value)); case SqlNodeType.TypeCase: { SqlTypeCase c1 = (SqlTypeCase)node1; SqlTypeCase c2 = (SqlTypeCase)node2; if (!AreEqual(c1.Discriminator, c2.Discriminator)) { return(false); } if (c1.Whens.Count != c2.Whens.Count) { return(false); } for (int i = 0, c = c1.Whens.Count; i < c; ++i) { if (!AreEqual(c1.Whens[i].Match, c2.Whens[i].Match)) { return(false); } if (!AreEqual(c1.Whens[i].TypeBinding, c2.Whens[i].TypeBinding)) { return(false); } } return(true); } case SqlNodeType.SearchedCase: { SqlSearchedCase c1 = (SqlSearchedCase)node1; SqlSearchedCase c2 = (SqlSearchedCase)node2; if (c1.Whens.Count != c2.Whens.Count) { return(false); } for (int i = 0, n = c1.Whens.Count; i < n; i++) { if (!AreEqual(c1.Whens[i].Match, c2.Whens[i].Match) || !AreEqual(c1.Whens[i].Value, c2.Whens[i].Value)) { return(false); } } return(AreEqual(c1.Else, c2.Else)); } case SqlNodeType.ClientCase: { SqlClientCase c1 = (SqlClientCase)node1; SqlClientCase c2 = (SqlClientCase)node2; if (c1.Whens.Count != c2.Whens.Count) { return(false); } for (int i = 0, n = c1.Whens.Count; i < n; i++) { if (!AreEqual(c1.Whens[i].Match, c2.Whens[i].Match) || !AreEqual(c1.Whens[i].Value, c2.Whens[i].Value)) { return(false); } } return(true); } case SqlNodeType.DiscriminatedType: { SqlDiscriminatedType dt1 = (SqlDiscriminatedType)node1; SqlDiscriminatedType dt2 = (SqlDiscriminatedType)node2; return(AreEqual(dt1.Discriminator, dt2.Discriminator)); } case SqlNodeType.SimpleCase: { SqlSimpleCase c1 = (SqlSimpleCase)node1; SqlSimpleCase c2 = (SqlSimpleCase)node2; if (c1.Whens.Count != c2.Whens.Count) { return(false); } for (int i = 0, n = c1.Whens.Count; i < n; i++) { if (!AreEqual(c1.Whens[i].Match, c2.Whens[i].Match) || !AreEqual(c1.Whens[i].Value, c2.Whens[i].Value)) { return(false); } } return(true); } case SqlNodeType.Like: { SqlLike like1 = (SqlLike)node1; SqlLike like2 = (SqlLike)node2; return(AreEqual(like1.Expression, like2.Expression) && AreEqual(like1.Pattern, like2.Pattern) && AreEqual(like1.Escape, like2.Escape)); } case SqlNodeType.Variable: { SqlVariable v1 = (SqlVariable)node1; SqlVariable v2 = (SqlVariable)node2; return(v1.Name == v2.Name); } case SqlNodeType.FunctionCall: { SqlFunctionCall f1 = (SqlFunctionCall)node1; SqlFunctionCall f2 = (SqlFunctionCall)node2; if (f1.Name != f2.Name) { return(false); } if (f1.Arguments.Count != f2.Arguments.Count) { return(false); } for (int i = 0, n = f1.Arguments.Count; i < n; i++) { if (!AreEqual(f1.Arguments[i], f2.Arguments[i])) { return(false); } } return(true); } case SqlNodeType.Link: { SqlLink l1 = (SqlLink)node1; SqlLink l2 = (SqlLink)node2; if (!MetaPosition.AreSameMember(l1.Member.Member, l2.Member.Member)) { return(false); } if (!AreEqual(l1.Expansion, l2.Expansion)) { return(false); } if (l1.KeyExpressions.Count != l2.KeyExpressions.Count) { return(false); } for (int i = 0, c = l1.KeyExpressions.Count; i < c; ++i) { if (!AreEqual(l1.KeyExpressions[i], l2.KeyExpressions[i])) { return(false); } } return(true); } case SqlNodeType.ExprSet: SqlExprSet es1 = (SqlExprSet)node1; SqlExprSet es2 = (SqlExprSet)node2; if (es1.Expressions.Count != es2.Expressions.Count) { return(false); } for (int i = 0, n = es1.Expressions.Count; i < n; i++) { if (!AreEqual(es1.Expressions[i], es2.Expressions[i])) { return(false); } } return(true); case SqlNodeType.OptionalValue: SqlOptionalValue ov1 = (SqlOptionalValue)node1; SqlOptionalValue ov2 = (SqlOptionalValue)node2; return(AreEqual(ov1.Value, ov2.Value)); case SqlNodeType.Row: case SqlNodeType.UserQuery: case SqlNodeType.StoredProcedureCall: case SqlNodeType.UserRow: case SqlNodeType.UserColumn: case SqlNodeType.Multiset: case SqlNodeType.ScalarSubSelect: case SqlNodeType.Element: case SqlNodeType.Exists: case SqlNodeType.Join: case SqlNodeType.Select: case SqlNodeType.New: case SqlNodeType.ClientQuery: case SqlNodeType.ClientArray: case SqlNodeType.Insert: case SqlNodeType.Update: case SqlNodeType.Delete: case SqlNodeType.MemberAssign: case SqlNodeType.Assign: case SqlNodeType.Block: case SqlNodeType.Union: case SqlNodeType.DoNotVisit: case SqlNodeType.MethodCall: case SqlNodeType.Nop: default: return(false); } }