forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdoAdapterUpserter.cs
More file actions
139 lines (123 loc) · 5.66 KB
/
AdoAdapterUpserter.cs
File metadata and controls
139 lines (123 loc) · 5.66 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
namespace Simple.Data.Ado
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Extensions;
class AdoAdapterUpserter
{
private readonly AdoAdapter _adapter;
private readonly IDbConnection _connection;
private readonly IDbTransaction _transaction;
public AdoAdapterUpserter(AdoAdapter adapter) : this(adapter, (IDbTransaction)null)
{
}
public AdoAdapterUpserter(AdoAdapter adapter, IDbConnection connection)
{
_adapter = adapter;
_connection = connection;
}
public AdoAdapterUpserter(AdoAdapter adapter, IDbTransaction transaction)
{
_adapter = adapter;
_transaction = transaction;
if (transaction != null) _connection = transaction.Connection;
}
public IDictionary<string, object> Upsert(string tableName, IDictionary<string, object> data, SimpleExpression criteria, bool resultRequired)
{
var connection = _connection ?? _adapter.CreateConnection();
using (connection.MaybeDisposable())
{
connection.OpenIfClosed();
return Upsert(tableName, data, criteria, resultRequired, connection);
}
}
private IDictionary<string, object> Upsert(string tableName, IDictionary<string, object> data, SimpleExpression criteria, bool resultRequired,
IDbConnection connection)
{
var finder = _transaction == null
? new AdoAdapterFinder(_adapter, connection)
: new AdoAdapterFinder(_adapter, _transaction);
var existing = finder.FindOne(tableName, criteria);
if (existing != null)
{
// Don't update columns used as criteria
var keys = criteria.GetOperandsOfType<ObjectReference>().Select(o => o.GetName().Homogenize());
var updateData = data.Where(kvp => keys.All(k => k != kvp.Key.Homogenize())).ToDictionary();
if (updateData.Count == 0)
{
return existing;
}
var commandBuilder = new UpdateHelper(_adapter.GetSchema()).GetUpdateCommand(tableName, updateData, criteria);
if (_transaction == null)
{
_adapter.Execute(commandBuilder, connection);
}
else
{
_adapter.Execute(commandBuilder, _transaction);
}
return resultRequired ? finder.FindOne(tableName, criteria) : null;
}
var inserter = _transaction == null
? new AdoAdapterInserter(_adapter, connection)
: new AdoAdapterInserter(_adapter, _transaction);
return inserter.Insert(tableName, data, resultRequired);
}
public IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, bool isResultRequired, Func<IDictionary<string, object>, Exception, bool> errorCallback)
{
foreach (var row in list)
{
IDictionary<string, object> result;
try
{
var criteria = ExpressionHelper.CriteriaDictionaryToExpression(tableName,
_adapter.GetKey(tableName, row));
result = Upsert(tableName, row, criteria, isResultRequired);
}
catch (Exception ex)
{
if (errorCallback(row, ex)) continue;
throw;
}
yield return result;
}
}
public IEnumerable<IDictionary<string, object>> UpsertMany(string tableName, IList<IDictionary<string, object>> list, IList<string> keyFieldNames, bool isResultRequired, Func<IDictionary<string, object>, Exception, bool> errorCallback)
{
foreach (var row in list)
{
IDictionary<string, object> result;
try
{
var criteria = GetCriteria(tableName, keyFieldNames, row);
result = Upsert(tableName, row, criteria, isResultRequired);
}
catch (Exception ex)
{
if (errorCallback(row, ex)) continue;
throw;
}
yield return result;
}
}
private static SimpleExpression GetCriteria(string tableName, IEnumerable<string> criteriaFieldNames,
IDictionary<string, object> record)
{
var criteria = new Dictionary<string, object>();
foreach (var criteriaFieldName in criteriaFieldNames)
{
var name = criteriaFieldName;
var keyValuePair = record.SingleOrDefault(kvp => kvp.Key.Homogenize().Equals(name.Homogenize()));
if (string.IsNullOrWhiteSpace(keyValuePair.Key))
{
throw new InvalidOperationException("Key field value not set.");
}
criteria.Add(criteriaFieldName, keyValuePair.Value);
record.Remove(keyValuePair);
}
return ExpressionHelper.CriteriaDictionaryToExpression(tableName, criteria);
}
}
}