Skip to content

Commit

Permalink
Add Contains method with StringComparison parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy-less committed Dec 2, 2024
1 parent 3df9abf commit 1ab711b
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions SQLiteSharp/TableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private CompileResult CompileExpression(Expression expression, List<object?> que
else if (expression.NodeType is ExpressionType.Call) {
MethodCallExpression call = (MethodCallExpression)expression;
CompileResult[] callArguments = new CompileResult[call.Arguments.Count];
CompileResult? callTarget = call.Object is not null ? CompileExpression(call.Object, queryParameters) : null;
CompileResult callTarget = call.Object is not null ? CompileExpression(call.Object, queryParameters) : default;

for (int i = 0; i < callArguments.Length; i++) {
callArguments[i] = CompileExpression(call.Arguments[i], queryParameters);
Expand All @@ -200,14 +200,30 @@ private CompileResult CompileExpression(Expression expression, List<object?> que
sqlCall = "(" + callArguments[0].CommandText + " like " + callArguments[1].CommandText + ")";
}
else if (call.Method.Name is "Contains" && callArguments.Length == 2) {
sqlCall = "(" + callArguments[1].CommandText + " in " + callArguments[0].CommandText + ")";
// string.Contains(string, StringComparison)
if (call.Object?.Type == typeof(string)) {
StringComparison comparison = (StringComparison)callArguments[1].Value!;
switch (comparison) {
case StringComparison.Ordinal:
case StringComparison.CurrentCulture:
sqlCall = "( instr(" + callTarget.CommandText + "," + callArguments[0].CommandText + ") >0 )";
break;
case StringComparison.OrdinalIgnoreCase:
case StringComparison.CurrentCultureIgnoreCase:
sqlCall = "(" + callTarget.CommandText + " like ( '%' || " + callArguments[0].CommandText + " || '%'))";
break;
}
}
else {
sqlCall = "(" + callArguments[1].CommandText + " in " + callArguments[0].CommandText + ")";
}
}
else if (call.Method.Name is "Contains" && callArguments.Length == 1) {
if (call.Object != null && call.Object.Type == typeof(string)) {
sqlCall = "( instr(" + callTarget!.Value.CommandText + "," + callArguments[0].CommandText + ") >0 )";
if (call.Object is not null && call.Object.Type == typeof(string)) {
sqlCall = "( instr(" + callTarget.CommandText + "," + callArguments[0].CommandText + ") >0 )";
}
else {
sqlCall = "(" + callArguments[0].CommandText + " in " + callTarget!.Value.CommandText + ")";
sqlCall = "(" + callArguments[0].CommandText + " in " + callTarget.CommandText + ")";
}
}
else if (call.Method.Name is "StartsWith" && callArguments.Length >= 1) {
Expand All @@ -217,10 +233,10 @@ private CompileResult CompileExpression(Expression expression, List<object?> que
}
switch (comparisonType) {
case StringComparison.Ordinal or StringComparison.CurrentCulture:
sqlCall = "( substr(" + callTarget!.Value.CommandText + ", 1, " + callArguments[0].Value!.ToString()!.Length + ") = " + callArguments[0].CommandText + ")";
sqlCall = "( substr(" + callTarget.CommandText + ", 1, " + callArguments[0].Value!.ToString()!.Length + ") = " + callArguments[0].CommandText + ")";
break;
case StringComparison.OrdinalIgnoreCase or StringComparison.CurrentCultureIgnoreCase:
sqlCall = "(" + callTarget!.Value.CommandText + " like (" + callArguments[0].CommandText + " || '%'))";
sqlCall = "(" + callTarget.CommandText + " like (" + callArguments[0].CommandText + " || '%'))";
break;
}
}
Expand All @@ -231,24 +247,24 @@ private CompileResult CompileExpression(Expression expression, List<object?> que
}
switch (comparisonType) {
case StringComparison.Ordinal or StringComparison.CurrentCulture:
sqlCall = "( substr(" + callTarget!.Value.CommandText + ", length(" + callTarget.Value.CommandText + ") - " + callArguments[0].Value!.ToString()!.Length + "+1, " + callArguments[0].Value!.ToString()!.Length + ") = " + callArguments[0].CommandText + ")";
sqlCall = "( substr(" + callTarget.CommandText + ", length(" + callTarget.CommandText + ") - " + callArguments[0].Value!.ToString()!.Length + "+1, " + callArguments[0].Value!.ToString()!.Length + ") = " + callArguments[0].CommandText + ")";
break;
case StringComparison.OrdinalIgnoreCase or StringComparison.CurrentCultureIgnoreCase:
sqlCall = "(" + callTarget!.Value.CommandText + " like ('%' || " + callArguments[0].CommandText + "))";
sqlCall = "(" + callTarget.CommandText + " like ('%' || " + callArguments[0].CommandText + "))";
break;
}
}
else if (call.Method.Name is "Equals" && callArguments.Length == 1) {
sqlCall = "(" + callTarget!.Value.CommandText + " = (" + callArguments[0].CommandText + "))";
sqlCall = "(" + callTarget.CommandText + " = (" + callArguments[0].CommandText + "))";
}
else if (call.Method.Name is "ToLower") {
sqlCall = "(lower(" + callTarget!.Value.CommandText + "))";
sqlCall = "(lower(" + callTarget.CommandText + "))";
}
else if (call.Method.Name is "ToUpper") {
sqlCall = "(upper(" + callTarget!.Value.CommandText + "))";
sqlCall = "(upper(" + callTarget.CommandText + "))";
}
else if (call.Method.Name is "Replace" && callArguments.Length == 2) {
sqlCall = "(replace(" + callTarget!.Value.CommandText + "," + callArguments[0].CommandText + "," + callArguments[1].CommandText + "))";
sqlCall = "(replace(" + callTarget.CommandText + "," + callArguments[0].CommandText + "," + callArguments[1].CommandText + "))";
}
else if (call.Method.Name is "IsNullOrEmpty" && callArguments.Length == 1) {
sqlCall = "(" + callArguments[0].CommandText + " is null or" + callArguments[0].CommandText + " ='' )";
Expand Down

0 comments on commit 1ab711b

Please sign in to comment.