Exemplo n.º 1
0
 internal virtual SqlExpression VisitSimpleCase(SqlSimpleCase c)
 {
     c.Expression = this.VisitExpression(c.Expression);
     for (int i = 0, n = c.Whens.Count; i < n; i++)
     {
         SqlWhen when = c.Whens[i];
         when.Match = this.VisitExpression(when.Match);
         when.Value = this.VisitExpression(when.Value);
     }
     return(c);
 }
Exemplo n.º 2
0
 internal virtual SqlExpression VisitSearchedCase(SqlSearchedCase c)
 {
     for (int i = 0, n = c.Whens.Count; i < n; i++)
     {
         SqlWhen when = c.Whens[i];
         when.Match = this.VisitExpression(when.Match);
         when.Value = this.VisitExpression(when.Value);
     }
     c.Else = this.VisitExpression(c.Else);
     return(c);
 }
            internal override SqlExpression VisitSimpleCase(SqlSimpleCase c)
            {
                SqlExpression expr = this.VisitExpression(c.Expression);

                SqlWhen[] whens = new SqlWhen[c.Whens.Count];
                for (int i = 0, n = whens.Length; i < n; i++)
                {
                    SqlWhen when = c.Whens[i];
                    whens[i] = new SqlWhen(this.VisitExpression(when.Match), this.VisitExpression(when.Value));
                }
                return(new SqlSimpleCase(c.ClrType, expr, whens, c.SourceExpression));
            }
Exemplo n.º 4
0
            internal override SqlExpression VisitSimpleCase(SqlSimpleCase c)
            {
                SqlExpression expr  = VisitExpression(c.Expression);
                var           array = new SqlWhen[c.Whens.Count];
                var           i     = 0;

                for (var num = array.Length; i < num; i++)
                {
                    var sqlWhen = c.Whens[i];
                    array[i] = new SqlWhen(VisitExpression(sqlWhen.Match), VisitExpression(sqlWhen.Value));
                }
                return(new SqlSimpleCase(c.ClrType, expr, array, c.SourceExpression));
            }
Exemplo n.º 5
0
            internal override SqlExpression VisitSimpleCase(SqlSimpleCase c)
            {
                c.Expression = this.VisitExpression(c.Expression);
                int compareWhen = 0;

                // Find the ELSE if it exists.
                for (int i = 0, n = c.Whens.Count; i < n; i++)
                {
                    if (c.Whens[i].Match == null)
                    {
                        compareWhen = i;
                        break;
                    }
                }

                c.Whens[compareWhen].Match = VisitExpression(c.Whens[compareWhen].Match);
                c.Whens[compareWhen].Value = VisitExpression(c.Whens[compareWhen].Value);

                // Compare each other when value to the compare when
                List <SqlWhen> newWhens         = new List <SqlWhen>();
                bool           allValuesLiteral = true;

                for (int i = 0, n = c.Whens.Count; i < n; i++)
                {
                    if (compareWhen != i)
                    {
                        SqlWhen when = c.Whens[i];
                        when.Match = this.VisitExpression(when.Match);
                        when.Value = this.VisitExpression(when.Value);
                        if (!SqlComparer.AreEqual(c.Whens[compareWhen].Value, when.Value))
                        {
                            newWhens.Add(when);
                        }
                        allValuesLiteral = allValuesLiteral && when.Value.NodeType == SqlNodeType.Value;
                    }
                }
                newWhens.Add(c.Whens[compareWhen]);

                // Did everything reduce to a single CASE?
                SqlExpression rewrite = TryToConsolidateAllValueExpressions(newWhens.Count, c.Whens[compareWhen].Value);

                if (rewrite != null)
                {
                    return(rewrite);
                }

                // Can it be a conjuction (or disjunction) of clauses?
                rewrite = TryToWriteAsSimpleBooleanExpression(c.ClrType, c.Expression, newWhens, allValuesLiteral);
                if (rewrite != null)
                {
                    return(rewrite);
                }

                // Can any WHEN clauses be reduced to fall into the ELSE clause?
                rewrite = TryToWriteAsReducedCase(c.ClrType, c.Expression, newWhens, c.Whens[compareWhen].Match, c.Whens.Count);
                if (rewrite != null)
                {
                    return(rewrite);
                }

                return(c);
            }