forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodNameParser.cs
More file actions
77 lines (62 loc) · 3.09 KB
/
MethodNameParser.cs
File metadata and controls
77 lines (62 loc) · 3.09 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
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using Simple.Data.Extensions;
namespace Simple.Data
{
internal static class MethodNameParser
{
internal static IDictionary<string, object> ParseFromBinder(InvokeMemberBinder binder, IList<object> args)
{
if (binder == null) throw new ArgumentNullException("binder");
if (binder.CallInfo.ArgumentNames != null && binder.CallInfo.ArgumentNames.Count > 0)
{
return ParseFromMethodName(binder.Name, binder.NamedArgumentsToDictionary(args));
}
if (binder.Name.Equals("findby", StringComparison.OrdinalIgnoreCase) || binder.Name.Equals("find_by", StringComparison.OrdinalIgnoreCase))
{
if (args.Count == 1)
{
return args[0].ObjectToDictionary();
}
throw new InvalidOperationException("Invalid criteria specification.");
}
return ParseFromMethodName(binder.Name, args);
}
internal static IDictionary<string, object> ParseFromMethodName(string methodName, IList<object> args)
{
if (args == null) throw new ArgumentNullException("args");
if (args.Count == 0) throw new ArgumentException("No parameters specified.");
var columns = GetColumns(RemoveCommandPart(methodName));
if (columns.Count == 0) throw new ArgumentException("No columns specified.");
return columns.Select((s,i) => new KeyValuePair<string, object>(s, args[i])).ToDictionary();
}
internal static IEnumerable<string> ParseCriteriaNamesFromMethodName(string methodName)
{
var columns = GetColumns(RemoveCommandPart(methodName));
if (columns.Count == 0) throw new ArgumentException("No columns specified.");
return columns.AsEnumerable();
}
internal static IDictionary<string, object> ParseFromMethodName(string methodName, IDictionary<string, object> args)
{
if (args == null) throw new ArgumentNullException("args");
if (args.Count == 0) throw new ArgumentException("No parameters specified.");
var columns = GetColumns(RemoveCommandPart(methodName));
if (columns.Count == 0) throw new ArgumentException("No columns specified.");
return columns.Select(s => new KeyValuePair<string, object>(s, args[s])).ToDictionary();
}
internal static string RemoveCommandPart(string methodName)
{
if (methodName == null) throw new ArgumentNullException("methodName");
if (!methodName.Contains("By")) return methodName;
return methodName.Substring(methodName.IndexOf("By") + 2);
}
internal static IList<string> GetColumns(string methodName)
{
var columns = methodName.Split(new[] { "And" }, StringSplitOptions.RemoveEmptyEntries);
return columns;
}
}
}