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

Commit 56355fb

Browse files
author
Oleg Nadymov
committed
Fix for VisitConditional: use string constant in true or false options.
1 parent 2ad45af commit 56355fb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,16 +1814,27 @@ protected virtual object VisitConditional(ConditionalExpression e)
18141814
if ((bool) test)
18151815
{
18161816
var ifTrue = Visit(e.IfTrue);
1817+
if (e.IfTrue.Type == typeof(string) && !(ifTrue is PartialSqlString))
1818+
ifTrue = ConvertToParam(ifTrue);
1819+
18171820
return ifTrue;
18181821
}
18191822

18201823
var ifFalse = Visit(e.IfFalse);
1824+
if (e.IfFalse.Type == typeof(string) && !(ifFalse is PartialSqlString))
1825+
ifFalse = ConvertToParam(ifFalse);
1826+
18211827
return ifFalse;
18221828
}
18231829
else
18241830
{
18251831
var ifTrue = Visit(e.IfTrue);
1832+
if (e.IfTrue.Type == typeof(string) && !(ifTrue is PartialSqlString))
1833+
ifTrue = ConvertToParam(ifTrue);
1834+
18261835
var ifFalse = Visit(e.IfFalse);
1836+
if (e.IfFalse.Type == typeof(string) && !(ifFalse is PartialSqlString))
1837+
ifFalse = ConvertToParam(ifFalse);
18271838

18281839
return new PartialSqlString($"(CASE WHEN {test} THEN {ifTrue} ELSE {ifFalse} END)");
18291840
}

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ public void Can_Where_using_IfConcat_filter()
575575
Assert.That(target.Count, Is.EqualTo(1));
576576
}
577577

578+
[Test]
579+
public void Can_Where_using_IfWithStringConstant_filter()
580+
{
581+
System.Linq.Expressions.Expression<Func<TestType, bool>> filter = x => (String.Concat("Text: ", x.TextCol) == null ? " " : String.Concat("Text: ", x.TextCol)).EndsWith("asdf");
582+
var q = Db.From<TestType>().Where(filter);
583+
Assert.That(q.ToSelectStatement(), Does.Contain("Text"));
584+
585+
var target = Db.Select(q);
586+
Assert.That(target.Count, Is.EqualTo(1));
587+
}
588+
578589
private int MethodReturningInt(int val)
579590
{
580591
return val;

0 commit comments

Comments
 (0)