diff --git a/QueryBuilder.Tests/InsertTests.cs b/QueryBuilder.Tests/InsertTests.cs
index 926e18b2..0c72260f 100644
--- a/QueryBuilder.Tests/InsertTests.cs
+++ b/QueryBuilder.Tests/InsertTests.cs
@@ -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; }
@@ -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]
@@ -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]);
}
diff --git a/QueryBuilder.Tests/UpdateTests.cs b/QueryBuilder.Tests/UpdateTests.cs
index bf3dd7d9..52a22bb5 100644
--- a/QueryBuilder.Tests/UpdateTests.cs
+++ b/QueryBuilder.Tests/UpdateTests.cs
@@ -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; }
@@ -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
@@ -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]);
}
diff --git a/QueryBuilder/IgnoreAttribute.cs b/QueryBuilder/IgnoreAttribute.cs
index 4174d106..bf31e9a5 100644
--- a/QueryBuilder/IgnoreAttribute.cs
+++ b/QueryBuilder/IgnoreAttribute.cs
@@ -23,5 +23,17 @@ namespace SqlKata
///
public class IgnoreAttribute : Attribute
{
+ public IgnoreOperation IgnoreOperation { get; }
+ public IgnoreAttribute(IgnoreOperation ignoreOperation = IgnoreOperation.Always)
+ {
+ IgnoreOperation = ignoreOperation;
+ }
+ }
+
+ public enum IgnoreOperation
+ {
+ OnInsert,
+ OnUpdate,
+ Always
}
}
diff --git a/QueryBuilder/Query.Update.cs b/QueryBuilder/Query.Update.cs
index d88aeb00..538ef498 100644
--- a/QueryBuilder/Query.Update.cs
+++ b/QueryBuilder/Query.Update.cs
@@ -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);
}
diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs
index 8435eca6..4314051b 100755
--- a/QueryBuilder/Query.cs
+++ b/QueryBuilder/Query.cs
@@ -402,14 +402,16 @@ public object FindVariable(string variable)
/// and will add it automatically to the Where clause
///
///
- private IEnumerable> BuildKeyValuePairsFromObject(object data, bool considerKeys = false)
+ private IEnumerable> BuildKeyValuePairsFromObject(object data, bool considerKeys = false, bool insert= true)
{
var dictionary = new Dictionary();
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;
}