Skip to content

Commit c8e43b6

Browse files
committed
ANTLR code cleanup
1 parent ffd01eb commit c8e43b6

11 files changed

+212
-230
lines changed

src/NHibernate/Hql/Ast/ANTLR/Exec/AbstractStatementExecutor.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,9 @@ protected SqlString GenerateIdInsertSelect(IQueryable persister, string tableAli
6363

6464
string rootTableName = persister.TableName;
6565
SqlString fromJoinFragment = persister.FromJoinFragment(tableAlias, true, false);
66-
SqlString whereJoinFragment = persister.WhereJoinFragment(tableAlias, true, false);
67-
68-
select.SetFromClause(rootTableName + ' ' + tableAlias + fromJoinFragment);
69-
70-
if (whereJoinFragment == null)
71-
{
72-
whereJoinFragment = SqlString.Empty;
73-
}
74-
else
75-
{
76-
whereJoinFragment = whereJoinFragment.Trim();
77-
if (whereJoinFragment.StartsWithCaseInsensitive("and "))
78-
{
79-
whereJoinFragment = whereJoinFragment.Substring(4);
80-
}
81-
}
66+
select.SetFromClause(rootTableName + " " + tableAlias + fromJoinFragment);
67+
68+
var whereJoinFragment = GetWhereJoinFragment(persister, tableAlias);
8269

8370
SqlString userWhereClause = SqlString.Empty;
8471
if (whereClause.ChildCount != 0)
@@ -114,6 +101,24 @@ protected SqlString GenerateIdInsertSelect(IQueryable persister, string tableAli
114101
return insert.ToSqlString();
115102
}
116103

104+
private static SqlString GetWhereJoinFragment(IJoinable persister, string tableAlias)
105+
{
106+
SqlString whereJoinFragment = persister.WhereJoinFragment(tableAlias, true, false);
107+
if (whereJoinFragment == null)
108+
{
109+
whereJoinFragment = SqlString.Empty;
110+
}
111+
else
112+
{
113+
whereJoinFragment = whereJoinFragment.Trim();
114+
if (whereJoinFragment.StartsWithCaseInsensitive("and "))
115+
{
116+
whereJoinFragment = whereJoinFragment.Substring(4);
117+
}
118+
}
119+
return whereJoinFragment;
120+
}
121+
117122
protected string GenerateIdSubselect(IQueryable persister)
118123
{
119124
return "select " + StringHelper.Join(", ", persister.IdentifierColumnNames) + " from " + persister.TemporaryIdTableName;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,18 @@ private void MutateRowValueConstructorSyntax(int operandColumnSpan)
9696
private static IType ExtractDataType(IASTNode operand)
9797
{
9898
IType type = null;
99-
if ( operand is SqlNode )
99+
var sqlNode = operand as SqlNode;
100+
if ( sqlNode != null )
100101
{
101-
type = ( ( SqlNode ) operand ).DataType;
102+
type = sqlNode.DataType;
102103
}
103-
if ( type == null && operand is IExpectedTypeAwareNode )
104+
if (type == null)
104105
{
105-
type = ( ( IExpectedTypeAwareNode ) operand ).ExpectedType;
106+
var expectedTypeAwareNode = operand as IExpectedTypeAwareNode;
107+
if (expectedTypeAwareNode != null)
108+
{
109+
type = expectedTypeAwareNode.ExpectedType;
110+
}
106111
}
107112
return type;
108113
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ public override IType DataType
2929
{
3030
// WHEN Child(0) THEN Child(1)
3131
IASTNode thenClause = whenOrElseClause.GetChild(1);
32-
if (thenClause is ISelectExpression && !(thenClause is ParameterNode))
32+
if (thenClause is ISelectExpression)
3333
{
34-
return (thenClause as ISelectExpression).DataType;
34+
if (!(thenClause is ParameterNode))
35+
{
36+
return (thenClause as ISelectExpression).DataType;
37+
}
3538
}
3639
}
3740
else if (whenOrElseClause.Type == HqlParser.ELSE)
3841
{
3942
// ELSE Child(0)
4043
IASTNode elseClause = whenOrElseClause.GetChild(0);
41-
if (elseClause is ISelectExpression && !(elseClause is ParameterNode))
44+
if (elseClause is ISelectExpression)
4245
{
43-
return (elseClause as ISelectExpression).DataType;
46+
if (!(elseClause is ParameterNode))
47+
{
48+
return (elseClause as ISelectExpression).DataType;
49+
}
4450
}
4551
}
4652
else

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public FromElement AddFromElement(string path, IASTNode alias)
164164
// The path may be a reference to an alias defined in the parent query.
165165
string classAlias = ( alias == null ) ? null : alias.Text;
166166
CheckForDuplicateClassAlias( classAlias );
167-
FromElementFactory factory = new FromElementFactory(this, null, path, classAlias, null, false);
167+
var factory = new FromElementFactory(this, null, path, classAlias, null, false);
168168
return factory.AddFromElement();
169169
}
170170

@@ -284,7 +284,7 @@ private static bool ProjectionListPredicate(IASTNode node)
284284

285285
private static bool FromElementPredicate(IASTNode node)
286286
{
287-
FromElement fromElement = node as FromElement;
287+
var fromElement = node as FromElement;
288288

289289
if (fromElement != null)
290290
{
@@ -296,7 +296,7 @@ private static bool FromElementPredicate(IASTNode node)
296296

297297
static bool ExplicitFromPredicate(IASTNode node)
298298
{
299-
FromElement fromElement = node as FromElement;
299+
var fromElement = node as FromElement;
300300

301301
if (fromElement != null)
302302
{
@@ -308,7 +308,7 @@ static bool ExplicitFromPredicate(IASTNode node)
308308

309309
private static bool CollectionFetchPredicate(IASTNode node)
310310
{
311-
FromElement fromElement = node as FromElement;
311+
var fromElement = node as FromElement;
312312

313313
if (fromElement != null)
314314
{
@@ -323,7 +323,7 @@ private FromElement FindIntendedAliasedFromElementBasedOnCrazyJPARequirements(st
323323
foreach (var entry in _fromElementByClassAlias)
324324
{
325325
string alias = entry.Key;
326-
if (alias.ToLowerInvariant() == specifiedAlias.ToLowerInvariant())
326+
if (string.Equals(alias, specifiedAlias, StringComparison.InvariantCultureIgnoreCase))
327327
{
328328
return entry.Value;
329329
}

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

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public FromElementType(FromElement fromElement, IEntityPersister persister, Enti
3636
_persister = persister;
3737
_entityType = entityType;
3838

39-
if (persister != null)
40-
{
41-
fromElement.Text = ((IQueryable)persister).TableName + " " + TableAlias;
42-
}
39+
var queryable = persister as IQueryable;
40+
if (queryable != null)
41+
fromElement.Text = queryable.TableName + " " + fromElement.TableAlias;
4342
}
4443

4544
protected FromElementType(FromElement fromElement)
@@ -111,22 +110,19 @@ public JoinSequence JoinSequence
111110
{
112111
get
113112
{
114-
if ( _joinSequence != null )
113+
if (_joinSequence != null)
115114
{
116115
return _joinSequence;
117116
}
118117

119118
// Class names in the FROM clause result in a JoinSequence (the old FromParser does this).
120-
if ( _persister is IJoinable )
121-
{
122-
IJoinable joinable = ( IJoinable ) _persister;
123-
return _fromElement.SessionFactoryHelper.CreateJoinSequence().SetRoot(joinable, TableAlias );
124-
}
125-
else
119+
var joinable = _persister as IJoinable;
120+
if (joinable != null)
126121
{
127-
return null; // TODO: Should this really return null? If not, figure out something better to do here.
122+
return _fromElement.SessionFactoryHelper.CreateJoinSequence().SetRoot(joinable, TableAlias);
128123
}
129124

125+
return null; // TODO: Should this really return null? If not, figure out something better to do here.
130126
}
131127
set { _joinSequence = value; }
132128
}
@@ -144,11 +140,14 @@ public string RenderIdentifierSelect(int size, int k)
144140
// Render the identifier select fragment using the table alias.
145141
if (_fromElement.FromClause.IsSubQuery)
146142
{
143+
var queryable = Queryable;
144+
if (queryable == null)
145+
return string.Empty;
146+
147147
// TODO: Replace this with a more elegant solution.
148-
string[] idColumnNames = (_persister != null) ?
149-
((IQueryable)_persister).IdentifierColumnNames : new String[0];
148+
string[] idColumnNames = queryable.IdentifierColumnNames;
150149

151-
StringBuilder buf = new StringBuilder();
150+
var buf = new StringBuilder();
152151
for (int i = 0; i < idColumnNames.Length; i++)
153152
{
154153
buf.Append(_fromElement.TableAlias).Append('.').Append(idColumnNames[i]);
@@ -158,11 +157,11 @@ public string RenderIdentifierSelect(int size, int k)
158157
}
159158
else
160159
{
161-
if (_persister == null)
162-
{
160+
var queryable = Queryable;
161+
if (queryable == null)
163162
throw new QueryException("not an entity");
164-
}
165-
string fragment = ((IQueryable)_persister).IdentifierSelectFragment(TableAlias, GetSuffix(size, k));
163+
164+
string fragment = queryable.IdentifierSelectFragment(TableAlias, GetSuffix(size, k));
166165
return TrimLeadingCommaAndSpaces(fragment);
167166
}
168167
}
@@ -202,53 +201,37 @@ public string RenderPropertySelect(int size, int k, bool allProperties)
202201
{
203202
CheckInitialized();
204203

205-
if (_persister == null)
206-
{
204+
var queryable = Queryable;
205+
if (queryable == null)
207206
return "";
208-
}
209-
else
210-
{
211-
string fragment = ((IQueryable)_persister).PropertySelectFragment(
212-
TableAlias,
213-
GetSuffix(size, k),
214-
allProperties
215-
);
216-
return TrimLeadingCommaAndSpaces(fragment);
217-
}
207+
208+
string fragment = queryable.PropertySelectFragment(TableAlias, GetSuffix(size, k), allProperties);
209+
210+
return TrimLeadingCommaAndSpaces(fragment);
218211
}
219212

220213
public string RenderCollectionSelectFragment(int size, int k)
221214
{
222215
if (_queryableCollection == null)
223-
{
224216
return "";
225-
}
226-
else
227-
{
228-
if (_collectionSuffix == null)
229-
{
230-
_collectionSuffix = GenerateSuffix(size, k);
231-
}
232-
string fragment = _queryableCollection.SelectFragment(CollectionTableAlias, _collectionSuffix);
233-
return TrimLeadingCommaAndSpaces(fragment);
234-
}
217+
218+
if (_collectionSuffix == null)
219+
_collectionSuffix = GenerateSuffix(size, k);
220+
221+
string fragment = _queryableCollection.SelectFragment(CollectionTableAlias, _collectionSuffix);
222+
return TrimLeadingCommaAndSpaces(fragment);
235223
}
236224

237225
public string RenderValueCollectionSelectFragment(int size, int k)
238226
{
239227
if (_queryableCollection == null)
240-
{
241228
return "";
242-
}
243-
else
244-
{
245-
if (_collectionSuffix == null)
246-
{
247-
_collectionSuffix = GenerateSuffix(size, k);
248-
}
249-
string fragment = _queryableCollection.SelectFragment(TableAlias, _collectionSuffix);
250-
return TrimLeadingCommaAndSpaces(fragment);
251-
}
229+
230+
if (_collectionSuffix == null)
231+
_collectionSuffix = GenerateSuffix(size, k);
232+
233+
string fragment = _queryableCollection.SelectFragment(TableAlias, _collectionSuffix);
234+
return TrimLeadingCommaAndSpaces(fragment);
252235
}
253236

254237
public bool IsEntity
@@ -258,23 +241,15 @@ public bool IsEntity
258241

259242
public bool IsCollectionOfValuesOrComponents
260243
{
261-
get
244+
get
262245
{
263-
if (_persister == null)
264-
{
265-
if (_queryableCollection == null)
266-
{
267-
return false;
268-
}
269-
else
270-
{
271-
return !_queryableCollection.ElementType.IsEntityType;
272-
}
273-
}
274-
else
275-
{
246+
if (_persister != null)
276247
return false;
277-
}
248+
249+
if (_queryableCollection == null)
250+
return false;
251+
252+
return !_queryableCollection.ElementType.IsEntityType;
278253
}
279254
}
280255

@@ -361,7 +336,7 @@ public string[] ToColumns(string tableAlias, string path, bool inSelect)
361336
/// </summary>
362337
public IQueryable Queryable
363338
{
364-
get { return (_persister is IQueryable) ? (IQueryable) _persister : null; }
339+
get { return _persister as IQueryable; }
365340
}
366341

367342
public virtual IQueryableCollection QueryableCollection
@@ -379,7 +354,6 @@ public virtual IQueryableCollection QueryableCollection
379354
// For many-to-many joins, use the tablename from the queryable collection for the default text.
380355
_fromElement.Text = _queryableCollection.TableName + " " + TableAlias;
381356
}
382-
383357
}
384358
}
385359

@@ -481,7 +455,7 @@ private bool IsCorrelation
481455
{
482456
FromClause top = _fromElement.Walker.GetFinalFromClause();
483457
return _fromElement.FromClause != _fromElement.Walker.CurrentFromClause &&
484-
_fromElement.FromClause == top;
458+
_fromElement.FromClause == top;
485459
}
486460
}
487461

@@ -491,7 +465,7 @@ private bool IsMultiTable
491465
{
492466
// should be safe to only ever expect EntityPersister references here
493467
return _fromElement.Queryable != null &&
494-
_fromElement.Queryable.IsMultiTable;
468+
_fromElement.Queryable.IsMultiTable;
495469
}
496470
}
497471

@@ -516,6 +490,5 @@ private static string TrimLeadingCommaAndSpaces(String fragment)
516490
fragment = fragment.Trim();
517491
return fragment.Trim();
518492
}
519-
520493
}
521494
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ public void RecursiveResolve(int level, bool impliedAtRoot, string classAlias, I
8585
}
8686

8787
ResolveFirstChild();
88-
bool impliedJoin = true;
89-
90-
if ( level == RootLevel && !impliedAtRoot )
91-
{
92-
impliedJoin = false;
93-
}
88+
bool impliedJoin = !(level == RootLevel && !impliedAtRoot);
9489

9590
Resolve( true, impliedJoin, classAlias, parent );
9691
}

0 commit comments

Comments
 (0)