From 9040c0b6d7e47d5a4ca5bc5c2bb4dbaec69b7cd6 Mon Sep 17 00:00:00 2001 From: animha Date: Mon, 11 Feb 2019 16:59:47 +0100 Subject: [PATCH] Add Contains method with StringComparison parameter. Managed to construct query with LIKE instead of instr() function for case-insensitive Contains. --- nuget/SQLite-net-base/SQLite-net-base.csproj | 3 +++ .../SQLite-net-sqlcipher.csproj | 3 +++ nuget/SQLite-net-std/SQLite-net-std.csproj | 3 +++ nuget/SQLite-net/SQLite-net.csproj | 8 ++++-- src/SQLite.cs | 14 ++++++++++ src/StringContainsExtension.cs | 26 +++++++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/StringContainsExtension.cs diff --git a/nuget/SQLite-net-base/SQLite-net-base.csproj b/nuget/SQLite-net-base/SQLite-net-base.csproj index dc49e7c5..947a8247 100644 --- a/nuget/SQLite-net-base/SQLite-net-base.csproj +++ b/nuget/SQLite-net-base/SQLite-net-base.csproj @@ -28,6 +28,9 @@ SQLiteAsync.cs + + StringContainsExtension.cs + diff --git a/nuget/SQLite-net-sqlcipher/SQLite-net-sqlcipher.csproj b/nuget/SQLite-net-sqlcipher/SQLite-net-sqlcipher.csproj index 04cc36cf..52eeaa77 100644 --- a/nuget/SQLite-net-sqlcipher/SQLite-net-sqlcipher.csproj +++ b/nuget/SQLite-net-sqlcipher/SQLite-net-sqlcipher.csproj @@ -32,5 +32,8 @@ SQLiteAsync.cs + + StringContainsExtension.cs + diff --git a/nuget/SQLite-net-std/SQLite-net-std.csproj b/nuget/SQLite-net-std/SQLite-net-std.csproj index 98975389..3b1b6163 100644 --- a/nuget/SQLite-net-std/SQLite-net-std.csproj +++ b/nuget/SQLite-net-std/SQLite-net-std.csproj @@ -32,5 +32,8 @@ SQLiteAsync.cs + + StringContainsExtension.cs + diff --git a/nuget/SQLite-net/SQLite-net.csproj b/nuget/SQLite-net/SQLite-net.csproj index 6d0caf95..7e49371b 100644 --- a/nuget/SQLite-net/SQLite-net.csproj +++ b/nuget/SQLite-net/SQLite-net.csproj @@ -32,7 +32,8 @@ false - + + true bin\Release\ RELEASE;USE_SQLITEPCL_RAW @@ -51,6 +52,9 @@ SQLiteAsync.cs + + StringContainsExtension.cs + @@ -78,4 +82,4 @@ --> - + \ No newline at end of file diff --git a/src/SQLite.cs b/src/SQLite.cs index 0ce075e0..e90327cb 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -3561,6 +3561,20 @@ private CompileResult CompileExpr (Expression expr, List queryArgs) sqlCall = "(" + args[0].CommandText + " like " + args[1].CommandText + ")"; } else if (call.Method.Name == "Contains" && args.Length == 2) { + if (call.Object != null && call.Object.Type == typeof(string)) { + var startsWithCmpOp = (StringComparison)args[1].Value; + switch (startsWithCmpOp) { + case StringComparison.Ordinal: + case StringComparison.CurrentCulture: + sqlCall = "( instr(" + obj.CommandText + "," + args[0].CommandText + ") >0 )"; + break; + case StringComparison.OrdinalIgnoreCase: + case StringComparison.CurrentCultureIgnoreCase: + sqlCall = "(" + obj.CommandText + " like ( '%' || " + args[0].CommandText + " || '%'))"; + break; + } + } + else sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")"; } else if (call.Method.Name == "Contains" && args.Length == 1) { diff --git a/src/StringContainsExtension.cs b/src/StringContainsExtension.cs new file mode 100644 index 00000000..5bf47743 --- /dev/null +++ b/src/StringContainsExtension.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SQLite_net +{ + /// + /// Class for Contains extension with StringComparison option + /// + public static class StringContainsExtension + { + /// + /// Contains extension with StringComparison option + /// + /// + /// + /// + /// + public static bool Contains (this string source, string value, StringComparison comparisonType) + { + throw new NotImplementedException ("Method not implemented: for sqlite purpose only"); + } + } +}