Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

75 lines (63 sloc) 3.23 kB
using System;
using System.Dynamic;
using System.Linq;
using Simple.Data.Extensions;
namespace Simple.Data.Commands
{
using System.Collections.Generic;
class UpdateByCommand : ICommand
{
public bool IsCommandFor(string method)
{
return method.Homogenize().StartsWith("updateby", StringComparison.InvariantCultureIgnoreCase);
}
public object Execute(DataStrategy dataStrategy, DynamicTable table, InvokeMemberBinder binder, object[] args)
{
if (binder.HasSingleUnnamedArgument())
{
return UpdateByKeyFields(table.GetQualifiedName(), dataStrategy, args[0],
MethodNameParser.ParseCriteriaNamesFromMethodName(binder.Name));
}
var criteria = MethodNameParser.ParseFromBinder(binder, args);
var criteriaExpression = ExpressionHelper.CriteriaDictionaryToExpression(table.GetQualifiedName(), criteria);
var data = binder.NamedArgumentsToDictionary(args)
.Where(kvp => !criteria.ContainsKey(kvp.Key))
.ToDictionary();
return dataStrategy.Update(table.GetQualifiedName(), data, criteriaExpression);
}
public object Execute(DataStrategy dataStrategy, SimpleQuery query, InvokeMemberBinder binder, object[] args)
{
throw new NotImplementedException();
}
public Func<object[], object> CreateDelegate(DataStrategy dataStrategy, DynamicTable table, InvokeMemberBinder binder, object[] args)
{
throw new NotImplementedException();
}
internal static object UpdateByKeyFields(string tableName, DataStrategy dataStrategy, object entity, IEnumerable<string> keyFieldNames)
{
var record = UpdateCommand.ObjectToDictionary(entity);
var list = record as IList<IDictionary<string, object>>;
if (list != null) return dataStrategy.UpdateMany(tableName, list, keyFieldNames);
var dict = record as IDictionary<string, object>;
var criteria = GetCriteria(keyFieldNames, dict);
var criteriaExpression = ExpressionHelper.CriteriaDictionaryToExpression(tableName, criteria);
return dataStrategy.Update(tableName, dict, criteriaExpression);
}
private static IEnumerable<KeyValuePair<string, object>> GetCriteria(IEnumerable<string> keyFieldNames, IDictionary<string, object> record)
{
var criteria = new Dictionary<string, object>();
foreach (var keyFieldName in keyFieldNames)
{
var name = keyFieldName;
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(keyFieldName, keyValuePair.Value);
record.Remove(keyValuePair);
}
return criteria;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.