Skip to content

Commit

Permalink
Cosmos: Add translator for ToLower method which map to LOWER built-in…
Browse files Browse the repository at this point in the history
… function (#24203)
  • Loading branch information
Marusyk authored Feb 22, 2021
1 parent 5b225e5 commit 15115d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private static readonly MethodInfo _startsWithMethodInfo
private static readonly MethodInfo _endsWithMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.EndsWith), new[] { typeof(string) });

private static readonly MethodInfo _toLowerMethodInfo
= typeof(string).GetRequiredRuntimeMethod(nameof(string.ToLower), Array.Empty<Type>());

private static readonly MethodInfo _firstOrDefaultMethodInfoWithoutArgs
= typeof(Enumerable).GetRuntimeMethods().Single(
m => m.Name == nameof(Enumerable.FirstOrDefault)
Expand Down Expand Up @@ -85,38 +88,43 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
{
if (_containsMethodInfo.Equals(method))
{
return TranslateSystemFunction("CONTAINS", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("CONTAINS", typeof(bool), instance, arguments[0]);
}

if (_startsWithMethodInfo.Equals(method))
{
return TranslateSystemFunction("STARTSWITH", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("STARTSWITH", typeof(bool), instance, arguments[0]);
}

if (_endsWithMethodInfo.Equals(method))
{
return TranslateSystemFunction("ENDSWITH", instance, arguments[0], typeof(bool));
return TranslateSystemFunction("ENDSWITH", typeof(bool), instance, arguments[0]);
}

if (_toLowerMethodInfo.Equals(method))
{
return TranslateSystemFunction("LOWER", method.ReturnType, instance);
}
}

if (_firstOrDefaultMethodInfoWithoutArgs.Equals(method))
{
return TranslateSystemFunction("LEFT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char));
return TranslateSystemFunction("LEFT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1));
}

if (_lastOrDefaultMethodInfoWithoutArgs.Equals(method))
{
return TranslateSystemFunction("RIGHT", arguments[0], _sqlExpressionFactory.Constant(1), typeof(char));
return TranslateSystemFunction("RIGHT", typeof(char), arguments[0], _sqlExpressionFactory.Constant(1));
}

if(_stringConcatWithTwoArguments.Equals(method))
if (_stringConcatWithTwoArguments.Equals(method))
{
return _sqlExpressionFactory.Add(
arguments[0],
arguments[1]);
}

if(_stringConcatWithThreeArguments.Equals(method))
if (_stringConcatWithThreeArguments.Equals(method))
{
return _sqlExpressionFactory.Add(
arguments[0],
Expand All @@ -139,7 +147,7 @@ public StringMethodTranslator([NotNull] ISqlExpressionFactory sqlExpressionFacto
return null;
}

private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, SqlExpression pattern, Type returnType)
=> _sqlExpressionFactory.Function(function, new[] { instance, pattern }, returnType);
private SqlExpression TranslateSystemFunction(string function, Type returnType, params SqlExpression[] arguments)
=> _sqlExpressionFactory.Function(function, arguments, returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_string_to_lower(bool async)
{
await base.Where_string_to_lower(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
WHERE ((c[""Discriminator""] = ""Customer"") AND (LOWER(c[""CustomerID""]) = ""alfki""))");
}

[ConditionalTheory(Skip = "Issue #17246")]
Expand Down

0 comments on commit 15115d8

Please sign in to comment.