Skip to content

Refactor DependentAlias handling logic in JoinWalker #2002

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 2 commits into from
Feb 6, 2019
Merged
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
35 changes: 18 additions & 17 deletions src/NHibernate/Loader/JoinWalker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NHibernate.Collection;
using NHibernate.Engine;
Expand All @@ -18,7 +19,7 @@ public class JoinWalker
private readonly HashSet<AssociationKey> visitedAssociationKeys = new HashSet<AssociationKey>();
private readonly IDictionary<string, IFilter> enabledFilters;
private readonly IDictionary<string, IFilter> enabledFiltersForManyToOne;
private static readonly Regex aliasRegex = new Regex(@"([\w]+)\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex aliasRegex = new Regex(@"[\w]+(?=\.)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

private string[] suffixes;
private string[] collectionSuffixes;
Expand Down Expand Up @@ -206,12 +207,12 @@ protected virtual SelectMode GetSelectMode(string path)
private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
{
TopologicalSorter g = new TopologicalSorter(fields.Count);
Dictionary<string, int> _indexes = new Dictionary<string, int>();
Dictionary<string, int> indexes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

// add vertices
for (int i = 0; i < fields.Count; i++)
{
_indexes[fields[i].Alias.ToLower()] = g.AddVertex(i);
indexes[fields[i].Alias] = g.AddVertex(i);
}

// add edges
Expand All @@ -222,9 +223,9 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
{
for (int j = 0; j < dependentAlias.DependsOn.Length; j++)
{
var dependentField = dependentAlias.DependsOn[j].ToLower();
var dependentField = dependentAlias.DependsOn[j];
int end;
if (_indexes.TryGetValue(dependentField, out end))
if (indexes.TryGetValue(dependentField, out end))
{
g.AddEdge(i, end);
}
Expand All @@ -240,26 +241,26 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
/// </summary>
private void AddAssociation(string subalias, OuterJoinableAssociation association)
{
subalias = subalias.ToLower();
var dependentAlias = new DependentAlias
{
Alias = subalias,
};
_dependentAliases.Add(dependentAlias);

var dependencies = new List<string>();
var on = association.On.ToString();
if (!String.IsNullOrEmpty(on))
if (!string.IsNullOrEmpty(on))
{
var dependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (Match match in aliasRegex.Matches(on))
{
string alias = match.Groups[1].Value;
if (alias == subalias) continue;
dependencies.Add(alias.ToLower());
string alias = match.Value;
if (string.Equals(alias, subalias, StringComparison.OrdinalIgnoreCase))
continue;
dependencies.Add(alias);
}
dependentAlias.DependsOn = dependencies.ToArray();
}

_dependentAliases.Add(new DependentAlias
{
Alias = subalias,
DependsOn = dependencies.ToArray()
});

associations.Add(association);
}

Expand Down