Skip to content

Commit 405c50d

Browse files
briandonahuejagregory
authored andcommitted
Added tests and support in ExpressionToSql for Booleans e.g.
Where(x.Active == true) Where(x.Active == false) Where(x.Active) Where(x.Active) Admittedly, my Expression knowledge is limited, so this might be a naive implementation, but hey, all tests passed!
1 parent dff5538 commit 405c50d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/FluentNHibernate.Testing/DomainModel/Mapping/ClassMapXmlCreationTester.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ public class ChildObject
581581
public virtual int Id { get; set; }
582582
public virtual string Name { get; set; }
583583
public virtual int Position { get; set; }
584+
public virtual bool Active { get; set; }
584585
}
585586

586587
public class MappedGenericObject<T>

src/FluentNHibernate.Testing/ExpressionToSqlTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ public void ConvertEqualsPropertyAndString()
5555
sql.ShouldEqual("Name = '1'");
5656
}
5757

58+
[Test]
59+
public void ConvertEqualsPropertyAndTrue()
60+
{
61+
var sql = ExpressionToSql.Convert<ChildObject>(x => x.Active == true);
62+
63+
sql.ShouldEqual("Active = 1");
64+
}
65+
66+
[Test]
67+
public void ConvertEqualsPropertyAndFalse()
68+
{
69+
var sql = ExpressionToSql.Convert<ChildObject>(x => x.Active == false);
70+
71+
sql.ShouldEqual("Active = 0");
72+
}
73+
74+
[Test]
75+
public void ConvertBooleanPropertyImplicitTrue()
76+
{
77+
var sql = ExpressionToSql.Convert<ChildObject>(x => x.Active);
78+
79+
sql.ShouldEqual("Active = 1");
80+
}
81+
82+
[Test]
83+
public void ConvertBooleanPropertyImplicitFalse()
84+
{
85+
var sql = ExpressionToSql.Convert<ChildObject>(x => !x.Active);
86+
87+
sql.ShouldEqual("Active = 0");
88+
}
89+
5890
[Test]
5991
public void ConvertGreater()
6092
{

src/FluentNHibernate/Utils/ExpressionToSql.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public static string Convert<T>(Expression<Func<T, bool>> expression)
3232
{
3333
if (expression.Body is BinaryExpression)
3434
return Convert<T>((BinaryExpression)expression.Body);
35+
var memberExpression = expression.Body as MemberExpression;
36+
if (memberExpression != null && memberExpression.Type == typeof(bool))
37+
return Convert(CreateExpression<T>(memberExpression)) + " = " + Convert(true);
38+
var unaryExpression = expression.Body as UnaryExpression;
39+
if (unaryExpression != null && unaryExpression.Type == typeof(bool) && unaryExpression.NodeType == ExpressionType.Not)
40+
return Convert(CreateExpression<T>(unaryExpression.Operand)) + " = " + Convert(false);
41+
42+
3543

3644
throw new NotImplementedException();
3745
}
@@ -135,6 +143,10 @@ private static string Convert(object value)
135143
{
136144
if (value is string)
137145
return "'" + value + "'";
146+
if (value is bool)
147+
{
148+
return (bool)value ? "1" : "0";
149+
}
138150

139151
return value.ToString();
140152
}

0 commit comments

Comments
 (0)