forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryAdapter.cs
More file actions
245 lines (215 loc) · 9.79 KB
/
InMemoryAdapter.cs
File metadata and controls
245 lines (215 loc) · 9.79 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
namespace Simple.Data
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using QueryPolyfills;
public class InMemoryAdapter : Adapter
{
private readonly Dictionary<string, string> _autoIncrementColumns = new Dictionary<string, string>();
private readonly Dictionary<string, string[]> _keyColumns = new Dictionary<string, string[]>();
private readonly Dictionary<string, List<IDictionary<string, object>>> _tables =
new Dictionary<string, List<IDictionary<string, object>>>();
private readonly ICollection<Join> _joins = new Collection<Join>();
private readonly IDictionary<string, IList<string>> _keys = new Dictionary<string, IList<string>>();
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> Get(string tableName, params object[] keyValues)
{
if (!_keys.ContainsKey(tableName)) throw new InvalidOperationException("No key specified for In-Memory table.");
var keys = _keys[tableName];
if (keys.Count != 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(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)
{
if (_autoIncrementColumns.ContainsKey(tableName))
{
object nextVal = GetTable(tableName).Select(d => d[_autoIncrementColumns[tableName]]).Max();
nextVal = ObjectMaths.Increment(nextVal);
data[_autoIncrementColumns[tableName]] = 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(IDictionary<string, object> data, IDictionary<string, object> record)
{
foreach (var kvp in data)
{
record[kvp.Key] = kvp.Value;
}
}
public override int Update(string tableName, IDictionary<string, object> data)
{
if (!_keyColumns.ContainsKey(tableName)) throw new InvalidOperationException("No key column(s) specified.");
IDictionary<string, object> row = null;
if (_keyColumns[tableName].Length == 1)
{
row =
GetTable(tableName).Single(
d => d[_keyColumns[tableName][0]] == data[_keyColumns[tableName][0]]);
}
else
{
IEnumerable<IDictionary<string, object>> rows = GetTable(tableName);
row = _keyColumns[tableName]
.Aggregate(rows, (current, keyColumn) => current.Where(d => d[keyColumn] == data[keyColumn]))
.Single();
}
UpdateRecord(data, row);
return 1;
}
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) && 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 SetKeyColumns(string tableName, params string[] columnNames)
{
_keyColumns[tableName] = columnNames;
}
public void ConfigureJoin(string masterTableName, string masterKey, string masterPropertyName, string detailTableName, string detailKey, string detailPropertyName)
{
var join = new Join(masterTableName, masterKey, masterPropertyName, detailTableName, detailKey,
detailPropertyName);
_joins.Add(join);
}
public class Join
{
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 Join(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; }
}
}
}
}