Skip to content

Commit 1f77db9

Browse files
committed
JoinFragment: explicitly remove " and " clause
1 parent 817d85b commit 1f77db9

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/NHibernate/Loader/OuterJoinableAssociation.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias,
3434
rhsColumns = JoinHelper.GetRHSColumnNames(joinableType, factory);
3535
on = new SqlString(joinableType.GetOnCondition(rhsAlias, factory, enabledFilters));
3636
if (SqlStringHelper.IsNotEmpty(withClause))
37-
{
38-
on = on.Count == 0
39-
? on.Append(withClause)
40-
: on.Append(" and ( ").Append(withClause).Append(" )");
41-
}
42-
37+
on = on.Append(" and ( ").Append(withClause).Append(" )");
4338
this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
4439
}
4540

@@ -161,7 +156,7 @@ public void AddManyToManyJoin(JoinFragment outerjoin, IQueryableCollection colle
161156
string manyToManyFilter = collection.GetManyToManyFilterFragment(rhsAlias, enabledFilters);
162157
SqlString condition = string.Empty.Equals(manyToManyFilter)
163158
? on
164-
: SqlStringHelper.IsEmpty(on) ? new SqlString(manyToManyFilter) :
159+
: SqlStringHelper.IsEmpty(on) ? new SqlString(manyToManyFilter) :
165160
on.Append(" and ").Append(manyToManyFilter);
166161

167162
outerjoin.AddJoin(joinable.TableName, rhsAlias, lhsColumns, rhsColumns, joinType, condition);

src/NHibernate/SqlCommand/ANSIJoinFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void AddJoin(string tableName, string alias, string[] fkColumns,
4242

4343
if (fkColumns.Length == 0)
4444
{
45-
_fromFragment.Add(on);
45+
AddBareCondition(_fromFragment, on);
4646
return;
4747
}
4848

src/NHibernate/SqlCommand/JoinFragment.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,25 @@ protected bool AddCondition(SqlStringBuilder buffer, string on)
5858
}
5959
}
6060

61+
/// <summary>
62+
/// Adds condition to buffer without adding " and " prefix. Existing " and" prefix is removed
63+
/// </summary>
64+
protected void AddBareCondition(SqlStringBuilder buffer, SqlString condition)
65+
{
66+
if (SqlStringHelper.IsEmpty(condition))
67+
return;
68+
69+
buffer.Add(
70+
condition.StartsWithCaseInsensitive(" and ")
71+
? condition.Substring(4)
72+
: condition);
73+
}
74+
6175
protected bool AddCondition(SqlStringBuilder buffer, SqlString on)
6276
{
6377
if (SqlStringHelper.IsNotEmpty(on))
6478
{
65-
if (buffer.Count > 0 && !on.StartsWithCaseInsensitive(" and"))
79+
if (!on.StartsWithCaseInsensitive(" and"))
6680
{
6781
buffer.Add(" and ");
6882
}

0 commit comments

Comments
 (0)