-
Notifications
You must be signed in to change notification settings - Fork 934
Table group joins for subclasses in Criteria #2545
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff71c45
Table group joins for subclasses in Criteria
bahusoid 3cdb206
Revert not needed change
bahusoid cee9163
Revert not needed change
bahusoid 44cf75c
Extract AppendWithClause
bahusoid 2ad5e7a
Apply suggestions from code review
bahusoid e3d19bf
Small refactoring
bahusoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using NHibernate.Persister.Entity; | ||
using NHibernate.SqlCommand; | ||
using NHibernate.Type; | ||
|
||
namespace NHibernate.Engine | ||
{ | ||
internal interface IJoin | ||
{ | ||
IJoinable Joinable { get; } | ||
string[] LHSColumns { get; } | ||
string Alias { get; } | ||
IAssociationType AssociationType { get; } | ||
JoinType JoinType { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NHibernate.Persister.Entity; | ||
using NHibernate.SqlCommand; | ||
|
||
namespace NHibernate.Engine | ||
{ | ||
//Generates table group join if neccessary. Example of generated query with table group join: | ||
// SELECT * | ||
// FROM Person person0_ | ||
// INNER JOIN ( | ||
// IndividualCustomer individual1_ | ||
// INNER JOIN Customer individual1_1_ ON individual1_.IndividualCustomerID = individual1_1_.Id | ||
// ) ON person0_.Id = individual1_.PersonID AND individual1_1_.Deleted = @p0 | ||
internal class TableGroupJoinHelper | ||
{ | ||
internal static bool ProcessAsTableGroupJoin(IReadOnlyList<IJoin> tableGroupJoinables, SqlString[] withClauseFragments, bool includeAllSubclassJoins, JoinFragment joinFragment, Func<string, bool> isSubclassIncluded, ISessionFactoryImplementor sessionFactoryImplementor) | ||
{ | ||
if (!NeedsTableGroupJoin(tableGroupJoinables, withClauseFragments, includeAllSubclassJoins)) | ||
return false; | ||
|
||
var first = tableGroupJoinables[0]; | ||
string joinString = ANSIJoinFragment.GetJoinString(first.JoinType); | ||
joinFragment.AddFromFragmentString( | ||
new SqlString( | ||
joinString, | ||
" (", | ||
first.Joinable.TableName, | ||
" ", | ||
first.Alias | ||
)); | ||
|
||
foreach (var join in tableGroupJoinables) | ||
{ | ||
if (join != first) | ||
joinFragment.AddJoin( | ||
join.Joinable.TableName, | ||
join.Alias, | ||
join.LHSColumns, | ||
JoinHelper.GetRHSColumnNames(join.AssociationType, sessionFactoryImplementor), | ||
join.JoinType, | ||
SqlString.Empty); | ||
|
||
bool include = includeAllSubclassJoins && isSubclassIncluded(join.Alias); | ||
// TODO (from hibernate): Think about if this could be made always true | ||
// NH Specific: made always true (original check: join.JoinType == JoinType.InnerJoin) | ||
const bool innerJoin = true; | ||
joinFragment.AddJoins( | ||
join.Joinable.FromJoinFragment(join.Alias, innerJoin, include), | ||
join.Joinable.WhereJoinFragment(join.Alias, innerJoin, include)); | ||
} | ||
|
||
var withClause = GetTableGroupJoinWithClause(withClauseFragments, first, sessionFactoryImplementor); | ||
joinFragment.AddFromFragmentString(withClause); | ||
return true; | ||
} | ||
|
||
private static bool NeedsTableGroupJoin(IReadOnlyList<IJoin> joins, SqlString[] withClauseFragments, bool includeSubclasses) | ||
{ | ||
// If we don't have a with clause, we don't need a table group join | ||
if (withClauseFragments.All(x => SqlStringHelper.IsEmpty(x))) | ||
{ | ||
return false; | ||
} | ||
|
||
// If we only have one join, a table group join is only necessary if subclass columns are used in the with clause | ||
if (joins.Count == 1) | ||
{ | ||
return joins[0].Joinable is AbstractEntityPersister persister && persister.HasSubclassJoins(includeSubclasses); | ||
//NH Specific: No alias processing | ||
//return isSubclassAliasDereferenced( joins[ 0], withClauseFragment ); | ||
} | ||
|
||
//NH Specific: No alias processing (see hibernate JoinSequence.NeedsTableGroupJoin) | ||
return true; | ||
} | ||
|
||
private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragments, IJoin first, ISessionFactoryImplementor factory) | ||
{ | ||
SqlStringBuilder fromFragment = new SqlStringBuilder(); | ||
fromFragment.Add(")").Add(" on "); | ||
|
||
string[] lhsColumns = first.LHSColumns; | ||
var isAssociationJoin = lhsColumns.Length > 0; | ||
if (isAssociationJoin) | ||
{ | ||
string rhsAlias = first.Alias; | ||
string[] rhsColumns = JoinHelper.GetRHSColumnNames(first.AssociationType, factory); | ||
fromFragment.Add(lhsColumns[0]).Add("=").Add(rhsAlias).Add(".").Add(rhsColumns[0]); | ||
for (int j = 1; j < lhsColumns.Length; j++) | ||
{ | ||
fromFragment.Add(" and ").Add(lhsColumns[j]).Add("=").Add(rhsAlias).Add(".").Add(rhsColumns[j]); | ||
} | ||
} | ||
|
||
AppendWithClause(fromFragment, isAssociationJoin, withClauseFragments); | ||
|
||
return fromFragment.ToSqlString(); | ||
} | ||
|
||
private static void AppendWithClause(SqlStringBuilder fromFragment, bool hasConditions, SqlString[] withClauseFragments) | ||
{ | ||
for (var i = 0; i < withClauseFragments.Length; i++) | ||
{ | ||
var withClause = withClauseFragments[i]; | ||
if (SqlStringHelper.IsEmpty(withClause)) | ||
continue; | ||
|
||
if (withClause.StartsWithCaseInsensitive(" and ")) | ||
{ | ||
if (!hasConditions) | ||
{ | ||
withClause = withClause.Substring(4); | ||
} | ||
} | ||
else if (hasConditions) | ||
{ | ||
fromFragment.Add(" and "); | ||
} | ||
|
||
fromFragment.Add(withClause); | ||
hasConditions = true; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug fix by the way, in case we have many non-empty with fragments and no association join.