/
ObjectReference.cs
377 lines (332 loc) · 15.4 KB
/
ObjectReference.cs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using Simple.Data.Commands;
namespace Simple.Data
{
/// <summary>
/// Represents a qualified reference to a data store object, such as a table or column.
/// </summary>
public class ObjectReference : SimpleReference, IEquatable<ObjectReference>
{
private readonly string _name;
private readonly ObjectReference _owner;
private readonly DataStrategy _dataStrategy;
internal ObjectReference(string name) : this(name, null, null, null)
{
}
public ObjectReference(string name, ObjectReference owner) : this(name, owner, null, null)
{
}
internal ObjectReference(string name, DataStrategy dataStrategy) : this(name, null, dataStrategy, null)
{
}
internal ObjectReference(string name, ObjectReference owner, DataStrategy dataStrategy) : this(name, owner, dataStrategy, null)
{
}
internal ObjectReference(string name, ObjectReference owner, DataStrategy dataStrategy, string alias) : base(alias)
{
_name = name;
_owner = owner;
_dataStrategy = dataStrategy;
}
/// <summary>
/// Gets the owner of the referenced object; <c>null</c> if the object is top-level.
/// </summary>
/// <returns>The owner.</returns>
public ObjectReference GetOwner()
{
return _owner;
}
public ObjectReference GetTop()
{
return ReferenceEquals(_owner, null) ? this : _owner.GetTop();
}
protected internal override DataStrategy FindDataStrategyInHierarchy()
{
return _dataStrategy ?? (ReferenceEquals(_owner, null) ? null : _owner.FindDataStrategyInHierarchy());
}
/// <summary>
/// Gets the name of the referenced object.
/// </summary>
/// <returns>The name.</returns>
public string GetName()
{
return _name;
}
public ObjectReference As(string alias)
{
return new ObjectReference(_name, _owner, _dataStrategy, alias);
}
public override string GetAliasOrName()
{
return GetAlias() ?? _name;
}
public AllColumnsSpecialReference AllColumns()
{
return new AllColumnsSpecialReference(this);
}
public AllColumnsSpecialReference Star()
{
return new AllColumnsSpecialReference(this);
}
public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
{
if (_dataStrategy != null)
{
if (TryInvokeDataStrategyMethod(args, out result)) return true;
if (_dataStrategy.TryInvokeFunction(_name, () => binder.ArgumentsToDictionary(args), out result)) return true;
}
throw new InvalidOperationException();
}
private bool TryInvokeDataStrategyMethod(object[] args, out object result)
{
var methodInfo = _dataStrategy.GetType().GetMethod(_name, args.Select(a => (a ?? new object()).GetType()).ToArray());
if (methodInfo != null)
{
result = methodInfo.Invoke(_dataStrategy, args);
return true;
}
result = null;
return false;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (base.TryInvokeMember(binder, args, out result)) return true;
if (_dataStrategy != null)
{
var table = new DynamicTable(_name, _dataStrategy);
if (table.TryInvokeMember(binder, args, out result))
{
_dataStrategy.SetMemberAsTable(this, table);
return true;
}
// Or it could be a schema reference...
var schema = new DynamicSchema(_name, _dataStrategy);
if (schema.TryInvokeMember(binder, args, out result))
{
_dataStrategy.SetMemberAsSchema(this);
return true;
}
}
var dataStrategy = FindDataStrategyInHierarchy();
if (dataStrategy != null)
{
var command = CommandFactory.GetCommandFor(binder.Name);
if (command != null)
{
var schema = dataStrategy.SetMemberAsSchema(_owner);
var table = schema.GetTable(_name);
table.TryInvokeMember(binder, args, out result);
//result = command.Execute(dataStrategy, table, binder, args);
}
else
{
if (dataStrategy.IsExpressionFunction(binder.Name, args))
{
result = new SimpleExpression(this, new SimpleFunction(binder.Name, args), SimpleExpressionType.Function);
}
else
{
result = new FunctionReference(binder.Name, this, args);
}
}
return true;
}
throw new InvalidOperationException();
}
/// <summary>
/// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
/// </summary>
/// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
/// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
/// <returns>
/// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
/// </returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = new ObjectReference(binder.Name, this);
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (base.TrySetMember(binder, value))
{
return true;
}
throw new BadExpressionException("Cannot assign values to object references.");
}
public dynamic this[string name]
{
get { return new ObjectReference(name, this); }
}
/// <summary>
/// Gets the names of all objects included in the reference as an array, with the uppermost object first.
/// </summary>
/// <returns></returns>
public string[] GetAllObjectNames()
{
if (ReferenceEquals(GetOwner(), null)) return new[] {_name};
return _owner.GetAllObjectNames().Concat(new[] {_name}).ToArray();
}
/// <summary>
/// Gets the names of all objects included in the reference as an array, with the uppermost object first.
/// </summary>
/// <returns></returns>
public Tuple<string,string>[] GetAllObjectNamesAndAliases()
{
if (ReferenceEquals(GetOwner(), null)) return new[] {Tuple.Create(_name, GetAlias())};
return _owner.GetAllObjectNamesAndAliases().Concat(new[] {Tuple.Create(_name, GetAlias())}).ToArray();
}
public string GetAllObjectNamesDotted()
{
return string.Join(".", GetAllObjectNames());
}
public static ObjectReference FromString(string source)
{
return FromStrings(source.Split('.'));
}
public static ObjectReference FromStrings(params string[] source)
{
return source.Where(current => !String.IsNullOrEmpty(current))
.Aggregate<string, ObjectReference>(null, (current, element) => new ObjectReference(element, current));
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(ObjectReference other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._name, _name) && Equals(other._owner, _owner);
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (ObjectReference)) return false;
return Equals((ObjectReference) obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
unchecked
{
return ((_name != null ? _name.GetHashCode() : 0)*397) ^ (!ReferenceEquals(_owner, null) ? _owner.GetHashCode() : 0);
}
}
public override string ToString()
{
if (!ReferenceEquals(_owner, null))
{
return string.Concat(_owner, ".", _name);
}
return _name;
}
/// <summary>
/// Implements the operator == to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.Equal"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator ==(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.Equal);
}
/// <summary>
/// Implements the operator != to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.NotEqual"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator !=(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.NotEqual);
}
/// <summary>
/// Implements the operator < to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.LessThan"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator <(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.LessThan);
}
/// <summary>
/// Implements the operator > to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.GreaterThan"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator >(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.GreaterThan);
}
/// <summary>
/// Implements the operator <= to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.LessThanOrEqual"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator <=(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.LessThanOrEqual);
}
/// <summary>
/// Implements the operator >= to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.GreaterThanOrEqual"/>.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="value">The value.</param>
/// <returns>The expression.</returns>
public static SimpleExpression operator >=(ObjectReference column, object value)
{
return new SimpleExpression(column, value, SimpleExpressionType.GreaterThanOrEqual);
}
public static MathReference operator +(ObjectReference column, object value)
{
return new MathReference(column, value, MathOperator.Add);
}
public static MathReference operator -(ObjectReference column, object value)
{
return new MathReference(column, value, MathOperator.Subtract);
}
public static MathReference operator *(ObjectReference column, object value)
{
return new MathReference(column, value, MathOperator.Multiply);
}
public static MathReference operator /(ObjectReference column, object value)
{
return new MathReference(column, value, MathOperator.Divide);
}
public static MathReference operator %(ObjectReference column, object value)
{
return new MathReference(column, value, MathOperator.Modulo);
}
public bool HasOwner()
{
return !ReferenceEquals(null, _owner);
}
}
}