Skip to content

Commit 3a6a13f

Browse files
fixup! Support evaluation of Guid.NewGuid() on db side
Add an option for failing or falling back in case of lack of dialect support
1 parent 310c756 commit 3a6a13f

File tree

3 files changed

+34
-52
lines changed

3 files changed

+34
-52
lines changed

src/NHibernate.Test/Async/Linq/PreEvaluationTests.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,6 @@ protected override void Configure(Configuration configuration)
4343
configuration.SetProperty(Environment.LinqToHqlFallbackOnPreEvaluation, FallbackOnPreEvaluation.ToString());
4444
}
4545

46-
[Test]
47-
public async Task CanQueryByNewGuidAsync()
48-
{
49-
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
50-
Assert.Ignore("Guid are not supported by the target database");
51-
52-
using (var spy = new SqlLogSpy())
53-
{
54-
var guid = Guid.NewGuid();
55-
var x = await (db.Orders.CountAsync(o => guid != Guid.NewGuid()));
56-
57-
Assert.That(x, Is.GreaterThan(0));
58-
AssertFunctionInSql("new_uuid", spy);
59-
}
60-
}
61-
62-
[Test]
63-
public async Task CanSelectNewGuidAsync()
64-
{
65-
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
66-
Assert.Ignore("Guid are not supported by the target database");
67-
68-
using (var spy = new SqlLogSpy())
69-
{
70-
var x =
71-
await (db
72-
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
73-
.OrderBy(o => o.id).Take(1).ToListAsync());
74-
75-
Assert.That(x, Has.Count.GreaterThan(0));
76-
AssertFunctionInSql("new_uuid", spy);
77-
}
78-
}
79-
8046
[Test]
8147
public async Task CanQueryByRandomDoubleAsync()
8248
{

src/NHibernate.Test/Linq/PreEvaluationTests.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,17 @@ public void CanQueryByNewGuid()
216216
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
217217
Assert.Ignore("Guid are not supported by the target database");
218218

219-
using (var spy = new SqlLogSpy())
220-
{
221-
var guid = Guid.NewGuid();
222-
var x = db.Orders.Count(o => guid != Guid.NewGuid());
219+
var isSupported = IsFunctionSupported("new_uuid");
220+
RunTest(
221+
isSupported,
222+
spy =>
223+
{
224+
var guid = Guid.NewGuid();
225+
var x = db.Orders.Count(o => guid != Guid.NewGuid());
223226

224-
Assert.That(x, Is.GreaterThan(0));
225-
AssertFunctionInSql("new_uuid", spy);
226-
}
227+
Assert.That(x, Is.GreaterThan(0));
228+
AssertFunctionInSql("new_uuid", spy);
229+
});
227230
}
228231

229232
[Test]
@@ -232,16 +235,19 @@ public void CanSelectNewGuid()
232235
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
233236
Assert.Ignore("Guid are not supported by the target database");
234237

235-
using (var spy = new SqlLogSpy())
236-
{
237-
var x =
238-
db
239-
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
240-
.OrderBy(o => o.id).Take(1).ToList();
238+
var isSupported = IsFunctionSupported("new_uuid");
239+
RunTest(
240+
isSupported,
241+
spy =>
242+
{
243+
var x =
244+
db
245+
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
246+
.OrderBy(o => o.id).Take(1).ToList();
241247

242-
Assert.That(x, Has.Count.GreaterThan(0));
243-
AssertFunctionInSql("new_uuid", spy);
244-
}
248+
Assert.That(x, Has.Count.GreaterThan(0));
249+
AssertFunctionInSql("new_uuid", spy);
250+
});
245251
}
246252

247253
[Test]

src/NHibernate/Linq/Functions/NewGuidHqlGenerator.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NHibernate.Hql.Ast;
77
using NHibernate.Linq.Visitors;
88
using NHibernate.Util;
9+
using Environment = NHibernate.Cfg.Environment;
910

1011
namespace NHibernate.Linq.Functions
1112
{
@@ -31,8 +32,17 @@ public override HqlTreeNode BuildHql(
3132

3233
public bool AllowPreEvaluation(MemberInfo member, ISessionFactoryImplementor factory)
3334
{
34-
// When not supported, allow pre-evaluation on client side as a fallback.
35-
return !factory.Dialect.Functions.ContainsKey("new_uuid");
35+
if (factory.Dialect.Functions.ContainsKey("new_uuid"))
36+
return false;
37+
38+
if (factory.Settings.LinqToHqlFallbackOnPreEvaluation)
39+
return true;
40+
41+
throw new QueryException(
42+
"Cannot translate NewGuid: new_uuid is " +
43+
$"not supported by {factory.Dialect}. Either enable the fallback on pre-evaluation " +
44+
$"({Environment.LinqToHqlFallbackOnPreEvaluation}) or evaluate NewGuid " +
45+
"outside of the query.");
3646
}
3747

3848
public bool IgnoreInstance(MemberInfo member)

0 commit comments

Comments
 (0)