Skip to content

Commit

Permalink
Merge pull request praeclarum#164 from Shiney/AddSubtract
Browse files Browse the repository at this point in the history
Added the ability to use + and - operators in expressions
  • Loading branch information
oysteinkrog committed Apr 11, 2015
2 parents 72c7a1f + 7eb47e4 commit 859b86e
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/SQLite.Net/TableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ private string CompileNullBinaryExpression(BinaryExpression expression, CompileR
expression.NodeType);
}

private string GetSqlName(Expression expr)
private string GetSqlName(BinaryExpression expr)
{
var n = expr.NodeType;
if (n == ExpressionType.GreaterThan)
Expand Down Expand Up @@ -609,6 +609,20 @@ private string GetSqlName(Expression expr)
{
return "!=";
}
if (n == ExpressionType.Add)
{
if (expr.Left.Type == typeof(string))
{
return "||";
}
return "+";

}
if (n == ExpressionType.Subtract)
{
return "-";
}

throw new NotSupportedException("Cannot get SQL for: " + n);
}

Expand Down
109 changes: 109 additions & 0 deletions tests/ArithmeticTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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 ArithmeticTest
{
public abstract class TestObjBase<T>
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }

public T Data { get; set; }

}

public class TestObjString : TestObjBase<string>
{
}

public class TestObjInt : TestObjBase<int>
{
}

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

[Test]
public void CanHaveAddInWhereClause()
{
int n = 20;
IEnumerable<TestObjInt> cq = from i in Enumerable.Range(1, n)
select new TestObjInt()
{
Data = i,
};

var db = new TestDb(TestPath.GetTempFileName());
db.InsertAll(cq);

TableQuery<TestObjInt> results = db.Table<TestObjInt>().Where(o => o.Data + 10 >= n);
Assert.AreEqual(results.Count(), 11);
Assert.AreEqual(results.OrderBy(o => o.Data).FirstOrDefault().Data, 10);
}

[Test]
public void CanHaveSubtractInWhereClause()
{
int n = 20;
IEnumerable<TestObjInt> cq = from i in Enumerable.Range(1, n)
select new TestObjInt()
{
Data = i,
};

var db = new TestDb(TestPath.GetTempFileName());
db.InsertAll(cq);

TableQuery<TestObjInt> results = db.Table<TestObjInt>().Where(o => o.Data - 10 >= 0);
Assert.AreEqual(results.Count(), 11);
Assert.AreEqual(results.OrderBy(o => o.Data).FirstOrDefault().Data, 10);
}

[Test]
public void AddForStringsMeansConcatenate()
{
int n = 20;
IEnumerable<TestObjString> cq = from i in Enumerable.Range(1, n)
select new TestObjString()
{
Data = i.ToString(),
};

var db = new TestDb(TestPath.GetTempFileName());
db.InsertAll(cq);

TableQuery<TestObjString> results = db.Table<TestObjString>().Where(o => o.Data + "1" == "11");

Assert.AreEqual(1, results.Count());
Assert.AreEqual("1", results.OrderBy(o => o.Data).FirstOrDefault().Data);
}
}
}

0 comments on commit 859b86e

Please sign in to comment.