internal ParameterDescriptionMessage Load(NpgsqlReadBuffer buf) { var numParams = buf.ReadUInt16(); TypeOIDs.Clear(); for (var i = 0; i < numParams; i++) TypeOIDs.Add(buf.ReadUInt32()); return this; }
public override NpgsqlTid Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { Debug.Assert(len == 6); var blockNumber = buf.ReadUInt32(); var offsetNumber = buf.ReadUInt16(); return(new NpgsqlTid(blockNumber, offsetNumber)); }
async ValueTask <BigInteger> INpgsqlTypeHandler <BigInteger> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription) { await buf.Ensure(4 *sizeof(short), async); var groups = (int)buf.ReadUInt16(); var weightLeft = (int)buf.ReadInt16(); var weightRight = weightLeft - groups + 1; var sign = buf.ReadUInt16(); buf.ReadInt16(); // dscale if (groups == 0) { return(sign switch { SignPositive or SignNegative => BigInteger.Zero, SignNan => throw new InvalidCastException("Numeric NaN not supported by BigInteger"), SignPinf => throw new InvalidCastException("Numeric Infinity not supported by BigInteger"), SignNinf => throw new InvalidCastException("Numeric -Infinity not supported by BigInteger"), _ => throw new InvalidCastException($"Numeric special value {sign} not supported") });
/// <inheritdoc /> public override decimal Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var result = new DecimalRaw(); var groups = buf.ReadInt16(); var weight = buf.ReadInt16() - groups + 1; var sign = buf.ReadUInt16(); if (sign == SignNan) { throw new InvalidCastException("Numeric NaN not supported by System.Decimal"); } if (sign == SignNegative) { DecimalRaw.Negate(ref result); } var scale = buf.ReadInt16(); if (scale < 0 is var exponential && exponential) { scale = (short)(-scale); }
/// <inheritdoc /> public override async ValueTask <decimal> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null) { await buf.Ensure(4 *sizeof(short), async); var result = new DecimalRaw(); var groups = buf.ReadInt16(); var weight = buf.ReadInt16() - groups + 1; var sign = buf.ReadUInt16(); if ((sign & SignSpecialMask) == SignSpecialMask) { throw sign switch { SignNan => new InvalidCastException("Numeric NaN not supported by System.Decimal"), SignPinf => new InvalidCastException("Numeric Infinity not supported by System.Decimal"), SignNinf => new InvalidCastException("Numeric -Infinity not supported by System.Decimal"), _ => new InvalidCastException($"Numeric special value {sign} not supported by System.Decimal") }; } if (sign == SignNegative) { DecimalRaw.Negate(ref result); } var scale = buf.ReadInt16(); if (scale < 0 is var exponential && exponential) { scale = (short)(-scale); } else { result.Scale = scale; } if (scale > MaxDecimalScale) { throw new OverflowException("Numeric value does not fit in a System.Decimal"); } var scaleDifference = exponential ? weight * MaxGroupScale : weight * MaxGroupScale + scale; if (groups > MaxGroupCount) { throw new OverflowException("Numeric value does not fit in a System.Decimal"); } await buf.Ensure(groups *sizeof(ushort), async); if (groups == MaxGroupCount) { while (groups-- > 1) { DecimalRaw.Multiply(ref result, MaxGroupSize); DecimalRaw.Add(ref result, buf.ReadUInt16()); } var group = buf.ReadUInt16(); var groupSize = DecimalRaw.Powers10[-scaleDifference]; if (group % groupSize != 0) { throw new OverflowException("Numeric value does not fit in a System.Decimal"); } DecimalRaw.Multiply(ref result, MaxGroupSize / groupSize); DecimalRaw.Add(ref result, group / groupSize); } else { while (groups-- > 0) { DecimalRaw.Multiply(ref result, MaxGroupSize); DecimalRaw.Add(ref result, buf.ReadUInt16()); } if (scaleDifference < 0) { DecimalRaw.Divide(ref result, DecimalRaw.Powers10[-scaleDifference]); } else { while (scaleDifference > 0) { var scaleChunk = Math.Min(DecimalRaw.MaxUInt32Scale, scaleDifference); DecimalRaw.Multiply(ref result, DecimalRaw.Powers10[scaleChunk]); scaleDifference -= scaleChunk; } } } return(result.Value); }
/// <inheritdoc /> public override decimal Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var result = new DecimalRaw(); var groups = buf.ReadInt16(); var weight = buf.ReadInt16() - groups + 1; var sign = buf.ReadUInt16(); if (sign == SignNan) { throw new InvalidCastException("Numeric NaN not supported by System.Decimal"); } if (sign == SignNegative) { DecimalRaw.Negate(ref result); } var scale = buf.ReadInt16(); if (scale > MaxDecimalScale) { throw new OverflowException("Numeric value does not fit in a System.Decimal"); } result.Scale = scale; var scaleDifference = scale + weight * MaxGroupScale; if (groups == MaxGroupCount) { while (groups-- > 1) { DecimalRaw.Multiply(ref result, MaxGroupSize); DecimalRaw.Add(ref result, buf.ReadUInt16()); } var group = buf.ReadUInt16(); var groupSize = DecimalRaw.Powers10[-scaleDifference]; if (group % groupSize != 0) { throw new OverflowException("Numeric value does not fit in a System.Decimal"); } DecimalRaw.Multiply(ref result, MaxGroupSize / groupSize); DecimalRaw.Add(ref result, group / groupSize); } else { while (groups-- > 0) { DecimalRaw.Multiply(ref result, MaxGroupSize); DecimalRaw.Add(ref result, buf.ReadUInt16()); } if (scaleDifference < 0) { DecimalRaw.Divide(ref result, DecimalRaw.Powers10[-scaleDifference]); } else { while (scaleDifference > 0) { var scaleChunk = Math.Min(DecimalRaw.MaxUInt32Scale, scaleDifference); DecimalRaw.Multiply(ref result, DecimalRaw.Powers10[scaleChunk]); scaleDifference -= scaleChunk; } } } return(result.Value); }
BigInteger INpgsqlSimpleTypeHandler <BigInteger> .Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription) { var result = new BigInteger(); var groups = buf.ReadInt16(); var weight = buf.ReadInt16() - groups + 1; var sign = buf.ReadUInt16(); if (sign == SignNan) { throw new NpgsqlSafeReadException(new InvalidCastException("Numeric NaN not supported by System.Numerics.BigInteger")); } var scale = buf.ReadInt16(); if (scale > 0) { throw new NpgsqlSafeReadException(new OverflowException("Numeric value does not fit in a System.Numerics.BigInteger")); } try { var scaleDifference = scale + weight * MaxGroupScale; if (groups == MaxGroupCount) { while (groups-- > 1) { result = result * MaxGroupSize; result = result + buf.ReadUInt16(); } var group = buf.ReadUInt16(); var groupSize = DecimalRaw.Powers10[-scaleDifference]; if (group % groupSize != 0) { throw new NpgsqlSafeReadException(new OverflowException("Numeric value does not fit in a System.Numerics.BigInteger")); } result = result * (MaxGroupSize / groupSize); result = result + (group / groupSize); } else { while (groups-- > 0) { result = result * MaxGroupSize; result = result + buf.ReadUInt16(); } if (scaleDifference < 0) { result = result / (BigInteger)Math.Pow(10, -scaleDifference); } else { while (scaleDifference > 0) { var scaleChunk = Math.Min(DecimalRaw.MaxUInt32Scale, scaleDifference); result = result * (BigInteger)Math.Pow(10, scaleChunk); scaleDifference -= scaleChunk; } } } } catch (OverflowException e) { throw new NpgsqlSafeReadException(e); } if (sign == SignNegative) { result = result * -1; } return(result); }