Skip to content

Commit 545a04c

Browse files
committed
Add Contains method with StringComparison parameter.
1 parent e8a24a8 commit 545a04c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/SQLite.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -4064,6 +4064,9 @@ private SQLiteCommand GenerateCommand (string selectionList)
40644064
}
40654065
cmdText += " offset " + _offset.Value;
40664066
}
4067+
4068+
//Remove all StringComparison arguments from args, otherwise the created command may unmatch with the real arguments.
4069+
args.RemoveAll (x => x is StringComparison);
40674070
return Connection.CreateCommand (cmdText, args.ToArray ());
40684071
}
40694072
}
@@ -4133,7 +4136,24 @@ private CompileResult CompileExpr (Expression expr, List<object> queryArgs)
41334136
sqlCall = "(" + args[0].CommandText + " like " + args[1].CommandText + ")";
41344137
}
41354138
else if (call.Method.Name == "Contains" && args.Length == 2) {
4136-
sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")";
4139+
//Add Contains method with StringComparison parameter.
4140+
if (call.Object != null && call.Object.Type == typeof (string) && args[1].Value is StringComparison comparison) {
4141+
switch (comparison) {
4142+
case StringComparison.Ordinal:
4143+
case StringComparison.CurrentCulture:
4144+
case StringComparison.InvariantCulture:
4145+
sqlCall = "(instr(" + obj.CommandText + "," + args[0].CommandText + ") > 0)";
4146+
break;
4147+
case StringComparison.OrdinalIgnoreCase:
4148+
case StringComparison.CurrentCultureIgnoreCase:
4149+
case StringComparison.InvariantCultureIgnoreCase:
4150+
sqlCall = "(" + obj.CommandText + " like ('%' ||" + args[0].CommandText + "|| '%'))";
4151+
break;
4152+
}
4153+
}
4154+
else {
4155+
sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")";
4156+
}
41374157
}
41384158
else if (call.Method.Name == "Contains" && args.Length == 1) {
41394159
if (call.Object != null && call.Object.Type == typeof (string)) {

0 commit comments

Comments
 (0)