decimal INpgsqlSimpleTypeHandler <decimal> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => Read(buf, len, fieldDescription);
/// <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); }
/// <inheritdoc /> public override uint Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) => buf.ReadUInt32();
internal override object ReadAsObject(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) => ReadAsObject(buf, len, false, fieldDescription).Result;
internal override Type GetFieldType(FieldDescription?fieldDescription = null) => fieldDescription != null && fieldDescription.TypeModifier == 1 ? typeof(bool) : typeof(BitArray);
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); }
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;
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); } }
OffsetDateTime INpgsqlSimpleTypeHandler <OffsetDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => ((INpgsqlSimpleTypeHandler <ZonedDateTime>) this).Read(buf, len, fieldDescription).ToOffsetDateTime();
/// <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); }
DateTimeOffset INpgsqlSimpleTypeHandler <DateTimeOffset> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => _bclHandler.Read <DateTimeOffset>(buf, len, fieldDescription);
/// <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); }
long INpgsqlSimpleTypeHandler <long> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => buf.ReadByte();
/// <inheritdoc /> public override char Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) => (char)buf.ReadByte();
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)); }
internal override Type GetFieldType(FieldDescription?fieldDescription = null) => typeof(Array);
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())); }
internal override Type GetProviderSpecificFieldType(FieldDescription?fieldDescription = null) => typeof(Array);
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.");
/// <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}" ); }
/// <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)); }
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.");
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);
/// <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); }
internal override Type GetProviderSpecificFieldType(FieldDescription?fieldDescription = null) => GetFieldType(fieldDescription);
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.");
public override NpgsqlLogSequenceNumber Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { Debug.Assert(len == 8); return(new NpgsqlLogSequenceNumber(buf.ReadUInt64())); }
TimeSpan INpgsqlSimpleTypeHandler <TimeSpan> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => _bclHandler.Read <TimeSpan>(buf, len, fieldDescription);
/// <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);
short INpgsqlSimpleTypeHandler <short> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => checked ((short)Read(buf, len, fieldDescription));