/
dev-npgsql
/
npgsql
Обзор
Документация
Войти
/
dev-npgsql
/
npgsql
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v6.0.13
src/Npgsql.NodaTime/Internal/TimestampTzHandler.cs
126 строк
6 KB
Shay Rojansky
Fix reading of NodaTime DateInterval with infinite upper bound (#4718)
14 окт 2022, 16:54
14 окт 2022, 16:54
dcd34dc
Код
Авторство
О чём код?
using System; using NodaTime; using Npgsql.BackendMessages; using Npgsql.Internal; using Npgsql.Internal.TypeHandling; using Npgsql.NodaTime.Properties; using Npgsql.PostgresTypes; using BclTimestampTzHandler = Npgsql.Internal.TypeHandlers.DateTimeHandlers.TimestampTzHandler; using static Npgsql.NodaTime.Internal.NodaTimeUtils; namespace Npgsql.NodaTime.Internal; sealed partial class TimestampTzHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<ZonedDateTime>, INpgsqlSimpleTypeHandler<OffsetDateTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>, INpgsqlSimpleTypeHandler<DateTime>, INpgsqlSimpleTypeHandler<long> { readonly BclTimestampTzHandler _bclHandler; public TimestampTzHandler(PostgresType postgresType) : base(postgresType) => _bclHandler = new BclTimestampTzHandler(postgresType); #region Read public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null) => ReadInstant(buf); internal static Instant ReadInstant(NpgsqlReadBuffer buf) => buf.ReadInt64() switch { long.MaxValue => DisableDateTimeInfinityConversions ? throw new InvalidCastException(NpgsqlNodaTimeStrings.CannotReadInfinityValue) : Instant.MaxValue, long.MinValue => DisableDateTimeInfinityConversions ? throw new InvalidCastException(NpgsqlNodaTimeStrings.CannotReadInfinityValue) : Instant.MinValue, var value => DecodeInstant(value) }; ZonedDateTime INpgsqlSimpleTypeHandler<ZonedDateTime>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription) => Read(buf, len, fieldDescription).InUtc(); OffsetDateTime INpgsqlSimpleTypeHandler<OffsetDateTime>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription) => Read(buf, len, fieldDescription).WithOffset(Offset.Zero); DateTimeOffset INpgsqlSimpleTypeHandler<DateTimeOffset>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription) => _bclHandler.Read<DateTimeOffset>(buf, len, fieldDescription); DateTime INpgsqlSimpleTypeHandler<DateTime>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription) => _bclHandler.Read<DateTime>(buf, len, fieldDescription); long INpgsqlSimpleTypeHandler<long>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription) => ((INpgsqlSimpleTypeHandler<long>)_bclHandler).Read(buf, len, fieldDescription); #endregion Read #region Write public override int ValidateAndGetLength(Instant value, NpgsqlParameter? parameter) => 8; int INpgsqlSimpleTypeHandler<ZonedDateTime>.ValidateAndGetLength(ZonedDateTime value, NpgsqlParameter? parameter) => value.Zone == DateTimeZone.Utc || LegacyTimestampBehavior ? 8 : throw new InvalidCastException( $"Cannot write ZonedDateTime with Zone={value.Zone} to PostgreSQL type 'timestamp with time zone', " + "only UTC is supported. " + "See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior."); public int ValidateAndGetLength(OffsetDateTime value, NpgsqlParameter? parameter) => value.Offset == Offset.Zero || LegacyTimestampBehavior ? 8 : throw new InvalidCastException( $"Cannot write OffsetDateTime with Offset={value.Offset} to PostgreSQL type 'timestamp with time zone', " + "only offset 0 (UTC) is supported. " + "See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior."); public override void Write(Instant value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => WriteInstant(value, buf); internal static void WriteInstant(Instant value, NpgsqlWriteBuffer buf) { if (!DisableDateTimeInfinityConversions) { if (value == Instant.MaxValue) { buf.WriteInt64(long.MaxValue); return; } if (value == Instant.MinValue) { buf.WriteInt64(long.MinValue); return; } } buf.WriteInt64(EncodeInstant(value)); } void INpgsqlSimpleTypeHandler<ZonedDateTime>.Write(ZonedDateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => Write(value.ToInstant(), buf, parameter); public void Write(OffsetDateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => Write(value.ToInstant(), buf, parameter); int INpgsqlSimpleTypeHandler<DateTimeOffset>.ValidateAndGetLength(DateTimeOffset value, NpgsqlParameter? parameter) => _bclHandler.ValidateAndGetLength(value, parameter); void INpgsqlSimpleTypeHandler<DateTimeOffset>.Write(DateTimeOffset value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => _bclHandler.Write(value, buf, parameter); int INpgsqlSimpleTypeHandler<DateTime>.ValidateAndGetLength(DateTime value, NpgsqlParameter? parameter) => ((INpgsqlSimpleTypeHandler<DateTime>)_bclHandler).ValidateAndGetLength(value, parameter); public int ValidateAndGetLength(long value, NpgsqlParameter? parameter) => ((INpgsqlSimpleTypeHandler<long>)_bclHandler).ValidateAndGetLength(value, parameter); void INpgsqlSimpleTypeHandler<DateTime>.Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => _bclHandler.Write(value, buf, parameter); void INpgsqlSimpleTypeHandler<long>.Write(long value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => ((INpgsqlSimpleTypeHandler<long>)_bclHandler).Write(value, buf, parameter); #endregion Write }