Skip to content

Commit 5478456

Browse files
committed
Partial fix for issue ThatRendle#232
1 parent 98e303e commit 5478456

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

Simple.Data.BehaviourTest/Query/ExplicitJoinTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ public void PassingObjectReferenceToOnThrowsBadExpressionException()
290290
() => _db.Activity.All().Join(_db.Location).On(_db.Location.ID_Location));
291291
}
292292

293+
[Test]
294+
public void PassingTablesWrongWayRoundThrowsBadExpressionException()
295+
{
296+
Assert.Throws<BadExpressionException>(
297+
() => _db.Activity.All().Join(_db.Activity, LocationId: _db.Location.ID_Location));
298+
}
299+
293300
class Activity
294301
{
295302
public int ID_Activity { get; set; }

Simple.Data.SqlTest/SqlQueryPagerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void ShouldCopeWithColumnsThatEndInFrom()
8686
from [dbo].[PromoPosts]
8787
ORDER BY [dbo].[PromoPosts].[ActiveFrom]";
8888

89-
var expected = @"WITH __Data AS (SELECT [dbo].[PromoPosts].[Id],[dbo].[PromoPosts].[ActiveFrom],[dbo].[PromoPosts].[ActiveTo],[dbo].[PromoPosts].[Created],[dbo].[PromoPosts].[Updated],ROW_NUMBER() OVER(ORDER BY [dbo].[PromoPosts].[ActiveFrom]) AS [_#_] from [dbo].[PromoPosts]) SELECT [Id],[ActiveFrom],[ActiveTo],[Created],[Updated] FROM __Data WHERE [_#_] BETWEEN 1 AND 25";
89+
var expected = @"with __data as (select [dbo].[promoposts].[id], row_number() over(order by [dbo].[promoposts].[activefrom]) as [_#_] from [dbo].[promoposts]) select [dbo].[promoposts].[id],[dbo].[promoposts].[activefrom],[dbo].[promoposts].[activeto],[dbo].[promoposts].[created],[dbo].[promoposts].[updated] from __data join [dbo].[promoposts] on [dbo].[promoposts].[id] = __data.[id] and [_#_] between 1 and 25";
9090
expected = expected.ToLowerInvariant();
9191

9292
var pagedSql = new SqlQueryPager().ApplyPaging(sql, new[] {"[dbo].[PromoPosts].[Id]"}, 0, 25).Single();

Simple.Data/BadExpressionException.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ public BadExpressionException(string message, Exception inner) : base(message, i
1818
protected BadExpressionException(SerializationInfo info, StreamingContext context) : base(info, context)
1919
{}
2020
}
21+
22+
[Serializable]
23+
public class BadJoinExpressionException : BadExpressionException
24+
{
25+
public BadJoinExpressionException()
26+
{}
27+
28+
public BadJoinExpressionException(string message) : base(message)
29+
{}
30+
31+
public BadJoinExpressionException(string message, Exception inner) : base(message, inner)
32+
{}
33+
34+
protected BadJoinExpressionException(SerializationInfo info, StreamingContext context) : base(info, context)
35+
{}
36+
}
2137
}

Simple.Data/SimpleQuery.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ private SimpleQuery ParseJoin(InvokeMemberBinder binder, object[] args)
599599
tableToJoin = dynamicTable.ToObjectReference();
600600
}
601601
}
602-
if (tableToJoin == null) throw new InvalidOperationException();
602+
if (tableToJoin == null) throw new BadJoinExpressionException("Incorrect join table specified");
603+
if (HomogenizedEqualityComparer.DefaultInstance.Equals(tableToJoin.GetAliasOrName(), _tableName))
604+
{
605+
throw new BadJoinExpressionException("Cannot join unaliased table to itself.");
606+
}
603607

604608
SimpleExpression joinExpression = null;
605609

@@ -613,7 +617,7 @@ private SimpleQuery ParseJoin(InvokeMemberBinder binder, object[] args)
613617
joinExpression = args[1] as SimpleExpression;
614618
}
615619

616-
if (joinExpression == null) throw new InvalidOperationException();
620+
if (joinExpression == null) throw new BadJoinExpressionException("Could not create join expression");
617621

618622
var type = binder.Name.Equals("join", StringComparison.OrdinalIgnoreCase) ? JoinType.Inner : JoinType.Outer;
619623
var newJoin = new JoinClause(tableToJoin, type, joinExpression);

0 commit comments

Comments
 (0)