Skip to content

Commit e225c73

Browse files
Merge pull request #249 from mota57/primary_key_attr
Primary key attr
2 parents e1d33a7 + ebe6a21 commit e225c73

File tree

4 files changed

+107
-25
lines changed

4 files changed

+107
-25
lines changed

QueryBuilder.Tests/UpdateTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,48 @@ 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+
Assert.Equal(
160+
"UPDATE \"ORDERPRODUCTCOMPOSITE\" SET \"ORDID\" = 'ORD01', \"PRODUCTID\" = 'PROD02', \"QUANTITY\" = 20, \"FAA\" = 'baz' WHERE \"ORDID\" = 'ORD01' AND \"PRODUCTID\" = 'PROD02'",
161+
c[EngineCodes.Firebird]);
162+
}
163+
121164
}
122165
}

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.Insert.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@ public partial class Query
99
{
1010
public Query AsInsert(object data, bool returnId = false)
1111
{
12-
var dictionary = new Dictionary<string, object>();
12+
var dictionary = BuildDictionaryOnInsert(data);
13+
14+
return AsInsert(dictionary, returnId);
15+
}
1316

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

18-
foreach (var item in props)
18+
private Dictionary<string, object> BuildDictionaryOnInsert(object data)
19+
{
20+
21+
var dictionary = new Dictionary<string, object>();
22+
var props = data.GetType().GetRuntimeProperties();
23+
24+
foreach (PropertyInfo property in props)
1925
{
20-
var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
21-
if (attr != null)
22-
{
23-
dictionary.Add(attr.Name, item.GetValue(data));
24-
}
25-
else
26+
if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null)
2627
{
27-
dictionary.Add(item.Name, item.GetValue(data));
28+
continue;
2829
}
30+
31+
var value = property.GetValue(data);
32+
33+
var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
34+
var name = colAttr?.Name ?? property.Name;
35+
36+
dictionary.Add(name, value);
2937
}
3038

31-
return AsInsert(dictionary, returnId);
39+
return dictionary;
3240
}
3341

3442
public Query AsInsert(IEnumerable<string> columns, IEnumerable<object> values)

QueryBuilder/Query.Update.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,47 @@
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+
36+
var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
37+
var name = colAttr?.Name ?? property.Name;
38+
if(colAttr != null)
2639
{
27-
dictionary.Add(item.Name, item.GetValue(data));
28-
}
40+
if((colAttr as KeyAttribute) != null)
41+
{
42+
this.Where(name, value);
43+
}
44+
}
45+
dictionary.Add(name, value);
2946
}
3047

31-
return AsUpdate(dictionary);
48+
return dictionary;
3249
}
3350

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

0 commit comments

Comments
 (0)