Skip to content

Commit fd05c2e

Browse files
committed
Rename SetLockMode to WithLock
1 parent a404c65 commit fd05c2e

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

src/NHibernate.Test/Linq/QueryLock.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,47 @@ public void CanSetLockLinqQueries()
1717
using (session.BeginTransaction())
1818
{
1919
var result = (from e in db.Customers
20-
select e).SetLockMode(LockMode.Upgrade).ToList();
20+
select e).WithLock(LockMode.Upgrade).ToList();
2121

2222
Assert.That(result, Has.Count.EqualTo(91));
2323
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
2424
AssertSeparateTransactionIsLockedOut(result[0].CustomerId);
2525
}
2626
}
2727

28+
[Test]
29+
public void CanSetLockOnSubquery()
30+
{
31+
using (session.BeginTransaction())
32+
{
33+
var result = (from c in db.Customers
34+
from o in c.Orders.DefaultIfEmpty()
35+
select o).ToList();
36+
37+
Assert.That(result, Has.Count.EqualTo(91));
38+
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
39+
}
40+
}
41+
42+
[Test]
43+
public void CanSetLockOnSubqueryHql()
44+
{
45+
using (session.BeginTransaction())
46+
{
47+
session
48+
.CreateQuery("select o from Customer c join c.Orders o")
49+
.SetLockMode("o", LockMode.Upgrade)
50+
.List();
51+
}
52+
}
2853

2954
[Test]
3055
public void CanSetLockOnLinqPagingQuery()
3156
{
3257
using (session.BeginTransaction())
3358
{
3459
var result = (from e in db.Customers
35-
select e).Skip(5).Take(5).SetLockMode(LockMode.Upgrade).ToList();
60+
select e).Skip(5).Take(5).WithLock(LockMode.Upgrade).ToList();
3661

3762
Assert.That(result, Has.Count.EqualTo(5));
3863
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
@@ -48,7 +73,7 @@ public void CanLockBeforeSkipOnLinqOrderedPageQuery()
4873
var result = (from e in db.Customers
4974
orderby e.CompanyName
5075
select e)
51-
.SetLockMode(LockMode.Upgrade).Skip(5).Take(5).ToList();
76+
.WithLock(LockMode.Upgrade).Skip(5).Take(5).ToList();
5277

5378
Assert.That(result, Has.Count.EqualTo(5));
5479
Assert.That(session.GetCurrentLockMode(result[0]), Is.EqualTo(LockMode.Upgrade));
@@ -70,7 +95,7 @@ private void AssertSeparateTransactionIsLockedOut(string customerId)
7095
from e in s2.Query<Customer>()
7196
where e.CustomerId == customerId
7297
select e
73-
).SetLockMode(LockMode.UpgradeNoWait)
98+
).WithLock(LockMode.UpgradeNoWait)
7499
.WithOptions(o => o.SetTimeout(5))
75100
.ToList();
76101
Assert.That(result2, Is.Not.Null);
@@ -107,7 +132,7 @@ private static IQueryable<Customer> BuildQueryableAllCustomers(
107132
IQueryable<Customer> dbCustomers,
108133
LockMode lockMode)
109134
{
110-
return (from e in dbCustomers select e).SetLockMode(lockMode).WithOptions(o => o.SetTimeout(5));
135+
return (from e in dbCustomers select e).WithLock(lockMode).WithOptions(o => o.SetTimeout(5));
111136
}
112137
}
113138
}

src/NHibernate/Linq/LinqExtensionMethods.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,15 +2514,21 @@ public static IQueryable<T> CacheRegion<T>(this IQueryable<T> query, string regi
25142514
public static IQueryable<T> Timeout<T>(this IQueryable<T> query, int timeout)
25152515
=> query.WithOptions(o => o.SetTimeout(timeout));
25162516

2517-
public static IQueryable<T> SetLockMode<T>(this IQueryable<T> query, LockMode lockMode)
2517+
public static IQueryable<T> WithLock<T>(this IQueryable<T> query, LockMode lockMode)
25182518
{
2519-
var method = ReflectHelper.GetMethod(() => SetLockMode(query, lockMode));
2519+
var method = ReflectHelper.GetMethod(() => WithLock(query, lockMode));
25202520

25212521
var callExpression = Expression.Call(method, query.Expression, Expression.Constant(lockMode));
25222522

25232523
return new NhQueryable<T>(query.Provider, callExpression);
25242524
}
25252525

2526+
public static IEnumerable<T> WithLock<T>(this IEnumerable<T> query, LockMode lockMode)
2527+
{
2528+
throw new InvalidOperationException(
2529+
"The NHibernate.Linq.LinqExtensionMethods.WithLock(IEnumerable<T>, LockMode) method can only be used in a Linq expression.");
2530+
}
2531+
25262532
/// <summary>
25272533
/// Allows to specify the parameter NHibernate type to use for a literal in a queryable expression.
25282534
/// </summary>

src/NHibernate/Linq/LockExpressionNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq.Expressions;
23
using Remotion.Linq.Clauses;
34
using Remotion.Linq.Parsing.Structure.IntermediateModel;

src/NHibernate/Linq/NhRelinqQueryParser.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Linq.Expressions;
35
using System.Reflection;
46
using NHibernate.Linq.ExpressionTransformers;
@@ -81,7 +83,11 @@ public NHibernateNodeTypeProvider()
8183
new[] { ReflectHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.ThenFetchMany<object, object, object>(null, null)) },
8284
typeof(ThenFetchManyExpressionNode));
8385
methodInfoRegistry.Register(
84-
new[] { ReflectHelper.GetMethodDefinition(() => LinqExtensionMethods.SetLockMode<object>(null, LockMode.Read)) },
86+
new[]
87+
{
88+
ReflectHelper.GetMethodDefinition(() => default(IQueryable<object>).WithLock(LockMode.Read)),
89+
ReflectHelper.GetMethodDefinition(() => default(IEnumerable<object>).WithLock(LockMode.Read))
90+
},
8591
typeof(LockExpressionNode));
8692

8793
var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();

src/NHibernate/Linq/Visitors/SubQueryFromClauseFlattener.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public class SubQueryFromClauseFlattener : NhQueryModelVisitorBase
1212
{
1313
private static readonly System.Type[] FlattenableResultOperators =
1414
{
15-
typeof (FetchOneRequest),
16-
typeof (FetchManyRequest)
15+
typeof(FetchOneRequest),
16+
typeof(FetchManyRequest),
17+
typeof(LockResultOperator)
1718
};
1819

1920
public static void ReWrite(QueryModel queryModel)
@@ -23,16 +24,14 @@ public static void ReWrite(QueryModel queryModel)
2324

2425
public override void VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
2526
{
26-
var subQueryExpression = fromClause.FromExpression as SubQueryExpression;
27-
if (subQueryExpression != null)
27+
if (fromClause.FromExpression is SubQueryExpression subQueryExpression)
2828
FlattenSubQuery(subQueryExpression, fromClause, queryModel, index + 1);
2929
base.VisitAdditionalFromClause(fromClause, queryModel, index);
3030
}
3131

3232
public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
3333
{
34-
var subQueryExpression = fromClause.FromExpression as SubQueryExpression;
35-
if (subQueryExpression != null)
34+
if (fromClause.FromExpression is SubQueryExpression subQueryExpression)
3635
FlattenSubQuery(subQueryExpression, fromClause, queryModel, 0);
3736
base.VisitMainFromClause(fromClause, queryModel);
3837
}

0 commit comments

Comments
 (0)