Skip to content

Commit bf1e116

Browse files
bahusoidfredericDelaporte
authored andcommitted
Use generic CollectingNodeVisitor in hql parser (#2039)
1 parent 7ae2b5d commit bf1e116

File tree

9 files changed

+73
-40
lines changed

9 files changed

+73
-40
lines changed

src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private void PostProcessDML(IRestrictableStatement statement)
390390
{
391391
statement.FromClause.Resolve();
392392

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

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

591-
foreach (FromElement fromElement in from.GetFromElements())
591+
foreach (FromElement fromElement in from.GetFromElementsTyped())
592592
{
593593
fromElement.IncludeSubclasses = false;
594594
}
@@ -966,10 +966,10 @@ bool IsNonQualifiedPropertyRef(IASTNode ident)
966966
return false;
967967
}
968968

969-
IList<IASTNode> fromElements = _currentFromClause.GetExplicitFromElements();
970-
if ( fromElements.Count == 1 )
969+
var fromElements = _currentFromClause.GetExplicitFromElementsTyped();
970+
if ( fromElements.Count == 1 )
971971
{
972-
FromElement fromElement = (FromElement) fromElements[0];
972+
FromElement fromElement = fromElements[0];
973973

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

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

983983
IASTNode LookupNonQualifiedProperty(IASTNode property)
984984
{
985-
FromElement fromElement = (FromElement) _currentFromClause.GetExplicitFromElements()[0];
985+
FromElement fromElement = _currentFromClause.GetExplicitFromElementsTyped()[0];
986986
IASTNode syntheticDotNode = GenerateSyntheticDotNodeForNonQualifiedPropertyRef( property, fromElement );
987987
return LookupProperty( syntheticDotNode, false, _currentClauseType == SELECT );
988988
}

src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ public bool ContainsCollectionFetches
285285
get
286286
{
287287
ErrorIfDML();
288-
IList<IASTNode> collectionFetches = ((QueryNode)_sqlAst).FromClause.GetCollectionFetches();
289-
return collectionFetches != null && collectionFetches.Count > 0;
288+
var collectionFetches = ((QueryNode)_sqlAst).FromClause.GetCollectionFetchesTyped();
289+
return collectionFetches.Count > 0;
290290
}
291291
}
292292

src/NHibernate/Hql/Ast/ANTLR/Tree/AssignmentSpecification.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public AssignmentSpecification(IASTNode eq, IQueryable persister)
7575
}
7676
else
7777
{
78-
var parameterList = ASTUtil.CollectChildren(rhs, IsParam);
78+
var parameterList = ASTUtil.CollectChildren<ParameterNode>(rhs, IsParam);
7979
_hqlParameters = new IParameterSpecification[parameterList.Count];
8080
int i = 0;
8181
foreach (ParameterNode parameterNode in parameterList)
@@ -150,4 +150,4 @@ public SqlString SqlAssignmentFragment
150150

151151
}
152152
}
153-
}
153+
}

src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,27 @@ public FromClause ParentFromClause
7272
{
7373
get { return _parentFromClause; }
7474
}
75-
75+
76+
//6.0 TODO: Replace with Typed version below
7677
public IList<IASTNode> GetExplicitFromElements()
7778
{
78-
return ASTUtil.CollectChildren(this, ExplicitFromPredicate);
79+
return ASTUtil.CollectChildren<IASTNode>(this, ExplicitFromPredicate);
7980
}
80-
81+
82+
internal IList<FromElement> GetExplicitFromElementsTyped()
83+
{
84+
return ASTUtil.CollectChildren<FromElement>(this, ExplicitFromPredicate);
85+
}
86+
87+
//6.0 TODO: Replace with Typed version below
8188
public IList<IASTNode> GetCollectionFetches()
8289
{
83-
return ASTUtil.CollectChildren(this, CollectionFetchPredicate);
90+
return ASTUtil.CollectChildren<IASTNode>(this, CollectionFetchPredicate);
91+
}
92+
93+
internal IList<FromElement> GetCollectionFetchesTyped()
94+
{
95+
return ASTUtil.CollectChildren<FromElement>(this, CollectionFetchPredicate);
8496
}
8597

8698
public FromElement FindCollectionJoin(String path)
@@ -198,27 +210,39 @@ public FromElement GetFromElement(string aliasOrClassName)
198210
return fromElement;
199211
}
200212

213+
//6.0 TODO: Replace with Typed version below
201214
/// <summary>
202215
/// Returns the list of from elements in order.
203216
/// </summary>
204217
/// <returns>The list of from elements (instances of FromElement).</returns>
205218
public IList<IASTNode> GetFromElements()
206219
{
207-
return ASTUtil.CollectChildren(this, FromElementPredicate);
220+
return ASTUtil.CollectChildren<IASTNode>(this, FromElementPredicate);
208221
}
209222

223+
internal IList<FromElement> GetFromElementsTyped()
224+
{
225+
return ASTUtil.CollectChildren<FromElement>(this, FromElementPredicate);
226+
}
227+
228+
//6.0 TODO: Replace with Typed version below
210229
/// <summary>
211230
/// Returns the list of from elements that will be part of the result set.
212231
/// </summary>
213232
/// <returns>the list of from elements that will be part of the result set.</returns>
214233
public IList<IASTNode> GetProjectionList()
215234
{
216-
return ASTUtil.CollectChildren(this, ProjectionListPredicate);
235+
return ASTUtil.CollectChildren<IASTNode>(this, ProjectionListPredicate);
236+
}
237+
238+
internal IList<FromElement> GetProjectionListTyped()
239+
{
240+
return ASTUtil.CollectChildren<FromElement>(this, ProjectionListPredicate);
217241
}
218242

219243
public FromElement GetFromElement()
220244
{
221-
return (FromElement)GetFromElements()[0];
245+
return GetFromElementsTyped()[0];
222246
}
223247

224248
public void AddDuplicateAlias(string alias, FromElement element)

src/NHibernate/Hql/Ast/ANTLR/Tree/IdentNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@ private IType GetNakedPropertyType(FromElement fromElement)
314314

315315
private FromElement LocateSingleFromElement()
316316
{
317-
IList<IASTNode> fromElements = Walker.CurrentFromClause.GetFromElements();
317+
var fromElements = Walker.CurrentFromClause.GetFromElementsTyped();
318318
if (fromElements == null || fromElements.Count != 1)
319319
{
320320
// TODO : should this be an error?
321321
return null;
322322
}
323323

324-
FromElement element = (FromElement)fromElements[0];
324+
FromElement element = fromElements[0];
325325
if (element.ClassAlias != null)
326326
{
327327
// naked property-refs cannot be used with an aliased from element

src/NHibernate/Hql/Ast/ANTLR/Tree/SelectClause.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void InitializeDerivedSelectClause(FromClause fromClause)
5151
// // NOTE : the isSubQuery() bit is a temporary hack...
5252
// throw new QuerySyntaxException( "JPA-QL compliance requires select clause" );
5353
// }
54-
IList<IASTNode> fromElements = fromClause.GetProjectionList();
54+
var fromElements = fromClause.GetProjectionListTyped();
5555

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

187187
ASTAppender appender = new ASTAppender(ASTFactory, this); // Get ready to start adding nodes.
188188
int size = fromElements.Count;

src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ public static IASTNode FindTypeInChildren(IASTNode parent, int type)
9999
return null;
100100
}
101101

102+
//Since 5.3
103+
[Obsolete("Use generic version instead")]
102104
public static IList<IASTNode> CollectChildren(IASTNode root, FilterPredicate predicate)
103105
{
104-
return new CollectingNodeVisitor(predicate).Collect(root);
106+
return new CollectingNodeVisitor<IASTNode>(predicate).Collect(root);
107+
}
108+
109+
public static IList<TNode> CollectChildren<TNode>(IASTNode root, FilterPredicate predicate) where TNode : IASTNode
110+
{
111+
return new CollectingNodeVisitor<TNode>(predicate).Collect(root);
105112
}
106113

107114
/// <summary>

src/NHibernate/Hql/Ast/ANTLR/Util/CollectingNodeVisitor.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ namespace NHibernate.Hql.Ast.ANTLR.Util
1212
[CLSCompliant(false)]
1313
public delegate bool FilterPredicate(IASTNode node);
1414

15+
//Since 5.3
16+
[Obsolete("Use generic version instead")]
1517
[CLSCompliant(false)]
16-
public class CollectingNodeVisitor : IVisitationStrategy
18+
public class CollectingNodeVisitor : CollectingNodeVisitor<IASTNode>
1719
{
18-
private readonly List<IASTNode> collectedNodes = new List<IASTNode>();
20+
public CollectingNodeVisitor(FilterPredicate predicate) : base(predicate)
21+
{
22+
}
23+
}
24+
25+
[CLSCompliant(false)]
26+
public class CollectingNodeVisitor<TNode> : IVisitationStrategy
27+
{
28+
private readonly List<TNode> collectedNodes = new List<TNode>();
1929
private readonly FilterPredicate predicate;
2030

2131
public CollectingNodeVisitor(FilterPredicate predicate)
@@ -29,17 +39,17 @@ public void Visit(IASTNode node)
2939
{
3040
if (predicate == null || predicate(node))
3141
{
32-
collectedNodes.Add(node);
42+
collectedNodes.Add((TNode) node);
3343
}
3444
}
3545

3646
#endregion
3747

38-
public IList<IASTNode> Collect(IASTNode root)
48+
public IList<TNode> Collect(IASTNode root)
3949
{
4050
var traverser = new NodeTraverser(this);
4151
traverser.TraverseDepthFirst(root);
4252
return collectedNodes;
4353
}
4454
}
45-
}
55+
}

src/NHibernate/Hql/Ast/ANTLR/Util/JoinProcessor.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
73
using NHibernate.Engine;
84
using NHibernate.Hql.Ast.ANTLR.Tree;
9-
using NHibernate.Impl;
10-
using NHibernate.Param;
115
using NHibernate.SqlCommand;
12-
using NHibernate.Type;
13-
using NHibernate.Util;
146

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

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

8981
for (int i = t.Count - 1; i >= 0; i--)
9082
{
@@ -93,7 +85,7 @@ public void ProcessJoins(IRestrictableStatement query)
9385
}
9486
else
9587
{
96-
fromElements = fromClause.GetFromElements();
88+
fromElements = fromClause.GetFromElementsTyped();
9789
}
9890

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

0 commit comments

Comments
 (0)