-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.cs
120 lines (97 loc) · 3.01 KB
/
Table.cs
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
namespace DatabaseSimulator;
public class Table<Model> : ITable<Model> where Model : TableModel
{
private readonly Dictionary<Guid, Row<Model>> Rows = new Dictionary<Guid, Row<Model>>();
internal Table(Row<Model> record) => Rows.Add(record.ID, record);
public int Count
{
get => Rows.Count;
}
public void Insert(Model record) => Rows.Add(record.ID, new Row<Model>(record));
public void InsertAll(IEnumerable<Model> records)
{
foreach (Model record in records)
Insert(record);
}
public Row<Model> Find(Guid id) => Rows[id];
public QueryResult<Row<Model>, Guid> FindAll(IEnumerable<Guid> listOfIDs)
{
QueryResult<Row<Model>, Guid> queryResult = new QueryResult<Row<Model>, Guid>();
foreach (Guid id in listOfIDs)
{
try
{
Row<Model> row = Rows[id];
queryResult.AddFound(row);
}
catch (System.Exception)
{
queryResult.AddNotFound(id);
}
}
return queryResult;
}
public List<Row<Model>> FindWhere(Func<Model, bool> where, int? limit = null)
{
List<Row<Model>> rows = Rows.Values.ToList();
List<Row<Model>> results = new List<Row<Model>>();
foreach (Row<Model> row in rows)
{
if (where(row.Record))
{
results.Add(row);
if (limit is not null && results.Count == limit)
break;
}
}
return results;
}
public Row<Model> FirstWhere(Func<Model, bool> where)
{
return FindWhere(where, 1).First();
}
public MutationResult<Guid, Guid> UpdateAll(IEnumerable<Guid> listOfIDs, Action<Model> updator)
{
MutationResult<Guid, Guid> result = new MutationResult<Guid, Guid>();
foreach (Guid id in listOfIDs)
{
try
{
Row<Model> row = Rows[id];
row.Update(updator);
result.AddSuccess(id);
}
catch (System.Exception)
{
result.AddFailure(id);
}
}
return result;
}
public List<Guid> UpdateWhere(Func<Model, bool> where, Action<Model> updator)
{
List<Guid> result = new List<Guid>();
FindWhere(where)
.ForEach(
row =>
{
row.Update(updator);
result.Add(row.ID);
}
);
return result;
}
public List<Row<Model>> GetAll() => Rows.Values.ToList();
public void Remove(Guid id) => Rows.Remove(id);
public List<Guid> RemoveWhere(Func<Model, bool> where)
{
List<Row<Model>> targetRows = FindWhere(where);
List<Guid> removedRows = new List<Guid>();
foreach (Row<Model> row in targetRows)
{
Rows.Remove(row.ID);
removedRows.Add(row.ID);
}
return removedRows;
}
}