Skip to content

Fix for NH-2285 #200

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 4 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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ current-test-configuration
# This will be copied from the build directory to the lib directory in order
# to satisfy later build steps. But it should not be committed.
NHibernate.dll
src/packages/Antlr4.StringTemplate.4.0.6.9004/Antlr4.StringTemplate.4.0.6.9004.nupkg
src/packages/Iesi.Collections.4.0.1.4000/Iesi.Collections.4.0.1.4000.nupkg
src/packages/Iesi.Collections.4.0.1.4000/lib/net40/Iesi.Collections.xml
src/packages/Remotion.Linq.1.15.9.0/Remotion.Linq.1.15.9.0.nupkg
src/packages/Remotion.Linq.1.15.9.0/lib/net45/Remotion.Linq.xml
src/packages/log4net.2.0.3/lib/net10-full/log4net.xml
src/packages/log4net.2.0.3/lib/net11-full/log4net.xml
src/packages/log4net.2.0.3/lib/net20-full/log4net.xml
src/packages/log4net.2.0.3/lib/net35-client/log4net.xml
src/packages/log4net.2.0.3/lib/net35-full/log4net.xml
src/packages/log4net.2.0.3/lib/net40-client/log4net.xml
src/packages/log4net.2.0.3/lib/net40-full/log4net.xml
src/packages/log4net.2.0.3/log4net.2.0.3.nupkg
Binary file removed Tools/nant/bin/NDoc.Documenter.NAnt.dll
Binary file not shown.
46 changes: 46 additions & 0 deletions src/NHibernate.Test/Linq/QueryLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 @@ -518,6 +518,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
29 changes: 19 additions & 10 deletions src/NHibernate/Linq/LinqExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static IQueryable<T> Query<T>(this IStatelessSession session)

public static IQueryable<T> Cacheable<T>(this IQueryable<T> query)
{
var method = ReflectionHelper.GetMethodDefinition(() => Cacheable<object>(null)).MakeGenericMethod(typeof (T));
var method = ReflectionHelper.GetMethodDefinition(() => Cacheable<object>(null)).MakeGenericMethod(typeof(T));

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

Expand All @@ -31,7 +31,7 @@ public static IQueryable<T> Cacheable<T>(this IQueryable<T> query)

public static IQueryable<T> CacheMode<T>(this IQueryable<T> query, CacheMode cacheMode)
{
var method = ReflectionHelper.GetMethodDefinition(() => CacheMode<object>(null, NHibernate.CacheMode.Normal)).MakeGenericMethod(typeof (T));
var method = ReflectionHelper.GetMethodDefinition(() => CacheMode<object>(null, NHibernate.CacheMode.Normal)).MakeGenericMethod(typeof(T));

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

Expand All @@ -40,7 +40,7 @@ public static IQueryable<T> CacheMode<T>(this IQueryable<T> query, CacheMode cac

public static IQueryable<T> CacheRegion<T>(this IQueryable<T> query, string region)
{
var method = ReflectionHelper.GetMethodDefinition(() => CacheRegion<object>(null, null)).MakeGenericMethod(typeof (T));
var method = ReflectionHelper.GetMethodDefinition(() => CacheRegion<object>(null, null)).MakeGenericMethod(typeof(T));

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

Expand All @@ -57,15 +57,24 @@ public static IQueryable<T> Timeout<T>(this IQueryable<T> query, int timeout)
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 IEnumerable<T> ToFuture<T>(this IQueryable<T> query)
{
var nhQueryable = query as QueryableBase<T>;
if (nhQueryable == null)
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");

var provider = (INhQueryProvider) nhQueryable.Provider;
var provider = (INhQueryProvider)nhQueryable.Provider;
var future = provider.ExecuteFuture(nhQueryable.Expression);
return (IEnumerable<T>) future;
return (IEnumerable<T>)future;
}

public static IFutureValue<T> ToFutureValue<T>(this IQueryable<T> query)
Expand All @@ -74,14 +83,14 @@ public static IFutureValue<T> ToFutureValue<T>(this IQueryable<T> query)
if (nhQueryable == null)
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");

var provider = (INhQueryProvider) nhQueryable.Provider;
var provider = (INhQueryProvider)nhQueryable.Provider;
var future = provider.ExecuteFuture(nhQueryable.Expression);
if (future is IEnumerable<T>)
{
return new FutureValue<T>(() => ((IEnumerable<T>) future));
return new FutureValue<T>(() => ((IEnumerable<T>)future));
}

return (IFutureValue<T>) future;
return (IFutureValue<T>)future;
}

public static IFutureValue<TResult> ToFutureValue<T, TResult>(this IQueryable<T> query, Expression<Func<IQueryable<T>, TResult>> selector)
Expand All @@ -90,13 +99,13 @@ public static IFutureValue<TResult> ToFutureValue<T, TResult>(this IQueryable<T>
if (nhQueryable == null)
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");

var provider = (INhQueryProvider) query.Provider;
var provider = (INhQueryProvider)query.Provider;

var expression = ReplacingExpressionTreeVisitor.Replace(selector.Parameters.Single(),
query.Expression,
selector.Body);

return (IFutureValue<TResult>) provider.ExecuteFuture(expression);
return (IFutureValue<TResult>)provider.ExecuteFuture(expression);
}
}
}
69 changes: 66 additions & 3 deletions src/NHibernate/Linq/NhRelinqQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ public NHibernateNodeTypeProvider()
new[]
{
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.Timeout<object>(null, 0)),
}, typeof (TimeoutExpressionNode)
}, typeof(TimeoutExpressionNode)
);

methodInfoRegistry.Register(
new[]
{
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.SetLockMode<object>(null, LockMode.Read)),
}, typeof(LockExpressionNode)
);
var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
nodeTypeProvider.InnerProviders.Add(methodInfoRegistry);
defaultNodeTypeProvider = nodeTypeProvider;
Expand All @@ -100,7 +106,8 @@ public System.Type GetNodeType(MethodInfo method)

public class AsQueryableExpressionNode : MethodCallExpressionNodeBase
{
public AsQueryableExpressionNode(MethodCallExpressionParseInfo parseInfo) : base(parseInfo)
public AsQueryableExpressionNode(MethodCallExpressionParseInfo parseInfo)
: base(parseInfo)
{
}

Expand All @@ -120,7 +127,8 @@ public class CacheableExpressionNode : ResultOperatorExpressionNodeBase
private readonly MethodCallExpressionParseInfo _parseInfo;
private readonly ConstantExpression _data;

public CacheableExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression data) : base(parseInfo, null, null)
public CacheableExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression data)
: base(parseInfo, null, null)
{
_parseInfo = parseInfo;
_data = data;
Expand Down Expand Up @@ -192,6 +200,29 @@ 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);
}
}

internal class TimeoutResultOperator : ResultOperatorBase
{
public MethodCallExpressionParseInfo ParseInfo { get; private set; }
Expand Down Expand Up @@ -222,4 +253,36 @@ 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)
{
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class QueryReferenceExpressionFlattener : ExpressionTreeVisitor
{
typeof(CacheableResultOperator),
typeof (TimeoutResultOperator),
typeof (LockResultOperator),
};

private QueryReferenceExpressionFlattener(QueryModel model)
Expand All @@ -33,7 +34,7 @@ protected override Expression VisitSubQueryExpression(SubQueryExpression subQuer
{
var subQueryModel = subQuery.QueryModel;
var hasBodyClauses = subQueryModel.BodyClauses.Count > 0;
if(hasBodyClauses)
if (hasBodyClauses)
{
return base.VisitSubQueryExpression(subQuery);
}
Expand Down
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,15 @@
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 @@ -313,6 +313,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