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

Commit 016dbf0

Browse files
OlegNadymovmythz
authored andcommitted
Fix for CustomSqlExpression with virtual boolean properties. (#619)
* Support for Conditional expression in filter * Fixes for conditional expressions in filter and sorting * Using GetFieldValue in InsertOnly just like in Insert. * Change SqliteBoolConverter DbType to Int32 * Unit test for SqliteBoolConverter, InsertOnly * Fix for parsing static method inside non-static method. * Just formatting * Change access modifier to public for method "IsSqlClass" * Add support for using the Length property of String * VisitLengthStringProperty fix for CustomSqlExpression * Fix for CustomSqlExpression with virtual boolean properties.
1 parent 22770ba commit 016dbf0

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,15 @@ protected virtual object VisitBinary(BinaryExpression b)
15761576
Swap(ref left, ref right); // Should be safe to swap for equality/inequality checks
15771577
}
15781578

1579+
if (right is bool &&
1580+
(left == null || left.ToString().Equals("null", StringComparison.OrdinalIgnoreCase)))
1581+
{
1582+
if (operand == "=")
1583+
return false; // "null == true/false" becomes "false"
1584+
if (operand == "<>")
1585+
return true; // "null != true/false" becomes "true"
1586+
}
1587+
15791588
if (right is bool && !IsFieldName(left) && !(b.Left is ConditionalExpression)) // Don't change anything when "expr" is a column name or ConditionalExpression - then we really want "ColName = 1" or (Case When 1=0 Then 1 Else 0 End = 1)
15801589
{
15811590
if (operand == "=")

tests/ServiceStack.OrmLite.Tests/CustomSqlExpressionTests.cs

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public class WaybillBase : ObjectBase
4646
[DataAnnotations.Ignore]
4747
public bool BoolVirtProperty => false;
4848

49+
[DataAnnotations.Ignore]
50+
public bool? NullableTrueBoolVirtProperty => true;
51+
52+
[DataAnnotations.Ignore]
53+
public bool? NullableNullBoolVirtProperty => null;
54+
4955
[DataAnnotations.Ignore]
5056
public Guid GuidVirtProperty => Guid.Parse("00000000-0000-0000-0000-000000000000");
5157

@@ -112,6 +118,10 @@ protected override Object GetMemberExpression(MemberExpression m)
112118
return null;
113119
if (m.Member.Name == nameof(WaybillBase.BoolVirtProperty))
114120
return false;
121+
if (m.Member.Name == nameof(WaybillBase.NullableTrueBoolVirtProperty))
122+
return true;
123+
if (m.Member.Name == nameof(WaybillBase.NullableNullBoolVirtProperty))
124+
return null;
115125
if (m.Member.Name == nameof(WaybillBase.GuidVirtProperty))
116126
return Guid.Parse("00000000-0000-0000-0000-000000000000");
117127
if (m.Member.Name == nameof(WaybillBase.DateVirtProperty))
@@ -603,5 +613,181 @@ public void Can_Where_using_StringLengthVirtualProperty5()
603613
var target = Db.Select(q);
604614
Assert.AreEqual(3, target.Count);
605615
}
616+
617+
[Test]
618+
public void Can_Where_using_StringLengthVirtualProperty6()
619+
{
620+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.BoolVirtProperty != null;//should be always true
621+
622+
var q = Db.From<WaybillBase>().Where(filter);
623+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where null"));
624+
625+
var target = Db.Select(q);
626+
Assert.AreEqual(3, target.Count);
627+
}
628+
629+
[Test]
630+
public void Can_Where_using_StringLengthVirtualProperty7()
631+
{
632+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => null != x.BoolVirtProperty;//should be always true
633+
634+
var q = Db.From<WaybillBase>().Where(filter);
635+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where null"));
636+
637+
var target = Db.Select(q);
638+
Assert.AreEqual(3, target.Count);
639+
}
640+
641+
[Test]
642+
public void Can_Where_using_StringLengthVirtualProperty8()
643+
{
644+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => null != x.BoolVirtProperty && x.Id > 0;
645+
646+
var q = Db.From<WaybillBase>().Where(filter);
647+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where null"));
648+
649+
var target = Db.Select(q);
650+
Assert.AreEqual(3, target.Count);
651+
}
652+
653+
[Test]
654+
public void Can_Where_using_StringLengthVirtualProperty9()
655+
{
656+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.BoolVirtProperty == null;//should be always false
657+
658+
var q = Db.From<WaybillBase>().Where(filter);
659+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where not (null)"));
660+
661+
var target = Db.Select(q);
662+
Assert.AreEqual(0, target.Count);
663+
}
664+
665+
[Test]
666+
public void Can_Where_using_StringLengthVirtualProperty10()
667+
{
668+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => null == x.BoolVirtProperty;//should be always false
669+
670+
var q = Db.From<WaybillBase>().Where(filter);
671+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where not (null)"));
672+
673+
var target = Db.Select(q);
674+
Assert.AreEqual(0, target.Count);
675+
}
676+
677+
[Test]
678+
public void Can_Where_using_StringLengthVirtualProperty11()
679+
{
680+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.NullableTrueBoolVirtProperty == null;
681+
682+
var q = Db.From<WaybillBase>().Where(filter);
683+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where null"));
684+
685+
var target = Db.Select(q);
686+
Assert.AreEqual(0, target.Count);
687+
}
688+
689+
[Test]
690+
public void Can_Where_using_StringLengthVirtualProperty12()
691+
{
692+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.NullableTrueBoolVirtProperty != null;
693+
694+
var q = Db.From<WaybillBase>().Where(filter);
695+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where not (null)"));
696+
697+
var target = Db.Select(q);
698+
Assert.AreEqual(3, target.Count);
699+
}
700+
701+
[Test]
702+
public void Can_Where_using_StringLengthVirtualProperty13()
703+
{
704+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.NullableNullBoolVirtProperty == null;
705+
706+
var q = Db.From<WaybillBase>().Where(filter);
707+
var target = Db.Select(q);
708+
Assert.AreEqual(3, target.Count);
709+
}
710+
711+
[Test]
712+
public void Can_Where_using_StringLengthVirtualProperty14()
713+
{
714+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.NullableNullBoolVirtProperty != null;
715+
716+
var q = Db.From<WaybillBase>().Where(filter);
717+
var target = Db.Select(q);
718+
Assert.AreEqual(0, target.Count);
719+
}
720+
721+
[Test]
722+
public void Can_Where_using_StringLengthVirtualProperty15()
723+
{
724+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.NullableNullBoolVirtProperty == true;
725+
726+
var q = Db.From<WaybillBase>().Where(filter);
727+
var target = Db.Select(q);
728+
Assert.AreEqual(0, target.Count);
729+
}
730+
731+
[Test]
732+
public void Can_Where_using_StringLengthVirtualProperty16()
733+
{
734+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.BoolVirtProperty != false;
735+
736+
var q = Db.From<WaybillBase>().Where(filter);
737+
Assert.That(q.ToSelectStatement().ToLower(), Does.Not.Contain("where null"));
738+
739+
var target = Db.Select(q);
740+
Assert.AreEqual(0, target.Count);
741+
}
742+
743+
[Test]
744+
public void Can_Where_using_StringLengthVirtualProperty17()
745+
{
746+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtPropertyNull == null;
747+
748+
var q = Db.From<WaybillBase>().Where(filter);
749+
var target = Db.Select(q);
750+
Assert.AreEqual(3, target.Count);
751+
}
752+
753+
[Test]
754+
public void Can_Where_using_StringLengthVirtualProperty18()
755+
{
756+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtPropertyNull != null;
757+
758+
var q = Db.From<WaybillBase>().Where(filter);
759+
var target = Db.Select(q);
760+
Assert.AreEqual(0, target.Count);
761+
}
762+
763+
[Test]
764+
public void Can_Where_using_StringLengthVirtualProperty19()
765+
{
766+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtPropertyNull == "null";
767+
768+
var q = Db.From<WaybillBase>().Where(filter);
769+
var target = Db.Select(q);
770+
Assert.AreEqual(0, target.Count);
771+
}
772+
773+
[Test]
774+
public void Can_Where_using_StringLengthVirtualProperty20()
775+
{
776+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == null;
777+
778+
var q = Db.From<WaybillBase>().Where(filter);
779+
var target = Db.Select(q);
780+
Assert.AreEqual(0, target.Count);
781+
}
782+
783+
[Test]
784+
public void Can_Where_using_StringLengthVirtualProperty21()
785+
{
786+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue";
787+
788+
var q = Db.From<WaybillBase>().Where(filter);
789+
var target = Db.Select(q);
790+
Assert.AreEqual(3, target.Count);
791+
}
606792
}
607793
}

0 commit comments

Comments
 (0)