5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
30 using System.Linq.Expressions;
\r
32 using DbLinq.Data.Linq.Sugar.Expressions;
\r
34 namespace DbLinq.Data.Linq.Sugar.ExpressionMutator.Implementation
\r
36 internal class BinaryExpressionMutator : IMutableExpression
\r
38 protected BinaryExpression BinaryExpression { get; private set; }
\r
40 public Expression Mutate(IList<Expression> operands)
\r
42 switch (BinaryExpression.NodeType)
\r
44 case ExpressionType.Add:
\r
45 if (BinaryExpression.Method != null)
\r
46 return Expression.Add(operands[0], operands[1], BinaryExpression.Method);
\r
47 return Expression.Add(operands[0], operands[1]);
\r
49 case ExpressionType.AddChecked:
\r
50 if (BinaryExpression.Method != null)
\r
51 return Expression.AddChecked(operands[0], operands[1], BinaryExpression.Method);
\r
52 return Expression.AddChecked(operands[0], operands[1]);
\r
54 case ExpressionType.Divide:
\r
55 if (BinaryExpression.Method != null)
\r
56 return Expression.Divide(operands[0], operands[1], BinaryExpression.Method);
\r
57 return Expression.Divide(operands[0], operands[1]);
\r
59 case ExpressionType.Modulo:
\r
60 if (BinaryExpression.Method != null)
\r
61 return Expression.Modulo(operands[0], operands[1], BinaryExpression.Method);
\r
62 return Expression.Modulo(operands[0], operands[1]);
\r
64 case ExpressionType.Multiply:
\r
65 if (BinaryExpression.Method != null)
\r
66 return Expression.Multiply(operands[0], operands[1], BinaryExpression.Method);
\r
67 return Expression.Multiply(operands[0], operands[1]);
\r
69 case ExpressionType.MultiplyChecked:
\r
70 if (BinaryExpression.Method != null)
\r
71 return Expression.MultiplyChecked(operands[0], operands[1], BinaryExpression.Method);
\r
72 return Expression.MultiplyChecked(operands[0], operands[1]);
\r
74 case ExpressionType.Power:
\r
75 if (BinaryExpression.Method != null)
\r
76 return Expression.Power(operands[0], operands[1], BinaryExpression.Method);
\r
77 return Expression.Power(operands[0], operands[1]);
\r
79 case ExpressionType.Subtract:
\r
80 if (BinaryExpression.Method != null)
\r
81 return Expression.Subtract(operands[0], operands[1], BinaryExpression.Method);
\r
82 return Expression.Subtract(operands[0], operands[1]);
\r
84 case ExpressionType.SubtractChecked:
\r
85 if (BinaryExpression.Method != null)
\r
86 return Expression.SubtractChecked(operands[0], operands[1], BinaryExpression.Method);
\r
87 return Expression.SubtractChecked(operands[0], operands[1]);
\r
89 case ExpressionType.And:
\r
90 if (BinaryExpression.Method != null)
\r
91 return Expression.And(operands[0], operands[1], BinaryExpression.Method);
\r
92 return Expression.And(operands[0], operands[1]);
\r
94 case ExpressionType.Or:
\r
95 if (BinaryExpression.Method != null)
\r
96 return Expression.Or(operands[0], operands[1], BinaryExpression.Method);
\r
97 return Expression.Or(operands[0], operands[1]);
\r
99 case ExpressionType.ExclusiveOr:
\r
100 if (BinaryExpression.Method != null)
\r
101 return Expression.ExclusiveOr(operands[0], operands[1], BinaryExpression.Method);
\r
102 return Expression.ExclusiveOr(operands[0], operands[1]);
\r
105 case ExpressionType.LeftShift:
\r
106 if (BinaryExpression.Method != null)
\r
107 return Expression.LeftShift(operands[0], operands[1], BinaryExpression.Method);
\r
108 return Expression.LeftShift(operands[0], operands[1]);
\r
110 case ExpressionType.RightShift:
\r
111 if (BinaryExpression.Method != null)
\r
112 return Expression.RightShift(operands[0], operands[1], BinaryExpression.Method);
\r
113 return Expression.RightShift(operands[0], operands[1]);
\r
116 case ExpressionType.AndAlso:
\r
117 if (BinaryExpression.Method != null)
\r
118 return Expression.AndAlso(operands[0], operands[1], BinaryExpression.Method);
\r
119 return Expression.AndAlso(operands[0], operands[1]);
\r
121 case ExpressionType.OrElse:
\r
122 if (BinaryExpression.Method != null)
\r
123 return Expression.OrElse(operands[0], operands[1], BinaryExpression.Method);
\r
124 return Expression.OrElse(operands[0], operands[1]);
\r
126 case ExpressionType.Equal:
\r
127 if (BinaryExpression.Method != null)
\r
128 return Expression.Equal(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
129 return Expression.Equal(operands[0], operands[1]);
\r
131 case ExpressionType.NotEqual:
\r
132 if (BinaryExpression.Method != null)
\r
133 return Expression.NotEqual(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
134 return Expression.NotEqual(operands[0], operands[1]);
\r
136 case ExpressionType.GreaterThanOrEqual:
\r
137 if (BinaryExpression.Method != null)
\r
138 return Expression.GreaterThanOrEqual(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
139 return Expression.GreaterThanOrEqual(operands[0], operands[1]);
\r
141 case ExpressionType.GreaterThan:
\r
142 if (BinaryExpression.Method != null)
\r
143 return Expression.GreaterThan(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
144 return Expression.GreaterThan(operands[0], operands[1]);
\r
146 case ExpressionType.LessThan:
\r
147 if (BinaryExpression.Method != null)
\r
148 return Expression.LessThan(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
149 return Expression.LessThan(operands[0], operands[1]);
\r
151 case ExpressionType.LessThanOrEqual:
\r
152 if (BinaryExpression.Method != null)
\r
153 return Expression.LessThanOrEqual(operands[0], operands[1], BinaryExpression.IsLiftedToNull, BinaryExpression.Method);
\r
154 return Expression.LessThanOrEqual(operands[0], operands[1]);
\r
157 case ExpressionType.Coalesce:
\r
158 if (BinaryExpression.Conversion != null)
\r
159 return Expression.Coalesce(operands[0], operands[1], BinaryExpression.Conversion);
\r
160 return Expression.Coalesce(operands[0], operands[1]);
\r
162 case ExpressionType.ArrayIndex:
\r
163 return Expression.ArrayIndex(operands[0], operands.Skip(1));
\r
165 throw new Exception();
\r
168 public IEnumerable<Expression> Operands
\r
172 yield return BinaryExpression.Left;
\r
173 yield return BinaryExpression.Right;
\r
177 public BinaryExpressionMutator(BinaryExpression expression)
\r
179 BinaryExpression = expression;
\r