Beispiel #1
0
 decimal INpgsqlSimpleTypeHandler <decimal> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => Read(buf, len, fieldDescription);
Beispiel #2
0
        /// <inheritdoc />
        public override async ValueTask <BitArray> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numBits   = buf.ReadInt32();
            var result    = new BitArray(numBits);
            var bytesLeft = len - 4;  // Remove leading number of bits

            if (bytesLeft == 0)
            {
                return(result);
            }

            var bitNo = 0;

            while (true)
            {
                var iterationEndPos = bytesLeft > buf.ReadBytesLeft
                    ? bytesLeft - buf.ReadBytesLeft
                    : 1;

                for (; bytesLeft > iterationEndPos; bytesLeft--)
                {
                    // ReSharper disable ShiftExpressionRealShiftCountIsZero
                    var chunk = buf.ReadByte();
                    result[bitNo++] = (chunk & (1 << 7)) != 0;
                    result[bitNo++] = (chunk & (1 << 6)) != 0;
                    result[bitNo++] = (chunk & (1 << 5)) != 0;
                    result[bitNo++] = (chunk & (1 << 4)) != 0;
                    result[bitNo++] = (chunk & (1 << 3)) != 0;
                    result[bitNo++] = (chunk & (1 << 2)) != 0;
                    result[bitNo++] = (chunk & (1 << 1)) != 0;
                    result[bitNo++] = (chunk & (1 << 0)) != 0;
                }

                if (bytesLeft == 1)
                {
                    break;
                }

                Debug.Assert(buf.ReadBytesLeft == 0);
                await buf.Ensure(Math.Min(bytesLeft, buf.Size), async);
            }

            if (bitNo < result.Length)
            {
                var remainder = result.Length - bitNo;
                await buf.Ensure(1, async);

                var lastChunk = buf.ReadByte();
                for (var i = 7; i >= 8 - remainder; i--)
                {
                    result[bitNo++] = (lastChunk & (1 << i)) != 0;
                }
            }

            return(result);
        }
Beispiel #3
0
 /// <inheritdoc />
 public override uint Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => buf.ReadUInt32();
Beispiel #4
0
 internal override object ReadAsObject(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => ReadAsObject(buf, len, false, fieldDescription).Result;
Beispiel #5
0
 internal override Type GetFieldType(FieldDescription?fieldDescription = null)
 => fieldDescription != null && fieldDescription.TypeModifier == 1 ? typeof(bool) : typeof(BitArray);
Beispiel #6
0
        async ValueTask <bool> INpgsqlTypeHandler <bool> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
        {
            await buf.Ensure(5, async);

            var bitLen = buf.ReadInt32();

            if (bitLen != 1)
            {
                throw new InvalidCastException("Can't convert a BIT(N) type to bool, only BIT(1)");
            }
            var b = buf.ReadByte();

            return((b & 128) != 0);
        }
Beispiel #7
0
 internal override object ReadAsObject(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => fieldDescription?.TypeModifier == 1
         ? (object)Read <bool>(buf, len, false, fieldDescription).Result
         : Read <BitArray>(buf, len, false, fieldDescription).Result;
Beispiel #8
0
 ZonedDateTime INpgsqlSimpleTypeHandler <ZonedDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 {
     try
     {
         var value = buf.ReadInt64();
         if (value == long.MaxValue || value == long.MinValue)
         {
             throw new NotSupportedException("Infinity values not supported for timestamp with time zone");
         }
         return(TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone]));
     }
     catch (Exception e) when(
         string.Equals(buf.Connection.Timezone, "localtime", StringComparison.OrdinalIgnoreCase) &&
         (e is TimeZoneNotFoundException || e is DateTimeZoneNotFoundException))
     {
         throw new TimeZoneNotFoundException(
                   "The special PostgreSQL timezone 'localtime' is not supported when reading values of type 'timestamp with time zone'. " +
                   "Please specify a real timezone in 'postgresql.conf' on the server, or set the 'PGTZ' environment variable on the client.",
                   e);
     }
 }
Beispiel #9
0
 OffsetDateTime INpgsqlSimpleTypeHandler <OffsetDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => ((INpgsqlSimpleTypeHandler <ZonedDateTime>) this).Read(buf, len, fieldDescription).ToOffsetDateTime();
Beispiel #10
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);
        }
Beispiel #11
0
 DateTimeOffset INpgsqlSimpleTypeHandler <DateTimeOffset> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => _bclHandler.Read <DateTimeOffset>(buf, len, fieldDescription);
Beispiel #12
0
        /// <inheritdoc />
        public override async ValueTask <NpgsqlPolygon> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numPoints = buf.ReadInt32();
            var result    = new NpgsqlPolygon(numPoints);

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

                result.Add(new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble()));
            }
            return(result);
        }
Beispiel #13
0
 long INpgsqlSimpleTypeHandler <long> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => buf.ReadByte();
Beispiel #14
0
 /// <inheritdoc />
 public override char Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => (char)buf.ReadByte();
Beispiel #15
0
        Duration INpgsqlSimpleTypeHandler <Duration> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
        {
            var microsecondsInDay = buf.ReadInt64();
            var days        = buf.ReadInt32();
            var totalMonths = buf.ReadInt32();

            if (totalMonths != 0)
            {
                throw new NpgsqlException("Cannot read PostgreSQL interval with non-zero months to NodaTime Duration. Try reading as a NodaTime Period instead.");
            }

            return(Duration.FromDays(days) + Duration.FromNanoseconds(microsecondsInDay * 1000));
        }
Beispiel #16
0
 internal override Type GetFieldType(FieldDescription?fieldDescription = null) => typeof(Array);
Beispiel #17
0
        async ValueTask <BitVector32> INpgsqlTypeHandler <BitVector32> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
        {
            if (len > 4 + 4)
            {
                throw new InvalidCastException("Can't read PostgreSQL bitstring with more than 32 bits into BitVector32");
            }

            await buf.Ensure(4 + 4, async);

            var numBits = buf.ReadInt32();

            return(numBits == 0
                ? new BitVector32(0)
                : new BitVector32(buf.ReadInt32()));
        }
Beispiel #18
0
 internal override Type GetProviderSpecificFieldType(FieldDescription?fieldDescription = null) => typeof(Array);
Beispiel #19
0
 ValueTask <string> INpgsqlTypeHandler <string> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
 => throw new NotSupportedException("Only writing string to PostgreSQL bitstring is supported, no reading.");
Beispiel #20
0
        /// <inheritdoc />
        protected internal override async ValueTask <TRequestedArray> ReadCustom <TRequestedArray>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            if (ArrayTypeInfo <TRequestedArray> .IsArray)
            {
                return((TRequestedArray)(object)await ArrayTypeInfo <TRequestedArray> .ReadArrayFunc(this, buf, async));
            }

            if (ArrayTypeInfo <TRequestedArray> .IsList)
            {
                return(await ArrayTypeInfo <TRequestedArray> .ReadListFunc(this, buf, async));
            }

            throw new InvalidCastException(fieldDescription == null
                ? $"Can't cast database type to {typeof(TRequestedArray).Name}"
                : $"Can't cast database type {fieldDescription.Handler.PgDisplayName} to {typeof(TRequestedArray).Name}"
                                           );
        }
Beispiel #21
0
        /// <inheritdoc />
        protected internal override async ValueTask <TRequestedArray> Read <TRequestedArray>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            if (ArrayTypeInfo <TRequestedArray> .ElementType == typeof(BitArray))
            {
                if (ArrayTypeInfo <TRequestedArray> .IsArray)
                {
                    return((TRequestedArray)(object) await ReadArray <BitArray>(buf, async));
                }

                if (ArrayTypeInfo <TRequestedArray> .IsList)
                {
                    return((TRequestedArray)(object) await ReadList <BitArray>(buf, async));
                }
            }

            if (ArrayTypeInfo <TRequestedArray> .ElementType == typeof(bool))
            {
                if (ArrayTypeInfo <TRequestedArray> .IsArray)
                {
                    return((TRequestedArray)(object) await ReadArray <bool>(buf, async));
                }

                if (ArrayTypeInfo <TRequestedArray> .IsList)
                {
                    return((TRequestedArray)(object) await ReadList <bool>(buf, async));
                }
            }

            return(await base.Read <TRequestedArray>(buf, len, async, fieldDescription));
        }
Beispiel #22
0
 ValueTask <ReadOnlyMemory <byte> > INpgsqlTypeHandler <ReadOnlyMemory <byte> > .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescriptioncancellationToken)
 => throw new NotSupportedException("Only writing ReadOnlyMemory<byte> to PostgreSQL bytea is supported, no reading.");
Beispiel #23
0
 internal override async ValueTask <object> ReadAsObject(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
 => fieldDescription?.TypeModifier == 1
         ? await ReadArray <bool>(buf, async)
         : await ReadArray <BitArray>(buf, async);
Beispiel #24
0
        /// <inheritdoc />
        public override async ValueTask <byte[]> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            var bytes = new byte[len];
            var pos   = 0;

            while (true)
            {
                var toRead = Math.Min(len - pos, buf.ReadBytesLeft);
                buf.ReadBytes(bytes, pos, toRead);
                pos += toRead;
                if (pos == len)
                {
                    break;
                }
                await buf.ReadMore(async);
            }
            return(bytes);
        }
Beispiel #25
0
 internal override Type GetProviderSpecificFieldType(FieldDescription?fieldDescription = null)
 => GetFieldType(fieldDescription);
Beispiel #26
0
 ValueTask <ArraySegment <byte> > INpgsqlTypeHandler <ArraySegment <byte> > .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
 => throw new NotSupportedException("Only writing ArraySegment<byte> to PostgreSQL bytea is supported, no reading.");
Beispiel #27
0
 public override NpgsqlLogSequenceNumber Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 {
     Debug.Assert(len == 8);
     return(new NpgsqlLogSequenceNumber(buf.ReadUInt64()));
 }
Beispiel #28
0
 TimeSpan INpgsqlSimpleTypeHandler <TimeSpan> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => _bclHandler.Read <TimeSpan>(buf, len, fieldDescription);
Beispiel #29
0
 /// <summary>
 /// Reads a value of type <typeparamref name="TDefault"/> with the given length from the provided buffer,
 /// with the assumption that it is entirely present in the provided memory buffer and no I/O will be
 /// required.
 /// </summary>
 /// <param name="buf">The buffer from which to read.</param>
 /// <param name="len">The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.</param>
 /// <param name="fieldDescription">Additional PostgreSQL information about the type, such as the length in varchar(30).</param>
 /// <returns>The fully-read value.</returns>
 public abstract TDefault Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null);
Beispiel #30
0
 short INpgsqlSimpleTypeHandler <short> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => checked ((short)Read(buf, len, fieldDescription));