|
5 | 5 |
|
6 | 6 | namespace SqlKata |
7 | 7 | { |
| 8 | + |
8 | 9 | public partial class Query |
9 | 10 | { |
10 | 11 |
|
11 | 12 | public Query AsUpdate(object data) |
12 | 13 | { |
13 | | - var dictionary = new Dictionary<string, object>(); |
| 14 | + var dictionary = BuildDictionaryOnUpdate(data); |
| 15 | + return AsUpdate(dictionary); |
| 16 | + } |
14 | 17 |
|
15 | | - var props = data.GetType().GetRuntimeProperties() |
16 | | - .Where(_ => _.GetCustomAttribute(typeof(IgnoreAttribute)) == null); |
17 | 18 |
|
18 | | - foreach (var item in props) |
| 19 | + private static Dictionary<Type, List<PropertyInfo>> CacheDictionaryProperties = new Dictionary<Type, List<PropertyInfo>>(); |
| 20 | + |
| 21 | + |
| 22 | + private Dictionary<string, object> BuildDictionaryOnUpdate(object data) |
| 23 | + { |
| 24 | + |
| 25 | + var dictionary = new Dictionary<string, object>(); |
| 26 | + var props = data.GetType().GetRuntimeProperties(); |
| 27 | + |
| 28 | + foreach (PropertyInfo property in props) |
19 | 29 | { |
20 | | - var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute; |
21 | | - if (attr != null) |
22 | | - { |
23 | | - dictionary.Add(attr.Name, item.GetValue(data)); |
| 30 | + if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null){ |
| 31 | + continue; |
24 | 32 | } |
25 | | - else |
| 33 | + |
| 34 | + var value = property.GetValue(data); |
| 35 | + var name = property.Name; |
| 36 | + |
| 37 | + var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute; |
| 38 | + if(colAttr != null) |
26 | 39 | { |
27 | | - dictionary.Add(item.Name, item.GetValue(data)); |
28 | | - } |
| 40 | + name = colAttr.Name; |
| 41 | + if((colAttr as KeyAttribute) != null) |
| 42 | + { |
| 43 | + this.Where(name, value); |
| 44 | + } |
| 45 | + } |
| 46 | + dictionary.Add(name, value); |
29 | 47 | } |
30 | 48 |
|
31 | | - return AsUpdate(dictionary); |
| 49 | + return dictionary; |
32 | 50 | } |
33 | 51 |
|
34 | 52 | public Query AsUpdate(IEnumerable<string> columns, IEnumerable<object> values) |
|
0 commit comments