Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions QueryBuilder.Tests/UpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,48 @@ public void UpdateWithIgnoreAndColumnProperties()
c[EngineCodes.Firebird]);
}



private class OrderProductComposite
{
public OrderProductComposite(string orderid, string productid, int quantity)
{
OrderId = orderid;
ProductId = productid;
Quantity = quantity;
Foo = "baz";
}

[Key("OrdId")]
public string OrderId { get; set; }

[Key]
public string ProductId { get; set; }

public int Quantity { get; set; }

[Column("Faa")]
public string Foo { get; set; }
}

[Fact]
public void UpdateWithKeyAttribute()
{
var order = new OrderProductComposite("ORD01", "PROD02", 20);

var query = new Query("OrderProductComposite").AsUpdate(order);

var c = Compile(query);


Assert.Equal(
"UPDATE [OrderProductComposite] SET [OrdId] = 'ORD01', [ProductId] = 'PROD02', [Quantity] = 20, [Faa] = 'baz' WHERE [OrdId] = 'ORD01' AND [ProductId] = 'PROD02'",
c[EngineCodes.SqlServer]);

Assert.Equal(
"UPDATE \"ORDERPRODUCTCOMPOSITE\" SET \"ORDID\" = 'ORD01', \"PRODUCTID\" = 'PROD02', \"QUANTITY\" = 20, \"FAA\" = 'baz' WHERE \"ORDID\" = 'ORD01' AND \"PRODUCTID\" = 'PROD02'",
c[EngineCodes.Firebird]);
}

}
}
14 changes: 14 additions & 0 deletions QueryBuilder/ColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,18 @@ public ColumnAttribute(string name)
}

}


/// <summary>
/// This class is used as metadata on a property to determine if it is a primary key
/// </summary>
public class KeyAttribute : ColumnAttribute
{
public KeyAttribute([System.Runtime.CompilerServices.CallerMemberName] string name = "")
: base(name)
{

}

}
}
34 changes: 21 additions & 13 deletions QueryBuilder/Query.Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@ public partial class Query
{
public Query AsInsert(object data, bool returnId = false)
{
var dictionary = new Dictionary<string, object>();
var dictionary = BuildDictionaryOnInsert(data);

return AsInsert(dictionary, returnId);
}

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

foreach (var item in props)
private Dictionary<string, object> BuildDictionaryOnInsert(object data)
{

var dictionary = new Dictionary<string, object>();
var props = data.GetType().GetRuntimeProperties();

foreach (PropertyInfo property in props)
{
var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
if (attr != null)
{
dictionary.Add(attr.Name, item.GetValue(data));
}
else
if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null)
{
dictionary.Add(item.Name, item.GetValue(data));
continue;
}

var value = property.GetValue(data);

var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
var name = colAttr?.Name ?? property.Name;

dictionary.Add(name, value);
}

return AsInsert(dictionary, returnId);
return dictionary;
}

public Query AsInsert(IEnumerable<string> columns, IEnumerable<object> values)
Expand Down
41 changes: 29 additions & 12 deletions QueryBuilder/Query.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@

namespace SqlKata
{

public partial class Query
{

public Query AsUpdate(object data)
{
var dictionary = new Dictionary<string, object>();
var dictionary = BuildDictionaryOnUpdate(data);
return AsUpdate(dictionary);
}

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

foreach (var item in props)
private static Dictionary<Type, List<PropertyInfo>> CacheDictionaryProperties = new Dictionary<Type, List<PropertyInfo>>();


private Dictionary<string, object> BuildDictionaryOnUpdate(object data)
{

var dictionary = new Dictionary<string, object>();
var props = data.GetType().GetRuntimeProperties();

foreach (PropertyInfo property in props)
{
var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
if (attr != null)
{
dictionary.Add(attr.Name, item.GetValue(data));
if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null){
continue;
}
else

var value = property.GetValue(data);

var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
var name = colAttr?.Name ?? property.Name;
if(colAttr != null)
{
dictionary.Add(item.Name, item.GetValue(data));
}
if((colAttr as KeyAttribute) != null)
{
this.Where(name, value);
}
}
dictionary.Add(name, value);
}

return AsUpdate(dictionary);
return dictionary;
}

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