From 23b8257fc8d8dc23d17b447d09b183dddecd152e Mon Sep 17 00:00:00 2001 From: Floris Verhoeven Date: Sun, 29 Nov 2020 14:15:45 +0100 Subject: [PATCH] Trim called without arguments throws index out of range (#77) * Check arguments before accessing in jet string method translator * Change expected result on to lower and to upper query tests * Change expected result on trim query tests * Remove original fix and fix condition chaining in trim clause generation Co-authored-by: Floris Verhoeven --- .../Internal/JetStringMethodTranslator.cs | 12 ++++++------ .../Query/SimpleQueryJetTest.Functions.cs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetStringMethodTranslator.cs b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetStringMethodTranslator.cs index 9957405..68e952a 100644 --- a/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetStringMethodTranslator.cs +++ b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetStringMethodTranslator.cs @@ -110,8 +110,8 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal // Jet TRIM does not take arguments. // _trimWithNoParam is only available since .NET Core 2.0 (or .NET Standard 2.1). if (Equals(method, _trimWithNoParam) || - Equals(method, _trimWithChars) && - (arguments[0] as SqlConstantExpression)?.Value == null || ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0) + Equals(method, _trimWithChars) && ((arguments[0] as SqlConstantExpression)?.Value == null || + ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0)) { return _sqlExpressionFactory.Function("TRIM", new[] {instance}, method.ReturnType); } @@ -119,8 +119,8 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal // Jet LTRIM does not take arguments // _trimStartWithNoParam is only available since .NET Core 2.0 (or .NET Standard 2.1). if (Equals(method, _trimStartWithNoParam) || - Equals(method, _trimStartWithChars) && - (arguments[0] as SqlConstantExpression)?.Value == null || ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0) + Equals(method, _trimStartWithChars) && ((arguments[0] as SqlConstantExpression)?.Value == null || + ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0)) { return _sqlExpressionFactory.Function("LTRIM", new[] {instance}, method.ReturnType); } @@ -128,8 +128,8 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal // Jet RTRIM does not take arguments // _trimEndWithNoParam is only available since .NET Core 2.0 (or .NET Standard 2.1). if (Equals(method, _trimEndWithNoParam) || - Equals(method, _trimEndWithChars) && - (arguments[0] as SqlConstantExpression)?.Value == null || ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0) + Equals(method, _trimEndWithChars) && ((arguments[0] as SqlConstantExpression)?.Value == null || + ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0)) { return _sqlExpressionFactory.Function("RTRIM", new[] {instance}, method.ReturnType); } diff --git a/test/EFCore.Jet.FunctionalTests/Query/SimpleQueryJetTest.Functions.cs b/test/EFCore.Jet.FunctionalTests/Query/SimpleQueryJetTest.Functions.cs index d285e38..a6f47f7 100644 --- a/test/EFCore.Jet.FunctionalTests/Query/SimpleQueryJetTest.Functions.cs +++ b/test/EFCore.Jet.FunctionalTests/Query/SimpleQueryJetTest.Functions.cs @@ -847,7 +847,7 @@ WHERE (NEWID() <> '00000000-0000-0000-0000-000000000000') OR NEWID() IS NULL"); AssertSql( $@"SELECT `c`.`CustomerID`, `c`.`Address`, `c`.`City`, `c`.`CompanyName`, `c`.`ContactName`, `c`.`ContactTitle`, `c`.`Country`, `c`.`Fax`, `c`.`Phone`, `c`.`PostalCode`, `c`.`Region` FROM `Customers` AS `c` -WHERE UPPER(`c`.`CustomerID`) = 'ALFKI'"); +WHERE UCASE(`c`.`CustomerID`) = 'ALFKI'"); } public override async Task Where_string_to_lower(bool isAsync) @@ -857,7 +857,7 @@ WHERE UPPER(`c`.`CustomerID`) = 'ALFKI'"); AssertSql( $@"SELECT `c`.`CustomerID`, `c`.`Address`, `c`.`City`, `c`.`CompanyName`, `c`.`ContactName`, `c`.`ContactTitle`, `c`.`Country`, `c`.`Fax`, `c`.`Phone`, `c`.`PostalCode`, `c`.`Region` FROM `Customers` AS `c` -WHERE LOWER(`c`.`CustomerID`) = 'alfki'"); +WHERE LCASE(`c`.`CustomerID`) = 'alfki'"); } public override async Task Where_functions_nested(bool isAsync) @@ -1413,7 +1413,7 @@ WHERE RTRIM(`c`.`ContactTitle`) = 'Owner'"); AssertSql( $@"SELECT `c`.`CustomerID`, `c`.`Address`, `c`.`City`, `c`.`CompanyName`, `c`.`ContactName`, `c`.`ContactTitle`, `c`.`Country`, `c`.`Fax`, `c`.`Phone`, `c`.`PostalCode`, `c`.`Region` FROM `Customers` AS `c` -WHERE LTRIM(RTRIM(`c`.`ContactTitle`)) = 'Owner'"); +WHERE TRIM(`c`.`ContactTitle`) = 'Owner'"); } [ConditionalTheory(Skip = "Issue#17328")]