Skip to content

Commit 0950059

Browse files
committed
Added abstract GetKey method to Adapter; removed Update without criteria
1 parent e1ce2d5 commit 0950059

File tree

12 files changed

+43
-90
lines changed

12 files changed

+43
-90
lines changed

Simple.Data.Ado/AdoAdapter.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public ISchemaProvider SchemaProvider
7676
get { return _connectionProvider.GetSchemaProvider(); }
7777
}
7878

79+
public override IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record)
80+
{
81+
var homogenizedRecord = new Dictionary<string, object>(record, HomogenizedEqualityComparer.DefaultInstance);
82+
return GetKeyFieldNames(tableName).ToDictionary(key => key,
83+
key => homogenizedRecord.ContainsKey(key) ? homogenizedRecord[key] : null);
84+
}
85+
7986
#region IAdapterWithRelation Members
8087

8188
/// <summary>
@@ -310,14 +317,6 @@ public override IEnumerable<IDictionary<string, object>> InsertMany(string table
310317
return new AdoAdapterInserter(this).InsertMany(tableName, data, onError, resultRequired);
311318
}
312319

313-
public override int Update(string tableName, IDictionary<string, object> data)
314-
{
315-
string[] keyFieldNames = GetKeyFieldNames(tableName).ToArray();
316-
if (keyFieldNames.Length == 0) throw new AdoAdapterException("No Primary Key found for implicit update");
317-
return Update(tableName, data, GetCriteria(tableName, keyFieldNames, data));
318-
}
319-
320-
321320
public override int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> data,
322321
IEnumerable<string> criteriaFieldNames)
323322
{

Simple.Data.Mocking/XmlMockAdapter.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public XElement Data
2727
get { return _data.Value; }
2828
}
2929

30+
public override IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record)
31+
{
32+
return GetKeyFieldNames(tableName).ToDictionary(key => key,
33+
key => record.ContainsKey(key) ? record[key] : null);
34+
}
35+
3036
public override IDictionary<string, object> Get(string tableName, params object[] keyValues)
3137
{
3238
throw new NotImplementedException();
@@ -128,22 +134,6 @@ public override bool IsExpressionFunction(string functionName, params object[] a
128134
return false;
129135
}
130136

131-
public override int Update(string tableName, IDictionary<string, object> data)
132-
{
133-
return UpdateByKeyFields(tableName, data, GetKeyFieldNames(tableName));
134-
}
135-
136-
internal int UpdateByKeyFields(string tableName, object entity, IEnumerable<string> keyFieldNames)
137-
{
138-
var record = ObjectToDictionary(entity);
139-
var list = record as IList<IDictionary<string, object>>;
140-
if (list != null) return UpdateMany(tableName, list, keyFieldNames);
141-
142-
var dict = record as IDictionary<string, object>;
143-
var criteria = GetCriteria(tableName, keyFieldNames, dict);
144-
return Update(tableName, dict, criteria);
145-
}
146-
147137
private IEnumerable<IDictionary<string, object>> FindAll(string tableName)
148138
{
149139
return GetTableElement(tableName).Elements().Select(e => e.AttributesToDictionary());

Simple.Data.UnitTest/AdapterFactoryTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ private object Create()
4747

4848
class StubAdapter : Adapter
4949
{
50+
public override IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record)
51+
{
52+
throw new NotImplementedException();
53+
}
54+
5055
public override IDictionary<string, object> Get(string tableName, params object[] keyValues)
5156
{
5257
throw new NotImplementedException();
@@ -86,10 +91,5 @@ public override bool IsExpressionFunction(string functionName, params object[] a
8691
{
8792
throw new NotImplementedException();
8893
}
89-
90-
public override int Update(string tableName, IDictionary<string, object> data)
91-
{
92-
throw new NotImplementedException();
93-
}
9494
}
9595
}

Simple.Data/Adapter.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ protected virtual void OnSetup()
5757
{
5858
}
5959

60+
/// <summary>
61+
/// Gets the key value(s) for the record.
62+
/// </summary>
63+
/// <param name="tableName">Name of the table.</param>
64+
/// <param name="record">The record.</param>
65+
/// <returns>An <c>IDictionary&lt;string,object&gt;</c> containing the key that uniquely identifies the record in the database.</returns>
66+
public abstract IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record);
67+
6068
/// <summary>
6169
/// Gets a single record from the specified "table" using its default key.
6270
/// </summary>
@@ -99,15 +107,6 @@ out IEnumerable<SimpleQueryClauseBase>
99107
/// <returns>The number of records affected by the update operation.</returns>
100108
public abstract int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria);
101109

102-
/// <summary>
103-
/// Updates the specified "table" according to default keys (to be handled by adapter).
104-
/// </summary>
105-
/// <param name="tableName">Name of the table.</param>
106-
/// <param name="data">The new values.</param>
107-
/// <returns>The number of records affected by the update operation.</returns>
108-
/// <remarks>For example, the Ado adapter will fulfil this functionality using Primary Key data.</remarks>
109-
public abstract int Update(string tableName, IDictionary<string, object> data);
110-
111110
/// <summary>
112111
/// Deletes from the specified table.
113112
/// </summary><param name="tableName">Name of the table.</param>
@@ -213,14 +212,16 @@ private void InsertManyWithoutReturn(string tableName, IEnumerable<IDictionary<s
213212
/// <param name="tableName">Name of the table.</param>
214213
/// <param name="data">The data.</param>
215214
/// <returns>The total number of records affected by the update operations.</returns>
216-
/// <remarks>This method has a default implementation based on the <see cref="Update(string,IDictionary{string, object})"/> method.
215+
/// <remarks>This method has a default implementation based on the <see cref="Update(string,IDictionary{string, object},SimpleExpression)"/> method.
217216
/// You should override this method if your adapter can optimize the operation.</remarks>
218217
public virtual int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> data)
219218
{
220219
int updateCount = 0;
221220
foreach (var row in data)
222221
{
223-
updateCount += Update(tableName, row);
222+
var key = GetKey(tableName, row);
223+
var criteria = ExpressionHelper.CriteriaDictionaryToExpression(tableName, key);
224+
updateCount += Update(tableName, row, criteria);
224225
}
225226
return updateCount;
226227
}

Simple.Data/Commands/UpdateCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ private static object UpdateUsingKeys(DataStrategy dataStrategy, DynamicTable ta
5353
if (list != null) return dataStrategy.UpdateMany(table.GetQualifiedName(), list);
5454

5555
var dict = record as IDictionary<string, object>;
56-
return dataStrategy.Update(table.GetQualifiedName(), dict);
56+
if (dict == null) throw new InvalidOperationException("Could not resolve data from passed object.");
57+
var key = dataStrategy.GetAdapter().GetKey(table.GetQualifiedName(), dict);
58+
dict = dict.Where(kvp => key.All(keyKvp => keyKvp.Key.Homogenize() != kvp.Key.Homogenize())).ToDictionary();
59+
var criteria = ExpressionHelper.CriteriaDictionaryToExpression(table.GetQualifiedName(), key);
60+
return dataStrategy.Update(table.GetQualifiedName(), dict, criteria);
5761
}
5862

5963
public Func<object[], object> CreateDelegate(DataStrategy dataStrategy, DynamicTable table, InvokeMemberBinder binder, object[] args)

Simple.Data/DataStrategy.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ internal DynamicSchema SetMemberAsSchema(ObjectReference reference, DynamicSchem
123123
/// </summary><param name="tableName">Name of the table.</param><param name="data">The new values.</param><param name="criteria">The expression to use as criteria for the update operation.</param><returns>The number of records affected by the update operation.</returns>
124124
internal abstract int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria);
125125

126-
/// <summary>
127-
/// Updates the specified "table" using key fields.
128-
/// </summary><param name="tableName">Name of the table.</param><param name="data">The new values.</param><param name="criteria">The expression to use as criteria for the update operation.</param><returns>The number of records affected by the update operation.</returns>
129-
internal abstract int Update(string tableName, IDictionary<string, object> data);
130-
131126
/// <summary>
132127
/// Deletes from the specified table.
133128
/// </summary><param name="tableName">Name of the table.</param><param name="criteria">The expression to use as criteria for the delete operation.</param><returns>The number of records which were deleted.</returns>

Simple.Data/Database.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,6 @@ internal override int Update(string tableName, IDictionary<string, object> data,
127127
return _adapter.Update(tableName, data, criteria);
128128
}
129129

130-
/// <summary>
131-
/// Updates the specified "table" using data-store specific key fields.
132-
/// </summary>
133-
/// <param name="tableName">Name of the table.</param>
134-
/// <param name="data">The new values.</param>
135-
/// <returns>The number of records affected by the update operation.</returns>
136-
internal override int Update(string tableName, IDictionary<string, object> data)
137-
{
138-
return _adapter.Update(tableName, data);
139-
}
140-
141130
/// <summary>
142131
/// Deletes from the specified table.
143132
/// </summary><param name="tableName">Name of the table.</param><param name="criteria">The expression to use as criteria for the delete operation.</param><returns>The number of records which were deleted.</returns>

Simple.Data/Extensions/BinderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static BinderExtensions()
3232
ResultDiscardedGetter = null;
3333
}
3434

35-
ResultDiscardedGetter = ResultDiscardedGetter ?? (_ => true);
35+
ResultDiscardedGetter = ResultDiscardedGetter ?? (_ => false);
3636
}
3737

3838
public static bool HasSingleUnnamedArgument(this InvokeMemberBinder binder)

Simple.Data/IAdapterWithTransactions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public interface IAdapterWithTransactions
6060
int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> dataList, IAdapterTransaction adapterTransaction);
6161

6262
int UpdateMany(string tableName, IEnumerable<IDictionary<string, object>> dataList, IAdapterTransaction adapterTransaction, IList<string> keyFields);
63-
int Update(string tableName, IDictionary<string, object> data, IAdapterTransaction adapterTransaction);
63+
6464
int UpdateMany(string tableName, IList<IDictionary<string, object>> dataList, IEnumerable<string> criteriaFieldNames, IAdapterTransaction adapterTransaction);
6565
}
6666
}

Simple.Data/InMemoryAdapter.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ private List<IDictionary<string, object>> GetTable(string tableName)
2323
return _tables[tableName];
2424
}
2525

26+
public override IDictionary<string, object> GetKey(string tableName, IDictionary<string, object> record)
27+
{
28+
if (!_keyColumns.ContainsKey(tableName)) return null;
29+
return _keyColumns[tableName].ToDictionary(key => key, key => record.ContainsKey(key) ? record[key] : null);
30+
}
31+
2632
public override IDictionary<string, object> Get(string tableName, params object[] keyValues)
2733
{
2834
if (!_keyColumns.ContainsKey(tableName)) throw new InvalidOperationException("No key specified for In-Memory table.");
@@ -146,27 +152,6 @@ private static void UpdateRecord(IDictionary<string, object> data, IDictionary<s
146152
}
147153
}
148154

149-
public override int Update(string tableName, IDictionary<string, object> data)
150-
{
151-
if (!_keyColumns.ContainsKey(tableName)) throw new InvalidOperationException("No key column(s) specified.");
152-
IDictionary<string, object> row;
153-
if (_keyColumns[tableName].Length == 1)
154-
{
155-
row =
156-
GetTable(tableName).Single(
157-
d => Equals(d[_keyColumns[tableName][0]], data[_keyColumns[tableName][0]]));
158-
}
159-
else
160-
{
161-
IEnumerable<IDictionary<string, object>> rows = GetTable(tableName);
162-
row = _keyColumns[tableName]
163-
.Aggregate(rows, (current, keyColumn) => current.Where(d => Equals(d[keyColumn], data[keyColumn])))
164-
.Single();
165-
}
166-
UpdateRecord(data, row);
167-
return 1;
168-
}
169-
170155
public override int Delete(string tableName, SimpleExpression criteria)
171156
{
172157
List<IDictionary<string, object>> deletions = Find(tableName, criteria).ToList();

0 commit comments

Comments
 (0)