From 545a04c59d98c0ade5ca30e71620bd8ff7102d0f Mon Sep 17 00:00:00 2001 From: microspaze Date: Tue, 16 Jan 2024 17:04:46 +0800 Subject: [PATCH] Add Contains method with StringComparison parameter. --- src/SQLite.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 5b6941c3..3a189dfe 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -4064,6 +4064,9 @@ private SQLiteCommand GenerateCommand (string selectionList) } cmdText += " offset " + _offset.Value; } + + //Remove all StringComparison arguments from args, otherwise the created command may unmatch with the real arguments. + args.RemoveAll (x => x is StringComparison); return Connection.CreateCommand (cmdText, args.ToArray ()); } } @@ -4133,7 +4136,24 @@ private CompileResult CompileExpr (Expression expr, List queryArgs) sqlCall = "(" + args[0].CommandText + " like " + args[1].CommandText + ")"; } else if (call.Method.Name == "Contains" && args.Length == 2) { - sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")"; + //Add Contains method with StringComparison parameter. + if (call.Object != null && call.Object.Type == typeof (string) && args[1].Value is StringComparison comparison) { + switch (comparison) { + case StringComparison.Ordinal: + case StringComparison.CurrentCulture: + case StringComparison.InvariantCulture: + sqlCall = "(instr(" + obj.CommandText + "," + args[0].CommandText + ") > 0)"; + break; + case StringComparison.OrdinalIgnoreCase: + case StringComparison.CurrentCultureIgnoreCase: + case StringComparison.InvariantCultureIgnoreCase: + 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) { if (call.Object != null && call.Object.Type == typeof (string)) {