~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/db4o-net/Db4objects.Db4o.NativeQueries/Db4objects.Db4o.NativeQueries/Optimization/ComparisonQueryGeneratingVisitor.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2004 - 2009  Versant Inc.  http://www.db4o.com */
2
 
 
3
 
using System;
4
 
using System.Reflection;
5
 
using Db4objects.Db4o.Instrumentation.Api;
6
 
using Db4objects.Db4o.Internal;
7
 
using Db4objects.Db4o.NativeQueries.Expr.Cmp;
8
 
using Db4objects.Db4o.NativeQueries.Expr.Cmp.Operand;
9
 
using Db4objects.Db4o.NativeQueries.Optimization;
10
 
using Sharpen.Lang.Reflect;
11
 
 
12
 
namespace Db4objects.Db4o.NativeQueries.Optimization
13
 
{
14
 
        internal sealed class ComparisonQueryGeneratingVisitor : IComparisonOperandVisitor
15
 
        {
16
 
                private object _predicate;
17
 
 
18
 
                private object _value = null;
19
 
 
20
 
                private readonly INativeClassFactory _classSource;
21
 
 
22
 
                private readonly IReferenceResolver _resolver;
23
 
 
24
 
                public object Value()
25
 
                {
26
 
                        return _value;
27
 
                }
28
 
 
29
 
                public void Visit(ConstValue operand)
30
 
                {
31
 
                        _value = operand.Value();
32
 
                }
33
 
 
34
 
                public void Visit(FieldValue operand)
35
 
                {
36
 
                        operand.Parent().Accept(this);
37
 
                        Type clazz = ((operand.Parent() is StaticFieldRoot) ? (Type)_value : _value.GetType
38
 
                                ());
39
 
                        try
40
 
                        {
41
 
                                FieldInfo field = Reflection4.GetField(clazz, operand.FieldName());
42
 
                                _value = field.GetValue(_value);
43
 
                        }
44
 
                        catch (Exception exc)
45
 
                        {
46
 
                                // arg is ignored for static
47
 
                                Sharpen.Runtime.PrintStackTrace(exc);
48
 
                        }
49
 
                }
50
 
 
51
 
                internal object Add(object a, object b)
52
 
                {
53
 
                        if (a is double || b is double)
54
 
                        {
55
 
                                return ((double)a) + ((double)b);
56
 
                        }
57
 
                        if (a is float || b is float)
58
 
                        {
59
 
                                return ((float)a) + ((float)b);
60
 
                        }
61
 
                        if (a is long || b is long)
62
 
                        {
63
 
                                return ((long)a) + ((long)b);
64
 
                        }
65
 
                        return ((int)a) + ((int)b);
66
 
                }
67
 
 
68
 
                internal object Subtract(object a, object b)
69
 
                {
70
 
                        if (a is double || b is double)
71
 
                        {
72
 
                                return ((double)a) - ((double)b);
73
 
                        }
74
 
                        if (a is float || b is float)
75
 
                        {
76
 
                                return ((float)a) - ((float)b);
77
 
                        }
78
 
                        if (a is long || b is long)
79
 
                        {
80
 
                                return ((long)a) - ((long)b);
81
 
                        }
82
 
                        return ((int)a) - ((int)b);
83
 
                }
84
 
 
85
 
                internal object Multiply(object a, object b)
86
 
                {
87
 
                        if (a is double || b is double)
88
 
                        {
89
 
                                return ((double)a) * ((double)b);
90
 
                        }
91
 
                        if (a is float || b is float)
92
 
                        {
93
 
                                return ((float)a) * ((float)b);
94
 
                        }
95
 
                        if (a is long || b is long)
96
 
                        {
97
 
                                return ((long)a) * ((long)b);
98
 
                        }
99
 
                        return ((int)a) * ((int)b);
100
 
                }
101
 
 
102
 
                internal object Divide(object a, object b)
103
 
                {
104
 
                        if (a is double || b is double)
105
 
                        {
106
 
                                return ((double)a) / ((double)b);
107
 
                        }
108
 
                        if (a is float || b is float)
109
 
                        {
110
 
                                return ((float)a) / ((float)b);
111
 
                        }
112
 
                        if (a is long || b is long)
113
 
                        {
114
 
                                return ((long)a) / ((long)b);
115
 
                        }
116
 
                        return ((int)a) / ((int)b);
117
 
                }
118
 
 
119
 
                public void Visit(ArithmeticExpression operand)
120
 
                {
121
 
                        operand.Left().Accept(this);
122
 
                        object left = _value;
123
 
                        operand.Right().Accept(this);
124
 
                        object right = _value;
125
 
                        switch (operand.Op().Id())
126
 
                        {
127
 
                                case ArithmeticOperator.AddId:
128
 
                                {
129
 
                                        _value = Add(left, right);
130
 
                                        break;
131
 
                                }
132
 
 
133
 
                                case ArithmeticOperator.SubtractId:
134
 
                                {
135
 
                                        _value = Subtract(left, right);
136
 
                                        break;
137
 
                                }
138
 
 
139
 
                                case ArithmeticOperator.MultiplyId:
140
 
                                {
141
 
                                        _value = Multiply(left, right);
142
 
                                        break;
143
 
                                }
144
 
 
145
 
                                case ArithmeticOperator.DivideId:
146
 
                                {
147
 
                                        _value = Divide(left, right);
148
 
                                        break;
149
 
                                }
150
 
                        }
151
 
                }
152
 
 
153
 
                public void Visit(CandidateFieldRoot root)
154
 
                {
155
 
                }
156
 
 
157
 
                public void Visit(PredicateFieldRoot root)
158
 
                {
159
 
                        _value = _predicate;
160
 
                }
161
 
 
162
 
                public void Visit(StaticFieldRoot root)
163
 
                {
164
 
                        try
165
 
                        {
166
 
                                _value = _classSource.ForName(root.Type.Name);
167
 
                        }
168
 
                        catch (TypeLoadException e)
169
 
                        {
170
 
                                Sharpen.Runtime.PrintStackTrace(e);
171
 
                        }
172
 
                }
173
 
 
174
 
                public void Visit(ArrayAccessValue operand)
175
 
                {
176
 
                        operand.Parent().Accept(this);
177
 
                        object parent = _value;
178
 
                        operand.Index().Accept(this);
179
 
                        int index = (int)_value;
180
 
                        _value = Sharpen.Runtime.GetArrayValue(parent, index);
181
 
                }
182
 
 
183
 
                public void Visit(MethodCallValue operand)
184
 
                {
185
 
                        operand.Parent().Accept(this);
186
 
                        object receiver = _value;
187
 
                        MethodInfo method = _resolver.Resolve(operand.Method);
188
 
                        try
189
 
                        {
190
 
                                _value = method.Invoke(IsStatic(method) ? null : receiver, Args(operand));
191
 
                        }
192
 
                        catch (Exception exc)
193
 
                        {
194
 
                                Sharpen.Runtime.PrintStackTrace(exc);
195
 
                                _value = null;
196
 
                        }
197
 
                }
198
 
 
199
 
                private object[] Args(MethodCallValue operand)
200
 
                {
201
 
                        IComparisonOperand[] args = operand.Args;
202
 
                        object[] @params = new object[args.Length];
203
 
                        for (int paramIdx = 0; paramIdx < args.Length; paramIdx++)
204
 
                        {
205
 
                                args[paramIdx].Accept(this);
206
 
                                @params[paramIdx] = _value;
207
 
                        }
208
 
                        return @params;
209
 
                }
210
 
 
211
 
                private bool IsStatic(MethodInfo method)
212
 
                {
213
 
                        return NativeQueriesPlatform.IsStatic(method);
214
 
                }
215
 
 
216
 
                public ComparisonQueryGeneratingVisitor(object predicate, INativeClassFactory classSource
217
 
                        , IReferenceResolver resolver) : base()
218
 
                {
219
 
                        _predicate = predicate;
220
 
                        _classSource = classSource;
221
 
                        _resolver = resolver;
222
 
                }
223
 
        }
224
 
}