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
35 changes: 35 additions & 0 deletions QueryBuilder.Tests/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,40 @@ public void InsertWithByteArray()
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (?, ?)", exemplar.RawSql);
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (@p0, @p1)", exemplar.Sql);
}


private class Account
{
public Account(string name, string currency = null, string created_at = null, string color = null)
{
this.name = name ?? throw new ArgumentNullException("name must be provided");
this.Currency = currency;
this.color = color;
}

public string name { get; set; }
[Column("currency_id")]
public string Currency { get; set; }
[Ignore]
public string color { get; set; }
}

[Fact]
public void InsertWithIgnoreAndColumnProperties()
{
var account = new Account(name: $"popular", color: $"blue", currency: "US");
var query = new Query("Account").AsInsert(account);

var c = Compile(query);

Assert.Equal(
"INSERT INTO [Account] ([name], [currency_id]) VALUES ('popular', 'US')",
c[EngineCodes.SqlServer]);

Assert.Equal(
"INSERT INTO \"ACCOUNT\" (\"NAME\", \"CURRENCY_ID\") VALUES ('popular', 'US')",
c[EngineCodes.Firebird]);
}

}
}
39 changes: 39 additions & 0 deletions QueryBuilder.Tests/UpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,44 @@ public void UpdateWithCte()
$"WITH [OldBooks] AS (SELECT * FROM [Books] WHERE [Date] < '{now}')\nUPDATE [Books] SET [Price] = '150' WHERE [Price] > 100",
c[EngineCodes.SqlServer]);
}


private class Book
{
public Book(string name, string author, decimal price = 1.0m, string color = null)
{
this.Name = name ?? throw new ArgumentNullException("name must be provided");
this.BookPrice = price;
this.color = color;
this.BookAuthor = author;
}

public string Name { get; set; }
[Column("Author")]
public string BookAuthor { get; set; }
[Column("Price")]
public decimal BookPrice { get; set; }
[Ignore]
public string color { get; set; }
}

[Fact]
public void UpdateWithIgnoreAndColumnProperties()
{
var book = new Book(name: $"SqlKataBook", author: "Kata", color: $"red", price: 100m);
var query = new Query("Book").AsUpdate(book);

var c = Compile(query);

Assert.Equal(
"UPDATE [Book] SET [Name] = 'SqlKataBook', [Author] = 'Kata', [Price] = 100",
c[EngineCodes.SqlServer]);


Assert.Equal(
"UPDATE \"BOOK\" SET \"NAME\" = 'SqlKataBook', \"AUTHOR\" = 'Kata', \"PRICE\" = 100",
c[EngineCodes.Firebird]);
}

}
}
21 changes: 21 additions & 0 deletions QueryBuilder/ColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace SqlKata
{
/// <summary>
/// This class is used as metadata on a property to generate different name in the output query.
/// </summary>
public class ColumnAttribute : Attribute
{
public string Name { get; private set; }
public ColumnAttribute(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("Name parameter is required");
}
Name = name;
}

}
}
24 changes: 24 additions & 0 deletions QueryBuilder/IgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace SqlKata
{
/// <summary>
/// This class is used as metadata to ignore a property on an object in order to exclude it from query
/// </summary>
/// <example>
/// <code>
/// public class Person
/// {
/// public string Name {get ;set;}
/// [Ignore]
/// public string PhoneNumber {get ;set;}
///
/// }
///
/// output: SELECT [Name] FROM [Person]
/// </code>
/// </example>
public class IgnoreAttribute : Attribute
{
}
}
14 changes: 12 additions & 2 deletions QueryBuilder/Query.Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ public Query AsInsert(object data, bool returnId = false)
{
var dictionary = new Dictionary<string, object>();

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

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

return AsInsert(dictionary, returnId);
Expand Down
13 changes: 11 additions & 2 deletions QueryBuilder/Query.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ public Query AsUpdate(object data)
{
var dictionary = new Dictionary<string, object>();

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

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

return AsUpdate(dictionary);
Expand Down