Skip to content
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

[Ignore] atbt discerning Insert/Update #722

Open
wants to merge 2 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
27 changes: 27 additions & 0 deletions QueryBuilder/IgnoreInsertAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace SqlKata
{
/// <summary>
/// This class is used as metadata to ignore a property on insert queries
/// </summary>
/// <example>
/// <code>
/// 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')
/// </code>
/// </example>
public class IgnoreInsertAttribute : Attribute
{
}
}
27 changes: 27 additions & 0 deletions QueryBuilder/IgnoreUpdateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace SqlKata
{
/// <summary>
/// This class is used as metadata to ignore a property on update queries
/// </summary>
/// <example>
/// <code>
/// 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')
/// </code>
/// </example>
public class IgnoreUpdateAttribute : Attribute
{
}
}
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) ||
(property.GetCustomAttribute(typeof(IgnoreUpdateAttribute)) != null && !insert) ||
(property.GetCustomAttribute(typeof(IgnoreInsertAttribute)) != null && insert))
{
continue;
}
Expand Down
Loading