|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Simple.Data.InMemory |
| 7 | +{ |
| 8 | + using QueryPolyfills; |
| 9 | + |
| 10 | + public class InMemoryAdapter : Adapter |
| 11 | + { |
| 12 | + private readonly Dictionary<string, List<IDictionary<string,object>>> _tables = new Dictionary<string, List<IDictionary<string, object>>>(); |
| 13 | + private readonly Dictionary<string,string> _autoIncrementColumns = new Dictionary<string, string>(); |
| 14 | + private readonly Dictionary<string, string[]> _keyColumns = new Dictionary<string, string[]>(); |
| 15 | + |
| 16 | + private List<IDictionary<string,object>> GetTable(string tableName) |
| 17 | + { |
| 18 | + tableName = tableName.ToLowerInvariant(); |
| 19 | + if (!_tables.ContainsKey(tableName)) _tables.Add(tableName, new List<IDictionary<string, object>>()); |
| 20 | + return _tables[tableName]; |
| 21 | + } |
| 22 | + |
| 23 | + public override IEnumerable<IDictionary<string, object>> Find(string tableName, SimpleExpression criteria) |
| 24 | + { |
| 25 | + var whereClauseHandler = new WhereClauseHandler(new WhereClause(criteria)); |
| 26 | + return whereClauseHandler.Run(GetTable(tableName)); |
| 27 | + } |
| 28 | + |
| 29 | + public override IEnumerable<IDictionary<string, object>> RunQuery(SimpleQuery query, out IEnumerable<SimpleQueryClauseBase> unhandledClauses) |
| 30 | + { |
| 31 | + unhandledClauses = query.Clauses.AsEnumerable(); |
| 32 | + return GetTable(query.TableName); |
| 33 | + } |
| 34 | + |
| 35 | + public override IDictionary<string, object> Insert(string tableName, IDictionary<string, object> data) |
| 36 | + { |
| 37 | + if (_autoIncrementColumns.ContainsKey(tableName)) |
| 38 | + { |
| 39 | + object nextVal = GetTable(tableName).Select(d => d[_autoIncrementColumns[tableName]]).Max(); |
| 40 | + nextVal = ObjectMaths.Increment(nextVal); |
| 41 | + data[_autoIncrementColumns[tableName]] = nextVal; |
| 42 | + } |
| 43 | + GetTable(tableName).Add(data); |
| 44 | + return data; |
| 45 | + } |
| 46 | + |
| 47 | + public override int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria) |
| 48 | + { |
| 49 | + int count = 0; |
| 50 | + foreach (var record in Find(tableName, criteria)) |
| 51 | + { |
| 52 | + UpdateRecord(data, record); |
| 53 | + ++count; |
| 54 | + } |
| 55 | + return count; |
| 56 | + } |
| 57 | + |
| 58 | + private static void UpdateRecord(IDictionary<string, object> data, IDictionary<string, object> record) |
| 59 | + { |
| 60 | + foreach (var kvp in data) |
| 61 | + { |
| 62 | + record[kvp.Key] = kvp.Value; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public override int Update(string tableName, IDictionary<string, object> data) |
| 67 | + { |
| 68 | + if (!_keyColumns.ContainsKey(tableName)) throw new InvalidOperationException("No key column(s) specified."); |
| 69 | + IDictionary<string, object> row = null; |
| 70 | + if (_keyColumns[tableName].Length == 1) |
| 71 | + { |
| 72 | + row = |
| 73 | + GetTable(tableName).Single( |
| 74 | + d => d[_keyColumns[tableName][0]] == data[_keyColumns[tableName][0]]); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + IEnumerable<IDictionary<string, object>> rows = GetTable(tableName); |
| 79 | + rows = _keyColumns[tableName].Aggregate(rows, (current, keyColumn) => current.Where(d => d[keyColumn] == data[keyColumn])); |
| 80 | + row = rows.Single(); |
| 81 | + } |
| 82 | + UpdateRecord(data, row); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + public override int Delete(string tableName, SimpleExpression criteria) |
| 87 | + { |
| 88 | + int count = 0; |
| 89 | + foreach (var record in Find(tableName, criteria)) |
| 90 | + { |
| 91 | + GetTable(tableName).Remove(record); |
| 92 | + ++count; |
| 93 | + } |
| 94 | + return count; |
| 95 | + } |
| 96 | + |
| 97 | + public override bool IsExpressionFunction(string functionName, params object[] args) |
| 98 | + { |
| 99 | + return functionName.Equals("like", StringComparison.OrdinalIgnoreCase) && args.Length == 1 && args[0] is string; |
| 100 | + } |
| 101 | + |
| 102 | + public void SetAutoIncrementColumn(string tableName, string columnName) |
| 103 | + { |
| 104 | + _autoIncrementColumns.Add(tableName, columnName); |
| 105 | + } |
| 106 | + |
| 107 | + public void SetKeyColumn(string tableName, string columnName) |
| 108 | + { |
| 109 | + _keyColumns[tableName] = new[] { columnName }; |
| 110 | + } |
| 111 | + |
| 112 | + public void SetKeyColumns(string tableName, params string[] columnNames) |
| 113 | + { |
| 114 | + _keyColumns[tableName] = columnNames; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments