forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicSchema.cs
More file actions
78 lines (67 loc) · 3.47 KB
/
DynamicSchema.cs
File metadata and controls
78 lines (67 loc) · 3.47 KB
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
namespace Simple.Data
{
using Commands;
public class DynamicSchema : DynamicObject
{
private readonly string _name;
private readonly DataStrategy _dataStrategy;
private readonly ConcurrentDictionary<string, dynamic> _tables = new ConcurrentDictionary<string, dynamic>();
public DynamicSchema(string name, DataStrategy dataStrategy)
{
_name = name;
_dataStrategy = dataStrategy;
}
/// <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)
{
return GetDynamicTable(binder.Name, out result);
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var adapterWithFunctions = _dataStrategy.GetAdapter() as IAdapterWithFunctions;
if (adapterWithFunctions != null && adapterWithFunctions.IsValidFunction(binder.Name))
{
var command = new ExecuteFunctionCommand(_dataStrategy.GetDatabase(), adapterWithFunctions, string.Format("{0}.{1}", _name, binder.Name),
binder.ArgumentsToDictionary(args));
return command.Execute(out result);
}
return base.TryInvokeMember(binder, args, out result);
}
public DynamicTable this[string name]
{
get { return GetTable(name); }
}
internal DynamicTable GetTable(string name)
{
dynamic table;
GetDynamicTable(name, out table);
return table;
}
internal string GetName()
{
return _name;
}
private bool GetDynamicTable(string name, out object result)
{
result = _tables.GetOrAdd(name, CreateDynamicTable);
return true;
}
private DynamicTable CreateDynamicTable(string name)
{
return new DynamicTable(name, _dataStrategy, this);
}
}
}