Skip to content

Commit 539dd61

Browse files
committed
adding ignore and column attributes
1 parent fe6ec21 commit 539dd61

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

QueryBuilder.Tests/InsertTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,40 @@ public void InsertWithByteArray()
134134
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (?, ?)", exemplar.RawSql);
135135
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (@p0, @p1)", exemplar.Sql);
136136
}
137+
138+
139+
private class Account
140+
{
141+
public Account(string name, string currency = null, string created_at = null, string color = null)
142+
{
143+
this.name = name ?? throw new ArgumentNullException("name must be provided");
144+
this.Currency = currency;
145+
this.color = color;
146+
}
147+
148+
public string name { get; set; }
149+
[Column("currency_id")]
150+
public string Currency { get; set; }
151+
[Ignore]
152+
public string color { get; set; }
153+
}
154+
155+
[Fact]
156+
public void InsertWithIgnoreAndColumnProperties()
157+
{
158+
var account = new Account(name: $"popular", color: $"blue", currency: "US");
159+
var query = new Query("Account").AsInsert(account);
160+
161+
var c = Compile(query);
162+
163+
Assert.Equal(
164+
"INSERT INTO [Account] ([name], [currency_id]) VALUES ('popular', 'US')",
165+
c[EngineCodes.SqlServer]);
166+
167+
Assert.Equal(
168+
"INSERT INTO \"ACCOUNT\" (\"NAME\", \"CURRENCY_ID\") VALUES ('popular', 'US')",
169+
c[EngineCodes.Firebird]);
170+
}
171+
137172
}
138173
}

QueryBuilder.Tests/UpdateTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,44 @@ public void UpdateWithCte()
7979
$"WITH [OldBooks] AS (SELECT * FROM [Books] WHERE [Date] < '{now}')\nUPDATE [Books] SET [Price] = '150' WHERE [Price] > 100",
8080
c[EngineCodes.SqlServer]);
8181
}
82+
83+
84+
private class Book
85+
{
86+
public Book(string name, string author, decimal price = 1.0m, string color = null)
87+
{
88+
this.Name = name ?? throw new ArgumentNullException("name must be provided");
89+
this.BookPrice = price;
90+
this.color = color;
91+
this.BookAuthor = author;
92+
}
93+
94+
public string Name { get; set; }
95+
[Column("Author")]
96+
public string BookAuthor { get; set; }
97+
[Column("Price")]
98+
public decimal BookPrice { get; set; }
99+
[Ignore]
100+
public string color { get; set; }
101+
}
102+
103+
[Fact]
104+
public void UpdateWithIgnoreAndColumnProperties()
105+
{
106+
var book = new Book(name: $"SqlKataBook", author: "Kata", color: $"red", price: 100m);
107+
var query = new Query("Book").AsUpdate(book);
108+
109+
var c = Compile(query);
110+
111+
Assert.Equal(
112+
"UPDATE [Book] SET [Name] = 'SqlKataBook', [Author] = 'Kata', [Price] = 100",
113+
c[EngineCodes.SqlServer]);
114+
115+
116+
Assert.Equal(
117+
"UPDATE \"BOOK\" SET \"NAME\" = 'SqlKataBook', \"AUTHOR\" = 'Kata', \"PRICE\" = 100",
118+
c[EngineCodes.Firebird]);
119+
}
120+
82121
}
83122
}

QueryBuilder/ColumnAttribute.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace SqlKata
4+
{
5+
/// <summary>
6+
/// This class is used as metadata on a property to generate different name in the output query.
7+
/// </summary>
8+
public class ColumnAttribute : Attribute
9+
{
10+
public string Name { get; private set; }
11+
public ColumnAttribute(string name)
12+
{
13+
if (string.IsNullOrEmpty(name))
14+
{
15+
throw new ArgumentNullException("Name parameter is required");
16+
}
17+
Name = name;
18+
}
19+
20+
}
21+
}

QueryBuilder/IgnoreAttribute.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace SqlKata
4+
{
5+
/// <summary>
6+
/// This class is used as metadata to ignore a property on an object in order to exclude it from query
7+
/// </summary>
8+
/// <example>
9+
/// <code>
10+
/// public class Person
11+
/// {
12+
/// public string Name {get ;set;}
13+
/// [Ignore]
14+
/// public string PhoneNumber {get ;set;}
15+
///
16+
/// }
17+
///
18+
/// output: SELECT [Name] FROM [Person]
19+
/// </code>
20+
/// </example>
21+
public class IgnoreAttribute : Attribute
22+
{
23+
}
24+
}

QueryBuilder/Query.Insert.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ public Query AsInsert(object data, bool returnId = false)
1111
{
1212
var dictionary = new Dictionary<string, object>();
1313

14-
var props = data.GetType().GetRuntimeProperties();
14+
var props = data.GetType()
15+
.GetRuntimeProperties()
16+
.Where(_ => _.GetCustomAttribute(typeof(IgnoreAttribute)) == null);
1517

1618
foreach (var item in props)
1719
{
18-
dictionary.Add(item.Name, item.GetValue(data));
20+
var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
21+
if (attr != null)
22+
{
23+
dictionary.Add(attr.Name, item.GetValue(data));
24+
}
25+
else
26+
{
27+
dictionary.Add(item.Name, item.GetValue(data));
28+
}
1929
}
2030

2131
return AsInsert(dictionary, returnId);

QueryBuilder/Query.Update.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@ public Query AsUpdate(object data)
1212
{
1313
var dictionary = new Dictionary<string, object>();
1414

15-
var props = data.GetType().GetRuntimeProperties();
15+
var props = data.GetType().GetRuntimeProperties()
16+
.Where(_ => _.GetCustomAttribute(typeof(IgnoreAttribute)) == null);
1617

1718
foreach (var item in props)
1819
{
19-
dictionary.Add(item.Name, item.GetValue(data));
20+
var attr = item.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
21+
if (attr != null)
22+
{
23+
dictionary.Add(attr.Name, item.GetValue(data));
24+
}
25+
else
26+
{
27+
dictionary.Add(item.Name, item.GetValue(data));
28+
}
2029
}
2130

2231
return AsUpdate(dictionary);

0 commit comments

Comments
 (0)