Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
159 lines (144 loc) · 7.83 KB

File metadata and controls

159 lines (144 loc) · 7.83 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Dynamic;
using Simple.Data.Commands;
using Simple.Data.Extensions;
namespace Simple.Data
{
using System.Collections;
/// <summary>
/// Represents a table in a database, or the nearest equivalent in other data stores.
/// </summary>
public class DynamicTable : DynamicObject
{
private readonly Dictionary<string, Func<object[], object>> _delegates;
private readonly ICollection _delegatesAsCollection;
private readonly string _tableName;
private readonly DynamicSchema _schema;
private readonly DataStrategy _dataStrategy;
/// <summary>
/// Initializes a new instance of the <see cref="DynamicTable"/> class.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="dataStrategy">The database which owns the table.</param>
internal DynamicTable(string tableName, DataStrategy dataStrategy)
: this(tableName, dataStrategy, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DynamicTable"/> class.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="dataStrategy">The database which owns the table.</param>
/// <param name="schema">The schema to which the table belongs.</param>
internal DynamicTable(string tableName, DataStrategy dataStrategy, DynamicSchema schema)
{
_delegates = new Dictionary<string, Func<object[], object>>();
_delegatesAsCollection = _delegates;
_tableName = tableName;
_schema = schema;
_dataStrategy = dataStrategy;
}
/// <summary>
/// Provides the implementation for operations that invoke a member. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as calling a method.
/// </summary>
/// <param name="binder">Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
/// <param name="args">The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, <paramref name="args[0]"/> is equal to 100.</param>
/// <param name="result">The result of the member invocation.</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 language-specific run-time exception is thrown.)
/// </returns>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var signature = FunctionSignature.FromBinder(binder, args);
Func<object[], object> func;
if (_delegatesAsCollection.IsSynchronized && _delegates.ContainsKey(signature))
{
func = _delegates[signature];
}
else
{
lock (_delegatesAsCollection.SyncRoot)
{
if (!_delegates.ContainsKey(signature))
{
func = CreateMemberDelegate(signature, binder, args);
_delegates.Add(signature, func);
}
else
{
func = _delegates[signature];
}
}
}
if (func != null)
{
result = func(args);
return true;
}
var command = CommandFactory.GetCommandFor(binder.Name);
if (command != null)
{
result = command.Execute(_dataStrategy, this, binder, args);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
private Func<object[],object> CreateMemberDelegate(string signature, InvokeMemberBinder binder, object[] args)
{
try
{
var command = CommandFactory.GetCommandFor(binder.Name);
if (command == null) return null;
return command.CreateDelegate(_dataStrategy, this, binder, args);
}
catch (NotImplementedException)
{
return null;
}
}
/// <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)
{
if (binder.Name == "All")
{
Trace.WriteLine("The dynamic 'All' property is deprecated; use the 'All()' method instead.");
result = GetAll().ToList();
return true;
}
result = new ObjectReference(binder.Name, new ObjectReference(_tableName, (_schema != null ? new ObjectReference(_schema.GetName()) : null), _dataStrategy));
return true;
}
public ObjectReference As(string alias)
{
return new ObjectReference(_tableName, (_schema != null ? new ObjectReference(_schema.GetName()) : null), _dataStrategy, alias);
}
public ObjectReference this[string name]
{
get { return new ObjectReference(name, new ObjectReference(_tableName, (_schema != null ? new ObjectReference(_schema.GetName()) : null), _dataStrategy)); }
}
internal string GetName()
{
return _tableName;
}
internal string GetQualifiedName()
{
return _schema != null ? _schema.GetName() + "." + _tableName : _tableName;
}
private IEnumerable<dynamic> GetAll()
{
return _dataStrategy.Find(_tableName, null).Select(dict => new SimpleRecord(dict, _tableName, _dataStrategy));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.