Skip to content

Commit ebe6a21

Browse files
committed
second draft
1 parent 75fb9ec commit ebe6a21

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

QueryBuilder.Tests/UpdateTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public void UpdateWithKeyAttribute()
155155
Assert.Equal(
156156
"UPDATE [OrderProductComposite] SET [OrdId] = 'ORD01', [ProductId] = 'PROD02', [Quantity] = 20, [Faa] = 'baz' WHERE [OrdId] = 'ORD01' AND [ProductId] = 'PROD02'",
157157
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]);
158162
}
159163

160164
}

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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ private Dictionary<string, object> BuildDictionaryOnUpdate(object data)
3232
}
3333

3434
var value = property.GetValue(data);
35-
var name = property.Name;
3635

3736
var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
37+
var name = colAttr?.Name ?? property.Name;
3838
if(colAttr != null)
3939
{
40-
name = colAttr.Name;
4140
if((colAttr as KeyAttribute) != null)
4241
{
4342
this.Where(name, value);

0 commit comments

Comments
 (0)