From f0f84c8fbe18e7085f41f358a4127f0f9a5de1e0 Mon Sep 17 00:00:00 2001 From: Miguel Lecinena Date: Tue, 23 Jul 2024 16:08:53 +0200 Subject: [PATCH 1/5] [Ignore] atbt discerning Insert/Update --- QueryBuilder/IgnoreInsertAttribute.cs | 27 +++++++++++++++++++++++++++ QueryBuilder/IgnoreUpdateAttribute.cs | 27 +++++++++++++++++++++++++++ QueryBuilder/Query.Update.cs | 2 +- QueryBuilder/Query.cs | 5 +++-- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 QueryBuilder/IgnoreInsertAttribute.cs create mode 100644 QueryBuilder/IgnoreUpdateAttribute.cs diff --git a/QueryBuilder/IgnoreInsertAttribute.cs b/QueryBuilder/IgnoreInsertAttribute.cs new file mode 100644 index 00000000..b0afc6b6 --- /dev/null +++ b/QueryBuilder/IgnoreInsertAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace SqlKata +{ + /// + /// This class is used as metadata to ignore a property on insert queries + /// + /// + /// + /// public class Person + /// { + /// public string Name {get ;set;} + /// + /// [IgnoreInsert] + /// public string PhoneNumber {get ;set;} + /// + /// } + /// + /// new Query("Table").Insert(new Person { Name = "User", PhoneNumber = "70123456" }) + /// + /// output: INSERT INTO [Table] ([Name]) VALUES('User') + /// + /// + public class IgnoreInsertAttribute : Attribute + { + } +} diff --git a/QueryBuilder/IgnoreUpdateAttribute.cs b/QueryBuilder/IgnoreUpdateAttribute.cs new file mode 100644 index 00000000..1b7cc003 --- /dev/null +++ b/QueryBuilder/IgnoreUpdateAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace SqlKata +{ + /// + /// This class is used as metadata to ignore a property on update queries + /// + /// + /// + /// public class Person + /// { + /// public string Name {get ;set;} + /// + /// [IgnoreUpdate] + /// public string PhoneNumber {get ;set;} + /// + /// } + /// + /// new Query("Table").Update(new Person { Name = "User", PhoneNumber = "70123456" }) + /// + /// output: UPDATE INTO [Table] ([Name]) VALUES('User') + /// + /// + public class IgnoreUpdateAttribute : Attribute + { + } +} 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..59f89e39 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -402,14 +402,15 @@ 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(IgnoreUpdateAttribute)) != null && !insert) || + (property.GetCustomAttribute(typeof(IgnoreInsertAttribute)) != null && insert)) { continue; } From 7cee54cc40f2e7cf1efe5bcb6da4ee4d22a0a333 Mon Sep 17 00:00:00 2001 From: Miguel Lecinena Date: Tue, 23 Jul 2024 16:21:46 +0200 Subject: [PATCH 2/5] fix: keep [Ignore] working --- QueryBuilder/Query.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index 59f89e39..d95b8dcb 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -409,7 +409,8 @@ private IEnumerable> BuildKeyValuePairsFromObject(o foreach (var property in props) { - if ((property.GetCustomAttribute(typeof(IgnoreUpdateAttribute)) != null && !insert) || + if ((property.GetCustomAttribute(typeof(IgnoreAttribute)) != null) || + (property.GetCustomAttribute(typeof(IgnoreUpdateAttribute)) != null && !insert) || (property.GetCustomAttribute(typeof(IgnoreInsertAttribute)) != null && insert)) { continue; From ec0d1b41c408d56d23bcb815609d088a71eeda41 Mon Sep 17 00:00:00 2001 From: Miguel Lecinena Date: Fri, 7 Mar 2025 18:38:26 +0100 Subject: [PATCH 3/5] fix change to work like an enum option of Ignore --- QueryBuilder/IgnoreAttribute.cs | 12 ++++++++++++ QueryBuilder/IgnoreInsertAttribute.cs | 27 --------------------------- QueryBuilder/IgnoreUpdateAttribute.cs | 27 --------------------------- QueryBuilder/Query.cs | 6 +++--- 4 files changed, 15 insertions(+), 57 deletions(-) delete mode 100644 QueryBuilder/IgnoreInsertAttribute.cs delete mode 100644 QueryBuilder/IgnoreUpdateAttribute.cs 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/IgnoreInsertAttribute.cs b/QueryBuilder/IgnoreInsertAttribute.cs deleted file mode 100644 index b0afc6b6..00000000 --- a/QueryBuilder/IgnoreInsertAttribute.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace SqlKata -{ - /// - /// This class is used as metadata to ignore a property on insert queries - /// - /// - /// - /// public class Person - /// { - /// public string Name {get ;set;} - /// - /// [IgnoreInsert] - /// public string PhoneNumber {get ;set;} - /// - /// } - /// - /// new Query("Table").Insert(new Person { Name = "User", PhoneNumber = "70123456" }) - /// - /// output: INSERT INTO [Table] ([Name]) VALUES('User') - /// - /// - public class IgnoreInsertAttribute : Attribute - { - } -} diff --git a/QueryBuilder/IgnoreUpdateAttribute.cs b/QueryBuilder/IgnoreUpdateAttribute.cs deleted file mode 100644 index 1b7cc003..00000000 --- a/QueryBuilder/IgnoreUpdateAttribute.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace SqlKata -{ - /// - /// This class is used as metadata to ignore a property on update queries - /// - /// - /// - /// public class Person - /// { - /// public string Name {get ;set;} - /// - /// [IgnoreUpdate] - /// public string PhoneNumber {get ;set;} - /// - /// } - /// - /// new Query("Table").Update(new Person { Name = "User", PhoneNumber = "70123456" }) - /// - /// output: UPDATE INTO [Table] ([Name]) VALUES('User') - /// - /// - public class IgnoreUpdateAttribute : Attribute - { - } -} diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index d95b8dcb..afea9f20 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -409,9 +409,9 @@ private IEnumerable> BuildKeyValuePairsFromObject(o foreach (var property in props) { - if ((property.GetCustomAttribute(typeof(IgnoreAttribute)) != null) || - (property.GetCustomAttribute(typeof(IgnoreUpdateAttribute)) != null && !insert) || - (property.GetCustomAttribute(typeof(IgnoreInsertAttribute)) != null && insert)) + 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; } From 337ec884510d37594c51581c29111d5c7708c01a Mon Sep 17 00:00:00 2001 From: Miguel Lecinena Date: Fri, 7 Mar 2025 18:59:15 +0100 Subject: [PATCH 4/5] feat adding UT to check the IgnoreOperation --- QueryBuilder.Tests/InsertTests.cs | 16 ++++++++++++---- QueryBuilder.Tests/UpdateTests.cs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/QueryBuilder.Tests/InsertTests.cs b/QueryBuilder.Tests/InsertTests.cs index 926e18b2..3130da38 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.OnInsert)] + public int pages { get; set; } + + [Ignore(IgnoreOperation.OnUpdate)] + 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..e6200e73 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.OnInsert)] + public int pages { get; set; } + + [Ignore(IgnoreOperation.OnUpdate)] + 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]); } From 73358c8a2bbd3bfc7da25f1cdceb4b2c714b42d0 Mon Sep 17 00:00:00 2001 From: Miguel Lecinena Date: Tue, 11 Mar 2025 10:17:38 +0100 Subject: [PATCH 5/5] fix inverted test and if --- QueryBuilder.Tests/InsertTests.cs | 4 ++-- QueryBuilder.Tests/UpdateTests.cs | 4 ++-- QueryBuilder/Query.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/QueryBuilder.Tests/InsertTests.cs b/QueryBuilder.Tests/InsertTests.cs index 3130da38..0c72260f 100644 --- a/QueryBuilder.Tests/InsertTests.cs +++ b/QueryBuilder.Tests/InsertTests.cs @@ -30,10 +30,10 @@ public Account(string name, string currency = null, string created_at = null, st [Ignore] public string color { get; set; } - [Ignore(IgnoreOperation.OnInsert)] + [Ignore(IgnoreOperation.OnUpdate)] public int pages { get; set; } - [Ignore(IgnoreOperation.OnUpdate)] + [Ignore(IgnoreOperation.OnInsert)] public int serie { get; set; } } diff --git a/QueryBuilder.Tests/UpdateTests.cs b/QueryBuilder.Tests/UpdateTests.cs index e6200e73..52a22bb5 100644 --- a/QueryBuilder.Tests/UpdateTests.cs +++ b/QueryBuilder.Tests/UpdateTests.cs @@ -34,10 +34,10 @@ public Book(string name, string author, decimal price = 1.0m, string color = nul [Ignore] public string color { get; set; } - [Ignore(IgnoreOperation.OnInsert)] + [Ignore(IgnoreOperation.OnUpdate)] public int pages { get; set; } - [Ignore(IgnoreOperation.OnUpdate)] + [Ignore(IgnoreOperation.OnInsert)] public int serie { get; set; } } diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index afea9f20..4314051b 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -410,8 +410,8 @@ private IEnumerable> BuildKeyValuePairsFromObject(o foreach (var property in props) { 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)) + (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; }