File: SpecialExpression.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (292 lines) | stat: -rw-r--r-- 13,870 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#region MIT license
// 
// MIT license
//
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq.Expressions;
using System.Linq;
using System.Collections.ObjectModel;

using DbLinq.Data.Linq.Sugar;
using DbLinq.Data.Linq.Sugar.ExpressionMutator;
using DbLinq.Data.Linq.Sugar.Expressions;

namespace DbLinq.Data.Linq.Sugar.Expressions
{
    /// <summary>
    /// Holds new expression types (sql related), all well as their operands
    /// </summary>
    [DebuggerDisplay("SpecialExpression {SpecialNodeType}")]
#if !MONO_STRICT
    public
#endif
    class SpecialExpression : OperandsMutableExpression, IExecutableExpression
    {
        public SpecialExpressionType SpecialNodeType { get { return (SpecialExpressionType)NodeType; } }

        protected static Type GetSpecialExpressionTypeType(SpecialExpressionType specialExpressionType, IList<Expression> operands)
        {
            Type defaultType;
            if (operands.Count > 0)
                defaultType = operands[0].Type;
            else
                defaultType = null;
            switch (specialExpressionType) // SETuse
            {
                case SpecialExpressionType.IsNull:
                case SpecialExpressionType.IsNotNull:
                    return typeof(bool);
                case SpecialExpressionType.Concat:
                    return typeof(string);
                case SpecialExpressionType.Count:
                    return typeof(int);
                case SpecialExpressionType.Exists:
                    return typeof(bool);
                case SpecialExpressionType.Like:
                    return typeof(bool);
                case SpecialExpressionType.Min:
                case SpecialExpressionType.Max:
                case SpecialExpressionType.Sum:
                    return defaultType; // for such methods, the type is related to the operands type
                case SpecialExpressionType.Average:
                    return typeof(double);
                case SpecialExpressionType.StringLength:
                    return typeof(int);
                case SpecialExpressionType.ToUpper:
                case SpecialExpressionType.ToLower:
                    return typeof(string);
                case SpecialExpressionType.In:
                    return typeof(bool);
                case SpecialExpressionType.Substring:
                    return defaultType;
                case SpecialExpressionType.Trim:
                case SpecialExpressionType.LTrim:
                case SpecialExpressionType.RTrim:
                    return typeof(string);
                case SpecialExpressionType.StringInsert:
                    return typeof(string);
                case SpecialExpressionType.Replace:
                    return typeof(string);
                case SpecialExpressionType.Remove:
                    return typeof(string);
                case SpecialExpressionType.IndexOf:
                    return typeof(int);
                case SpecialExpressionType.Year:
                case SpecialExpressionType.Month:
                case SpecialExpressionType.Day:
                case SpecialExpressionType.Hour:
                case SpecialExpressionType.Second:
                case SpecialExpressionType.Minute:
                case SpecialExpressionType.Millisecond:
                    return typeof(int);
                case SpecialExpressionType.Now:
                case SpecialExpressionType.Date:
                    return typeof(DateTime);
                case SpecialExpressionType.DateDiffInMilliseconds:
                    return typeof(long);
                case SpecialExpressionType.Abs:
                case SpecialExpressionType.Exp:
                case SpecialExpressionType.Floor:
                case SpecialExpressionType.Ln:
                case SpecialExpressionType.Log:
                case SpecialExpressionType.Pow:
                case SpecialExpressionType.Round:
                case SpecialExpressionType.Sign:
                case SpecialExpressionType.Sqrt:
                    return defaultType;

                default:
                    throw Error.BadArgument("S0058: Unknown SpecialExpressionType value {0}", specialExpressionType);
            }
        }

        public SpecialExpression(SpecialExpressionType expressionType, params Expression[] operands)
            : base((ExpressionType)expressionType, GetSpecialExpressionTypeType(expressionType, operands), operands)
        {
        }

        public SpecialExpression(SpecialExpressionType expressionType, IList<Expression> operands)
            : base((ExpressionType)expressionType, GetSpecialExpressionTypeType(expressionType, operands), operands)
        {
        }

        protected override Expression Mutate2(IList<Expression> newOperands)
        {
            return new SpecialExpression((SpecialExpressionType)NodeType, newOperands);
        }

        public object Execute()
        {
            switch (SpecialNodeType) // SETuse
            {
                case SpecialExpressionType.IsNull:
                    return operands[0].Evaluate() == null;
                case SpecialExpressionType.IsNotNull:
                    return operands[0].Evaluate() != null;
                case SpecialExpressionType.Concat:
                    {
                        var values = new List<string>();
                        foreach (var operand in operands)
                        {
                            var value = operand.Evaluate();
                            if (value != null)
                                values.Add(System.Convert.ToString(value, CultureInfo.InvariantCulture));
                            else
                                values.Add(null);
                        }
                        return string.Concat(values.ToArray());
                    }
                case SpecialExpressionType.Count:
                    {
                        var value = operands[0].Evaluate();
                        // TODO: string is IEnumerable. See what we do here
                        if (value is IEnumerable)
                        {
                            int count = 0;
                            foreach (var dontCare in (IEnumerable)value)
                                count++;
                            return count;
                        }
                        // TODO: by default, shall we answer 1 or throw an exception?
                        return 1;
                    }
                case SpecialExpressionType.Exists:
                    {
                        var value = operands[0].Evaluate();
                        // TODO: string is IEnumerable. See what we do here
                        if (value is IEnumerable)
                        {
                            return true;
                        }
                        // TODO: by default, shall we answer 1 or throw an exception?
                        return false;
                    }
                case SpecialExpressionType.Min:
                    {
                        decimal? min = null;
                        foreach (var operand in operands)
                        {
                            var value = System.Convert.ToDecimal(operand.Evaluate());
                            if (!min.HasValue || value < min.Value)
                                min = value;
                        }
                        return System.Convert.ChangeType(min.Value, operands[0].Type);
                    }
                case SpecialExpressionType.Max:
                    {
                        decimal? max = null;
                        foreach (var operand in operands)
                        {
                            var value = System.Convert.ToDecimal(operand.Evaluate());
                            if (!max.HasValue || value > max.Value)
                                max = value;
                        }
                        return System.Convert.ChangeType(max.Value, operands[0].Type);
                    }
                case SpecialExpressionType.Sum:
                    {
                        decimal sum = operands.Select(op => System.Convert.ToDecimal(op.Evaluate())).Sum();
                        return System.Convert.ChangeType(sum, operands.First().Type);
                    }
                case SpecialExpressionType.Average:
                    {
                        decimal sum = 0;
                        foreach (var operand in operands)
                            sum += System.Convert.ToDecimal(operand.Evaluate());
                        return sum / operands.Count;
                    }
                case SpecialExpressionType.StringLength:
                    return operands[0].Evaluate().ToString().Length;
                case SpecialExpressionType.ToUpper:
                    return operands[0].Evaluate().ToString().ToUpper();
                case SpecialExpressionType.ToLower:
                    return operands[0].Evaluate().ToString().ToLower();
                case SpecialExpressionType.Substring:
                    return EvaluateStandardCallInvoke("SubString", operands);
                case SpecialExpressionType.In:
                    throw new NotImplementedException();
                case SpecialExpressionType.Replace:
                    return EvaluateStandardCallInvoke("Replace", operands);
                case SpecialExpressionType.Remove:
                    return EvaluateStandardCallInvoke("Remove", operands);
                case SpecialExpressionType.IndexOf:
                    return EvaluateStandardCallInvoke("IndexOf", operands);
                case SpecialExpressionType.Year:
                    return ((DateTime)operands[0].Evaluate()).Year;
                case SpecialExpressionType.Month:
                    return ((DateTime)operands[0].Evaluate()).Month;
                case SpecialExpressionType.Day:
                    return ((DateTime)operands[0].Evaluate()).Day;
                case SpecialExpressionType.Hour:
                    return ((DateTime)operands[0].Evaluate()).Hour;
                case SpecialExpressionType.Minute:
                    return ((DateTime)operands[0].Evaluate()).Minute;
                case SpecialExpressionType.Second:
                    return ((DateTime)operands[0].Evaluate()).Second;
                case SpecialExpressionType.Millisecond:
                    return ((DateTime)operands[0].Evaluate()).Millisecond;
                case SpecialExpressionType.Now:
                    return DateTime.Now;
                case SpecialExpressionType.Date:
                    return ((DateTime)operands[0].Evaluate());
                case SpecialExpressionType.DateDiffInMilliseconds:
                    return ((DateTime)operands[0].Evaluate()) - ((DateTime)operands[1].Evaluate());
                case SpecialExpressionType.Abs:
                case SpecialExpressionType.Exp:
                case SpecialExpressionType.Floor:
                case SpecialExpressionType.Ln:
                case SpecialExpressionType.Log:
                case SpecialExpressionType.Pow:
                case SpecialExpressionType.Round:
                case SpecialExpressionType.Sign:
                case SpecialExpressionType.Sqrt:
                    return EvaluateMathCallInvoke(SpecialNodeType, operands);
                default:
                    throw Error.BadArgument("S0116: Unknown SpecialExpressionType ({0})", SpecialNodeType);
            }
        }

        private object EvaluateMathCallInvoke(SpecialExpressionType SpecialNodeType, ReadOnlyCollection<Expression> operands)
        {
            return typeof(Math).GetMethod(SpecialNodeType.ToString(), operands.Skip(1).Select(op => op.Type).ToArray())
                    .Invoke(null, operands.Skip(1).Select(op => op.Evaluate()).ToArray());
        }
        protected object EvaluateStatardMemberAccess(string propertyName, ReadOnlyCollection<Expression> operands)
        {
            return operands[0].Type.GetProperty(propertyName).GetValue(operands.First().Evaluate(), null);
        }
        protected object EvaluateStandardCallInvoke(string methodName, ReadOnlyCollection<Expression> operands)
        {
            return operands[0].Type.GetMethod(methodName,
                                       operands.Skip(1).Select(op => op.Type).ToArray())
                                       .Invoke(operands[0].Evaluate(),
                                               operands.Skip(1).Select(op => op.Evaluate()).ToArray());
        }
    }
}