Ejemplo n.º 1
0
        internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov)
        {
            SqlExpression hasValue = this.VisitExpression(sov.HasValue);
            SqlExpression value    = this.VisitExpression(sov.Value);

            return(new SqlOptionalValue(hasValue, value));
        }
        internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov)
        {
            bool saveCanJoin = this.canJoin;

            this.canJoin = false;
            try
            {
                return(base.VisitOptionalValue(sov));
            }
            finally
            {
                this.canJoin = saveCanJoin;
            }
        }
Ejemplo n.º 3
0
 internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     if (this.isDebugMode) {
         sb.Append("opt(");
         this.Visit(sov.HasValue);
         sb.Append(", ");
         this.Visit(sov.Value);
         sb.Append(")");
         return sov;
     } else {
         throw Error.InvalidFormatNode("OptionalValue");
     }
 }
Ejemplo n.º 4
0
        private void FlattenGroupByExpression(List <SqlExpression> exprs, SqlExpression expr)
        {
            SqlNew sn = expr as SqlNew;

            if (sn != null)
            {
                foreach (SqlMemberAssign ma in sn.Members)
                {
                    this.FlattenGroupByExpression(exprs, ma.Expression);
                }
                foreach (SqlExpression arg in sn.Args)
                {
                    this.FlattenGroupByExpression(exprs, arg);
                }
            }
            else if (expr.NodeType == SqlNodeType.TypeCase)
            {
                SqlTypeCase tc = (SqlTypeCase)expr;
                this.FlattenGroupByExpression(exprs, tc.Discriminator);
                foreach (SqlTypeCaseWhen when in tc.Whens)
                {
                    this.FlattenGroupByExpression(exprs, when.TypeBinding);
                }
            }
            else if (expr.NodeType == SqlNodeType.Link)
            {
                SqlLink link = (SqlLink)expr;
                if (link.Expansion != null)
                {
                    this.FlattenGroupByExpression(exprs, link.Expansion);
                }
                else
                {
                    foreach (SqlExpression key in link.KeyExpressions)
                    {
                        this.FlattenGroupByExpression(exprs, key);
                    }
                }
            }
            else if (expr.NodeType == SqlNodeType.OptionalValue)
            {
                SqlOptionalValue sop = (SqlOptionalValue)expr;
                this.FlattenGroupByExpression(exprs, sop.HasValue);
                this.FlattenGroupByExpression(exprs, sop.Value);
            }
            else if (expr.NodeType == SqlNodeType.OuterJoinedValue)
            {
                this.FlattenGroupByExpression(exprs, ((SqlUnary)expr).Operand);
            }
            else if (expr.NodeType == SqlNodeType.DiscriminatedType)
            {
                SqlDiscriminatedType dt = (SqlDiscriminatedType)expr;
                this.FlattenGroupByExpression(exprs, dt.Discriminator);
            }
            else
            {
                // this expression should have been 'pushed-down' in SqlBinder, so we
                // should only find column-references & expr-sets unless the expression could not
                // be columnized (in which case it was a bad group-by expression.)
                if (expr.NodeType != SqlNodeType.ColumnRef &&
                    expr.NodeType != SqlNodeType.ExprSet)
                {
                    if (!expr.SqlType.CanBeColumn)
                    {
                        throw Error.InvalidGroupByExpressionType(expr.SqlType.ToQueryString());
                    }
                    throw Error.InvalidGroupByExpression();
                }
                exprs.Add(expr);
            }
        }
Ejemplo n.º 5
0
 internal virtual SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     sov.HasValue = this.VisitExpression(sov.HasValue);
     sov.Value = this.VisitExpression(sov.Value);
     return sov;
 }
Ejemplo n.º 6
0
 internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     bool saveCanJoin = this.canJoin;
     this.canJoin = false;
     try {
         return base.VisitOptionalValue(sov);
     }
     finally {
         this.canJoin = saveCanJoin;
     }
 }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
 internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     SqlExpression hasValue = this.VisitExpression(sov.HasValue);
     SqlExpression value = this.VisitExpression(sov.Value);
     return new SqlOptionalValue(hasValue, value);
 }
Ejemplo n.º 9
0
        private SqlSelect VisitDefaultIfEmpty(Expression sequence) {
            SqlSelect select = this.VisitSequence(sequence);
            SqlAlias alias = new SqlAlias(select);
            SqlAliasRef aliasRef = new SqlAliasRef(alias);

            SqlExpression opt = new SqlOptionalValue(
                new SqlColumn(
                    "test",
                    sql.Unary(SqlNodeType.OuterJoinedValue,
                        sql.Value(typeof(int?), this.typeProvider.From(typeof(int)), 1, false, this.dominatingExpression)
                        )
                    ),
                    sql.Unary(SqlNodeType.OuterJoinedValue, aliasRef)
                );
            SqlSelect optSelect = new SqlSelect(opt, alias, this.dominatingExpression);

            alias = new SqlAlias(optSelect);
            aliasRef = new SqlAliasRef(alias);

            SqlExpression litNull = sql.TypedLiteralNull(typeof(string), this.dominatingExpression);
            SqlSelect selNull = new SqlSelect(litNull, null, this.dominatingExpression);
            SqlAlias aliasNull = new SqlAlias(selNull);

            SqlJoin join = new SqlJoin(SqlJoinType.OuterApply, aliasNull, alias, null, this.dominatingExpression);

            return new SqlSelect(aliasRef, join, this.dominatingExpression);
        }
Ejemplo n.º 10
0
 internal virtual SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     sov.HasValue = this.VisitExpression(sov.HasValue);
     sov.Value = this.VisitExpression(sov.Value);
     return sov;
 }
Ejemplo n.º 11
0
 internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov) {
     sov.HasValue = this.VisitNamedExpression(sov.HasValue, "test");
     sov.Value = this.VisitExpression(sov.Value);
     return sov;
 }
Ejemplo n.º 12
0
        private SqlExpression ExpandTogether(List <SqlExpression> exprs)
        {
            switch (exprs[0].NodeType)
            {
            case SqlNodeType.MethodCall:
            {
                SqlMethodCall[] mcs = new SqlMethodCall[exprs.Count];
                for (int i = 0; i < mcs.Length; ++i)
                {
                    mcs[i] = (SqlMethodCall)exprs[i];
                }

                List <SqlExpression> expandedArgs = new List <SqlExpression>();

                for (int i = 0; i < mcs[0].Arguments.Count; ++i)
                {
                    List <SqlExpression> args = new List <SqlExpression>();
                    for (int j = 0; j < mcs.Length; ++j)
                    {
                        args.Add(mcs[j].Arguments[i]);
                    }
                    SqlExpression expanded = this.ExpandTogether(args);
                    expandedArgs.Add(expanded);
                }
                return(factory.MethodCall(mcs[0].Method, mcs[0].Object, expandedArgs.ToArray(), mcs[0].SourceExpression));
            }

            case SqlNodeType.ClientCase:
            {
                // Are they all the same?
                SqlClientCase[] scs = new SqlClientCase[exprs.Count];
                scs[0] = (SqlClientCase)exprs[0];
                for (int i = 1; i < scs.Length; ++i)
                {
                    scs[i] = (SqlClientCase)exprs[i];
                }

                // Expand expressions together.
                List <SqlExpression> expressions = new List <SqlExpression>();
                for (int i = 0; i < scs.Length; ++i)
                {
                    expressions.Add(scs[i].Expression);
                }
                SqlExpression expression = this.ExpandTogether(expressions);

                // Expand individual expressions together.
                List <SqlClientWhen> whens = new List <SqlClientWhen>();
                for (int i = 0; i < scs[0].Whens.Count; ++i)
                {
                    List <SqlExpression> scos = new List <SqlExpression>();
                    for (int j = 0; j < scs.Length; ++j)
                    {
                        SqlClientWhen when = scs[j].Whens[i];
                        scos.Add(when.Value);
                    }
                    whens.Add(new SqlClientWhen(scs[0].Whens[i].Match, this.ExpandTogether(scos)));
                }

                return(new SqlClientCase(scs[0].ClrType, expression, whens, scs[0].SourceExpression));
            }

            case SqlNodeType.TypeCase:
            {
                // Are they all the same?
                SqlTypeCase[] tcs = new SqlTypeCase[exprs.Count];
                tcs[0] = (SqlTypeCase)exprs[0];
                for (int i = 1; i < tcs.Length; ++i)
                {
                    tcs[i] = (SqlTypeCase)exprs[i];
                }

                // Expand discriminators together.
                List <SqlExpression> discriminators = new List <SqlExpression>();
                for (int i = 0; i < tcs.Length; ++i)
                {
                    discriminators.Add(tcs[i].Discriminator);
                }
                SqlExpression discriminator = this.ExpandTogether(discriminators);
                // Write expanded discriminators back in.
                for (int i = 0; i < tcs.Length; ++i)
                {
                    tcs[i].Discriminator = discriminators[i];
                }
                // Expand individual type bindings together.
                List <SqlTypeCaseWhen> whens = new List <SqlTypeCaseWhen>();
                for (int i = 0; i < tcs[0].Whens.Count; ++i)
                {
                    List <SqlExpression> scos = new List <SqlExpression>();
                    for (int j = 0; j < tcs.Length; ++j)
                    {
                        SqlTypeCaseWhen when = tcs[j].Whens[i];
                        scos.Add(when.TypeBinding);
                    }
                    SqlExpression expanded = this.ExpandTogether(scos);
                    whens.Add(new SqlTypeCaseWhen(tcs[0].Whens[i].Match, expanded));
                }

                return(factory.TypeCase(tcs[0].ClrType, tcs[0].RowType, discriminator, whens, tcs[0].SourceExpression));
            }

            case SqlNodeType.New:
            {
                // first verify all are similar client objects...
                SqlNew[] cobs = new SqlNew[exprs.Count];
                cobs[0] = (SqlNew)exprs[0];
                for (int i = 1, n = exprs.Count; i < n; i++)
                {
                    if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.New)
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                    cobs[i] = (SqlNew)exprs[1];
                    if (cobs[i].Members.Count != cobs[0].Members.Count)
                    {
                        throw Error.UnionDifferentMembers();
                    }
                    for (int m = 0, mn = cobs[0].Members.Count; m < mn; m++)
                    {
                        if (cobs[i].Members[m].Member != cobs[0].Members[m].Member)
                        {
                            throw Error.UnionDifferentMemberOrder();
                        }
                    }
                }
                SqlMemberAssign[] bindings = new SqlMemberAssign[cobs[0].Members.Count];
                for (int m = 0, mn = bindings.Length; m < mn; m++)
                {
                    List <SqlExpression> mexprs = new List <SqlExpression>();
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        mexprs.Add(cobs[i].Members[m].Expression);
                    }
                    bindings[m] = new SqlMemberAssign(cobs[0].Members[m].Member, this.ExpandTogether(mexprs));
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        cobs[i].Members[m].Expression = mexprs[i];
                    }
                }
                SqlExpression[] arguments = new SqlExpression[cobs[0].Args.Count];
                for (int m = 0, mn = arguments.Length; m < mn; ++m)
                {
                    List <SqlExpression> mexprs = new List <SqlExpression>();
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        mexprs.Add(cobs[i].Args[m]);
                    }
                    arguments[m] = ExpandTogether(mexprs);
                }
                return(factory.New(cobs[0].MetaType, cobs[0].Constructor, arguments, cobs[0].ArgMembers, bindings, exprs[0].SourceExpression));
            }

            case SqlNodeType.Link:
            {
                SqlLink[] links = new SqlLink[exprs.Count];
                links[0] = (SqlLink)exprs[0];
                for (int i = 1, n = exprs.Count; i < n; i++)
                {
                    if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.Link)
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                    links[i] = (SqlLink)exprs[i];
                    if (links[i].KeyExpressions.Count != links[0].KeyExpressions.Count ||
                        links[i].Member != links[0].Member ||
                        (links[i].Expansion != null) != (links[0].Expansion != null))
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                }
                SqlExpression[]      kexprs = new SqlExpression[links[0].KeyExpressions.Count];
                List <SqlExpression> lexprs = new List <SqlExpression>();
                for (int k = 0, nk = links[0].KeyExpressions.Count; k < nk; k++)
                {
                    lexprs.Clear();
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        lexprs.Add(links[i].KeyExpressions[k]);
                    }
                    kexprs[k] = this.ExpandTogether(lexprs);
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        links[i].KeyExpressions[k] = lexprs[i];
                    }
                }
                SqlExpression expansion = null;
                if (links[0].Expansion != null)
                {
                    lexprs.Clear();
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        lexprs.Add(links[i].Expansion);
                    }
                    expansion = this.ExpandTogether(lexprs);
                    for (int i = 0, n = exprs.Count; i < n; i++)
                    {
                        links[i].Expansion = lexprs[i];
                    }
                }
                return(new SqlLink(links[0].Id, links[0].RowType, links[0].ClrType, links[0].SqlType, links[0].Expression, links[0].Member, kexprs, expansion, links[0].SourceExpression));
            }

            case SqlNodeType.Value:
            {
                /*
                 * ExprSet of all literals of the same value reduce to just a single literal.
                 */
                SqlValue val0 = (SqlValue)exprs[0];
                for (int i = 1; i < exprs.Count; ++i)
                {
                    SqlValue val = (SqlValue)exprs[i];
                    if (!Equals(val.Value, val0.Value))
                    {
                        return(this.ExpandIntoExprSet(exprs));
                    }
                }
                return(val0);
            }

            case SqlNodeType.OptionalValue:
            {
                if (exprs[0].SqlType.CanBeColumn)
                {
                    goto default;
                }
                List <SqlExpression> hvals = new List <SqlExpression>(exprs.Count);
                List <SqlExpression> vals  = new List <SqlExpression>(exprs.Count);
                for (int i = 0, n = exprs.Count; i < n; i++)
                {
                    if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.OptionalValue)
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                    SqlOptionalValue sov = (SqlOptionalValue)exprs[i];
                    hvals.Add(sov.HasValue);
                    vals.Add(sov.Value);
                }
                return(new SqlOptionalValue(this.ExpandTogether(hvals), this.ExpandTogether(vals)));
            }

            case SqlNodeType.OuterJoinedValue:
            {
                if (exprs[0].SqlType.CanBeColumn)
                {
                    goto default;
                }
                List <SqlExpression> values = new List <SqlExpression>(exprs.Count);
                for (int i = 0, n = exprs.Count; i < n; i++)
                {
                    if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.OuterJoinedValue)
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                    SqlUnary su = (SqlUnary)exprs[i];
                    values.Add(su.Operand);
                }
                return(factory.Unary(SqlNodeType.OuterJoinedValue, this.ExpandTogether(values)));
            }

            case SqlNodeType.DiscriminatedType:
            {
                SqlDiscriminatedType sdt0 = (SqlDiscriminatedType)exprs[0];
                List <SqlExpression> foos = new List <SqlExpression>(exprs.Count);
                foos.Add(sdt0.Discriminator);
                for (int i = 1, n = exprs.Count; i < n; i++)
                {
                    SqlDiscriminatedType sdtN = (SqlDiscriminatedType)exprs[i];
                    if (sdtN.TargetType != sdt0.TargetType)
                    {
                        throw Error.UnionIncompatibleConstruction();
                    }
                    foos.Add(sdtN.Discriminator);
                }
                return(factory.DiscriminatedType(this.ExpandTogether(foos), ((SqlDiscriminatedType)exprs[0]).TargetType));
            }

            case SqlNodeType.ClientQuery:
            case SqlNodeType.Multiset:
            case SqlNodeType.Element:
            case SqlNodeType.Grouping:
                throw Error.UnionWithHierarchy();

            default:
                return(this.ExpandIntoExprSet(exprs));
            }
        }
Ejemplo n.º 13
0
        internal static bool AreEqual(SqlNode node1, SqlNode node2)
        {
            if (node1 == node2)
            {
                return(true);
            }
            if ((node1 != null) && (node2 != null))
            {
                if (node1.NodeType == SqlNodeType.SimpleCase)
                {
                    node1 = UnwrapTrivialCaseExpression((SqlSimpleCase)node1);
                }
                if (node2.NodeType == SqlNodeType.SimpleCase)
                {
                    node2 = UnwrapTrivialCaseExpression((SqlSimpleCase)node2);
                }
                if (node1.NodeType != node2.NodeType)
                {
                    if (node1.NodeType == SqlNodeType.ExprSet)
                    {
                        SqlExprSet set   = (SqlExprSet)node1;
                        int        num   = 0;
                        int        count = set.Expressions.Count;
                        while (num < count)
                        {
                            if (AreEqual(set.Expressions[num], node2))
                            {
                                return(true);
                            }
                            num++;
                        }
                    }
                    else if (node2.NodeType == SqlNodeType.ExprSet)
                    {
                        SqlExprSet set2 = (SqlExprSet)node2;
                        int        num3 = 0;
                        int        num4 = set2.Expressions.Count;
                        while (num3 < num4)
                        {
                            if (AreEqual(node1, set2.Expressions[num3]))
                            {
                                return(true);
                            }
                            num3++;
                        }
                    }
                    return(false);
                }
                if (node1.Equals(node2))
                {
                    return(true);
                }
                switch (node1.NodeType)
                {
                case SqlNodeType.Add:
                case SqlNodeType.And:
                case SqlNodeType.BitAnd:
                case SqlNodeType.BitOr:
                case SqlNodeType.BitXor:
                case SqlNodeType.Concat:
                case SqlNodeType.Div:
                case SqlNodeType.EQ:
                case SqlNodeType.EQ2V:
                case SqlNodeType.LE:
                case SqlNodeType.LT:
                case SqlNodeType.GE:
                case SqlNodeType.GT:
                case SqlNodeType.Mod:
                case SqlNodeType.Mul:
                case SqlNodeType.NE:
                case SqlNodeType.NE2V:
                case SqlNodeType.Or:
                case SqlNodeType.Sub:
                {
                    SqlBinary binary  = (SqlBinary)node1;
                    SqlBinary binary2 = (SqlBinary)node2;
                    if (!AreEqual(binary.Left, binary2.Left))
                    {
                        return(false);
                    }
                    return(AreEqual(binary.Right, binary2.Right));
                }

                case SqlNodeType.Alias:
                    return(AreEqual(((SqlAlias)node1).Node, ((SqlAlias)node2).Node));

                case SqlNodeType.AliasRef:
                    return(AreEqual(((SqlAliasRef)node1).Alias, ((SqlAliasRef)node2).Alias));

                case SqlNodeType.Avg:
                case SqlNodeType.BitNot:
                case SqlNodeType.ClrLength:
                case SqlNodeType.Count:
                case SqlNodeType.Covar:
                case SqlNodeType.IsNotNull:
                case SqlNodeType.IsNull:
                case SqlNodeType.Max:
                case SqlNodeType.Min:
                case SqlNodeType.Negate:
                case SqlNodeType.Not:
                case SqlNodeType.Not2V:
                case SqlNodeType.OuterJoinedValue:
                case SqlNodeType.Stddev:
                case SqlNodeType.Sum:
                case SqlNodeType.ValueOf:
                    return(AreEqual(((SqlUnary)node1).Operand, ((SqlUnary)node2).Operand));

                case SqlNodeType.Between:
                {
                    SqlBetween between  = (SqlBetween)node1;
                    SqlBetween between2 = (SqlBetween)node1;
                    if (!AreEqual(between.Expression, between2.Expression) ||
                        !AreEqual(between.Start, between2.Start))
                    {
                        return(false);
                    }
                    return(AreEqual(between.End, between2.End));
                }

                case SqlNodeType.Cast:
                case SqlNodeType.Convert:
                case SqlNodeType.Treat:
                {
                    SqlUnary unary  = (SqlUnary)node1;
                    SqlUnary unary2 = (SqlUnary)node2;
                    if ((unary.ClrType != unary2.ClrType) || !(unary.SqlType == unary2.SqlType))
                    {
                        return(false);
                    }
                    return(AreEqual(unary.Operand, unary2.Operand));
                }

                case SqlNodeType.ClientCase:
                {
                    SqlClientCase case5 = (SqlClientCase)node1;
                    SqlClientCase case6 = (SqlClientCase)node2;
                    if (case5.Whens.Count == case6.Whens.Count)
                    {
                        int num9  = 0;
                        int num10 = case5.Whens.Count;
                        while (num9 < num10)
                        {
                            if (!AreEqual(case5.Whens[num9].Match, case6.Whens[num9].Match) ||
                                !AreEqual(case5.Whens[num9].Value, case6.Whens[num9].Value))
                            {
                                return(false);
                            }
                            num9++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.Column:
                {
                    SqlColumn column  = (SqlColumn)node1;
                    SqlColumn column2 = (SqlColumn)node2;
                    return((column == column2) ||
                           (((column.Expression != null) && (column2.Expression != null)) &&
                            AreEqual(column.Expression, column2.Expression)));
                }

                case SqlNodeType.ColumnRef:
                {
                    SqlColumnRef cref = (SqlColumnRef)node1;
                    SqlColumnRef ref3 = (SqlColumnRef)node2;
                    return(GetBaseColumn(cref) == GetBaseColumn(ref3));
                }

                case SqlNodeType.DiscriminatedType:
                {
                    SqlDiscriminatedType type  = (SqlDiscriminatedType)node1;
                    SqlDiscriminatedType type2 = (SqlDiscriminatedType)node2;
                    return(AreEqual(type.Discriminator, type2.Discriminator));
                }

                case SqlNodeType.ExprSet:
                {
                    SqlExprSet set3 = (SqlExprSet)node1;
                    SqlExprSet set4 = (SqlExprSet)node2;
                    if (set3.Expressions.Count == set4.Expressions.Count)
                    {
                        int num17 = 0;
                        int num18 = set3.Expressions.Count;
                        while (num17 < num18)
                        {
                            if (!AreEqual(set3.Expressions[num17], set4.Expressions[num17]))
                            {
                                return(false);
                            }
                            num17++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.FunctionCall:
                {
                    SqlFunctionCall call  = (SqlFunctionCall)node1;
                    SqlFunctionCall call2 = (SqlFunctionCall)node2;
                    if (!(call.Name != call2.Name))
                    {
                        if (call.Arguments.Count != call2.Arguments.Count)
                        {
                            return(false);
                        }
                        int num13 = 0;
                        int num14 = call.Arguments.Count;
                        while (num13 < num14)
                        {
                            if (!AreEqual(call.Arguments[num13], call2.Arguments[num13]))
                            {
                                return(false);
                            }
                            num13++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.Link:
                {
                    SqlLink link  = (SqlLink)node1;
                    SqlLink link2 = (SqlLink)node2;
                    if (MetaPosition.AreSameMember(link.Member.Member, link2.Member.Member))
                    {
                        if (!AreEqual(link.Expansion, link2.Expansion))
                        {
                            return(false);
                        }
                        if (link.KeyExpressions.Count != link2.KeyExpressions.Count)
                        {
                            return(false);
                        }
                        int num15 = 0;
                        int num16 = link.KeyExpressions.Count;
                        while (num15 < num16)
                        {
                            if (!AreEqual(link.KeyExpressions[num15], link2.KeyExpressions[num15]))
                            {
                                return(false);
                            }
                            num15++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.Like:
                {
                    SqlLike like  = (SqlLike)node1;
                    SqlLike like2 = (SqlLike)node2;
                    if (!AreEqual(like.Expression, like2.Expression) || !AreEqual(like.Pattern, like2.Pattern))
                    {
                        return(false);
                    }
                    return(AreEqual(like.Escape, like2.Escape));
                }

                case SqlNodeType.Member:
                    if (((SqlMember)node1).Member != ((SqlMember)node2).Member)
                    {
                        return(false);
                    }
                    return(AreEqual(((SqlMember)node1).Expression, ((SqlMember)node2).Expression));

                case SqlNodeType.OptionalValue:
                {
                    SqlOptionalValue value2 = (SqlOptionalValue)node1;
                    SqlOptionalValue value3 = (SqlOptionalValue)node2;
                    return(AreEqual(value2.Value, value3.Value));
                }

                case SqlNodeType.Parameter:
                    return(node1 == node2);

                case SqlNodeType.SearchedCase:
                {
                    SqlSearchedCase case3 = (SqlSearchedCase)node1;
                    SqlSearchedCase case4 = (SqlSearchedCase)node2;
                    if (case3.Whens.Count == case4.Whens.Count)
                    {
                        int num7 = 0;
                        int num8 = case3.Whens.Count;
                        while (num7 < num8)
                        {
                            if (!AreEqual(case3.Whens[num7].Match, case4.Whens[num7].Match) ||
                                !AreEqual(case3.Whens[num7].Value, case4.Whens[num7].Value))
                            {
                                return(false);
                            }
                            num7++;
                        }
                        return(AreEqual(case3.Else, case4.Else));
                    }
                    return(false);
                }

                case SqlNodeType.SimpleCase:
                {
                    SqlSimpleCase case7 = (SqlSimpleCase)node1;
                    SqlSimpleCase case8 = (SqlSimpleCase)node2;
                    if (case7.Whens.Count == case8.Whens.Count)
                    {
                        int num11 = 0;
                        int num12 = case7.Whens.Count;
                        while (num11 < num12)
                        {
                            if (!AreEqual(case7.Whens[num11].Match, case8.Whens[num11].Match) ||
                                !AreEqual(case7.Whens[num11].Value, case8.Whens[num11].Value))
                            {
                                return(false);
                            }
                            num11++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.Table:
                    return(((SqlTable)node1).MetaTable == ((SqlTable)node2).MetaTable);

                case SqlNodeType.TypeCase:
                {
                    SqlTypeCase @case = (SqlTypeCase)node1;
                    SqlTypeCase case2 = (SqlTypeCase)node2;
                    if (AreEqual(@case.Discriminator, case2.Discriminator))
                    {
                        if (@case.Whens.Count != case2.Whens.Count)
                        {
                            return(false);
                        }
                        int num5 = 0;
                        int num6 = @case.Whens.Count;
                        while (num5 < num6)
                        {
                            if (!AreEqual(@case.Whens[num5].Match, case2.Whens[num5].Match))
                            {
                                return(false);
                            }
                            if (!AreEqual(@case.Whens[num5].TypeBinding, case2.Whens[num5].TypeBinding))
                            {
                                return(false);
                            }
                            num5++;
                        }
                        return(true);
                    }
                    return(false);
                }

                case SqlNodeType.Variable:
                {
                    SqlVariable variable  = (SqlVariable)node1;
                    SqlVariable variable2 = (SqlVariable)node2;
                    return(variable.Name == variable2.Name);
                }

                case SqlNodeType.Value:
                    return(object.Equals(((SqlValue)node1).Value, ((SqlValue)node2).Value));
                }
            }
            return(false);
        }
        internal static bool AreSimilar(SqlExpression node1, SqlExpression node2)
        {
            if (node1 == node2)
            {
                return(true);
            }
            if (node1 == null || node2 == null)
            {
                return(false);
            }
            if (node1.NodeType != node2.NodeType ||
                node1.ClrType != node2.ClrType ||
                node1.SqlType != node2.SqlType)
            {
                return(false);
            }
            switch (node1.NodeType)
            {
            case SqlNodeType.New:
            {
                SqlNew new1 = (SqlNew)node1;
                SqlNew new2 = (SqlNew)node2;
                if (new1.Args.Count != new2.Args.Count ||
                    new1.Members.Count != new2.Members.Count)
                {
                    return(false);
                }
                for (int i = 0, n = new1.Args.Count; i < n; i++)
                {
                    if (!AreSimilar(new1.Args[i], new2.Args[i]))
                    {
                        return(false);
                    }
                }
                for (int i = 0, n = new1.Members.Count; i < n; i++)
                {
                    if (!MetaPosition.AreSameMember(new1.Members[i].Member, new2.Members[i].Member) ||
                        !AreSimilar(new1.Members[i].Expression, new2.Members[i].Expression))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.ColumnRef:
            {
                SqlColumnRef cref1 = (SqlColumnRef)node1;
                SqlColumnRef cref2 = (SqlColumnRef)node2;
                return(cref1.Column.Ordinal == cref2.Column.Ordinal);
            }

            case SqlNodeType.Link:
            {
                SqlLink l1 = (SqlLink)node1;
                SqlLink l2 = (SqlLink)node2;
                if (!MetaPosition.AreSameMember(l1.Member.Member, l2.Member.Member))
                {
                    return(false);
                }
                if (l1.KeyExpressions.Count != l2.KeyExpressions.Count)
                {
                    return(false);
                }
                for (int i = 0, c = l1.KeyExpressions.Count; i < c; ++i)
                {
                    if (!AreSimilar(l1.KeyExpressions[i], l2.KeyExpressions[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.Value:
                return(Object.Equals(((SqlValue)node1).Value, ((SqlValue)node2).Value));

            case SqlNodeType.OptionalValue:
            {
                SqlOptionalValue ov1 = (SqlOptionalValue)node1;
                SqlOptionalValue ov2 = (SqlOptionalValue)node2;
                return(AreSimilar(ov1.Value, ov2.Value));
            }

            case SqlNodeType.ValueOf:
            case SqlNodeType.OuterJoinedValue:
                return(AreSimilar(((SqlUnary)node1).Operand, ((SqlUnary)node2).Operand));

            case SqlNodeType.Lift:
                return(AreSimilar(((SqlLift)node1).Expression, ((SqlLift)node2).Expression));

            case SqlNodeType.Grouping:
            {
                SqlGrouping g1 = (SqlGrouping)node1;
                SqlGrouping g2 = (SqlGrouping)node2;
                return(AreSimilar(g1.Key, g2.Key) && AreSimilar(g1.Group, g2.Group));
            }

            case SqlNodeType.ClientArray:
            {
                SqlClientArray a1 = (SqlClientArray)node1;
                SqlClientArray a2 = (SqlClientArray)node2;
                if (a1.Expressions.Count != a2.Expressions.Count)
                {
                    return(false);
                }
                for (int i = 0, n = a1.Expressions.Count; i < n; i++)
                {
                    if (!AreSimilar(a1.Expressions[i], a2.Expressions[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.UserColumn:
                return(((SqlUserColumn)node1).Name == ((SqlUserColumn)node2).Name);

            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 (!AreSimilar(c1.Whens[i].Match, c2.Whens[i].Match) ||
                        !AreSimilar(c1.Whens[i].Value, c2.Whens[i].Value))
                    {
                        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 (!AreSimilar(c1.Whens[i].Match, c2.Whens[i].Match) ||
                        !AreSimilar(c1.Whens[i].Value, c2.Whens[i].Value))
                    {
                        return(false);
                    }
                }
                return(AreSimilar(c1.Else, c2.Else));
            }

            case SqlNodeType.TypeCase:
            {
                SqlTypeCase c1 = (SqlTypeCase)node1;
                SqlTypeCase c2 = (SqlTypeCase)node2;
                if (!AreSimilar(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 (!AreSimilar(c1.Whens[i].Match, c2.Whens[i].Match))
                    {
                        return(false);
                    }
                    if (!AreSimilar(c1.Whens[i].TypeBinding, c2.Whens[i].TypeBinding))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.DiscriminatedType:
            {
                SqlDiscriminatedType dt1 = (SqlDiscriminatedType)node1;
                SqlDiscriminatedType dt2 = (SqlDiscriminatedType)node2;
                return(AreSimilar(dt1.Discriminator, dt2.Discriminator));
            }

            case SqlNodeType.JoinedCollection:
            {
                SqlJoinedCollection j1 = (SqlJoinedCollection)node1;
                SqlJoinedCollection j2 = (SqlJoinedCollection)node2;
                return(AreSimilar(j1.Count, j2.Count) && AreSimilar(j1.Expression, j2.Expression));
            }

            case SqlNodeType.Member:
            {
                SqlMember m1 = (SqlMember)node1;
                SqlMember m2 = (SqlMember)node2;
                return(m1.Member == m2.Member && AreSimilar(m1.Expression, m2.Expression));
            }

            case SqlNodeType.ClientQuery:
            {
                SqlClientQuery cq1 = (SqlClientQuery)node1;
                SqlClientQuery cq2 = (SqlClientQuery)node2;
                if (cq1.Arguments.Count != cq2.Arguments.Count)
                {
                    return(false);
                }
                for (int i = 0, n = cq1.Arguments.Count; i < n; i++)
                {
                    if (!AreSimilar(cq1.Arguments[i], cq2.Arguments[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.MethodCall:
            {
                SqlMethodCall mc1 = (SqlMethodCall)node1;
                SqlMethodCall mc2 = (SqlMethodCall)node2;
                if (mc1.Method != mc2.Method || !AreSimilar(mc1.Object, mc2.Object))
                {
                    return(false);
                }
                if (mc1.Arguments.Count != mc2.Arguments.Count)
                {
                    return(false);
                }
                for (int i = 0, n = mc1.Arguments.Count; i < n; i++)
                {
                    if (!AreSimilar(mc1.Arguments[i], mc2.Arguments[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case SqlNodeType.ClientParameter:
            default:
                return(false);
            }
        }
Ejemplo n.º 15
0
 internal override SqlExpression VisitOptionalValue(SqlOptionalValue sov)
 {
     sov.HasValue = this.VisitNamedExpression(sov.HasValue, "test");
     sov.Value    = this.VisitExpression(sov.Value);
     return(sov);
 }