@@ -32,28 +32,26 @@ internal sealed class NhPartialEvaluatingExpressionVisitor : RelinqExpressionVis
3232 /// </summary>
3333 public static Expression EvaluateIndependentSubtrees (
3434 Expression expressionTree ,
35- IEvaluatableExpressionFilter evaluatableExpressionFilter ,
36- IDictionary < ConstantExpression , QueryVariable > queryVariables )
35+ PreTransformationParameters preTransformationParameters )
3736 {
38- var partialEvaluationInfo = EvaluatableTreeFindingExpressionVisitor . Analyze ( expressionTree , evaluatableExpressionFilter ) ;
39- var visitor = new NhPartialEvaluatingExpressionVisitor ( partialEvaluationInfo , evaluatableExpressionFilter , queryVariables ) ;
37+ var partialEvaluationInfo = EvaluatableTreeFindingExpressionVisitor . Analyze (
38+ expressionTree ,
39+ preTransformationParameters . EvaluatableExpressionFilter ) ;
40+ var visitor = new NhPartialEvaluatingExpressionVisitor ( partialEvaluationInfo , preTransformationParameters ) ;
4041
4142 return visitor . Visit ( expressionTree ) ;
4243 }
4344
4445 // _partialEvaluationInfo contains a list of the expressions that are safe to be evaluated.
4546 private readonly PartialEvaluationInfo _partialEvaluationInfo ;
46- private readonly IEvaluatableExpressionFilter _evaluatableExpressionFilter ;
47- private readonly IDictionary < ConstantExpression , QueryVariable > _queryVariables ;
47+ private readonly PreTransformationParameters _preTransformationParameters ;
4848
4949 private NhPartialEvaluatingExpressionVisitor (
5050 PartialEvaluationInfo partialEvaluationInfo ,
51- IEvaluatableExpressionFilter evaluatableExpressionFilter ,
52- IDictionary < ConstantExpression , QueryVariable > queryVariables )
51+ PreTransformationParameters preTransformationParameters )
5352 {
5453 _partialEvaluationInfo = partialEvaluationInfo ;
55- _evaluatableExpressionFilter = evaluatableExpressionFilter ;
56- _queryVariables = queryVariables ;
54+ _preTransformationParameters = preTransformationParameters ;
5755 }
5856
5957 public override Expression Visit ( Expression expression )
@@ -81,7 +79,7 @@ public override Expression Visit(Expression expression)
8179
8280 if ( evaluatedExpression != expression )
8381 {
84- evaluatedExpression = EvaluateIndependentSubtrees ( evaluatedExpression , _evaluatableExpressionFilter , _queryVariables ) ;
82+ evaluatedExpression = EvaluateIndependentSubtrees ( evaluatedExpression , _preTransformationParameters ) ;
8583 }
8684
8785 // When having multiple level closure, we have to evaluate each closure independently
@@ -90,13 +88,15 @@ public override Expression Visit(Expression expression)
9088 evaluatedExpression = VisitConstant ( constantExpression ) ;
9189 }
9290
93- // Variables in expressions are never a constant, they are encapsulated as fields of a compiler generated class
91+ // Variables in expressions are never a constant, they are encapsulated as fields of a compiler generated class.
92+ // Skip detecting variables for DML queries as HQL does not support reusing parameters for them.
9493 if ( expression . NodeType != ExpressionType . Constant &&
94+ _preTransformationParameters . QueryMode == QueryMode . Select &&
9595 evaluatedExpression is ConstantExpression variableConstant &&
96- ! _queryVariables . ContainsKey ( variableConstant ) &&
96+ ! _preTransformationParameters . QueryVariables . ContainsKey ( variableConstant ) &&
9797 IsVariable ( expression , out var path , out var closureContext ) )
9898 {
99- _queryVariables . Add ( variableConstant , new QueryVariable ( path , closureContext ) ) ;
99+ _preTransformationParameters . QueryVariables . Add ( variableConstant , new QueryVariable ( path , closureContext ) ) ;
100100 }
101101
102102 return evaluatedExpression ;
@@ -106,7 +106,7 @@ protected override Expression VisitConstant(ConstantExpression expression)
106106 {
107107 if ( expression . Value is Expression value )
108108 {
109- return EvaluateIndependentSubtrees ( value , _evaluatableExpressionFilter , _queryVariables ) ;
109+ return EvaluateIndependentSubtrees ( value , _preTransformationParameters ) ;
110110 }
111111
112112 return base . VisitConstant ( expression ) ;
0 commit comments