Skip to content

Add AnyOf to TableQuery to build more complex OR clauses. #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/SQLite.Net/TableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@ public TableQuery<T> Where([NotNull] Expression<Func<T, bool>> predExpr)
return q;
}

[PublicAPI]
public TableQuery<T> AnyOf(params Expression<Func<T, bool>>[] predExprs)
{
if (predExprs.Length == 0)
throw new ArgumentException("predExprs is empty");

Expression[] preds = new Expression[predExprs.Length];
int i = 0;
foreach (Expression<Func<T, bool>> predExpr in predExprs)
{
if (predExpr == null)
{
throw new ArgumentNullException("predExpr");
}
if (predExpr.NodeType != ExpressionType.Lambda)
{
throw new NotSupportedException("Must be a predicate");
}
var lambda = (LambdaExpression)predExpr;
var pred = lambda.Body;
preds[i] = pred;

i++;
}

var q = Clone<T>();
q.AddAnyOf(preds);
return q;
}

[PublicAPI]
public TableQuery<T> Take(int n)
{
Expand Down Expand Up @@ -277,6 +307,27 @@ private void AddWhere([NotNull] Expression pred)
}
}

private void AddAnyOf(Expression[] preds)
{
if (preds.Length == 0)
{
throw new ArgumentException("preds is empty");
}
if (_limit != null || _offset != null)
{
throw new NotSupportedException("Cannot call anyof after a skip or a take");
}

var expr = preds[0];
for (int i = 1, len = preds.Length; i < len; i++)
{
var pred = preds[i];
expr = Expression.OrElse(expr, pred);
}

AddWhere(expr);
}

[PublicAPI]
public TableQuery<TResult> Join<TInner, TKey, TResult>(
TableQuery<TInner> inner,
Expand Down
81 changes: 81 additions & 0 deletions tests/AnyOfTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using SQLite.Net.Attributes;

#if __WIN32__
using SQLitePlatformTest = SQLite.Net.Platform.Win32.SQLitePlatformWin32;
#elif WINDOWS_PHONE
using SQLitePlatformTest = SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8;
#elif __WINRT__
using SQLitePlatformTest = SQLite.Net.Platform.WinRT.SQLitePlatformWinRT;
#elif __IOS__
using SQLitePlatformTest = SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS;
#elif __ANDROID__
using SQLitePlatformTest = SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid;
#else
using SQLitePlatformTest = SQLite.Net.Platform.Generic.SQLitePlatformGeneric;
#endif


namespace SQLite.Net.Tests
{
[TestFixture]
internal class AnyOfTest
{
public class TestObj
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }

public string ColumnA { get; set; }
public string ColumnB { get; set; }

}

public class TestDb : SQLiteConnection
{
public TestDb(String path)
: base(new SQLitePlatformTest(), path)
{
CreateTable<TestObj>();

Insert(new TestObj { ColumnA = "Foo", ColumnB = "Bar" });
Insert(new TestObj { ColumnA = "Bar", ColumnB = "Baz" });
Insert(new TestObj { ColumnA = "Baz", ColumnB = "Qux" });
}
}

[Test]
public void ParamsList()
{
var db = new TestDb(TestPath.GetTempFileName());

TableQuery<TestObj> results = db.Table<TestObj>().AnyOf(o => o.ColumnA == "Foo", o => o.ColumnB == "Qux");
List<TestObj> resultsList = results.OrderBy(o => o.Id).ToList();

Assert.AreEqual(2, resultsList.Count);
Assert.AreEqual("Bar", resultsList[0].ColumnB);
Assert.AreEqual("Baz", resultsList[1].ColumnA);
}

[Test]
public void Array()
{
var db = new TestDb(TestPath.GetTempFileName());

List<Expression<Func<TestObj, bool>>> preds = new List<Expression<Func<TestObj, bool>>>();
preds.Add(o => o.ColumnA == "Foo");
preds.Add(o => o.ColumnB == "Qux");

TableQuery<TestObj> results = db.Table<TestObj>().AnyOf(preds.ToArray());
List<TestObj> resultsList = results.OrderBy(o => o.Id).ToList();

Assert.AreEqual(2, resultsList.Count);
Assert.AreEqual("Bar", resultsList[0].ColumnB);
Assert.AreEqual("Baz", resultsList[1].ColumnA);
}
}
}