Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow override of cardinality on With clauses

  • Loading branch information...
commit 69caf3a5a5b242631ba78e01dba59eb5d0e522e5 1 parent 231e362
@markrendle authored
View
60 Simple.Data.Ado/QueryBuilder.cs
@@ -110,28 +110,11 @@ private void HandleWithClauses()
{
if (withClause.ObjectReference.GetOwner().IsNull())
{
- var joinClause =
- _query.Clauses.OfType<JoinClause>().FirstOrDefault(j => j.Table.GetAliasOrName() == withClause.ObjectReference.GetAliasOrName());
- if (joinClause != null)
- {
- _columns =
- _columns.Concat(
- _schema.FindTable(joinClause.Table.GetName()).Columns.Select(
- c => new ObjectReference(c.ActualName, joinClause.Table)))
- .ToArray();
- relationTypeDict[joinClause.Table] = withClause.Type == WithType.One
- ? RelationType.ManyToOne
- : RelationType.OneToMany;
- }
+ HandleWithClauseUsingAssociatedJoinClause(relationTypeDict, withClause);
}
else
{
- relationTypeDict[withClause.ObjectReference] = RelationType.None;
- _columns =
- _columns.Concat(
- _schema.FindTable(withClause.ObjectReference.GetName()).Columns.Select(
- c => new ObjectReference(c.ActualName, withClause.ObjectReference)))
- .ToArray();
+ HandleWithClauseUsingNaturalJoin(withClause, relationTypeDict);
}
}
_columns =
@@ -141,6 +124,45 @@ private void HandleWithClauses()
}
}
+ private void HandleWithClauseUsingAssociatedJoinClause(Dictionary<ObjectReference, RelationType> relationTypeDict, WithClause withClause)
+ {
+ var joinClause =
+ _query.Clauses.OfType<JoinClause>().FirstOrDefault(
+ j => j.Table.GetAliasOrName() == withClause.ObjectReference.GetAliasOrName());
+ if (joinClause != null)
+ {
+ _columns =
+ _columns.Concat(
+ _schema.FindTable(joinClause.Table.GetName()).Columns.Select(
+ c => new ObjectReference(c.ActualName, joinClause.Table)))
+ .ToArray();
+ relationTypeDict[joinClause.Table] = WithTypeToRelationType(withClause.Type, RelationType.OneToMany);
+ }
+ }
+
+ private void HandleWithClauseUsingNaturalJoin(WithClause withClause, Dictionary<ObjectReference, RelationType> relationTypeDict)
+ {
+ relationTypeDict[withClause.ObjectReference] = WithTypeToRelationType(withClause.Type, RelationType.None);
+ _columns =
+ _columns.Concat(
+ _schema.FindTable(withClause.ObjectReference.GetName()).Columns.Select(
+ c => new ObjectReference(c.ActualName, withClause.ObjectReference)))
+ .ToArray();
+ }
+
+ private static RelationType WithTypeToRelationType(WithType withType, RelationType defaultRelationType)
+ {
+ switch (withType)
+ {
+ case WithType.One:
+ return RelationType.ManyToOne;
+ case WithType.Many:
+ return RelationType.OneToMany;
+ default:
+ return defaultRelationType;
+ }
+ }
+
private bool IsCoreTable(ObjectReference tableReference)
{
if (ReferenceEquals(tableReference, null)) throw new ArgumentNullException("tableReference");
View
16 Simple.Data.BehaviourTest/Query/WithTest.cs
@@ -83,6 +83,22 @@ public void SingleWithClauseUsingReferenceShouldUseJoin()
}
[Test]
+ public void SingleWithOneClauseUsingReferenceShouldUseJoinAndForceOne()
+ {
+ const string expectedSql = "select " +
+ "[dbo].[department].[id],[dbo].[department].[name]," +
+ "[dbo].[employee].[id] as [__with1__employee__id],[dbo].[employee].[name] as [__with1__employee__name]," +
+ "[dbo].[employee].[managerid] as [__with1__employee__managerid],[dbo].[employee].[departmentid] as [__with1__employee__departmentid]" +
+ " from [dbo].[department] left join [dbo].[employee] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])";
+
+ var q = _db.Departments.All().WithOne(_db.Departments.Employee);
+
+ EatException(() => q.ToList());
+
+ GeneratedSqlIs(expectedSql);
+ }
+
+ [Test]
public void SingleWithClauseUsingReferenceWithAliasShouldApplyAliasToSql()
{
const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," +
Please sign in to comment.
Something went wrong with that request. Please try again.