Skip to content

NH-2285 - Support for LockMode in linq provider #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/NHibernate.Test/Linq/QueryLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NHibernate.Engine;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Linq
{
public class QueryLock : LinqTestCase
{

[Test]
public void CanSetLockLinqQueries()
{
var result = (from e in db.Customers
where e.CompanyName == "Corp"
select e).SetLockMode(LockMode.Upgrade).ToList();

}


[Test]
public void CanSetLockOnLinqPagingQuery()
{
var result = (from e in db.Customers
where e.CompanyName == "Corp"
select e).Skip(5).Take(5).SetLockMode(LockMode.Upgrade).ToList();
}


[Test]
public void CanLockBeforeSkipOnLinqOrderedPageQuery()
{
var result = (from e in db.Customers
orderby e.CompanyName
select e)
.SetLockMode(LockMode.Upgrade).Skip(5).Take(5).ToList();


}


}

}

1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@
<Compile Include="Linq\DateTimeTests.cs" />
<Compile Include="Linq\ExpressionSessionLeakTest.cs" />
<Compile Include="Linq\LoggingTests.cs" />
<Compile Include="Linq\QueryLock.cs" />
<Compile Include="Linq\QueryTimeoutTests.cs" />
<Compile Include="Linq\JoinTests.cs" />
<Compile Include="Linq\CustomExtensionsExample.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Linq/GroupBy/AggregatingGroupByRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static class AggregatingGroupByRewriter
typeof (AnyResultOperator),
typeof (AllResultOperator),
typeof (TimeoutResultOperator),
typeof (LockResultOperator),
};

public static void ReWrite(QueryModel queryModel)
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Linq/LinqExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public static IQueryable<T> Cacheable<T>(this IQueryable<T> query)

return new NhQueryable<T>(query.Provider, callExpression);
}
public static IQueryable<T> SetLockMode<T>(this IQueryable<T> query, LockMode lockMode)
{
var method = ReflectionHelper.GetMethodDefinition(() => SetLockMode<object>(null, LockMode.Read)).MakeGenericMethod(typeof(T));

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

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


public static IQueryable<T> CacheMode<T>(this IQueryable<T> query, CacheMode cacheMode)
{
Expand Down
62 changes: 62 additions & 0 deletions src/NHibernate/Linq/NhRelinqQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public NHibernateNodeTypeProvider()
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.Timeout<object>(null, 0)),
}, typeof (TimeoutExpressionNode)
);
methodInfoRegistry.Register(
new[]
{
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.SetLockMode<object>(null, LockMode.Read)),
}, typeof(LockExpressionNode)
);


var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
nodeTypeProvider.InnerProviders.Add(methodInfoRegistry);
Expand Down Expand Up @@ -137,6 +144,30 @@ protected override ResultOperatorBase CreateResultOperator(ClauseGenerationConte
}
}

internal class LockExpressionNode : ResultOperatorExpressionNodeBase
{
private readonly MethodCallExpressionParseInfo _parseInfo;
private readonly ConstantExpression _lockMode;

public LockExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression lockMode)
: base(parseInfo, null, null)
{
_parseInfo = parseInfo;
_lockMode = lockMode;
}

public override Expression Resolve(ParameterExpression inputParameter, Expression expressionToBeResolved, ClauseGenerationContext clauseGenerationContext)
{
return Source.Resolve(inputParameter, expressionToBeResolved, clauseGenerationContext);
}

protected override ResultOperatorBase CreateResultOperator(ClauseGenerationContext clauseGenerationContext)
{
return new LockResultOperator(_parseInfo, _lockMode);
}
}


public class CacheableResultOperator : ResultOperatorBase
{
public MethodCallExpressionParseInfo ParseInfo { get; private set; }
Expand Down Expand Up @@ -222,4 +253,35 @@ public override void TransformExpressions(Func<Expression, Expression> transform
{
}
}
internal class LockResultOperator : ResultOperatorBase
{
public MethodCallExpressionParseInfo ParseInfo { get; private set; }
public ConstantExpression LockMode { get; private set; }

public LockResultOperator(MethodCallExpressionParseInfo parseInfo, ConstantExpression lockMode)
{
ParseInfo = parseInfo;
LockMode = lockMode;
}

public override IStreamedData ExecuteInMemory(IStreamedData input)
{
throw new NotImplementedException();
}

public override IStreamedDataInfo GetOutputDataInfo(IStreamedDataInfo inputInfo)
{
return inputInfo;
}

public override ResultOperatorBase Clone(CloneContext cloneContext)
{
throw new NotImplementedException();
}

public override void TransformExpressions(Func<Expression, Expression> transformation)
{
}
}

}
1 change: 1 addition & 0 deletions src/NHibernate/Linq/ReWriters/ResultOperatorRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private class ResultOperatorExpressionRewriter : ExpressionTreeVisitor
typeof(OfTypeResultOperator),
typeof(CacheableResultOperator),
typeof(TimeoutResultOperator),
typeof (LockResultOperator),
typeof(CastResultOperator), // see ProcessCast class
};

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Linq/Visitors/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static QueryModelVisitor()
ResultOperatorMap.Add<FetchManyRequest, ProcessFetchMany>();
ResultOperatorMap.Add<CacheableResultOperator, ProcessCacheable>();
ResultOperatorMap.Add<TimeoutResultOperator, ProcessTimeout>();
ResultOperatorMap.Add<LockResultOperator, ProcessLock>();
ResultOperatorMap.Add<OfTypeResultOperator, ProcessOfType>();
ResultOperatorMap.Add<CastResultOperator, ProcessCast>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
{
internal class ProcessLock : IResultOperatorProcessor<LockResultOperator>
{
public void Process(LockResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
{
tree.AddAdditionalCriteria((q, p) => q.SetLockMode(queryModelVisitor.Model.MainFromClause.ItemName, (LockMode)resultOperator.LockMode.Value));
}
}
}

1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@
<Compile Include="Linq\Visitors\PossibleValueSet.cs" />
<Compile Include="Linq\Visitors\QuerySourceIdentifier.cs" />
<Compile Include="Linq\Visitors\ResultOperatorAndOrderByJoinDetector.cs" />
<Compile Include="Linq\Visitors\ResultOperatorProcessors\ProcessLock.cs" />
<Compile Include="Linq\Visitors\ResultOperatorProcessors\ProcessTimeout.cs" />
<Compile Include="Linq\Visitors\ResultOperatorProcessors\ProcessAggregateFromSeed.cs" />
<Compile Include="Linq\NestedSelects\NestedSelectRewriter.cs" />
Expand Down