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
419 lines (353 loc) · 17.5 KB

File metadata and controls

419 lines (353 loc) · 17.5 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
namespace Simple.Data
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using QueryPolyfills;
public partial class InMemoryAdapter : Adapter, IAdapterWithFunctions
{
private readonly Dictionary<string, string> _autoIncrementColumns;
private readonly Dictionary<string, string[]> _keyColumns;
private readonly Dictionary<string, List<IDictionary<string, object>>> _tables;
[Flags]
private enum FunctionFlags
{
None = 0x00000000,
PassThru = 0x00000001
}
private class FunctionInfo
{
public FunctionInfo(Delegate func, FunctionFlags flags)
{
Delegate = func;
Flags = flags;
}
public Delegate Delegate { get; private set; }
public FunctionFlags Flags { get; private set; }
}
private readonly Dictionary<string, FunctionInfo> _functions = new Dictionary<string, FunctionInfo>();
private readonly ICollection<JoinInfo> _joins = new Collection<JoinInfo>();
public InMemoryAdapter() : this(StringComparer.OrdinalIgnoreCase)
{
}
public InMemoryAdapter(IEqualityComparer<string> nameComparer)
{
_nameComparer = nameComparer;
_keyColumns = new Dictionary<string, string[]>(_nameComparer);
_autoIncrementColumns = new Dictionary<string, string>(_nameComparer);
_tables = new Dictionary<string, List<IDictionary<string, object>>>(nameComparer);
}
private List<IDictionary<string, object>> GetTable(string tableName)
{
tableName = tableName.ToLowerInvariant();
if (!_tables.ContainsKey(tableName)) _tables.Add(tableName, new List<IDictionary<string, object>>());
return _tables[tableName];
}
public override IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record)
{
if (!_keyColumns.ContainsKey(tableName)) return null;
return _keyColumns[tableName].ToDictionary(key => key, key => record.ContainsKey(key) ? record[key] : null);
}
public override IList<string> GetKeyNames(string tableName)
{
if (!_keyColumns.ContainsKey(tableName)) return null;
return _keyColumns[tableName];
}
public override IDictionary<string, object> Get(string tableName, params object[] keyValues)
{
if (!_keyColumns.ContainsKey(tableName)) throw new InvalidOperationException("No key specified for In-Memory table.");
var keys = _keyColumns[tableName];
if (keys.Length != keyValues.Length) throw new ArgumentException("Incorrect number of values for key.");
var expression = new ObjectReference(keys[0]) == keyValues[0];
for (int i = 1; i < keyValues.Length; i++)
{
expression = expression && new ObjectReference(keys[i]) == keyValues[i];
}
return Find(tableName, expression).FirstOrDefault();
}
public override IEnumerable<IDictionary<string, object>> Find(string tableName, SimpleExpression criteria)
{
var whereClauseHandler = new WhereClauseHandler(tableName, new WhereClause(criteria));
return whereClauseHandler.Run(GetTable(tableName));
}
public override IEnumerable<IDictionary<string, object>> RunQuery(SimpleQuery query,
out IEnumerable<SimpleQueryClauseBase>
unhandledClauses)
{
unhandledClauses = query.Clauses.AsEnumerable();
return GetTable(query.TableName);
}
public override IDictionary<string, object> Insert(string tableName, IDictionary<string, object> data, bool resultRequired)
{
data = new Dictionary<string, object>(data, _nameComparer);
if (_autoIncrementColumns.ContainsKey(tableName))
{
var table = GetTable(tableName);
var autoIncrementColumn = _autoIncrementColumns[tableName];
if(!data.ContainsKey(autoIncrementColumn))
{
data.Add(autoIncrementColumn, 0);
}
object nextVal = 0;
if(table.Count > 0)
{
nextVal = table.Select(d => d[autoIncrementColumn]).Max();
}
nextVal = ObjectMaths.Increment(nextVal);
data[autoIncrementColumn] = nextVal;
}
GetTable(tableName).Add(data);
AddAsDetail(tableName, data);
AddAsMaster(tableName, data);
return data;
}
private void AddAsDetail(string tableName, IDictionary<string, object> data)
{
foreach (var @join in _joins.Where(j => j.DetailTableName.Equals(tableName, StringComparison.OrdinalIgnoreCase)))
{
if (!data.ContainsKey(@join.DetailKey)) continue;
foreach (
var master in
GetTable(@join.MasterTableName).Where(
d => d.ContainsKey(@join.MasterKey) && d[@join.MasterKey].Equals(data[@join.DetailKey])))
{
data[@join.MasterPropertyName] = master;
if (!master.ContainsKey(@join.DetailPropertyName))
{
master.Add(@join.DetailPropertyName, new List<IDictionary<string, object>> {data});
}
else
{
((List<IDictionary<string, object>>) master[@join.DetailPropertyName]).Add(data);
}
}
}
}
private void AddAsMaster(string tableName, IDictionary<string, object> data)
{
foreach (var @join in _joins.Where(j => j.MasterTableName.Equals(tableName, StringComparison.OrdinalIgnoreCase)))
{
if (!data.ContainsKey(@join.MasterKey)) continue;
foreach (
var detail in
GetTable(@join.DetailTableName).Where(
d => d.ContainsKey(@join.DetailKey) && d[@join.DetailKey].Equals(data[@join.MasterKey])))
{
detail[@join.MasterPropertyName] = data;
if (!data.ContainsKey(@join.DetailPropertyName))
{
data.Add(@join.DetailPropertyName, new List<IDictionary<string, object>> {data});
}
else
{
((List<IDictionary<string, object>>) data[@join.DetailPropertyName]).Add(data);
}
}
}
}
public override int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria)
{
int count = 0;
foreach (var record in Find(tableName, criteria))
{
UpdateRecord(data, record);
++count;
}
return count;
}
private static void UpdateRecord(IEnumerable<KeyValuePair<string, object>> data, IDictionary<string, object> record)
{
foreach (var kvp in data)
{
record[kvp.Key] = kvp.Value;
}
}
public override int Delete(string tableName, SimpleExpression criteria)
{
List<IDictionary<string, object>> deletions = Find(tableName, criteria).ToList();
foreach (var record in deletions)
{
GetTable(tableName).Remove(record);
}
return deletions.Count;
}
public override bool IsExpressionFunction(string functionName, params object[] args)
{
return (functionName.Equals("like", StringComparison.OrdinalIgnoreCase) ||
functionName.Equals("notlike", StringComparison.OrdinalIgnoreCase))
&& args.Length == 1
&& args[0] is string;
}
public void SetAutoIncrementColumn(string tableName, string columnName)
{
_autoIncrementColumns.Add(tableName, columnName);
}
public void SetKeyColumn(string tableName, string columnName)
{
_keyColumns[tableName] = new[] {columnName};
}
public void SetAutoIncrementKeyColumn(string tableName, string columnName)
{
SetKeyColumn(tableName, columnName);
SetAutoIncrementColumn(tableName, columnName);
}
public void SetKeyColumns(string tableName, params string[] columnNames)
{
_keyColumns[tableName] = columnNames;
}
/// <summary>
/// Set up an implicit join between two tables.
/// </summary>
/// <param name="masterTableName">The name of the 'master' table</param>
/// <param name="masterKey">The 'primary key'</param>
/// <param name="masterPropertyName">The name to give the lookup property in the detail objects</param>
/// <param name="detailTableName">The name of the 'master' table</param>
/// <param name="detailKey">The 'foreign key'</param>
/// <param name="detailPropertyName">The name to give the collection property in the master object</param>
public void ConfigureJoin(string masterTableName, string masterKey, string masterPropertyName, string detailTableName, string detailKey, string detailPropertyName)
{
var join = new JoinInfo(masterTableName, masterKey, masterPropertyName, detailTableName, detailKey,
detailPropertyName);
_joins.Add(join);
}
public JoinConfig Join
{
get { return new JoinConfig(_joins);}
}
private IEqualityComparer<string> _nameComparer = EqualityComparer<string>.Default;
internal class JoinInfo
{
private readonly string _masterTableName;
private readonly string _masterKey;
private readonly string _masterPropertyName;
private readonly string _detailTableName;
private readonly string _detailKey;
private readonly string _detailPropertyName;
public JoinInfo(string masterTableName, string masterKey, string masterPropertyName, string detailTableName, string detailKey, string detailPropertyName)
{
_masterTableName = masterTableName;
_masterKey = masterKey;
_masterPropertyName = masterPropertyName;
_detailTableName = detailTableName;
_detailKey = detailKey;
_detailPropertyName = detailPropertyName;
}
public string DetailPropertyName
{
get { return _detailPropertyName; }
}
public string DetailKey
{
get { return _detailKey; }
}
public string DetailTableName
{
get { return _detailTableName; }
}
public string MasterPropertyName
{
get { return _masterPropertyName; }
}
public string MasterKey
{
get { return _masterKey; }
}
public string MasterTableName
{
get { return _masterTableName; }
}
}
public override IDictionary<string, object> Upsert(string tableName, IDictionary<string, object> dict, SimpleExpression criteriaExpression, bool isResultRequired, IAdapterTransaction adapterTransaction)
{
return Upsert(tableName, dict, criteriaExpression, isResultRequired);
}
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, IEnumerable<string> keyFieldNames, IAdapterTransaction adapterTransaction, bool isResultRequired, Func<IDictionary<string,object>,Exception,bool> errorCallback)
{
return UpsertMany(tableName, list, keyFieldNames, isResultRequired, errorCallback);
}
public override IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, IAdapterTransaction adapterTransaction, bool isResultRequired, Func<IDictionary<string, object>, Exception, bool> errorCallback)
{
return UpsertMany(tableName, list, isResultRequired, errorCallback);
}
public IDictionary<string, object> Get(string tableName, IAdapterTransaction transaction, params object[] parameterValues)
{
return Get(tableName, parameterValues);
}
public class JoinConfig
{
private readonly ICollection<JoinInfo> _joins;
private JoinInfo _joinInfo;
internal JoinConfig(ICollection<JoinInfo> joins)
{
_joins = joins;
_joinInfo = new JoinInfo(null,null,null,null,null,null);
}
public JoinConfig Master(string tableName, string keyName, string propertyNameInDetailRecords = null)
{
if (_joins.Contains(_joinInfo)) _joins.Remove(_joinInfo);
_joinInfo = new JoinInfo(tableName, keyName, propertyNameInDetailRecords ?? tableName, _joinInfo.DetailTableName,
_joinInfo.DetailKey, _joinInfo.DetailPropertyName);
_joins.Add(_joinInfo);
return this;
}
public JoinConfig Detail(string tableName, string keyName, string propertyNameInMasterRecords = null)
{
if (_joins.Contains(_joinInfo)) _joins.Remove(_joinInfo);
_joinInfo = new JoinInfo(_joinInfo.MasterTableName, _joinInfo.MasterKey, _joinInfo.MasterPropertyName,
tableName, keyName,
propertyNameInMasterRecords ?? tableName);
_joins.Add(_joinInfo);
return this;
}
}
public void AddFunction<TResult>(string functionName, Func<TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddFunction<T,TResult>(string functionName, Func<T,TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddFunction<T1,T2,TResult>(string functionName, Func<T1,T2,TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddFunction<T1,T2,T3,TResult>(string functionName, Func<T1,T2,T3,TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddFunction<T1, T2, T3, T4, TResult>(string functionName, Func<T1, T2, T3, T4, TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddDelegate(string functionName, Delegate function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.None));
}
public void AddFunction<TResult>(string functionName, Func<IDictionary<string, object>, TResult> function)
{
_functions.Add(functionName, new FunctionInfo(function, FunctionFlags.PassThru));
}
public bool IsValidFunction(string functionName)
{
return _functions.ContainsKey(functionName);
}
public IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> Execute(string functionName, IDictionary<string, object> parameters)
{
if (!_functions.ContainsKey(functionName)) throw new InvalidOperationException(string.Format("Function '{0}' not found.", functionName));
var obj = ((_functions[functionName].Flags & FunctionFlags.PassThru) == FunctionFlags.PassThru) ?
_functions[functionName].Delegate.DynamicInvoke(parameters) :
_functions[functionName].Delegate.DynamicInvoke(parameters.Values.ToArray());
var dict = obj as IDictionary<string, object>;
if (dict != null) return new List<IEnumerable<IDictionary<string, object>>> { new List<IDictionary<string, object>> { dict } };
var list = obj as IEnumerable<IDictionary<string, object>>;
if (list != null) return new List<IEnumerable<IDictionary<string, object>>> { list };
return obj as IEnumerable<IEnumerable<IDictionary<string, object>>>;
}
public IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> Execute(string functionName, IDictionary<string, object> parameters, IAdapterTransaction transaction)
{
return Execute(functionName, parameters);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.