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

Commit 935226c

Browse files
committed
Fix SQL Server bool condition in JOIN
1 parent ac9616c commit 935226c

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ private SqlExpression<T> InternalJoin(string joinType,
135135
useFieldName = true;
136136
sep = " ";
137137

138+
if (!tableDefs.Contains(sourceDef))
139+
tableDefs.Add(sourceDef);
140+
if (!tableDefs.Contains(targetDef))
141+
tableDefs.Add(targetDef);
142+
138143
var isCrossJoin = "CROSS JOIN".Equals(joinType);
144+
139145
var sqlExpr = joinExpr != null
140146
? InternalCreateSqlFromExpression(joinExpr, isCrossJoin)
141147
: InternalCreateSqlFromDefinitions(sourceDef, targetDef, isCrossJoin);
@@ -146,11 +152,6 @@ private SqlExpression<T> InternalJoin(string joinType,
146152

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

149-
if (!tableDefs.Contains(sourceDef))
150-
tableDefs.Add(sourceDef);
151-
if (!tableDefs.Contains(targetDef))
152-
tableDefs.Add(targetDef);
153-
154155
return this;
155156
}
156157

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ public class TestType
367367
public int Id { get; set; }
368368
public string TextCol { get; set; }
369369
public bool BoolCol { get; set; }
370+
public bool? NullableBoolCol { get; set; }
371+
public DateTime DateCol { get; set; }
372+
public TestEnum EnumCol { get; set; }
373+
public TestType ComplexObjCol { get; set; }
374+
public int? NullableIntCol { get; set; }
375+
}
376+
377+
public class TestType2
378+
{
379+
public int Id { get; set; }
380+
public string TextCol { get; set; }
381+
public bool BoolCol { get; set; }
382+
public bool? NullableBoolCol { get; set; }
370383
public DateTime DateCol { get; set; }
371384
public TestEnum EnumCol { get; set; }
372385
public TestType ComplexObjCol { get; set; }
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Diagnostics;
2+
using NUnit.Framework;
3+
using ServiceStack.DataAnnotations;
4+
using ServiceStack.Text;
5+
6+
namespace ServiceStack.OrmLite.Tests.Issues
7+
{
8+
public class JoinBoolSqlServerIssue : OrmLiteTestBase
9+
{
10+
[Test]
11+
public void Can_Join_on_bool()
12+
{
13+
using (var db = OpenDbConnection())
14+
{
15+
db.DropAndCreateTable<TestType>();
16+
db.DropAndCreateTable<TestType2>();
17+
18+
var q = db.From<TestType>()
19+
.LeftJoin<TestType2>((t1, t2) => t2.BoolCol == true && t1.Id == t2.Id);
20+
var results = db.Select(q);
21+
22+
q = db.From<TestType>()
23+
.LeftJoin<TestType2>((t1, t2) => t2.NullableBoolCol == true && t1.Id == t2.Id);
24+
results = db.Select(q);
25+
26+
results.PrintDump();
27+
}
28+
}
29+
30+
[Test]
31+
public void Can_compare_bool_in_join_expression()
32+
{
33+
var db = OpenDbConnection();
34+
35+
db.DropTable<CardHolder>();
36+
db.DropTable<Account>();
37+
38+
db.CreateTable<Account>();
39+
db.CreateTable<CardHolder>();
40+
41+
var exp1 = db.From<Account>()
42+
.LeftJoin<CardHolder>((a, ch) => a.Id == ch.AccountId && ch.TestBoolB == true);
43+
44+
Debug.WriteLine(exp1.BodyExpression);
45+
db.Select(exp1).PrintDump();
46+
47+
var exp2 = db.From<Account>()
48+
.LeftJoin<CardHolder>((a, ch) => a.Id == ch.AccountId && a.TestBoolA == true);
49+
50+
Debug.WriteLine(exp2.BodyExpression);
51+
db.Select(exp2).PrintDump();
52+
53+
54+
var exp3 = db.From<Account>()
55+
.Where(a => a.TestBoolA == true);
56+
Debug.WriteLine(exp3.BodyExpression);
57+
db.Select(exp3).PrintDump();
58+
}
59+
}
60+
61+
public class Account
62+
{
63+
[PrimaryKey]
64+
public int Id { get; set; }
65+
66+
public bool TestBoolA { get; set; }
67+
}
68+
69+
public class CardHolder
70+
{
71+
[PrimaryKey]
72+
public int Id { get; set; }
73+
74+
[References(typeof(Account))]
75+
public int AccountId { get; set; }
76+
77+
public bool TestBoolB { get; set; }
78+
}
79+
80+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="DateTimeTests.cs" />
130130
<Compile Include="DefaultValueTests.cs" />
131131
<Compile Include="Issues\BelongsToIssue.cs" />
132+
<Compile Include="Issues\JoinBoolSqlServerIssue.cs" />
132133
<Compile Include="Issues\MergingNestedSqlExpressionIssue.cs" />
133134
<Compile Include="Legacy\ApiSqliteLegacyTests.cs" />
134135
<Compile Include="Legacy\ApiSqlServerLegacyTests.cs" />

0 commit comments

Comments
 (0)