Exemplo n.º 1
0
 public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 {
     if (_integerFormat)
     {
         var value = buf.ReadInt64();
         if (_convertInfinityDateTime)
         {
             if (value == long.MaxValue)
             {
                 return(Instant.MaxValue);
             }
             if (value == long.MinValue)
             {
                 return(Instant.MinValue);
             }
         }
         return(Decode(value));
     }
     else
     {
         var value = buf.ReadDouble();
         if (_convertInfinityDateTime)
         {
             if (double.IsPositiveInfinity(value))
             {
                 return(Instant.MaxValue);
             }
             if (double.IsNegativeInfinity(value))
             {
                 return(Instant.MinValue);
             }
         }
         return(Decode(value));
     }
 }
Exemplo n.º 2
0
        // Binary Format: int64 expressing microseconds, int32 expressing timezone in seconds, negative

        #region Read

        public override DateTimeOffset Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
        {
            // Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted.
            var ticks  = _integerFormat ? buf.ReadInt64() * 10 : (long)(buf.ReadDouble() * TimeSpan.TicksPerSecond);
            var offset = new TimeSpan(0, 0, -buf.ReadInt32());

            return(new DateTimeOffset(ticks, offset));
        }
Exemplo n.º 3
0
 public override LocalTime Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 {
     if (_integerFormat)
     {
         // PostgreSQL time resolution == 1 microsecond == 10 ticks
         return(LocalTime.FromTicksSinceMidnight(buf.ReadInt64() * 10));
     }
     else
     {
         return(LocalTime.FromTicksSinceMidnight((long)(buf.ReadDouble() * NodaConstants.TicksPerSecond)));
     }
 }
Exemplo n.º 4
0
 public override TimeSpan Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 {
     if (_integerFormat)
     {
         // PostgreSQL time resolution == 1 microsecond == 10 ticks
         return(new TimeSpan(buf.ReadInt64() * 10));
     }
     else
     {
         return(TimeSpan.FromSeconds(buf.ReadDouble()));
     }
 }
Exemplo n.º 5
0
        /// <inheritdoc />
        public override async ValueTask <NpgsqlPath> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(5, async);

            var open = buf.ReadByte() switch
            {
                1 => false,
                0 => true,
                _ => throw new Exception("Error decoding binary geometric path: bad open byte")
            };

            var numPoints = buf.ReadInt32();
            var result    = new NpgsqlPath(numPoints, open);

            for (var i = 0; i < numPoints; i++)
            {
                await buf.Ensure(16, async);

                result.Add(new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble()));
            }
            return(result);
        }
Exemplo n.º 6
0
 protected override NpgsqlTimeSpan ReadPsv(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 {
     if (_integerFormat)
     {
         var ticks = buf.ReadInt64();
         var day   = buf.ReadInt32();
         var month = buf.ReadInt32();
         return(new NpgsqlTimeSpan(month, day, ticks * 10));
     }
     else
     {
         var seconds = buf.ReadDouble();
         var day     = buf.ReadInt32();
         var month   = buf.ReadInt32();
         return(new NpgsqlTimeSpan(month, day, (long)(seconds * TimeSpan.TicksPerSecond)));
     }
 }
Exemplo n.º 7
0
        public override Period Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
        {
            var microsecondsInDay = _integerFormat ? buf.ReadInt64() : (long)(buf.ReadDouble() * 1000000);
            var days        = buf.ReadInt32();
            var totalMonths = buf.ReadInt32();

            // Nodatime will normalize most things (i.e. nanoseconds to milliseconds, seconds...)
            // but it will not normalize months to years.
            var months = totalMonths % 12;
            var years  = totalMonths / 12;

            return(new PeriodBuilder
            {
                Nanoseconds = microsecondsInDay * 1000,
                Days = days,
                Months = months,
                Years = years
            }.Build().Normalize());
        }
Exemplo n.º 8
0
 public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 {
     if (_integerFormat)
     {
         var value = buf.ReadInt64();
         if (value == long.MaxValue || value == long.MinValue)
         {
             throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone"));
         }
         return(TimestampHandler.Decode(value));
     }
     else
     {
         var value = buf.ReadDouble();
         if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value))
         {
             throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone"));
         }
         return(TimestampHandler.Decode(value));
     }
 }
Exemplo n.º 9
0
 LocalDateTime INpgsqlSimpleTypeHandler <LocalDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription)
 {
     if (_integerFormat)
     {
         var value = buf.ReadInt64();
         if (value == long.MaxValue || value == long.MinValue)
         {
             throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported when reading LocalDateTime, read as Instant instead"));
         }
         return(Decode(value).InUtc().LocalDateTime);
     }
     else
     {
         var value = buf.ReadDouble();
         if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value))
         {
             throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported when reading LocalDateTime, read as Instant instead"));
         }
         return(Decode(value).InUtc().LocalDateTime);
     }
 }
        NpgsqlDateTime ReadTimeStampUsingFloatingPointFormat(NpgsqlReadBuffer buf)
        {
            var value = buf.ReadDouble();

            if (double.IsPositiveInfinity(value))
            {
                return(NpgsqlDateTime.Infinity);
            }
            if (double.IsNegativeInfinity(value))
            {
                return(NpgsqlDateTime.NegativeInfinity);
            }
            if (value >= 0d)
            {
                var date = (int)(value / 86400d);
                var time = value % 86400d;

                date += 730119;                  // 730119 = days since era (0001-01-01) for 2000-01-01
                time *= TimeSpan.TicksPerSecond; // seconds to Ticks

                return(new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan((long)time)));
            }
            else
            {
                value = -value;
                var date = (int)(value / 86400d);
                var time = value % 86400d;
                if (time != 0d)
                {
                    ++date;
                    time = 86400d - time;
                }

                date  = 730119 - date;           // 730119 = days since era (0001-01-01) for 2000-01-01
                time *= TimeSpan.TicksPerSecond; // seconds to Ticks

                return(new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan((long)time)));
            }
        }
Exemplo n.º 11
0
 /// <inheritdoc />
 public override NpgsqlPoint Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble());
Exemplo n.º 12
0
        async Task <PostgisGeometry> DoRead(NpgsqlReadBuffer buf, WkbIdentifier id, ByteOrder bo, bool async)
        {
            switch (id)
            {
            case WkbIdentifier.Point:
                await buf.Ensure(16, async);

                return(new PostgisPoint(buf.ReadDouble(bo), buf.ReadDouble(bo)));

            case WkbIdentifier.LineString:
            {
                await buf.Ensure(4, async);

                var points = new Coordinate2D[buf.ReadInt32(bo)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(16, async);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                }
                return(new PostgisLineString(points));
            }

            case WkbIdentifier.Polygon:
            {
                await buf.Ensure(4, async);

                var rings = new Coordinate2D[buf.ReadInt32(bo)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(4, async);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(bo)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                    }
                }
                return(new PostgisPolygon(rings));
            }

            case WkbIdentifier.MultiPoint:
            {
                await buf.Ensure(4, async);

                var points = new Coordinate2D[buf.ReadInt32(bo)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(21, async);

                    await buf.Skip(5, async);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                }
                return(new PostgisMultiPoint(points));
            }

            case WkbIdentifier.MultiLineString:
            {
                await buf.Ensure(4, async);

                var rings = new Coordinate2D[buf.ReadInt32(bo)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(9, async);

                    await buf.Skip(5, async);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(bo)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                    }
                }
                return(new PostgisMultiLineString(rings));
            }

            case WkbIdentifier.MultiPolygon:
            {
                await buf.Ensure(4, async);

                var pols = new Coordinate2D[buf.ReadInt32(bo)][][];

                for (var ipol = 0; ipol < pols.Length; ipol++)
                {
                    await buf.Ensure(9, async);

                    await buf.Skip(5, async);

                    pols[ipol] = new Coordinate2D[buf.ReadInt32(bo)][];
                    for (var irng = 0; irng < pols[ipol].Length; irng++)
                    {
                        await buf.Ensure(4, async);

                        pols[ipol][irng] = new Coordinate2D[buf.ReadInt32(bo)];
                        for (var ipts = 0; ipts < pols[ipol][irng].Length; ipts++)
                        {
                            await buf.Ensure(16, async);

                            pols[ipol][irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                        }
                    }
                }
                return(new PostgisMultiPolygon(pols));
            }

            case WkbIdentifier.GeometryCollection:
            {
                await buf.Ensure(4, async);

                var g = new PostgisGeometry[buf.ReadInt32(bo)];

                for (var i = 0; i < g.Length; i++)
                {
                    await buf.Ensure(5, async);

                    var elemBo = (ByteOrder)buf.ReadByte();
                    var elemId = (WkbIdentifier)(buf.ReadUInt32(bo) & 7);

                    g[i] = await DoRead(buf, elemId, elemBo, async);
                }
                return(new PostgisGeometryCollection(g));
            }

            default:
                throw new InvalidOperationException("Unknown Postgis identifier.");
            }
        }
Exemplo n.º 13
0
 /// <inheritdoc />
 public override double Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => buf.ReadDouble();
Exemplo n.º 14
0
 // Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted.
 public override OffsetTime Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
 => new OffsetTime(_integerFormat
         ? LocalTime.FromTicksSinceMidnight(buf.ReadInt64() * 10)
         : LocalTime.FromTicksSinceMidnight((long)(buf.ReadDouble() * NodaConstants.TicksPerSecond)),
                   Offset.FromSeconds(-buf.ReadInt32()));