Skip to content

Commit 2bfbfdd

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 8219bd2 commit 2bfbfdd

File tree

3 files changed

+88
-22
lines changed

3 files changed

+88
-22
lines changed

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,28 @@ public async Task CanQueryByNewGuidAsync()
330330
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
331331
Assert.Ignore("Guid are not supported by the target database");
332332

333+
var isSupported = IsFunctionSupported("new_uuid");
333334
using (var spy = new SqlLogSpy())
334335
{
335-
var guid = Guid.NewGuid();
336-
var x = await (db.Orders.CountAsync(o => guid != Guid.NewGuid()));
336+
try
337+
{
338+
var guid = Guid.NewGuid();
339+
var x = await (db.Orders.CountAsync(o => guid != Guid.NewGuid()));
337340

338-
Assert.That(x, Is.GreaterThan(0));
339-
AssertFunctionInSql("new_uuid", spy);
341+
Assert.That(x, Is.GreaterThan(0));
342+
AssertFunctionInSql("new_uuid", spy);
343+
}
344+
catch (QueryException)
345+
{
346+
if (!isSupported && !FallbackOnPreEvaluation)
347+
// Expected failure
348+
return;
349+
throw;
350+
}
340351
}
352+
353+
if (!isSupported && !FallbackOnPreEvaluation)
354+
Assert.Fail("The test should have thrown a QueryException, but has not thrown anything");
341355
}
342356

343357
[Test]
@@ -346,16 +360,30 @@ public async Task CanSelectNewGuidAsync()
346360
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
347361
Assert.Ignore("Guid are not supported by the target database");
348362

363+
var isSupported = IsFunctionSupported("new_uuid");
349364
using (var spy = new SqlLogSpy())
350365
{
351-
var x =
352-
await (db
353-
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
354-
.OrderBy(o => o.id).Take(1).ToListAsync());
366+
try
367+
{
368+
var x =
369+
await (db
370+
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
371+
.OrderBy(o => o.id).Take(1).ToListAsync());
355372

356-
Assert.That(x, Has.Count.GreaterThan(0));
357-
AssertFunctionInSql("new_uuid", spy);
373+
Assert.That(x, Has.Count.GreaterThan(0));
374+
AssertFunctionInSql("new_uuid", spy);
375+
}
376+
catch (QueryException)
377+
{
378+
if (!isSupported && !FallbackOnPreEvaluation)
379+
// Expected failure
380+
return;
381+
throw;
382+
}
358383
}
384+
385+
if (!isSupported && !FallbackOnPreEvaluation)
386+
Assert.Fail("The test should have thrown a QueryException, but has not thrown anything");
359387
}
360388

361389
private void AssertFunctionInSql(string functionName, SqlLogSpy spy)

src/NHibernate.Test/Linq/PreEvaluationTests.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,28 @@ public void CanQueryByNewGuid()
318318
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
319319
Assert.Ignore("Guid are not supported by the target database");
320320

321+
var isSupported = IsFunctionSupported("new_uuid");
321322
using (var spy = new SqlLogSpy())
322323
{
323-
var guid = Guid.NewGuid();
324-
var x = db.Orders.Count(o => guid != Guid.NewGuid());
324+
try
325+
{
326+
var guid = Guid.NewGuid();
327+
var x = db.Orders.Count(o => guid != Guid.NewGuid());
325328

326-
Assert.That(x, Is.GreaterThan(0));
327-
AssertFunctionInSql("new_uuid", spy);
329+
Assert.That(x, Is.GreaterThan(0));
330+
AssertFunctionInSql("new_uuid", spy);
331+
}
332+
catch (QueryException)
333+
{
334+
if (!isSupported && !FallbackOnPreEvaluation)
335+
// Expected failure
336+
return;
337+
throw;
338+
}
328339
}
340+
341+
if (!isSupported && !FallbackOnPreEvaluation)
342+
Assert.Fail("The test should have thrown a QueryException, but has not thrown anything");
329343
}
330344

331345
[Test]
@@ -334,16 +348,30 @@ public void CanSelectNewGuid()
334348
if (!TestDialect.SupportsSqlType(SqlTypeFactory.Guid))
335349
Assert.Ignore("Guid are not supported by the target database");
336350

351+
var isSupported = IsFunctionSupported("new_uuid");
337352
using (var spy = new SqlLogSpy())
338353
{
339-
var x =
340-
db
341-
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
342-
.OrderBy(o => o.id).Take(1).ToList();
354+
try
355+
{
356+
var x =
357+
db
358+
.Orders.Select(o => new { id = o.OrderId, g = Guid.NewGuid() })
359+
.OrderBy(o => o.id).Take(1).ToList();
343360

344-
Assert.That(x, Has.Count.GreaterThan(0));
345-
AssertFunctionInSql("new_uuid", spy);
361+
Assert.That(x, Has.Count.GreaterThan(0));
362+
AssertFunctionInSql("new_uuid", spy);
363+
}
364+
catch (QueryException)
365+
{
366+
if (!isSupported && !FallbackOnPreEvaluation)
367+
// Expected failure
368+
return;
369+
throw;
370+
}
346371
}
372+
373+
if (!isSupported && !FallbackOnPreEvaluation)
374+
Assert.Fail("The test should have thrown a QueryException, but has not thrown anything");
347375
}
348376

349377
private void AssertFunctionInSql(string functionName, SqlLogSpy spy)

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
}

0 commit comments

Comments
 (0)