Skip to content

Commit 02162be

Browse files
committed
Fixed object-name-related JOINing issue ThatRendle#157
1 parent 4799bbb commit 02162be

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

Simple.Data.Ado/QueryBuilder.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,19 @@ private void HandleJoins()
206206
? joiner.GetJoinClauses(_tableName, GetObjectReferences(_columns).Where(o => !joinClauses.Any(j => o.GetOwner().Equals(j.Table))), JoinType.Outer)
207207
: Enumerable.Empty<string>();
208208

209-
var joins = string.Join(" ", fromTable.Concat(fromJoins)
210-
.Concat(fromCriteria)
211-
.Concat(fromHavingCriteria)
212-
.Concat(fromColumnList)
213-
.Distinct());
209+
var joinList = fromTable.Concat(fromJoins).Concat(fromCriteria).Concat(fromHavingCriteria).Concat(fromColumnList).Select(s => s.Trim()).Distinct().ToList();
210+
211+
var leftJoinList = joinList.Where(s => s.StartsWith("LEFT ", StringComparison.OrdinalIgnoreCase)).ToList();
212+
213+
foreach (var leftJoin in leftJoinList)
214+
{
215+
if (joinList.Any(s => s.Equals(leftJoin.Substring(5), StringComparison.OrdinalIgnoreCase)))
216+
{
217+
joinList.Remove(leftJoin);
218+
}
219+
}
220+
221+
var joins = string.Join(" ", joinList);
214222

215223
if (!string.IsNullOrWhiteSpace(joins))
216224
{

Simple.Data.BehaviourTest/Query/WithTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,23 @@ public void MultipleWithClauseJustDoesEverythingYouWouldHope()
172172

173173
GeneratedSqlIs(expectedSql);
174174
}
175+
176+
/// <summary>
177+
/// Test for issue #157
178+
/// </summary>
179+
[Test]
180+
public void CriteriaReferencesShouldNotBeDuplicatedInSql()
181+
{
182+
const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," +
183+
"[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," +
184+
"[dbo].[department].[id] as [__with1__department__id],[dbo].[department].[name] as [__with1__department__name]" +
185+
" from [dbo].[employee] join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])" +
186+
" where [dbo].[department].[name] = @p1";
187+
188+
var q = _db.Employees.FindAll(_db.Employees.Department.Name == "Dev").WithDepartment();
189+
EatException(() => q.ToList());
190+
191+
GeneratedSqlIs(expectedSql);
192+
}
175193
}
176194
}

Simple.Data.SqlTest/QueryTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,18 @@ public void WithClauseShouldPreselectDetailTableAsCollection()
331331
Assert.AreEqual(1, orders.Count);
332332
}
333333

334+
[Test]
335+
public void WithClauseWithJoinCriteriaShouldPreselectDetailTableAsCollection()
336+
{
337+
var db = DatabaseHelper.Open();
338+
var result = db.Customers.FindAll(db.Customers.Order.OrderId == 1).WithOrders().FirstOrDefault() as IDictionary<string, object>;
339+
Assert.IsNotNull(result);
340+
Assert.Contains("Orders", (ICollection)result.Keys);
341+
var orders = result["Orders"] as IList<IDictionary<string, object>>;
342+
Assert.IsNotNull(orders);
343+
Assert.AreEqual(1, orders.Count);
344+
}
345+
334346
[Test]
335347
public void WithClauseShouldPreselectMasterTableAsDictionary()
336348
{

0 commit comments

Comments
 (0)