From f1737ac6145f3954b823a5d61a82521c78512d3f Mon Sep 17 00:00:00 2001 From: Martin Pum Date: Fri, 3 Jun 2016 12:05:28 +0200 Subject: [PATCH] Added support for REPLACE-function in WHERE-clause --- src/SQLite.Net/TableQuery.cs | 4 +++ tests/ReplaceTest.cs | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/ReplaceTest.cs diff --git a/src/SQLite.Net/TableQuery.cs b/src/SQLite.Net/TableQuery.cs index 588881a26..96c4f989a 100644 --- a/src/SQLite.Net/TableQuery.cs +++ b/src/SQLite.Net/TableQuery.cs @@ -432,6 +432,10 @@ private CompileResult CompileExpr([NotNull] Expression expr, List queryA { sqlCall = "(upper(" + obj.CommandText + "))"; } + else if (call.Method.Name == "Replace" && args.Length == 2) + { + sqlCall = "(replace(" + obj.CommandText + ", " + args[0].CommandText + ", " + args[1].CommandText + "))"; + } else { sqlCall = call.Method.Name.ToLower() + "(" + diff --git a/tests/ReplaceTest.cs b/tests/ReplaceTest.cs new file mode 100644 index 000000000..5674d5bec --- /dev/null +++ b/tests/ReplaceTest.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; +using SQLite.Net.Attributes; +using SQLite.Net.Interop; + +namespace SQLite.Net.Tests +{ + [TestFixture] + public class ReplaceTest + { + public class TestObj + { + [AutoIncrement, PrimaryKey] + public int Id { get; set; } + + public string Name { get; set; } + + public override string ToString() + { + return string.Format("[TestObj: Id={0}, Name={1}]", Id, Name); + } + } + + public class TestDb : SQLiteConnection + { + public TestDb(ISQLitePlatform sqlitePlatform, string path) + : base(sqlitePlatform, path) + { + CreateTable(); + } + } + + + [Test] + + public void ReplaceInWhere() + { + string testElement = "Element"; + string alternateElement = "Alternate"; + string replacedElement = "ReplacedElement"; + + int n = 20; + IEnumerable cq = from i in Enumerable.Range(1, n) + select new TestObj + { + Name = (i % 2 == 0) ? testElement : alternateElement + }; + + var db = new TestDb(new SQLitePlatformTest(), TestPath.CreateTemporaryDatabase()); + + db.InsertAll(cq); + + db.TraceListener = DebugTraceListener.Instance; + + + List result = (from o in db.Table() where o.Name.Replace(testElement, replacedElement) == replacedElement select o).ToList(); + Assert.AreEqual(10, result.Count); + + } + + } +} \ No newline at end of file