Skip to content
Draft
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
7 changes: 5 additions & 2 deletions src/NHibernate/Linq/Visitors/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static ExpressionToHqlTranslationResults GenerateHqlQuery(QueryModel quer
}

private readonly IntermediateHqlTree _hqlTree;
private readonly HashSet<Expression> _hqlCandidates;
private readonly NhLinqExpressionReturnType? _rootReturnType;
private static readonly ResultOperatorMap ResultOperatorMap;
private bool _serverSide = true;
Expand Down Expand Up @@ -165,6 +166,7 @@ private QueryModelVisitor(VisitorParameters visitorParameters, bool root, QueryM
_root = root;
_rootReturnType = root ? rootReturnType : null;
_hqlTree = new IntermediateHqlTree(root, _queryMode);
_hqlCandidates = new HashSet<Expression>();
}

private void Visit()
Expand Down Expand Up @@ -476,7 +478,7 @@ public override void VisitSelectClause(SelectClause selectClause, QueryModel que

private HqlSelect GetSelectClause(Expression selectClause)
{
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters);
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters, _hqlCandidates);

visitor.VisitSelector(selectClause, !_root);

Expand Down Expand Up @@ -537,7 +539,7 @@ private void VisitDeleteClause(Expression expression)
return;

// We only need to check there is no unexpected select, for avoiding silently ignoring them.
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters);
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters, new HashSet<Expression>());
visitor.VisitSelector(expression);

if (visitor.ProjectionExpression != null)
Expand Down Expand Up @@ -566,6 +568,7 @@ public override void VisitOrderByClause(OrderByClause orderByClause, QueryModel
: (HqlDirectionStatement)_hqlTree.TreeBuilder.Descending();

_hqlTree.AddOrderByClause(orderBy, direction);
_hqlCandidates.Add(clause.Expression);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/NHibernate/Linq/Visitors/SelectClauseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ public class SelectClauseVisitor : RelinqExpressionVisitor
private HashSet<Expression> _hqlNodes;
private readonly ParameterExpression _inputParameter;
private readonly VisitorParameters _parameters;
private readonly HashSet<Expression> _hqlCandidates;
private int _iColumn;
private List<HqlExpression> _hqlTreeNodes = new List<HqlExpression>();
private readonly HqlGeneratorExpressionVisitor _hqlVisitor;

public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters)
// Since v5.6
[Obsolete("Use overload providing expressions to be executed with HQL.")]
public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters) :
this(inputType, parameters, new HashSet<Expression>())
{ }

public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters, HashSet<Expression> hqlCandidates)
{
_inputParameter = Expression.Parameter(inputType, "input");
_parameters = parameters;
_hqlCandidates = hqlCandidates;
_hqlVisitor = new HqlGeneratorExpressionVisitor(_parameters);
}

Expand Down Expand Up @@ -69,9 +77,9 @@ public void VisitSelector(Expression expression, bool isSubQuery)

if (distinct != null)
{
var treeNodes = new List<HqlTreeNode>(_hqlTreeNodes.Count + 1) {_hqlTreeBuilder.Distinct()};
var treeNodes = new List<HqlTreeNode>(_hqlTreeNodes.Count + 1) { _hqlTreeBuilder.Distinct() };
treeNodes.AddRange(_hqlTreeNodes);
_hqlTreeNodes = new List<HqlExpression>(1) {_hqlTreeBuilder.ExpressionSubTreeHolder(treeNodes)};
_hqlTreeNodes = new List<HqlExpression>(1) { _hqlTreeBuilder.ExpressionSubTreeHolder(treeNodes) };
}
}

Expand All @@ -82,7 +90,7 @@ public override Expression Visit(Expression expression)
return null;
}

if (_hqlNodes.Contains(expression))
if (_hqlNodes.Contains(expression) || _hqlCandidates.Contains(expression))
{
// Pure HQL evaluation
_hqlTreeNodes.Add(_hqlVisitor.Visit(expression).AsExpression());
Expand Down
Loading