Skip to content

Commit 9040c0b

Browse files
committed
Add Contains method with StringComparison parameter.
Managed to construct query with LIKE instead of instr() function for case-insensitive Contains.
1 parent 91c17ad commit 9040c0b

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

nuget/SQLite-net-base/SQLite-net-base.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<Compile Include="..\..\src\SQLiteAsync.cs">
2929
<Link>SQLiteAsync.cs</Link>
3030
</Compile>
31+
<Compile Include="..\..\src\StringContainsExtension.cs">
32+
<Link>StringContainsExtension.cs</Link>
33+
</Compile>
3134
</ItemGroup>
3235
<ItemGroup>
3336
<PackageReference Include="SQLitePCLRaw.core" Version="1.1.13" />

nuget/SQLite-net-sqlcipher/SQLite-net-sqlcipher.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
<Compile Include="..\..\src\SQLiteAsync.cs">
3333
<Link>SQLiteAsync.cs</Link>
3434
</Compile>
35+
<Compile Include="..\..\src\StringContainsExtension.cs">
36+
<Link>StringContainsExtension.cs</Link>
37+
</Compile>
3538
</ItemGroup>
3639
</Project>

nuget/SQLite-net-std/SQLite-net-std.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
<Compile Include="..\..\src\SQLiteAsync.cs">
3333
<Link>SQLiteAsync.cs</Link>
3434
</Compile>
35+
<Compile Include="..\..\src\StringContainsExtension.cs">
36+
<Link>StringContainsExtension.cs</Link>
37+
</Compile>
3538
</ItemGroup>
3639
</Project>

nuget/SQLite-net/SQLite-net.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
</PropertyGroup>
3333
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3434
<DebugSymbols>false</DebugSymbols>
35-
<DebugType></DebugType>
35+
<DebugType>
36+
</DebugType>
3637
<Optimize>true</Optimize>
3738
<OutputPath>bin\Release\</OutputPath>
3839
<DefineConstants>RELEASE;USE_SQLITEPCL_RAW</DefineConstants>
@@ -51,6 +52,9 @@
5152
<Compile Include="..\..\src\SQLiteAsync.cs">
5253
<Link>SQLiteAsync.cs</Link>
5354
</Compile>
55+
<Compile Include="..\..\src\StringContainsExtension.cs">
56+
<Link>StringContainsExtension.cs</Link>
57+
</Compile>
5458
</ItemGroup>
5559
<ItemGroup>
5660
<Folder Include="Properties\" />
@@ -78,4 +82,4 @@
7882
</Target>
7983
-->
8084
<Import Project="..\..\packages\NuGet.Build.Packaging.0.2.2\build\NuGet.Build.Packaging.targets" Condition="Exists('..\..\packages\NuGet.Build.Packaging.0.2.2\build\NuGet.Build.Packaging.targets')" />
81-
</Project>
85+
</Project>

src/SQLite.cs

+14
Original file line numberDiff line numberDiff line change
@@ -3561,6 +3561,20 @@ private CompileResult CompileExpr (Expression expr, List<object> queryArgs)
35613561
sqlCall = "(" + args[0].CommandText + " like " + args[1].CommandText + ")";
35623562
}
35633563
else if (call.Method.Name == "Contains" && args.Length == 2) {
3564+
if (call.Object != null && call.Object.Type == typeof(string)) {
3565+
var startsWithCmpOp = (StringComparison)args[1].Value;
3566+
switch (startsWithCmpOp) {
3567+
case StringComparison.Ordinal:
3568+
case StringComparison.CurrentCulture:
3569+
sqlCall = "( instr(" + obj.CommandText + "," + args[0].CommandText + ") >0 )";
3570+
break;
3571+
case StringComparison.OrdinalIgnoreCase:
3572+
case StringComparison.CurrentCultureIgnoreCase:
3573+
sqlCall = "(" + obj.CommandText + " like ( '%' || " + args[0].CommandText + " || '%'))";
3574+
break;
3575+
}
3576+
}
3577+
else
35643578
sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")";
35653579
}
35663580
else if (call.Method.Name == "Contains" && args.Length == 1) {

src/StringContainsExtension.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SQLite_net
8+
{
9+
/// <summary>
10+
/// Class for Contains extension with StringComparison option
11+
/// </summary>
12+
public static class StringContainsExtension
13+
{
14+
/// <summary>
15+
/// Contains extension with StringComparison option
16+
/// </summary>
17+
/// <param name="source"></param>
18+
/// <param name="value"></param>
19+
/// <param name="comparisonType"></param>
20+
/// <returns></returns>
21+
public static bool Contains (this string source, string value, StringComparison comparisonType)
22+
{
23+
throw new NotImplementedException ("Method not implemented: for sqlite purpose only");
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)