forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.cs
More file actions
311 lines (281 loc) · 16.7 KB
/
Adapter.cs
File metadata and controls
311 lines (281 loc) · 16.7 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
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
namespace Simple.Data
{
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Extensions;
/// <summary>
/// Provides a base class for adapters to persist data to data storage systems.
/// Authors may derive from this class to create support for all kinds of databases or data stores.
/// </summary>
public abstract class Adapter
{
private readonly ExpandoObject _settings = new ExpandoObject();
/// <summary>
/// Gets an <see cref="ExpandoObject"/> with the settings for this <see cref="Adapter"/>.
/// This property may be cast to an <see cref="IDictionary{TKey,TValue}"/> with a <see cref="string"/>
/// key and <see cref="object"/> value for non-dynamic access.
/// </summary>
/// <value>The settings.</value>
protected dynamic Settings
{
get { return _settings; }
}
/// <summary>
/// Performs initial setup of the Adapter, allowing an object to be passed in with Adapter-specific settings.
/// </summary>
/// <param name="settings">An <see cref="ExpandoObject"/> or an anonymous-typed object with any adapter-specific settings.</param>
public void Setup(object settings)
{
Setup(settings.ObjectToDictionary());
}
/// <summary>
/// Performs initial setup of the Adapter, allowing an object to be passed in with Adapter-specific settings.
/// </summary>
/// <param name="settings">A list of name/value pairs with any adapter-specific settings.</param>
public void Setup(IEnumerable<KeyValuePair<string, object>> settings)
{
var settingsAsDictionary = (IDictionary<string, object>) _settings;
foreach (var keyValuePair in settings)
{
settingsAsDictionary.Add(keyValuePair);
}
OnSetup();
}
/// <summary>
/// Called when the <see cref="Setup(object)"/> method is called, after the settings have been set.
/// </summary>
/// <remarks>It is not necessary to call <c>base.OnSetup()</c> when overriding this method.</remarks>
protected virtual void OnSetup()
{
}
/// <summary>
/// Gets a single record from the specified "table" using its default key.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="keyValues">The values to search with.</param>
/// <returns>The record matching the supplied key. If no record is found, return <c>null</c>.</returns>
public abstract IDictionary<string, object> Get(string tableName, params object[] keyValues);
/// <summary>
/// Finds data from the specified "table".
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="criteria">The criteria. This may be <c>null</c>, in which case all records should be returned.</param>
/// <returns>The list of records matching the criteria. If no records are found, return an empty list.</returns>
public abstract IEnumerable<IDictionary<string, object>> Find(string tableName, SimpleExpression criteria);
/// <summary>
/// Runs a <see cref="SimpleQuery"/>.
/// </summary>
/// <param name="query">The <see cref="SimpleQuery"/> to run.</param>
/// <param name="unhandledClauses">Use this to return any clauses your adapter is unable to interpret.</param>
/// <returns></returns>
public abstract IEnumerable<IDictionary<string, object>> RunQuery(SimpleQuery query,
out IEnumerable<SimpleQueryClauseBase>
unhandledClauses);
/// <summary>
/// Inserts a record into the specified "table".
/// </summary><param name="tableName">Name of the table.</param>
/// <param name="data">The values to insert.</param>
/// <returns>If possible, return the newly inserted row, including any automatically-set values such as primary keys or timestamps.</returns>
public abstract IDictionary<string, object> Insert(string tableName, IDictionary<string, object> data);
/// <summary>
/// Updates the specified "table" according to specified criteria.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="data">The new values.</param>
/// <param name="criteria">The expression to use as criteria for the update operation.</param>
/// <returns>The number of records affected by the update operation.</returns>
public abstract int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria);
/// <summary>
/// Updates the specified "table" according to default keys (to be handled by adapter).
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="data">The new values.</param>
/// <returns>The number of records affected by the update operation.</returns>
/// <remarks>For example, the Ado adapter will fulfil this functionality using Primary Key data.</remarks>
public abstract int Update(string tableName, IDictionary<string, object> data);
/// <summary>
/// Deletes from the specified table.
/// </summary><param name="tableName">Name of the table.</param>
/// <param name="criteria">The expression to use as criteria for the delete operation.</param>
/// <returns>The number of records which were deleted.</returns>
public abstract int Delete(string tableName, SimpleExpression criteria);
/// <summary>
/// Checks to see whether the passed function name is a valid function for the Adapter.
/// </summary>
/// <param name="functionName">The name of the function.</param>
/// <param name="args">The values passed (for overload resolution etc).</param>
/// <returns><c>true</c> if the name represents a function; otherwise, <c>false</c>.</returns>
public abstract bool IsExpressionFunction(string functionName, params object[] args);
/// <summary>
/// Finds a single record.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="criteria">The criteria.</param>
/// <returns>A dictionary containing the record.</returns>
/// <remarks>This method has a default implementation based on the <see cref="Find(string,SimpleExpression)"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual IDictionary<string, object> FindOne(string tableName, SimpleExpression criteria)
{
return Find(tableName, criteria).FirstOrDefault();
}
/// <summary>
/// Inserts multiple records into a table.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="dataList">The data.</param>
/// <returns>If possible, return the newly inserted rows, including any automatically-set values such as primary keys or timestamps.</returns>
/// <remarks>This method has a default implementation based on the <see cref="Insert(string,IDictionary{string, object})"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual IEnumerable<IDictionary<string, object>> InsertMany(string tableName,
IEnumerable<IDictionary<string, object>> dataList)
{
foreach (var row in dataList)
{
yield return Insert(tableName, row);
}
}
/// <summary>
/// Updates multiple records in a table.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="data">The data.</param>
/// <returns>The total number of records affected by the update operations.</returns>
/// <remarks>This method has a default implementation based on the <see cref="Update(string,IDictionary{string, object})"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> data)
{
int updateCount = 0;
foreach (var row in data)
{
updateCount += Update(tableName, row);
}
return updateCount;
}
/// <summary>
/// Runs the query as an <see cref="IObservable{T}"/>, for Reactive Extension joy.
/// </summary>
/// <param name="query">The query.</param>
/// <param name="unhandledClauses">The unhandled clauses.</param>
/// <returns>A cold <see cref="IObservable{T}"/> which will start pushing when subscribed.</returns>
/// <remarks>This method has a default implementation based on the <see cref="RunQuery"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual IObservable<IDictionary<string, object>> RunQueryAsObservable(SimpleQuery query,
out
IEnumerable
<SimpleQueryClauseBase>
unhandledClauses)
{
return RunQuery(query, out unhandledClauses).ToObservable();
}
/// <summary>
/// Runs multiple queries.
/// </summary>
/// <param name="queries">The queries.</param>
/// <param name="unhandledClauses">The unhandled clauses.</param>
/// <returns>A list of lists of dictionaries. Data.</returns>
/// <remarks>This method has a default implementation based on the <see cref="RunQuery"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual IEnumerable<IEnumerable<IDictionary<string, object>>> RunQueries(SimpleQuery[] queries,
List
<
IEnumerable
<SimpleQueryClauseBase>>
unhandledClauses)
{
foreach (var query in queries)
{
IEnumerable<SimpleQueryClauseBase> unhandledClausesForThisQuery;
var result = RunQuery(query, out unhandledClausesForThisQuery);
unhandledClauses.Add(unhandledClausesForThisQuery);
yield return result;
}
}
/// <summary>
/// Updates the specified "table" according to default keys (to be handled by adapter).
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="dataList">A list of objects to be updated.</param>
/// <param name="criteriaFieldNames">The list of field names to be used for criteria.</param>
/// <returns>The number of records affected by the update operation.</returns>
/// <remarks>For example, the Ado adapter will fulfil this functionality using Primary Key data.</remarks>
/// <remarks>This method has a default implementation based on the
/// <see cref="Update(string,System.Collections.Generic.IDictionary{string,object},Simple.Data.SimpleExpression)"/> method.
/// You should override this method if your adapter can optimize the operation.</remarks>
public virtual int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> dataList,
IEnumerable<string> criteriaFieldNames)
{
int updatedCount = 0;
var criteriaFieldNameList = criteriaFieldNames.ToList();
foreach (var data in dataList)
{
updatedCount += Update(tableName, data, GetCriteria(tableName, criteriaFieldNameList, data));
}
return updatedCount;
}
/// <summary>
/// Creates a delegate which can accept an array of values to use as criteria against a cached template.
/// When called, the delegate should return a single record using the passed values as criteria.
/// </summary><param name="tableName">Name of the table.</param>
/// <param name="criteria">The criteria to use as a template for the delegate</param>
/// <returns>A <c>Func<object[], IDictionary<string, object>>"</c> which finds a record.</returns>
public virtual Func<object[], IDictionary<string, object>> CreateFindOneDelegate(string tableName,
SimpleExpression criteria)
{
throw new NotImplementedException();
}
/// <summary>
/// Creates a delegate which can accept an array of values to use as criteria against a cached template.
/// When called, the delegate should return zero or more records using the passed values as criteria.
/// </summary><param name="tableName">Name of the table.</param>
/// <param name="criteria">The criteria to use as a template for the delegate</param>
/// <returns>A <c>Func<object[], IDictionary<string, object>>"</c> which finds a record.</returns>
public virtual Func<object[], IEnumerable<IDictionary<string, object>>> CreateFindDelegate(string tableName,
SimpleExpression
criteria)
{
throw new NotImplementedException();
}
/// <summary>
/// Creates a criteria expression.
/// </summary>
/// <param name="tableName">The name of the root table for the expression.</param>
/// <param name="criteriaFieldNames">The names of fields to be used for the criteria.</param>
/// <param name="record">The name/value pairs to be used as criteria.</param>
/// <returns>A <see cref="SimpleExpression"/> criteria object.</returns>
protected static SimpleExpression GetCriteria(string tableName, IEnumerable<string> criteriaFieldNames,
IDictionary<string, object> record)
{
var criteria = new Dictionary<string, object>();
foreach (var criteriaFieldName in criteriaFieldNames)
{
var name = criteriaFieldName;
var keyValuePair = record.Where(kvp => kvp.Key.Homogenize().Equals(name.Homogenize())).SingleOrDefault();
if (string.IsNullOrWhiteSpace(keyValuePair.Key))
{
throw new InvalidOperationException("Key field value not set.");
}
criteria.Add(criteriaFieldName, keyValuePair.Value);
record.Remove(keyValuePair);
}
return ExpressionHelper.CriteriaDictionaryToExpression(tableName, criteria);
}
public void Reset()
{
OnReset();
}
protected virtual void OnReset()
{
}
private OptimizingDelegateFactory _optimizingDelegateFactory;
public OptimizingDelegateFactory OptimizingDelegateFactory
{
get { return _optimizingDelegateFactory ?? (_optimizingDelegateFactory = CreateOptimizingDelegateFactory()); }
}
private OptimizingDelegateFactory CreateOptimizingDelegateFactory()
{
return MefHelper.GetAdjacentComponent<OptimizingDelegateFactory>(this.GetType()) ?? new DefaultOptimizingDelegateFactory();
}
}
}