Skip to content

Use generic CollectingNodeVisitor in hql parser #2039

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

Merged
merged 4 commits into from
Mar 21, 2019
Merged
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
14 changes: 7 additions & 7 deletions src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private void PostProcessDML(IRestrictableStatement statement)
{
statement.FromClause.Resolve();

var fromElement = (FromElement)statement.FromClause.GetFromElements()[0];
var fromElement = statement.FromClause.GetFromElementsTyped()[0];
IQueryable persister = fromElement.Queryable;
// Make #@%$^#^&# sure no alias is applied to the table name
fromElement.Text = persister.TableName;
Expand Down Expand Up @@ -495,7 +495,7 @@ void ProcessQuery(IASTNode select, IASTNode query)
joinProcessor.ProcessJoins(rs);

// Attach any mapping-defined "ORDER BY" fragments
foreach (FromElement fromElement in qn.FromClause.GetProjectionList())
foreach (FromElement fromElement in qn.FromClause.GetProjectionListTyped())
{
if ( fromElement.IsFetch && fromElement.QueryableCollection != null )
{
Expand Down Expand Up @@ -588,7 +588,7 @@ void BeforeSelectClause()
// Turn off includeSubclasses on all FromElements.
FromClause from = CurrentFromClause;

foreach (FromElement fromElement in from.GetFromElements())
foreach (FromElement fromElement in from.GetFromElementsTyped())
{
fromElement.IncludeSubclasses = false;
}
Expand Down Expand Up @@ -966,10 +966,10 @@ bool IsNonQualifiedPropertyRef(IASTNode ident)
return false;
}

IList<IASTNode> fromElements = _currentFromClause.GetExplicitFromElements();
if ( fromElements.Count == 1 )
var fromElements = _currentFromClause.GetExplicitFromElementsTyped();
if ( fromElements.Count == 1 )
{
FromElement fromElement = (FromElement) fromElements[0];
FromElement fromElement = fromElements[0];

log.Info("attempting to resolve property [{0}] as a non-qualified ref", identText);

Expand All @@ -982,7 +982,7 @@ bool IsNonQualifiedPropertyRef(IASTNode ident)

IASTNode LookupNonQualifiedProperty(IASTNode property)
{
FromElement fromElement = (FromElement) _currentFromClause.GetExplicitFromElements()[0];
FromElement fromElement = _currentFromClause.GetExplicitFromElementsTyped()[0];
IASTNode syntheticDotNode = GenerateSyntheticDotNodeForNonQualifiedPropertyRef( property, fromElement );
return LookupProperty( syntheticDotNode, false, _currentClauseType == SELECT );
}
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public bool ContainsCollectionFetches
get
{
ErrorIfDML();
IList<IASTNode> collectionFetches = ((QueryNode)_sqlAst).FromClause.GetCollectionFetches();
return collectionFetches != null && collectionFetches.Count > 0;
var collectionFetches = ((QueryNode)_sqlAst).FromClause.GetCollectionFetchesTyped();
return collectionFetches.Count > 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/AssignmentSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public AssignmentSpecification(IASTNode eq, IQueryable persister)
}
else
{
var parameterList = ASTUtil.CollectChildren(rhs, IsParam);
var parameterList = ASTUtil.CollectChildren<ParameterNode>(rhs, IsParam);
_hqlParameters = new IParameterSpecification[parameterList.Count];
int i = 0;
foreach (ParameterNode parameterNode in parameterList)
Expand Down Expand Up @@ -150,4 +150,4 @@ public SqlString SqlAssignmentFragment

}
}
}
}
38 changes: 31 additions & 7 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ public FromClause ParentFromClause
{
get { return _parentFromClause; }
}


//6.0 TODO: Replace with Typed version below
public IList<IASTNode> GetExplicitFromElements()
{
return ASTUtil.CollectChildren(this, ExplicitFromPredicate);
return ASTUtil.CollectChildren<IASTNode>(this, ExplicitFromPredicate);
}


internal IList<FromElement> GetExplicitFromElementsTyped()
{
return ASTUtil.CollectChildren<FromElement>(this, ExplicitFromPredicate);
}

//6.0 TODO: Replace with Typed version below
public IList<IASTNode> GetCollectionFetches()
{
return ASTUtil.CollectChildren(this, CollectionFetchPredicate);
return ASTUtil.CollectChildren<IASTNode>(this, CollectionFetchPredicate);
}

internal IList<FromElement> GetCollectionFetchesTyped()
{
return ASTUtil.CollectChildren<FromElement>(this, CollectionFetchPredicate);
}

public FromElement FindCollectionJoin(String path)
Expand Down Expand Up @@ -198,27 +210,39 @@ public FromElement GetFromElement(string aliasOrClassName)
return fromElement;
}

//6.0 TODO: Replace with Typed version below
/// <summary>
/// Returns the list of from elements in order.
/// </summary>
/// <returns>The list of from elements (instances of FromElement).</returns>
public IList<IASTNode> GetFromElements()
{
return ASTUtil.CollectChildren(this, FromElementPredicate);
return ASTUtil.CollectChildren<IASTNode>(this, FromElementPredicate);
}

internal IList<FromElement> GetFromElementsTyped()
{
return ASTUtil.CollectChildren<FromElement>(this, FromElementPredicate);
}

//6.0 TODO: Replace with Typed version below
/// <summary>
/// Returns the list of from elements that will be part of the result set.
/// </summary>
/// <returns>the list of from elements that will be part of the result set.</returns>
public IList<IASTNode> GetProjectionList()
{
return ASTUtil.CollectChildren(this, ProjectionListPredicate);
return ASTUtil.CollectChildren<IASTNode>(this, ProjectionListPredicate);
}

internal IList<FromElement> GetProjectionListTyped()
{
return ASTUtil.CollectChildren<FromElement>(this, ProjectionListPredicate);
}

public FromElement GetFromElement()
{
return (FromElement)GetFromElements()[0];
return GetFromElementsTyped()[0];
}

public void AddDuplicateAlias(string alias, FromElement element)
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/IdentNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ private IType GetNakedPropertyType(FromElement fromElement)

private FromElement LocateSingleFromElement()
{
IList<IASTNode> fromElements = Walker.CurrentFromClause.GetFromElements();
var fromElements = Walker.CurrentFromClause.GetFromElementsTyped();
if (fromElements == null || fromElements.Count != 1)
{
// TODO : should this be an error?
return null;
}

FromElement element = (FromElement)fromElements[0];
FromElement element = fromElements[0];
if (element.ClassAlias != null)
{
// naked property-refs cannot be used with an aliased from element
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/SelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void InitializeDerivedSelectClause(FromClause fromClause)
// // NOTE : the isSubQuery() bit is a temporary hack...
// throw new QuerySyntaxException( "JPA-QL compliance requires select clause" );
// }
IList<IASTNode> fromElements = fromClause.GetProjectionList();
var fromElements = fromClause.GetProjectionListTyped();

ASTAppender appender = new ASTAppender(ASTFactory, this); // Get ready to start adding nodes.
int size = fromElements.Count;
Expand Down Expand Up @@ -182,7 +182,7 @@ public void InitializeExplicitSelectClause(FromClause fromClause)
if (!Walker.IsShallowQuery)
{
// add the fetched entities
IList<IASTNode> fromElements = fromClause.GetProjectionList();
var fromElements = fromClause.GetProjectionListTyped();

ASTAppender appender = new ASTAppender(ASTFactory, this); // Get ready to start adding nodes.
int size = fromElements.Count;
Expand Down
9 changes: 8 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ public static IASTNode FindTypeInChildren(IASTNode parent, int type)
return null;
}

//Since 5.3
[Obsolete("Use generic version instead")]
public static IList<IASTNode> CollectChildren(IASTNode root, FilterPredicate predicate)
{
return new CollectingNodeVisitor(predicate).Collect(root);
return new CollectingNodeVisitor<IASTNode>(predicate).Collect(root);
}

public static IList<TNode> CollectChildren<TNode>(IASTNode root, FilterPredicate predicate) where TNode : IASTNode
{
return new CollectingNodeVisitor<TNode>(predicate).Collect(root);
}

/// <summary>
Expand Down
20 changes: 15 additions & 5 deletions src/NHibernate/Hql/Ast/ANTLR/Util/CollectingNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ namespace NHibernate.Hql.Ast.ANTLR.Util
[CLSCompliant(false)]
public delegate bool FilterPredicate(IASTNode node);

//Since 5.3
[Obsolete("Use generic version instead")]
[CLSCompliant(false)]
public class CollectingNodeVisitor : IVisitationStrategy
public class CollectingNodeVisitor : CollectingNodeVisitor<IASTNode>
{
private readonly List<IASTNode> collectedNodes = new List<IASTNode>();
public CollectingNodeVisitor(FilterPredicate predicate) : base(predicate)
{
}
}

[CLSCompliant(false)]
public class CollectingNodeVisitor<TNode> : IVisitationStrategy
{
private readonly List<TNode> collectedNodes = new List<TNode>();
private readonly FilterPredicate predicate;

public CollectingNodeVisitor(FilterPredicate predicate)
Expand All @@ -29,17 +39,17 @@ public void Visit(IASTNode node)
{
if (predicate == null || predicate(node))
{
collectedNodes.Add(node);
collectedNodes.Add((TNode) node);
}
}

#endregion

public IList<IASTNode> Collect(IASTNode root)
public IList<TNode> Collect(IASTNode root)
{
var traverser = new NodeTraverser(this);
traverser.TraverseDepthFirst(root);
return collectedNodes;
}
}
}
}
16 changes: 4 additions & 12 deletions src/NHibernate/Hql/Ast/ANTLR/Util/JoinProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate.Engine;
using NHibernate.Hql.Ast.ANTLR.Tree;
using NHibernate.Impl;
using NHibernate.Param;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;

namespace NHibernate.Hql.Ast.ANTLR.Util
{
Expand Down Expand Up @@ -74,7 +66,7 @@ public void ProcessJoins(IRestrictableStatement query)
FromClause fromClause = query.FromClause;
var supportRootAlias = !(query is DeleteStatement || query is UpdateStatement);

IList<IASTNode> fromElements;
IList<FromElement> fromElements;
if ( DotNode.UseThetaStyleImplicitJoins )
{
// for regression testing against output from the old parser...
Expand All @@ -83,8 +75,8 @@ public void ProcessJoins(IRestrictableStatement query)
// expected by the old parser; this is definitely another of those "only needed
// for regression purposes". The SyntheticAndFactory, then, simply injects them as it
// encounters them.
fromElements = new List<IASTNode>();
IList<IASTNode> t = fromClause.GetFromElements();
fromElements = new List<FromElement>();
var t = fromClause.GetFromElementsTyped();

for (int i = t.Count - 1; i >= 0; i--)
{
Expand All @@ -93,7 +85,7 @@ public void ProcessJoins(IRestrictableStatement query)
}
else
{
fromElements = fromClause.GetFromElements();
fromElements = fromClause.GetFromElementsTyped();
}

// Iterate through the alias,JoinSequence pairs and generate SQL token nodes.
Expand Down