forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableGroupJoinHelper.cs
155 lines (135 loc) · 5.07 KB
/
TableGroupJoinHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Persister.Collection;
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, isSubclassIncluded))
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,
join.RHSColumns,
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);
joinFragment.AddFromFragmentString(withClause);
return true;
}
// detect cases when withClause is used on multiple tables or when join keys depend on subclass columns
private static bool NeedsTableGroupJoin(IReadOnlyList<IJoin> joins, SqlString[] withClauseFragments, bool includeSubclasses, Func<string, bool> isSubclassIncluded)
{
bool hasWithClause = withClauseFragments.Any(x => SqlStringHelper.IsNotEmpty(x));
//NH Specific: No alias processing (see hibernate JoinSequence.NeedsTableGroupJoin)
if (joins.Count > 1 && hasWithClause)
return true;
foreach (var join in joins)
{
var entityPersister = GetEntityPersister(join.Joinable, out var isManyToMany);
if (entityPersister?.HasSubclassJoins(includeSubclasses && isSubclassIncluded(join.Alias)) != true)
continue;
if (hasWithClause)
return true;
if (!isManyToMany // many-to-many keys are stored in separate table
&& entityPersister.ColumnsDependOnSubclassJoins(join.RHSColumns))
{
return true;
}
}
return false;
}
private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragments, IJoin first)
{
SqlStringBuilder fromFragment = new SqlStringBuilder();
fromFragment.Add(")").Add(" on ");
string[] lhsColumns = first.LHSColumns;
var isAssociationJoin = lhsColumns.Length > 0;
if (isAssociationJoin)
{
var entityPersister = GetEntityPersister(first.Joinable, out var isManyToMany);
string rhsAlias = first.Alias;
string[] rhsColumns = first.RHSColumns;
for (int j = 0; j < lhsColumns.Length; j++)
{
fromFragment.Add(lhsColumns[j])
.Add("=")
.Add((entityPersister == null || isManyToMany) // many-to-many keys are stored in separate table
? rhsAlias
: entityPersister.GenerateTableAliasForColumn(rhsAlias, rhsColumns[j]))
.Add(".")
.Add(rhsColumns[j]);
if (j != lhsColumns.Length - 1)
fromFragment.Add(" and ");
}
}
AppendWithClause(fromFragment, isAssociationJoin, withClauseFragments);
return fromFragment.ToSqlString();
}
private static AbstractEntityPersister GetEntityPersister(IJoinable joinable, out bool isManyToMany)
{
isManyToMany = false;
if (!joinable.IsCollection)
return joinable as AbstractEntityPersister;
var collection = (IQueryableCollection) joinable;
isManyToMany = collection.IsManyToMany;
return collection.ElementType.IsEntityType ? collection.ElementPersister as AbstractEntityPersister : null;
}
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;
}
}
}
}