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

Commit fd7c1d4

Browse files
committed
Support for value expression when in sub expression
1 parent 283a775 commit fd7c1d4

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ protected internal virtual object Visit(Expression exp)
11851185
return VisitNewArray(exp as NewArrayExpression);
11861186
case ExpressionType.MemberInit:
11871187
return VisitMemberInit(exp as MemberInitExpression);
1188+
case ExpressionType.Index:
1189+
return VisitIndexExpression(exp as IndexExpression);
11881190
default:
11891191
return exp.ToString();
11901192
}
@@ -1483,14 +1485,30 @@ protected virtual object VisitUnary(UnaryExpression u)
14831485
return GetNotValue(o);
14841486
case ExpressionType.Convert:
14851487
if (u.Method != null)
1486-
{
14871488
return CachedExpressionCompiler.Evaluate(u);
1488-
}
14891489
break;
14901490
}
14911491
return Visit(u.Operand);
14921492
}
14931493

1494+
protected virtual object VisitIndexExpression(IndexExpression e)
1495+
{
1496+
var arg = e.Arguments[0];
1497+
var constant = arg as ConstantExpression;
1498+
var oIndex = constant != null
1499+
? constant.Value
1500+
: CachedExpressionCompiler.Evaluate(arg);
1501+
1502+
var index = (int)Convert.ChangeType(oIndex, typeof(int));
1503+
var oCollection = CachedExpressionCompiler.Evaluate(e.Object);
1504+
1505+
var list = oCollection as List<object>;
1506+
if (list != null)
1507+
return list[index];
1508+
1509+
throw new NotImplementedException("Unknown Expression: " + e);
1510+
}
1511+
14941512
private object GetNotValue(object o)
14951513
{
14961514
if (o as PartialSqlString == null)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using NUnit.Framework;
3+
using ServiceStack.DataAnnotations;
4+
using ServiceStack.Logging;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.Tests.Issues
8+
{
9+
public class AnyObjectClass
10+
{
11+
[Alias("_id")]
12+
public Guid Id { get; set; }
13+
14+
[Alias("_identity")]
15+
public Guid? Identity { get; set; }
16+
17+
[Alias("_name")]
18+
[StringLength(250)]
19+
public string Name { get; set; }
20+
}
21+
22+
public class SqlExpressionNullConstantIssue : OrmLiteTestBase
23+
{
24+
[Test]
25+
public void Can_compare_null_constant_in_subquery()
26+
{
27+
LogManager.LogFactory = new ConsoleLogFactory();
28+
29+
using (var db = OpenDbConnection())
30+
{
31+
db.DropAndCreateTable<AnyObjectClass>();
32+
33+
var inQ = db.From<AnyObjectClass>()
34+
.Where(y => y.Identity != null)
35+
.Select(y => y.Identity.Value);
36+
37+
var q = db.From<AnyObjectClass>().Where(x => Sql.In(x.Identity, inQ));
38+
39+
var results = db.Select(q);
40+
41+
results.PrintDump();
42+
}
43+
}
44+
45+
[Test]
46+
public void Can_compare_null_constant_in_subquery_nested_in_SqlExpression()
47+
{
48+
LogManager.LogFactory = new ConsoleLogFactory();
49+
50+
using (var db = OpenDbConnection())
51+
{
52+
db.DropAndCreateTable<AnyObjectClass>();
53+
54+
var q = db.From<AnyObjectClass>().Where(x => Sql.In(x.Identity,
55+
db.From<AnyObjectClass>()
56+
.Where(y => y.Identity != null)
57+
.Select(y => y.Identity.Value)));
58+
59+
var results = db.Select(q);
60+
61+
results.PrintDump();
62+
}
63+
}
64+
}
65+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
<Compile Include="Issues\BelongsToIssue.cs" />
132132
<Compile Include="Issues\JoinBoolSqlServerIssue.cs" />
133133
<Compile Include="Issues\MergingNestedSqlExpressionIssue.cs" />
134+
<Compile Include="Issues\SqlExpressionNullConstantIssue.cs" />
134135
<Compile Include="Legacy\ApiSqliteLegacyTests.cs" />
135136
<Compile Include="Legacy\ApiSqlServerLegacyTests.cs" />
136137
<Compile Include="SchemaTests.cs" />

0 commit comments

Comments
 (0)