Skip to content

Commit 7810b9c

Browse files
maca88fredericDelaporte
authored andcommitted
NH-3869 Added an option for adding comments into LINQ queries
1 parent ca534fe commit 7810b9c

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg;
13+
using NHibernate.Linq;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.Linq
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class QueryCommentTestsAsync : LinqTestCase
21+
{
22+
protected override void Configure(Configuration configuration)
23+
{
24+
configuration.SetProperty(Environment.UseSqlComments, "true");
25+
}
26+
27+
[Test]
28+
public async Task CanSetCommentOnLinqQueriesAsync()
29+
{
30+
using (var sl = new SqlLogSpy())
31+
{
32+
var comment = "This is my comment";
33+
var result = await ((from e in db.Customers
34+
where e.CompanyName == "Bon app'"
35+
select e).WithOptions(o => o.SetComment(comment)).ToListAsync());
36+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
37+
38+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
39+
}
40+
}
41+
42+
[Test]
43+
public async Task CanSetCommentOnLinqPagingQueryAsync()
44+
{
45+
using (var sl = new SqlLogSpy())
46+
{
47+
var comment = "This is my comment";
48+
var result = await ((from e in db.Customers
49+
select e).Skip(1).Take(1).WithOptions(o => o.SetComment(comment)).ToListAsync());
50+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
51+
52+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
53+
}
54+
}
55+
56+
[Test]
57+
public async Task CanSetCommentBeforeSkipOnLinqOrderedPageQueryAsync()
58+
{
59+
using (var sl = new SqlLogSpy())
60+
{
61+
var comment = "This is my comment";
62+
var result = await ((from e in db.Customers
63+
orderby e.CompanyName
64+
select e).WithOptions(o => o.SetComment(comment)).Skip(5).Take(5).ToListAsync());
65+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
66+
67+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
68+
}
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Linq;
2+
using NHibernate.Cfg;
3+
using NHibernate.Linq;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.Linq
7+
{
8+
[TestFixture]
9+
public class QueryCommentTests : LinqTestCase
10+
{
11+
protected override void Configure(Configuration configuration)
12+
{
13+
configuration.SetProperty(Environment.UseSqlComments, "true");
14+
}
15+
16+
[Test]
17+
public void CanSetCommentOnLinqQueries()
18+
{
19+
using (var sl = new SqlLogSpy())
20+
{
21+
var comment = "This is my comment";
22+
var result = (from e in db.Customers
23+
where e.CompanyName == "Bon app'"
24+
select e).WithOptions(o => o.SetComment(comment)).ToList();
25+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
26+
27+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
28+
}
29+
}
30+
31+
[Test]
32+
public void CanSetCommentOnLinqPagingQuery()
33+
{
34+
using (var sl = new SqlLogSpy())
35+
{
36+
var comment = "This is my comment";
37+
var result = (from e in db.Customers
38+
select e).Skip(1).Take(1).WithOptions(o => o.SetComment(comment)).ToList();
39+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
40+
41+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
42+
}
43+
}
44+
45+
[Test]
46+
public void CanSetCommentBeforeSkipOnLinqOrderedPageQuery()
47+
{
48+
using (var sl = new SqlLogSpy())
49+
{
50+
var comment = "This is my comment";
51+
var result = (from e in db.Customers
52+
orderby e.CompanyName
53+
select e).WithOptions(o => o.SetComment(comment)).Skip(5).Take(5).ToList();
54+
var sql = sl.Appender.GetEvents()[0].RenderedMessage;
55+
56+
Assert.That(sql.IndexOf(comment), Is.GreaterThan(0));
57+
}
58+
}
59+
}
60+
}

src/NHibernate/Linq/NhQueryableOptions.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class NhQueryableOptions
1313
protected string CacheRegion { get; private set; }
1414
protected int? Timeout { get; private set; }
1515
protected bool? ReadOnly { get; private set; }
16+
protected string Comment { get; private set; }
1617

1718
#pragma warning disable 618
1819
/// <inheritdoc />
@@ -100,6 +101,17 @@ public NhQueryableOptions SetReadOnly(bool readOnly)
100101
ReadOnly = readOnly;
101102
return this;
102103
}
104+
105+
/// <summary>
106+
/// Set a comment that will be prepended before the generated SQL.
107+
/// </summary>
108+
/// <param name="comment">The comment to prepend.</param>
109+
/// <returns><see langword="this"/> (for method chaining).</returns>
110+
public NhQueryableOptions SetComment(string comment)
111+
{
112+
Comment = comment;
113+
return this;
114+
}
103115

104116
protected internal NhQueryableOptions Clone()
105117
{
@@ -109,7 +121,8 @@ protected internal NhQueryableOptions Clone()
109121
CacheMode = CacheMode,
110122
CacheRegion = CacheRegion,
111123
Timeout = Timeout,
112-
ReadOnly = ReadOnly
124+
ReadOnly = ReadOnly,
125+
Comment = Comment
113126
};
114127
}
115128

@@ -129,6 +142,9 @@ protected internal void Apply(IQuery query)
129142

130143
if (ReadOnly.HasValue)
131144
query.SetReadOnly(ReadOnly.Value);
145+
146+
if (!string.IsNullOrEmpty(Comment))
147+
query.SetComment(Comment);
132148
}
133149
}
134150
}

0 commit comments

Comments
 (0)