forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdoAdapterRelatedFinder.cs
More file actions
79 lines (67 loc) · 3.28 KB
/
AdoAdapterRelatedFinder.cs
File metadata and controls
79 lines (67 loc) · 3.28 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Simple.Data.Ado.Schema;
namespace Simple.Data.Ado
{
class AdoAdapterRelatedFinder
{
private readonly Lazy<ConcurrentDictionary<Tuple<string, string>, TableJoin>> _tableJoins =
new Lazy<ConcurrentDictionary<Tuple<string, string>, TableJoin>>(
() => new ConcurrentDictionary<Tuple<string, string>, TableJoin>(), LazyThreadSafetyMode.ExecutionAndPublication
);
private readonly AdoAdapter _adapter;
public AdoAdapterRelatedFinder(AdoAdapter adapter)
{
_adapter = adapter;
}
public bool IsValidRelation(string tableName, string relatedTableName)
{
return TryJoin(tableName, relatedTableName) != null;
}
public object FindRelated(string tableName, IDictionary<string, object> row, string relatedTableName)
{
var join = TryJoin(tableName, relatedTableName);
if (join == null) throw new AdoAdapterException("Could not resolve relationship.");
if (join.Master == _adapter.GetSchema().FindTable(tableName))
{
return GetDetail(row, join);
}
return GetMaster(row, join);
}
private TableJoin TryJoin(string tableName, string relatedTableName)
{
return _tableJoins.Value.GetOrAdd(Tuple.Create(tableName, relatedTableName),
t => TryCreateJoin(t.Item1, t.Item2));
}
private TableJoin TryCreateJoin(string tableName, string relatedTableName)
{
return TryMasterJoin(tableName, relatedTableName) ?? TryDetailJoin(tableName, relatedTableName);
}
private TableJoin TryMasterJoin(string tableName, string relatedTableName)
{
return _adapter.GetSchema().FindTable(tableName).GetMaster(relatedTableName);
}
private TableJoin TryDetailJoin(string tableName, string relatedTableName)
{
return _adapter.GetSchema().FindTable(tableName).GetDetail(relatedTableName);
}
private IDictionary<string, object> GetMaster(IDictionary<string, object> row, TableJoin masterJoin)
{
var criteria = new Dictionary<string, object> { { masterJoin.MasterColumn.ActualName, row[masterJoin.DetailColumn.HomogenizedName] } };
return _adapter.Find(masterJoin.Master.ActualName,
ExpressionHelper.CriteriaDictionaryToExpression(masterJoin.Master.ActualName,
criteria)).FirstOrDefault();
}
private SimpleQuery GetDetail(IDictionary<string, object> row, TableJoin join)
{
var criteria = ExpressionHelper.CriteriaDictionaryToExpression(join.Detail.ActualName,
new Dictionary<string, object>
{{join.DetailColumn.ActualName, row[join.MasterColumn.HomogenizedName]}});
return new SimpleQuery(_adapter, null, join.Detail.ActualName).Where(criteria);
}
}
}