Skip to content

Commit

Permalink
Make previously HQL:ed expressions automatic candidates in SELECT
Browse files Browse the repository at this point in the history
  • Loading branch information
gliljas committed Jan 3, 2025
1 parent 0c9e442 commit 45ff1cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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
10 changes: 6 additions & 4 deletions src/NHibernate/Linq/Visitors/SelectClauseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ 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)
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 +71,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 +84,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

0 comments on commit 45ff1cf

Please sign in to comment.