From b0a8a80f93adf324c9f9f927a3f0f8b4a40f0f0c Mon Sep 17 00:00:00 2001 From: "Tanielian V. Barreira" Date: Wed, 4 Mar 2020 13:57:00 -0300 Subject: [PATCH 1/3] Added Convert.ToDateTime as a supported method for translation #1297 --- .../ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs index 41ba1cb55..79c2e1fbb 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs @@ -16,6 +16,7 @@ public class NpgsqlConvertTranslator : IMethodCallTranslator { [nameof(Convert.ToBoolean)] = "bool", [nameof(Convert.ToByte)] = "smallint", + [nameof(Convert.ToDateTime)] = "timestamp with time zone", [nameof(Convert.ToDecimal)] = "numeric", [nameof(Convert.ToDouble)] = "double precision", [nameof(Convert.ToInt16)] = "smallint", @@ -28,6 +29,7 @@ public class NpgsqlConvertTranslator : IMethodCallTranslator { typeof(bool), typeof(byte), + typeof(DateTime), typeof(decimal), typeof(double), typeof(float), From 6baa90fba8e39ff7a25f77edd99b615b7ef5c66c Mon Sep 17 00:00:00 2001 From: "Tanielian V. Barreira" Date: Fri, 4 Sep 2020 13:15:00 -0300 Subject: [PATCH 2/3] Resolved changes requested during review --- .../Internal/NpgsqlConvertTranslator.cs | 18 +++++++++--------- .../Query/NorthwindFunctionsQueryNpgsqlTest.cs | 14 +++++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs index b7b422699..b252ad953 100644 --- a/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs +++ b/src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlConvertTranslator.cs @@ -17,15 +17,15 @@ public class NpgsqlConvertTranslator : IMethodCallTranslator { static readonly Dictionary TypeMapping = new Dictionary { - [nameof(Convert.ToBoolean)] = "bool", - [nameof(Convert.ToByte)] = "smallint", - [nameof(Convert.ToDateTime)] = "timestamp with time zone", - [nameof(Convert.ToDecimal)] = "numeric", - [nameof(Convert.ToDouble)] = "double precision", - [nameof(Convert.ToInt16)] = "smallint", - [nameof(Convert.ToInt32)] = "int", - [nameof(Convert.ToInt64)] = "bigint", - [nameof(Convert.ToString)] = "text" + [nameof(Convert.ToBoolean)] = "bool", + [nameof(Convert.ToByte)] = "smallint", + [nameof(Convert.ToDateTime)] = "timestamp with time zone", + [nameof(Convert.ToDecimal)] = "numeric", + [nameof(Convert.ToDouble)] = "double precision", + [nameof(Convert.ToInt16)] = "smallint", + [nameof(Convert.ToInt32)] = "int", + [nameof(Convert.ToInt64)] = "bigint", + [nameof(Convert.ToString)] = "text" }; static readonly List SupportedTypes = new List diff --git a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs index 11d982ad7..f38e94b24 100644 --- a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -41,6 +41,18 @@ public override Task Where_math_log_new_base(bool async) public override Task Convert_ToString(bool async) => base.Convert_ToString(async); + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual async Task Convert_ToDateTime(bool async) + { + await AssertQuery( + async, + ss => ss.Set().Where(o => o.OrderDate.HasValue && o.OrderDate == Convert.ToDateTime(o.OrderDate.ToString())), + entryCount: 830); + + AssertContainsSqlFragment(@"WHERE (o.""OrderDate"" IS NOT NULL) AND (o.""OrderDate"" = CAST(CAST(o.""OrderDate"" AS text) AS timestamp without time zone))"); + } + #region Substring [ConditionalTheory] From dba3629e093c81cff982ceef52d642e6c9bfbbba Mon Sep 17 00:00:00 2001 From: "Tanielian V. Barreira" Date: Fri, 4 Sep 2020 14:18:59 -0300 Subject: [PATCH 3/3] Improved Convert.ToDateTime test --- .../Query/NorthwindFunctionsQueryNpgsqlTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs index f38e94b24..18ba64bfb 100644 --- a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs @@ -47,10 +47,10 @@ public virtual async Task Convert_ToDateTime(bool async) { await AssertQuery( async, - ss => ss.Set().Where(o => o.OrderDate.HasValue && o.OrderDate == Convert.ToDateTime(o.OrderDate.ToString())), + ss => ss.Set().Where(o => o.OrderDate == Convert.ToDateTime(o.OrderDate.ToString())), entryCount: 830); - AssertContainsSqlFragment(@"WHERE (o.""OrderDate"" IS NOT NULL) AND (o.""OrderDate"" = CAST(CAST(o.""OrderDate"" AS text) AS timestamp without time zone))"); + AssertContainsSqlFragment(@"WHERE (o.""OrderDate"" = CAST(CAST(o.""OrderDate"" AS text) AS timestamp without time zone)) OR (o.""OrderDate"" IS NULL)"); } #region Substring