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

Commit 9f0b0e8

Browse files
committed
Change Sql.In to work as expected
1 parent 759a094 commit 9f0b0e8

File tree

4 files changed

+93
-32
lines changed

4 files changed

+93
-32
lines changed

src/ServiceStack.OrmLite.Sqlite/SqliteExpressionVisitor.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Text;
45
using System.Linq.Expressions;
@@ -49,27 +50,14 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
4950
var lambda = Expression.Lambda<Func<object>>(member);
5051
var getter = lambda.Compile();
5152

52-
var inArgs = getter() as object[];
53+
var inArgs = Sql.Flatten(getter() as IEnumerable);
5354

5455
var sIn = new StringBuilder();
5556
foreach (var e in inArgs)
5657
{
57-
if (e.GetType().ToString() != "System.Collections.Generic.List`1[System.Object]")
58-
{
59-
sIn.AppendFormat("{0}{1}",
60-
sIn.Length > 0 ? "," : "",
61-
OrmLiteConfig.DialectProvider.GetQuotedValue(e, e.GetType()));
62-
}
63-
else
64-
{
65-
var listArgs = e as IList<Object>;
66-
foreach (Object el in listArgs)
67-
{
68-
sIn.AppendFormat("{0}{1}",
69-
sIn.Length > 0 ? "," : "",
70-
OrmLiteConfig.DialectProvider.GetQuotedValue(el, el.GetType()));
71-
}
72-
}
58+
sIn.AppendFormat("{0}{1}",
59+
sIn.Length > 0 ? "," : "",
60+
OrmLiteConfig.DialectProvider.GetQuotedValue(e, e.GetType()));
7361
}
7462
statement = string.Format("{0} {1} ({2})", quotedColName, m.Method.Name, sIn);
7563
break;

src/ServiceStack.OrmLite/Expressions/Sql.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
using System;
2-
using System.Linq;
3-
using System.Linq.Expressions;
1+
using System.Collections;
2+
using System.Linq;
43
using System.Collections.Generic;
4+
55
namespace ServiceStack.OrmLite
66
{
77
public static class Sql
88
{
9-
public static bool In<T>(T value, params object[] list)
10-
{
11-
if(value == null)
12-
return false;
9+
public static bool In<T, TItem>(T value, params TItem[] list)
10+
{
11+
return value != null && Flatten(list).Any(obj => obj.ToString() == value.ToString());
12+
}
13+
14+
public static List<object> Flatten(IEnumerable list)
15+
{
16+
var ret = new List<object>();
17+
if (list == null) return ret;
1318

14-
foreach (var obj in list)
15-
{
16-
if (obj == null) continue;
17-
if (obj.ToString() == value.ToString()) return true;
18-
}
19+
foreach (var item in list)
20+
{
21+
if (item == null) continue;
1922

20-
return false;
21-
}
23+
var arr = item as IEnumerable;
24+
if (arr != null && !(item is string))
25+
{
26+
ret.AddRange(arr.Cast<object>());
27+
}
28+
else
29+
{
30+
ret.Add(item);
31+
}
32+
}
33+
return ret;
34+
}
2235

23-
public static string Desc<T>(T value) {
36+
public static string Desc<T>(T value)
37+
{
2438
return value==null? "": value.ToString() + " DESC";
2539
}
2640

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
using ServiceStack.OrmLite.Sqlite;
4+
using ServiceStack.Text;
5+
6+
namespace ServiceStack.OrmLite.Tests
7+
{
8+
[TestFixture]
9+
public class ExpressionTests
10+
{
11+
[TestFixtureSetUp]
12+
public void TestFixtureSetUp()
13+
{
14+
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
15+
}
16+
17+
public static SqliteExpressionVisitor<Person> expr()
18+
{
19+
return new SqliteExpressionVisitor<Person>();
20+
}
21+
22+
[Test]
23+
public void Does_support_Sql_In_on_int_collections()
24+
{
25+
var ids = new[] { 1, 2, 3 };
26+
27+
Assert.That(expr().Where(q => Sql.In(q.Id, 1, 2, 3)).WhereExpression,
28+
Is.EqualTo("WHERE \"Id\" In (1,2,3)"));
29+
30+
Assert.That(expr().Where(q => Sql.In(q.Id, ids)).WhereExpression,
31+
Is.EqualTo("WHERE \"Id\" In (1,2,3)"));
32+
33+
Assert.That(expr().Where(q => Sql.In(q.Id, ids.ToList())).WhereExpression,
34+
Is.EqualTo("WHERE \"Id\" In (1,2,3)"));
35+
36+
Assert.That(expr().Where(q => Sql.In(q.Id, ids.ToList().Cast<object>())).WhereExpression,
37+
Is.EqualTo("WHERE \"Id\" In (1,2,3)"));
38+
}
39+
40+
[Test]
41+
public void Does_support_Sql_In_on_string_collections()
42+
{
43+
var ids = new[] { "A", "B", "C" };
44+
45+
Assert.That(expr().Where(q => Sql.In(q.FirstName, "A", "B", "C")).WhereExpression,
46+
Is.EqualTo("WHERE \"FirstName\" In ('A','B','C')"));
47+
48+
Assert.That(expr().Where(q => Sql.In(q.FirstName, ids)).WhereExpression,
49+
Is.EqualTo("WHERE \"FirstName\" In ('A','B','C')"));
50+
51+
Assert.That(expr().Where(q => Sql.In(q.FirstName, ids.ToList())).WhereExpression,
52+
Is.EqualTo("WHERE \"FirstName\" In ('A','B','C')"));
53+
54+
Assert.That(expr().Where(q => Sql.In(q.FirstName, ids.ToList().Cast<object>())).WhereExpression,
55+
Is.EqualTo("WHERE \"FirstName\" In ('A','B','C')"));
56+
}
57+
}
58+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<ItemGroup>
101101
<Compile Include="ApiExpressionTests.cs" />
102102
<Compile Include="EnumTests.cs" />
103+
<Compile Include="ExpressionTests.cs" />
103104
<Compile Include="ExpressionVisitorTests.cs" />
104105
<Compile Include="Expression\AdditiveExpressionsTest.cs" />
105106
<Compile Include="Expression\ConditionalExpressionTest.cs" />

0 commit comments

Comments
 (0)