Skip to content

[Ignore] atbt discerning Insert/Update #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions QueryBuilder.Tests/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class InsertTests : TestSupport
{
private class Account
{
public Account(string name, string currency = null, string created_at = null, string color = null)
public Account(string name, string currency = null, string created_at = null, string color = null, int pages = 0, int serie = 1)
{
this.name = name ?? throw new ArgumentNullException(nameof(name));
this.Currency = currency;
this.color = color;
this.pages = pages;
this.serie = serie;
}

public string name { get; set; }
Expand All @@ -27,6 +29,12 @@ public Account(string name, string currency = null, string created_at = null, st

[Ignore]
public string color { get; set; }

[Ignore(IgnoreOperation.OnUpdate)]
public int pages { get; set; }

[Ignore(IgnoreOperation.OnInsert)]
public int serie { get; set; }
}

[Fact]
Expand Down Expand Up @@ -163,17 +171,17 @@ public void InsertWithByteArray()
[Fact]
public void InsertWithIgnoreAndColumnProperties()
{
var account = new Account(name: $"popular", color: $"blue", currency: "US");
var account = new Account(name: $"popular", color: $"blue", currency: "US", pages: 100, serie: 10);
var query = new Query("Account").AsInsert(account);

var c = Compile(query);

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

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

Expand Down
16 changes: 12 additions & 4 deletions QueryBuilder.Tests/UpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class UpdateTests : TestSupport
{
private class Book
{
public Book(string name, string author, decimal price = 1.0m, string color = null)
public Book(string name, string author, decimal price = 1.0m, string color = null, int pages = 0, int serie = 1)
{
this.Name = name ?? throw new ArgumentNullException(nameof(name));
this.BookPrice = price;
this.color = color;
this.BookAuthor = author;
this.pages = pages;
this.serie = serie;
}

public string Name { get; set; }
Expand All @@ -31,6 +33,12 @@ public Book(string name, string author, decimal price = 1.0m, string color = nul

[Ignore]
public string color { get; set; }

[Ignore(IgnoreOperation.OnUpdate)]
public int pages { get; set; }

[Ignore(IgnoreOperation.OnInsert)]
public int serie { get; set; }
}

private class OrderProductComposite
Expand Down Expand Up @@ -132,17 +140,17 @@ public void UpdateWithCte()
[Fact]
public void UpdateWithIgnoreAndColumnProperties()
{
var book = new Book(name: $"SqlKataBook", author: "Kata", color: $"red", price: 100m);
var book = new Book(name: $"SqlKataBook", author: "Kata", color: $"red", price: 100m, pages: 100, serie:10);
var query = new Query("Book").AsUpdate(book);

var c = Compile(query);

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

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

Expand Down
12 changes: 12 additions & 0 deletions QueryBuilder/IgnoreAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,17 @@ namespace SqlKata
/// </example>
public class IgnoreAttribute : Attribute
{
public IgnoreOperation IgnoreOperation { get; }
public IgnoreAttribute(IgnoreOperation ignoreOperation = IgnoreOperation.Always)
{
IgnoreOperation = ignoreOperation;
}
}

public enum IgnoreOperation
{
OnInsert,
OnUpdate,
Always
}
}
2 changes: 1 addition & 1 deletion QueryBuilder/Query.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Query
{
public Query AsUpdate(object data)
{
var dictionary = BuildKeyValuePairsFromObject(data, considerKeys: true);
var dictionary = BuildKeyValuePairsFromObject(data, considerKeys: true, insert: false);

return AsUpdate(dictionary);
}
Expand Down
6 changes: 4 additions & 2 deletions QueryBuilder/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,16 @@ public object FindVariable(string variable)
/// and will add it automatically to the Where clause
/// </param>
/// <returns></returns>
private IEnumerable<KeyValuePair<string, object>> BuildKeyValuePairsFromObject(object data, bool considerKeys = false)
private IEnumerable<KeyValuePair<string, object>> BuildKeyValuePairsFromObject(object data, bool considerKeys = false, bool insert= true)
{
var dictionary = new Dictionary<string, object>();
var props = CacheDictionaryProperties.GetOrAdd(data.GetType(), type => type.GetRuntimeProperties().ToArray());

foreach (var property in props)
{
if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null)
if ((property.GetCustomAttribute(typeof(IgnoreAttribute)) != null && ((IgnoreAttribute)property.GetCustomAttribute(typeof(IgnoreAttribute))).IgnoreOperation == IgnoreOperation.Always) ||
(property.GetCustomAttribute(typeof(IgnoreAttribute)) != null && ((IgnoreAttribute)property.GetCustomAttribute(typeof(IgnoreAttribute))).IgnoreOperation == IgnoreOperation.OnInsert && insert) ||
(property.GetCustomAttribute(typeof(IgnoreAttribute)) != null && ((IgnoreAttribute)property.GetCustomAttribute(typeof(IgnoreAttribute))).IgnoreOperation == IgnoreOperation.OnUpdate && !insert))
{
continue;
}
Expand Down