Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Fix SQL Server bool condition in JOIN
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 10, 2016
1 parent ac9616c commit 935226c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ public SqlExpression<T> CustomJoin(string joinString)
useFieldName = true;
sep = " ";

if (!tableDefs.Contains(sourceDef))
tableDefs.Add(sourceDef);
if (!tableDefs.Contains(targetDef))
tableDefs.Add(targetDef);

var isCrossJoin = "CROSS JOIN".Equals(joinType);

var sqlExpr = joinExpr != null
? InternalCreateSqlFromExpression(joinExpr, isCrossJoin)
: InternalCreateSqlFromDefinitions(sourceDef, targetDef, isCrossJoin);
Expand All @@ -146,11 +152,6 @@ public SqlExpression<T> CustomJoin(string joinString)

FromExpression += " {0} {1} {2}".Fmt(joinType, SqlTable(joinDef), sqlExpr);

if (!tableDefs.Contains(sourceDef))
tableDefs.Add(sourceDef);
if (!tableDefs.Contains(targetDef))
tableDefs.Add(targetDef);

return this;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ public class TestType
public int Id { get; set; }
public string TextCol { get; set; }
public bool BoolCol { get; set; }
public bool? NullableBoolCol { get; set; }
public DateTime DateCol { get; set; }
public TestEnum EnumCol { get; set; }
public TestType ComplexObjCol { get; set; }
public int? NullableIntCol { get; set; }
}

public class TestType2
{
public int Id { get; set; }
public string TextCol { get; set; }
public bool BoolCol { get; set; }
public bool? NullableBoolCol { get; set; }
public DateTime DateCol { get; set; }
public TestEnum EnumCol { get; set; }
public TestType ComplexObjCol { get; set; }
Expand Down
80 changes: 80 additions & 0 deletions tests/ServiceStack.OrmLite.Tests/Issues/JoinBoolSqlServerIssue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Diagnostics;
using NUnit.Framework;
using ServiceStack.DataAnnotations;
using ServiceStack.Text;

namespace ServiceStack.OrmLite.Tests.Issues
{
public class JoinBoolSqlServerIssue : OrmLiteTestBase
{
[Test]
public void Can_Join_on_bool()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<TestType>();
db.DropAndCreateTable<TestType2>();

var q = db.From<TestType>()
.LeftJoin<TestType2>((t1, t2) => t2.BoolCol == true && t1.Id == t2.Id);
var results = db.Select(q);

q = db.From<TestType>()
.LeftJoin<TestType2>((t1, t2) => t2.NullableBoolCol == true && t1.Id == t2.Id);
results = db.Select(q);

results.PrintDump();
}
}

[Test]
public void Can_compare_bool_in_join_expression()
{
var db = OpenDbConnection();

db.DropTable<CardHolder>();
db.DropTable<Account>();

db.CreateTable<Account>();
db.CreateTable<CardHolder>();

var exp1 = db.From<Account>()
.LeftJoin<CardHolder>((a, ch) => a.Id == ch.AccountId && ch.TestBoolB == true);

Debug.WriteLine(exp1.BodyExpression);
db.Select(exp1).PrintDump();

var exp2 = db.From<Account>()
.LeftJoin<CardHolder>((a, ch) => a.Id == ch.AccountId && a.TestBoolA == true);

Debug.WriteLine(exp2.BodyExpression);
db.Select(exp2).PrintDump();


var exp3 = db.From<Account>()
.Where(a => a.TestBoolA == true);
Debug.WriteLine(exp3.BodyExpression);
db.Select(exp3).PrintDump();
}
}

public class Account
{
[PrimaryKey]
public int Id { get; set; }

public bool TestBoolA { get; set; }
}

public class CardHolder
{
[PrimaryKey]
public int Id { get; set; }

[References(typeof(Account))]
public int AccountId { get; set; }

public bool TestBoolB { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Compile Include="DateTimeTests.cs" />
<Compile Include="DefaultValueTests.cs" />
<Compile Include="Issues\BelongsToIssue.cs" />
<Compile Include="Issues\JoinBoolSqlServerIssue.cs" />
<Compile Include="Issues\MergingNestedSqlExpressionIssue.cs" />
<Compile Include="Legacy\ApiSqliteLegacyTests.cs" />
<Compile Include="Legacy\ApiSqlServerLegacyTests.cs" />
Expand Down

0 comments on commit 935226c

Please sign in to comment.