Skip to content

Commit 114ed4e

Browse files
committed
Add translations for NodaTime LocalDate.{At,AtMidnight}
Closes #3576
1 parent cc3245c commit 114ed4e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMethodCallTranslatorPlugin.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,27 @@ public NpgsqlNodaTimeMethodCallTranslator(
281281
return _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.Distance, arguments[1], arguments[2]);
282282
}
283283

284+
if (method.DeclaringType == typeof(LocalDate))
285+
{
286+
return method.Name switch
287+
{
288+
nameof(LocalDate.At) => new SqlBinaryExpression(
289+
ExpressionType.Add,
290+
_sqlExpressionFactory.ApplyDefaultTypeMapping(instance!),
291+
_sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]),
292+
typeof(LocalDateTime),
293+
_typeMappingSource.FindMapping(typeof(LocalDateTime))),
294+
295+
nameof(LocalDate.AtMidnight) => new SqlBinaryExpression(
296+
ExpressionType.Add,
297+
_sqlExpressionFactory.ApplyDefaultTypeMapping(instance!),
298+
new SqlConstantExpression(new LocalTime(0, 0, 0), _typeMappingSource.FindMapping(typeof(LocalTime))),
299+
typeof(LocalDateTime),
300+
_typeMappingSource.FindMapping(typeof(LocalDateTime))),
301+
302+
_ => null
303+
};
304+
}
284305
return null;
285306
}
286307

test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/LocalDateTranslationsTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,38 @@ LIMIT 1
177177
""");
178178
}
179179

180+
[ConditionalTheory]
181+
[MemberData(nameof(IsAsyncData))]
182+
public async Task At(bool async)
183+
{
184+
await AssertQuery(
185+
async,
186+
ss => ss.Set<NodaTimeTypes>().Where(t => t.LocalDate.At(new LocalTime(12, 30, 0)) == new LocalDateTime(2018, 4, 20, 12, 30, 0)));
187+
188+
AssertSql(
189+
"""
190+
SELECT n."Id", n."DateInterval", n."Duration", n."Instant", n."InstantRange", n."Interval", n."LocalDate", n."LocalDate2", n."LocalDateRange", n."LocalDateTime", n."LocalTime", n."Long", n."OffsetTime", n."Period", n."TimeZoneId", n."ZonedDateTime"
191+
FROM "NodaTimeTypes" AS n
192+
WHERE n."LocalDate" + TIME '12:30:00' = TIMESTAMP '2018-04-20T12:30:00'
193+
""");
194+
}
195+
196+
[ConditionalTheory]
197+
[MemberData(nameof(IsAsyncData))]
198+
public async Task AtMidnight(bool async)
199+
{
200+
await AssertQuery(
201+
async,
202+
ss => ss.Set<NodaTimeTypes>().Where(t => t.LocalDate.AtMidnight() == new LocalDateTime(2018, 4, 20, 0, 0, 0)));
203+
204+
AssertSql(
205+
"""
206+
SELECT n."Id", n."DateInterval", n."Duration", n."Instant", n."InstantRange", n."Interval", n."LocalDate", n."LocalDate2", n."LocalDateRange", n."LocalDateTime", n."LocalTime", n."Long", n."OffsetTime", n."Period", n."TimeZoneId", n."ZonedDateTime"
207+
FROM "NodaTimeTypes" AS n
208+
WHERE n."LocalDate" + TIME '00:00:00' = TIMESTAMP '2018-04-20T00:00:00'
209+
""");
210+
}
211+
180212
private NodaTimeContext CreateContext()
181213
=> Fixture.CreateContext();
182214

0 commit comments

Comments
 (0)