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
33 using System.Data.Linq.Sugar;
\r
34 using System.Data.Linq.Sugar.ExpressionMutator;
\r
35 using System.Data.Linq.Sugar.Expressions;
\r
37 using DbLinq.Data.Linq.Sugar;
\r
38 using DbLinq.Data.Linq.Sugar.ExpressionMutator;
\r
39 using DbLinq.Data.Linq.Sugar.Expressions;
\r
43 namespace System.Data.Linq.Sugar.Implementation
\r
45 namespace DbLinq.Data.Linq.Sugar.Implementation
\r
48 internal class SpecialExpressionTranslator : ISpecialExpressionTranslator
\r
51 /// Translate a hierarchy's SpecialExpressions to Expressions
\r
53 /// <param name="expression"></param>
\r
54 /// <returns></returns>
\r
55 public Expression Translate(Expression expression)
\r
57 return expression.Recurse(Analyzer);
\r
60 protected virtual Expression Analyzer(Expression expression)
\r
62 if (expression is SpecialExpression)
\r
63 return Translate((SpecialExpression)expression);
\r
64 else if (expression is StartIndexOffsetExpression)
\r
65 return Translate(((StartIndexOffsetExpression)expression).InnerExpression);
\r
70 /// Translates a SpecialExpression to standard Expression equivalent
\r
72 /// <param name="specialExpression"></param>
\r
73 /// <returns></returns>
\r
74 protected virtual Expression Translate(SpecialExpression specialExpression)
\r
76 var operands = specialExpression.Operands.ToList();
\r
77 switch (specialExpression.SpecialNodeType) // SETuse
\r
79 case SpecialExpressionType.IsNull:
\r
80 return TranslateIsNull(operands);
\r
81 case SpecialExpressionType.IsNotNull:
\r
82 return TranslateIsNotNull(operands);
\r
83 case SpecialExpressionType.Concat:
\r
84 return TranslateConcat(operands);
\r
85 //case SpecialExpressionType.Count:
\r
87 //case SpecialExpressionType.Like:
\r
89 //case SpecialExpressionType.Min:
\r
91 //case SpecialExpressionType.Max:
\r
93 //case SpecialExpressionType.Sum:
\r
95 //case SpecialExpressionType.Average:
\r
97 case SpecialExpressionType.StringLength:
\r
98 return TranslateStringLength(operands);
\r
99 case SpecialExpressionType.ToUpper:
\r
100 return GetStandardCallInvoke("ToUpper", operands);
\r
101 case SpecialExpressionType.ToLower:
\r
102 return GetStandardCallInvoke("ToLower", operands);
\r
103 //case SpecialExpressionType.In:
\r
106 case SpecialExpressionType.StringInsert:
\r
107 return GetStandardCallInvoke("Insert", operands);
\r
108 case SpecialExpressionType.Substring:
\r
109 case SpecialExpressionType.Trim:
\r
110 case SpecialExpressionType.LTrim:
\r
111 case SpecialExpressionType.RTrim:
\r
112 case SpecialExpressionType.Replace:
\r
113 case SpecialExpressionType.Remove:
\r
114 case SpecialExpressionType.IndexOf:
\r
115 case SpecialExpressionType.Year:
\r
116 case SpecialExpressionType.Month:
\r
117 case SpecialExpressionType.Day:
\r
118 case SpecialExpressionType.Hour:
\r
119 case SpecialExpressionType.Minute:
\r
120 case SpecialExpressionType.Millisecond:
\r
121 return GetStandardCallInvoke(specialExpression.SpecialNodeType.ToString(), operands);
\r
122 case SpecialExpressionType.Now:
\r
123 return GetDateTimeNowCall(operands);
\r
124 case SpecialExpressionType.DateDiffInMilliseconds:
\r
125 return GetCallDateDiffInMilliseconds(operands);
\r
127 throw Error.BadArgument("S0078: Implement translator for {0}", specialExpression.SpecialNodeType);
\r
132 private Expression GetCallDateDiffInMilliseconds(List<Expression> operands)
\r
134 return Expression.MakeMemberAccess(Expression.Subtract(operands.First(), operands.ElementAt(1)),
\r
135 typeof(TimeSpan).GetProperty("TotalMilliseconds"));
\r
138 private Expression GetDateTimeNowCall(List<Expression> operands)
\r
140 return Expression.Call(typeof(DateTime).GetProperty("Now").GetGetMethod());
\r
143 private Expression TranslateStringLength(List<Expression> operands)
\r
145 return Expression.MakeMemberAccess(operands[0], typeof(string).GetProperty("Length"));
\r
148 protected virtual Expression GetStandardCallInvoke(string methodName, List<Expression> operands)
\r
150 var parametersExpressions = operands.Skip(1);
\r
151 return Expression.Call(operands[0],
\r
152 operands[0].Type.GetMethod(methodName, parametersExpressions.Select(op => op.Type).ToArray()),
\r
153 parametersExpressions);
\r
156 //protected virtual Expression TranslateRemove(List<Expression> operands)
\r
158 // if (operands.Count > 2)
\r
160 // return Expression.Call(operands[0],
\r
161 // typeof(string).GetMethod("Remove", new[] { typeof(int), typeof(int) }),
\r
162 // operands[1], operands[2]);
\r
164 // return Expression.Call(operands[0],
\r
165 // typeof(string).GetMethod("Remove", new[] { typeof(int) }),
\r
169 //protected virtual Expression TranslateStringIndexOf(List<Expression> operands)
\r
171 // if (operands.Count == 2 && operands[1].Type == typeof(string))
\r
173 // return Expression.Call(operands[0],
\r
174 // typeof(string).GetMethod("IndexOf", new[] { typeof(string)}),
\r
177 // throw new NotSupportedException();
\r
180 //protected virtual Expression TranslateReplace(List<Expression> operands)
\r
182 // if (operands.ElementAt(1).Type == typeof(string))
\r
184 // return Expression.Call(operands[0],
\r
185 // typeof(string).GetMethod("Replace", new[] { typeof(string), typeof(string) }),
\r
186 // operands[1], operands[2]);
\r
188 // return Expression.Call(operands[0],
\r
189 // typeof(string).GetMethod("Replace", new[] { typeof(char), typeof(char) }),
\r
190 // operands[1], operands[2]);
\r
192 //protected virtual Expression TranslateInsertString(List<Expression> operands)
\r
194 // return Expression.Call(operands.First(), typeof(string).GetMethod("Insert"), operands[1], operands[2]);
\r
197 //protected virtual Expression TranslateTrim(List<Expression> operands)
\r
199 // return Expression.Call(operands.First(), typeof(string).GetMethod("Trim", new Type[] { }));
\r
201 //protected virtual Expression TranslateSubString(List<Expression> operands)
\r
203 // if (operands.Count > 2)
\r
205 // return Expression.Call(operands[0],
\r
206 // typeof(string).GetMethod("Substring", new[] { operands[1].Type, operands[2].Type }),
\r
207 // operands[1], operands[2]);
\r
210 // return Expression.Call(operands[0],
\r
211 // typeof(string).GetMethod("Substring", new[] { operands[1].Type }),
\r
215 //protected virtual Expression TranslateToLower(List<Expression> operands)
\r
217 // return Expression.Call(operands[0], typeof(string).GetMethod("ToLower", new Type[0]));
\r
220 //protected virtual Expression TranslateToUpper(List<Expression> operands)
\r
222 // return Expression.Call(operands[0], typeof(string).GetMethod("ToUpper", new Type[0]));
\r
225 protected virtual Expression TranslateConcat(List<Expression> operands)
\r
227 return Expression.Add(operands[0], operands[1]);
\r
230 protected virtual Expression TranslateIsNotNull(List<Expression> operands)
\r
232 return Expression.NotEqual(operands[0], Expression.Constant(null));
\r
235 protected virtual Expression TranslateIsNull(List<Expression> operands)
\r
237 return Expression.Equal(operands[0], Expression.Constant(null));
\r