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

Commit fe3d241

Browse files
committed
Added support for visiting of virtual bool properties in custom SqlExpression
1 parent 40e11ec commit fe3d241

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,13 @@ protected virtual object VisitLambda(LambdaExpression lambda)
12641264

12651265
if (m.Expression != null)
12661266
{
1267-
string r = VisitMemberAccess(m).ToString();
1268-
if (m.Expression.Type.IsNullableType())
1267+
var r = VisitMemberAccess(m);
1268+
if (!(r is PartialSqlString))
12691269
return r;
12701270

1271+
if (m.Expression.Type.IsNullableType())
1272+
return r.ToString();
1273+
12711274
return $"{r}={GetQuotedTrueValue()}";
12721275
}
12731276

@@ -1290,13 +1293,21 @@ protected virtual object VisitBinary(BinaryExpression b)
12901293
var operand = BindOperant(b.NodeType); //sep= " " ??
12911294
if (operand == "AND" || operand == "OR")
12921295
{
1293-
left = IsBooleanComparison(b.Left)
1294-
? new PartialSqlString($"{VisitMemberAccess((MemberExpression) b.Left)}={GetQuotedTrueValue()}")
1295-
: Visit(b.Left);
1296+
if (IsBooleanComparison(b.Left))
1297+
{
1298+
left = VisitMemberAccess((MemberExpression) b.Left);
1299+
if (left is PartialSqlString)
1300+
left = new PartialSqlString($"{left}={GetQuotedTrueValue()}");
1301+
}
1302+
else left = Visit(b.Left);
12961303

1297-
right = IsBooleanComparison(b.Right)
1298-
? new PartialSqlString($"{VisitMemberAccess((MemberExpression) b.Right)}={GetQuotedTrueValue()}")
1299-
: Visit(b.Right);
1304+
if (IsBooleanComparison(b.Right))
1305+
{
1306+
right = VisitMemberAccess((MemberExpression)b.Right);
1307+
if (right is PartialSqlString)
1308+
right = new PartialSqlString($"{right}={GetQuotedTrueValue()}");
1309+
}
1310+
else right = Visit(b.Right);
13001311

13011312
if (!(left is PartialSqlString) && !(right is PartialSqlString))
13021313
{

tests/ServiceStack.OrmLite.Tests/CustomSqlExpressionTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Waybill
1818
public string VirtProperty => "WaybillVirtPropertyValue";
1919

2020
public string VirtProperty2 => "WaybillVirtPropertyValue2";
21+
22+
public bool BoolVirtProperty => false;
2123
}
2224

2325
public class CustomSqlServerDialectProvider : SqliteOrmLiteDialectProvider
@@ -43,6 +45,8 @@ protected override Object GetMemberExpression(MemberExpression m)
4345
return "WaybillVirtPropertyValue";
4446
if (m.Member.Name == nameof(Waybill.VirtProperty2))
4547
return "WaybillVirtPropertyValue2";
48+
if (m.Member.Name == nameof(Waybill.BoolVirtProperty))
49+
return false;
4650
}
4751

4852
return base.GetMemberExpression(m);
@@ -153,5 +157,63 @@ public void Can_Where_using_constant_filter8()
153157
var target = Db.Select(q);
154158
Assert.AreEqual(0, target.Count);
155159
}
160+
161+
[Test]
162+
public void Can_Where_using_constant_filter9()
163+
{
164+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.BoolVirtProperty;
165+
var q = Db.From<Waybill>().Where(filter);
166+
var target = Db.Select(q);
167+
Assert.AreEqual(0, target.Count);
168+
}
169+
170+
[Test]
171+
public void Can_Where_using_constant_filter10()
172+
{
173+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty;
174+
var q = Db.From<Waybill>().Where(filter);
175+
var target = Db.Select(q);
176+
Assert.AreEqual(3, target.Count);
177+
}
178+
179+
[Test]
180+
public void Can_Where_using_constant_filter11()
181+
{
182+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.BoolVirtProperty && x.VirtProperty == "WaybillVirtPropertyValue";
183+
var q = Db.From<Waybill>().Where(filter);
184+
var target = Db.Select(q);
185+
Assert.AreEqual(0, target.Count);
186+
}
187+
188+
[Test]
189+
public void Can_Where_using_constant_filter12()
190+
{
191+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty || x.VirtProperty == "WaybillVirtPropertyValue";
192+
var q = Db.From<Waybill>().Where(filter);
193+
var target = Db.Select(q);
194+
Assert.AreEqual(3, target.Count);
195+
}
196+
197+
[Test]
198+
public void Can_Where_using_constant_filter13()
199+
{
200+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty &&
201+
x.VirtProperty == "WaybillVirtPropertyValue" &&
202+
x.Number == 100;
203+
var q = Db.From<Waybill>().Where(filter);
204+
var target = Db.Select(q);
205+
Assert.AreEqual(1, target.Count);
206+
}
207+
208+
[Test]
209+
public void Can_Where_using_constant_filter14()
210+
{
211+
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.Number == 100 &&
212+
(x.BoolVirtProperty ||
213+
x.VirtProperty == "WaybillVirtPropertyValue");
214+
var q = Db.From<Waybill>().Where(filter);
215+
var target = Db.Select(q);
216+
Assert.AreEqual(1, target.Count);
217+
}
156218
}
157219
}

0 commit comments

Comments
 (0)