diff --git a/docs/pages/guide/gridifyGlobalConfiguration.md b/docs/pages/guide/gridifyGlobalConfiguration.md index 02f580ac..3a58756c 100644 --- a/docs/pages/guide/gridifyGlobalConfiguration.md +++ b/docs/pages/guide/gridifyGlobalConfiguration.md @@ -47,6 +47,13 @@ some ORMs like NHibernate don't support this. You can disable this behavior by s - type: `bool` - default: `false` +### DefaultDateTimeKind + +By default, Gridify uses the `DateTimeKind.Unspecified` when parsing dates. You can change this behavior by setting this property to `DateTimeKind.Utc` or `DateTimeKind.Local`. This option is useful when you want to use Gridify with a database that requires a specific `DateTimeKind`, for example when using npgsql and postgresql. + +- type: `DateTimeKind` +- default: `null` + ## CustomOperators Using the `Register` method of this property you can add your own custom operators. diff --git a/docs/pages/guide/gridifyMapper.md b/docs/pages/guide/gridifyMapper.md index f04b0d18..abb80517 100644 --- a/docs/pages/guide/gridifyMapper.md +++ b/docs/pages/guide/gridifyMapper.md @@ -144,6 +144,17 @@ By setting this to `false`, Gridify don't allow searching on null values using t var mapper = new GridifyMapper(q => q.AllowNullSearch = false); ``` +### DefaultDateTimeKind + +By setting this property to a `DateTimeKind` value, you can change the default `DateTimeKind` used when parsing dates. + +- type: `DateTimeKind` +- default: `null` + +``` csharp +var mapper = new GridifyMapper(q => q.DefaultDateTimeKind = DateTimeKind.Utc); +``` + ## Filtering on Nested Collections You can use LINQ `Select` and `SelectMany` methods to filter your data using its nested collections. diff --git a/src/Gridify/Builder/BaseQueryBuilder.cs b/src/Gridify/Builder/BaseQueryBuilder.cs index f08829c1..9131172e 100644 --- a/src/Gridify/Builder/BaseQueryBuilder.cs +++ b/src/Gridify/Builder/BaseQueryBuilder.cs @@ -198,6 +198,14 @@ private static object AddIndexerNullCheck(IGMap gMap, object query) { return BuildAlwaysFalseQuery(parameter); } + + if (value is DateTime dateTime) + { + if (mapper.Configuration.DefaultDateTimeKind.HasValue) + { + value = DateTime.SpecifyKind(dateTime, mapper.Configuration.DefaultDateTimeKind.Value); + } + } } // handle case-Insensitive search diff --git a/src/Gridify/GridifyGlobalConfiguration.cs b/src/Gridify/GridifyGlobalConfiguration.cs index 23a91350..b1eb4229 100644 --- a/src/Gridify/GridifyGlobalConfiguration.cs +++ b/src/Gridify/GridifyGlobalConfiguration.cs @@ -46,6 +46,13 @@ public static class GridifyGlobalConfiguration /// Default is false /// public static bool DisableNullChecks { get; set; } = false; + + /// + /// By default, DateTimeKind.Unspecified is used. + /// You can change this behavior by setting this property to a DateTimeKind value. + /// Default is null + /// + public static DateTimeKind? DefaultDateTimeKind { get; set; } = null; /// /// Specifies how field names are inferred from CLR property names. diff --git a/src/Gridify/GridifyMapperConfiguration.cs b/src/Gridify/GridifyMapperConfiguration.cs index 45dfed77..aed7526e 100644 --- a/src/Gridify/GridifyMapperConfiguration.cs +++ b/src/Gridify/GridifyMapperConfiguration.cs @@ -23,6 +23,13 @@ public record GridifyMapperConfiguration /// Default is false /// public bool IgnoreNotMappedFields { get; set; } = GridifyGlobalConfiguration.IgnoreNotMappedFields; + + /// + /// By default, DateTimeKind.Unspecified is used. + /// You can change this behavior by setting this property to a DateTimeKind value. + /// Default is null + /// + public DateTimeKind? DefaultDateTimeKind { get; set; } = GridifyGlobalConfiguration.DefaultDateTimeKind; /// /// Specifies how field names are inferred from CLR property names. diff --git a/test/EntityFrameworkPostgreSqlIntegrationTests/Issue188Tests.cs b/test/EntityFrameworkPostgreSqlIntegrationTests/Issue188Tests.cs new file mode 100644 index 00000000..44638a31 --- /dev/null +++ b/test/EntityFrameworkPostgreSqlIntegrationTests/Issue188Tests.cs @@ -0,0 +1,30 @@ +using EntityFrameworkIntegrationTests.cs; +using Microsoft.EntityFrameworkCore; +using Xunit; + +namespace Gridify.Tests; + +public class Issue188Tests +{ + + private readonly MyDbContext _dbContext = new(); + + [Fact] + public void DateTimeFilteringWithUTCKind_UsingGridifyMapper_ShouldNotThrowException() + { + var mapper = new GridifyMapper(q => q.DefaultDateTimeKind = DateTimeKind.Utc) + .GenerateMappings(); + _dbContext.Users.ApplyFiltering("CreateDate>2023-11-14", mapper).ToQueryString(); + } + + [Fact] + public void DateTimeFilteringWithUTCKind_UsingGlobalConfiguration_ShouldNotThrowException() + { + GridifyGlobalConfiguration.DefaultDateTimeKind = DateTimeKind.Utc; + _dbContext.Users.ApplyFiltering("CreateDate>2023-11-14").ToQueryString(); + GridifyGlobalConfiguration.DefaultDateTimeKind = null; + } + +} + +