|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using NodaTime; |
| 6 | +using Npgsql.BackendMessages; |
| 7 | +using Npgsql.Internal; |
| 8 | +using Npgsql.Internal.TypeHandlers; |
| 9 | +using Npgsql.Internal.TypeHandling; |
| 10 | +using Npgsql.PostgresTypes; |
| 11 | +using NpgsqlTypes; |
| 12 | + |
| 13 | +namespace Npgsql.NodaTime.Internal |
| 14 | +{ |
| 15 | + public partial class DateMultirangeHandler : MultirangeHandler<LocalDate>, |
| 16 | + INpgsqlTypeHandler<DateInterval[]>, INpgsqlTypeHandler<List<DateInterval>> |
| 17 | + { |
| 18 | + public DateMultirangeHandler(PostgresMultirangeType multirangePostgresType, NpgsqlTypeHandler subtypeHandler) |
| 19 | + : base(multirangePostgresType, new DateRangeHandler(multirangePostgresType.Subrange, subtypeHandler)) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public override Type GetFieldType(FieldDescription? fieldDescription = null) => typeof(DateInterval[]); |
| 24 | + public override Type GetProviderSpecificFieldType(FieldDescription? fieldDescription = null) => typeof(DateInterval[]); |
| 25 | + |
| 26 | + public override async ValueTask<object> ReadAsObject(NpgsqlReadBuffer buf, int len, bool async, |
| 27 | + FieldDescription? fieldDescription = null) |
| 28 | + => (await Read<DateInterval[]>(buf, len, async, fieldDescription))!; |
| 29 | + |
| 30 | + async ValueTask<DateInterval[]> INpgsqlTypeHandler<DateInterval[]>.Read( |
| 31 | + NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription) |
| 32 | + { |
| 33 | + await buf.Ensure(4, async); |
| 34 | + var numRanges = buf.ReadInt32(); |
| 35 | + var multirange = new DateInterval[numRanges]; |
| 36 | + |
| 37 | + for (var i = 0; i < multirange.Length; i++) |
| 38 | + { |
| 39 | + await buf.Ensure(4, async); |
| 40 | + var rangeLen = buf.ReadInt32(); |
| 41 | + var range = await RangeHandler.Read(buf, rangeLen, async, fieldDescription); |
| 42 | + multirange[i] = new(range.LowerBound, range.UpperBound - Period.FromDays(1)); |
| 43 | + } |
| 44 | + |
| 45 | + return multirange; |
| 46 | + } |
| 47 | + |
| 48 | + async ValueTask<List<DateInterval>> INpgsqlTypeHandler<List<DateInterval>>.Read( |
| 49 | + NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription) |
| 50 | + { |
| 51 | + await buf.Ensure(4, async); |
| 52 | + var numRanges = buf.ReadInt32(); |
| 53 | + var multirange = new List<DateInterval>(numRanges); |
| 54 | + |
| 55 | + for (var i = 0; i < numRanges; i++) |
| 56 | + { |
| 57 | + await buf.Ensure(4, async); |
| 58 | + var rangeLen = buf.ReadInt32(); |
| 59 | + var range = await RangeHandler.Read(buf, rangeLen, async, fieldDescription); |
| 60 | + multirange.Add(new(range.LowerBound, range.UpperBound - Period.FromDays(1))); |
| 61 | + } |
| 62 | + |
| 63 | + return multirange; |
| 64 | + } |
| 65 | + |
| 66 | + public int ValidateAndGetLength(DateInterval[] value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter) |
| 67 | + => ValidateAndGetLengthCore(value, ref lengthCache); |
| 68 | + |
| 69 | + public int ValidateAndGetLength(List<DateInterval> value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter) |
| 70 | + => ValidateAndGetLengthCore(value, ref lengthCache); |
| 71 | + |
| 72 | + int ValidateAndGetLengthCore(IList<DateInterval> value, ref NpgsqlLengthCache? lengthCache) |
| 73 | + { |
| 74 | + lengthCache ??= new NpgsqlLengthCache(1); |
| 75 | + if (lengthCache.IsPopulated) |
| 76 | + return lengthCache.Get(); |
| 77 | + |
| 78 | + var sum = 4 + 4 * value.Count; |
| 79 | + for (var i = 0; i < value.Count; i++) |
| 80 | + { |
| 81 | + var interval = value[i]; |
| 82 | + sum += RangeHandler.ValidateAndGetLength( |
| 83 | + new NpgsqlRange<LocalDate>(interval.Start, interval.End), |
| 84 | + ref lengthCache, |
| 85 | + parameter: null); |
| 86 | + } |
| 87 | + |
| 88 | + return lengthCache.Set(sum); |
| 89 | + } |
| 90 | + |
| 91 | + public async Task Write( |
| 92 | + DateInterval[] value, |
| 93 | + NpgsqlWriteBuffer buf, |
| 94 | + NpgsqlLengthCache? lengthCache, |
| 95 | + NpgsqlParameter? parameter, |
| 96 | + bool async, |
| 97 | + CancellationToken cancellationToken = default) |
| 98 | + { |
| 99 | + if (buf.WriteSpaceLeft < 4) |
| 100 | + await buf.Flush(async, cancellationToken); |
| 101 | + |
| 102 | + buf.WriteInt32(value.Length); |
| 103 | + |
| 104 | + for (var i = 0; i < value.Length; i++) |
| 105 | + { |
| 106 | + var interval = value[i]; |
| 107 | + await RangeHandler.WriteWithLength( |
| 108 | + new NpgsqlRange<LocalDate>(interval.Start, interval.End), buf, lengthCache, parameter: null, async, cancellationToken); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public async Task Write( |
| 113 | + List<DateInterval> value, |
| 114 | + NpgsqlWriteBuffer buf, |
| 115 | + NpgsqlLengthCache? lengthCache, |
| 116 | + NpgsqlParameter? parameter, |
| 117 | + bool async, |
| 118 | + CancellationToken cancellationToken = default) |
| 119 | + { |
| 120 | + if (buf.WriteSpaceLeft < 4) |
| 121 | + await buf.Flush(async, cancellationToken); |
| 122 | + |
| 123 | + buf.WriteInt32(value.Count); |
| 124 | + |
| 125 | + for (var i = 0; i < value.Count; i++) |
| 126 | + { |
| 127 | + var interval = value[i]; |
| 128 | + await RangeHandler.WriteWithLength( |
| 129 | + new NpgsqlRange<LocalDate>(interval.Start, interval.End), buf, lengthCache, parameter: null, async, cancellationToken); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments