2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / Sugar / ExpressionMutator / Implementation / BinaryExpressionMutator.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \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
13 // \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
16 // \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
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Collections.Generic;\r
29 using System.Linq;\r
30 using System.Linq.Expressions;\r
31 \r
32 using DbLinq.Data.Linq.Sugar.Expressions;\r
33 \r
34 namespace DbLinq.Data.Linq.Sugar.ExpressionMutator.Implementation\r
35 {\r
36     internal class BinaryExpressionMutator : IMutableExpression\r
37     {\r
38         protected BinaryExpression BinaryExpression { get; private set; }\r
39 \r
40         public Expression Mutate(IList<Expression> operands)\r
41         {\r
42             switch (BinaryExpression.NodeType)\r
43             {\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
48 \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
53 \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
58 \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
63 \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
68 \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
73 \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
78 \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
83 \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
88 \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
93 \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
98 \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
103 \r
104 \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
109 \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
114 \r
115 \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
120 \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
125 \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
130 \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
135 \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
140 \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
145 \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
150 \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
155 \r
156 \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
161 \r
162                 case ExpressionType.ArrayIndex:\r
163                     return Expression.ArrayIndex(operands[0], operands.Skip(1));\r
164             }\r
165             throw new Exception();\r
166         }\r
167 \r
168         public IEnumerable<Expression> Operands\r
169         {\r
170             get \r
171             {\r
172                 yield return BinaryExpression.Left;\r
173                 yield return BinaryExpression.Right;\r
174             }\r
175         }\r
176 \r
177         public BinaryExpressionMutator(BinaryExpression expression)\r
178         {\r
179             BinaryExpression = expression;\r
180         }\r
181     }\r
182 }