Skip to content

Commit 75fb9ec

Browse files
committed
second draft
1 parent 3e3a054 commit 75fb9ec

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

QueryBuilder.Tests/UpdateTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,44 @@ public void UpdateWithIgnoreAndColumnProperties()
118118
c[EngineCodes.Firebird]);
119119
}
120120

121+
122+
123+
private class OrderProductComposite
124+
{
125+
public OrderProductComposite(string orderid, string productid, int quantity)
126+
{
127+
OrderId = orderid;
128+
ProductId = productid;
129+
Quantity = quantity;
130+
Foo = "baz";
131+
}
132+
133+
[Key("OrdId")]
134+
public string OrderId { get; set; }
135+
136+
[Key]
137+
public string ProductId { get; set; }
138+
139+
public int Quantity { get; set; }
140+
141+
[Column("Faa")]
142+
public string Foo { get; set; }
143+
}
144+
145+
[Fact]
146+
public void UpdateWithKeyAttribute()
147+
{
148+
var order = new OrderProductComposite("ORD01", "PROD02", 20);
149+
150+
var query = new Query("OrderProductComposite").AsUpdate(order);
151+
152+
var c = Compile(query);
153+
154+
155+
Assert.Equal(
156+
"UPDATE [OrderProductComposite] SET [OrdId] = 'ORD01', [ProductId] = 'PROD02', [Quantity] = 20, [Faa] = 'baz' WHERE [OrdId] = 'ORD01' AND [ProductId] = 'PROD02'",
157+
c[EngineCodes.SqlServer]);
158+
}
159+
121160
}
122161
}

QueryBuilder/ColumnAttribute.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@ public ColumnAttribute(string name)
1818
}
1919

2020
}
21+
22+
23+
/// <summary>
24+
/// This class is used as metadata on a property to determine if it is a primary key
25+
/// </summary>
26+
public class KeyAttribute : ColumnAttribute
27+
{
28+
public KeyAttribute([System.Runtime.CompilerServices.CallerMemberName] string name = "")
29+
: base(name)
30+
{
31+
32+
}
33+
34+
}
2135
}

QueryBuilder/Query.Update.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,48 @@
55

66
namespace SqlKata
77
{
8+
89
public partial class Query
910
{
1011

1112
public Query AsUpdate(object data)
1213
{
13-
var dictionary = new Dictionary<string, object>();
14+
var dictionary = BuildDictionaryOnUpdate(data);
15+
return AsUpdate(dictionary);
16+
}
1417

15-
var props = data.GetType().GetRuntimeProperties()
16-
.Where(_ => _.GetCustomAttribute(typeof(IgnoreAttribute)) == null);
1718

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)
1929
{
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;
2432
}
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)
2639
{
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);
2947
}
3048

31-
return AsUpdate(dictionary);
49+
return dictionary;
3250
}
3351

3452
public Query AsUpdate(IEnumerable<string> columns, IEnumerable<object> values)

0 commit comments

Comments
 (0)