Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for issue #184

  • Loading branch information...
commit 19d5d78b496771fdf18d75f7a4831ce5b394fb0f 1 parent 09e979f
@markrendle authored
View
4 Simple.Data.Ado/ObjectName.cs
@@ -57,7 +57,7 @@ public bool Equals(ObjectName other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
- return Equals(other._schema, _schema) && Equals(other._name, _name);
+ return Equals(other._schema, _schema) && Equals(other._name, _name) && Equals(other._alias, _alias);
}
/// <summary>
@@ -86,7 +86,7 @@ public override int GetHashCode()
{
unchecked
{
- return ((_schema??string.Empty).GetHashCode()*397) ^ _name.GetHashCode();
+ return ((_schema??string.Empty).GetHashCode()*397) ^ (_name.GetHashCode()*397) ^ ((_alias??string.Empty).GetHashCode());
}
}
View
14 Simple.Data.Ado/QueryBuilder.cs
@@ -223,7 +223,7 @@ private void HandleJoins()
var fromHavingCriteria = joiner.GetJoinClauses(_tableName, _havingCriteria);
var fromColumnList = _columns.Any(r => !(r is SpecialReference))
- ? joiner.GetJoinClauses(_tableName, GetObjectReferences(_columns).Where(o => !joinClauses.Any(j => o.GetOwner().Equals(j.Table))), JoinType.Outer)
+ ? GetJoinClausesFromColumnList(joinClauses, joiner)
: Enumerable.Empty<string>();
var joinList = fromTable.Concat(fromJoins).Concat(fromCriteria).Concat(fromHavingCriteria).Concat(fromColumnList).Select(s => s.Trim()).Distinct().ToList();
@@ -246,6 +246,18 @@ private void HandleJoins()
}
}
+ private IEnumerable<string> GetJoinClausesFromColumnList(IEnumerable<JoinClause> joinClauses, Joiner joiner)
+ {
+ return joiner.GetJoinClauses(_tableName, GetObjectReferences(_columns)
+ .Where(o => !joinClauses.Any(j => ObjectReferenceIsInJoinClause(j, o))), JoinType.Outer);
+
+ }
+
+ private static bool ObjectReferenceIsInJoinClause(JoinClause clause, ObjectReference reference)
+ {
+ return reference.GetOwner().GetAliasOrName().Equals(clause.Table.GetAliasOrName());
+ }
+
private IEnumerable<ObjectReference> GetObjectReferences(IEnumerable<SimpleReference> source)
{
var list = source.ToList();
View
26 Simple.Data.BehaviourTest/Query/WithTest.cs
@@ -254,7 +254,7 @@ public void CriteriaReferencesShouldNotBeDuplicatedInSql()
/// Test for issue #184
/// </summary>
[Test]
- public void CriteriaReferencesShouldUseWithAlias()
+ public void CriteriaReferencesShouldUseWithAliasOutValue()
{
const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," +
"[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," +
@@ -272,5 +272,29 @@ public void CriteriaReferencesShouldUseWithAlias()
GeneratedSqlIs(expectedSql);
}
+
+ /// <summary>
+ /// Test for issue #184
+ /// </summary>
+ [Test]
+ public void CriteriaReferencesShouldUseSeparateJoinFromWithAlias()
+ {
+ const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," +
+ "[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," +
+ "[foo].[id] as [__with1__foo__id],[foo].[name] as [__with1__foo__name]" +
+ " from [dbo].[employee]" +
+ " join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])" +
+ " left join [dbo].[department] [foo] on ([foo].[id] = [dbo].[employee].[departmentid])" +
+ " where ([dbo].[employee].[name] like @p1" +
+ " and [dbo].[department].[name] = @p2)";
+
+ var q = _db.Employees.Query()
+ .Where(_db.Employees.Name.Like("A%"))
+ .WithOne(_db.Employees.Department.As("Foo"))
+ .Where(_db.Employees.Department.Name == "Admin");
+ EatException(() => q.ToList());
+
+ GeneratedSqlIs(expectedSql);
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.