public override void Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
 {
     if (ConvertInfinityDateTime)
     {
         if (value == DateTime.MaxValue)
         {
             if (_integerFormat)
             {
                 buf.WriteInt64(long.MaxValue);
             }
             else
             {
                 buf.WriteDouble(double.PositiveInfinity);
             }
             return;
         }
         if (value == DateTime.MinValue)
         {
             if (_integerFormat)
             {
                 buf.WriteInt64(long.MinValue);
             }
             else
             {
                 buf.WriteDouble(double.NegativeInfinity);
             }
             return;
         }
     }
     Write(new NpgsqlDateTime(value), buf, parameter);
 }
示例#2
0
        /// <inheritdoc />
        public override void Write(NpgsqlDateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            if (value.IsInfinity)
            {
                buf.WriteInt64(long.MaxValue);
                return;
            }

            if (value.IsNegativeInfinity)
            {
                buf.WriteInt64(long.MinValue);
                return;
            }

            var uSecsTime = value.Time.Ticks / 10;

            if (value >= new NpgsqlDateTime(2000, 1, 1, 0, 0, 0))
            {
                var uSecsDate = (value.Date.DaysSinceEra - 730119) * 86400000000L;
                buf.WriteInt64(uSecsDate + uSecsTime);
            }
            else
            {
                var uSecsDate = (730119 - value.Date.DaysSinceEra) * 86400000000L;
                buf.WriteInt64(-(uSecsDate - uSecsTime));
            }
        }
        void WriteTimeStampUsingIntegerFormat(NpgsqlDateTime value, NpgsqlWriteBuffer buf)
        {
            if (value.IsInfinity)
            {
                buf.WriteInt64(long.MaxValue);
                return;
            }

            if (value.IsNegativeInfinity)
            {
                buf.WriteInt64(long.MinValue);
                return;
            }

            var uSecsTime = value.Time.Ticks / 10;

            if (value >= new NpgsqlDateTime(2000, 1, 1, 0, 0, 0))
            {
                var uSecsDate = (value.Date.DaysSinceEra - 730119) * 86400000000L;
                buf.WriteInt64(uSecsDate + uSecsTime);
            }
            else
            {
                var uSecsDate = (730119 - value.Date.DaysSinceEra) * 86400000000L;
                buf.WriteInt64(-(uSecsDate - uSecsTime));
            }
        }
示例#4
0
        public void Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            if (_integerFormat)
            {
                buf.WriteInt64(value.TimeOfDay.Ticks / 10);
            }
            else
            {
                buf.WriteDouble((double)value.TimeOfDay.Ticks / TimeSpan.TicksPerSecond);
            }

            switch (value.Kind)
            {
            case DateTimeKind.Utc:
                buf.WriteInt32(0);
                break;

            case DateTimeKind.Unspecified:
            // Treat as local...
            case DateTimeKind.Local:
                buf.WriteInt32(-(int)(TimeZoneInfo.Local.BaseUtcOffset.Ticks / TimeSpan.TicksPerSecond));
                break;

            default:
                throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {value.Kind} of enum {nameof(DateTimeKind)}. Please file a bug.");
            }
        }
        public override void Write(decimal value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            if (value == 0M)
            {
                buf.WriteInt64(0);
                return;
            }

            var negative = value < 0;

            if (negative)
            {
                value = -value;
            }

            int numGroups, weight, fractionDigits;

            GetNumericHeader(value, out numGroups, out weight, out fractionDigits);

            buf.WriteInt16(numGroups);
            buf.WriteInt16(weight);
            buf.WriteInt16(negative ? 0x4000 : 0x0000);
            buf.WriteInt16(fractionDigits);
            for (int i = 0, pos = weight + 7; i < numGroups; i++, pos--)
            {
                buf.WriteInt16((ushort)(value / Decimals[pos]));
                value %= Decimals[pos];
            }
        }
示例#6
0
        public override void Write(decimal value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            var raw             = Unsafe.As <decimal, DecimalRaw>(ref value);
            var scaleDifference = MoneyScale - raw.Scale;

            if (scaleDifference > 0)
            {
                DecimalRaw.Multiply(ref raw, DecimalRaw.Powers10[scaleDifference]);
            }
            else
            {
                while (scaleDifference < 0)
                {
                    var scaleChunk = Math.Min(DecimalRaw.MaxUInt32Scale, -scaleDifference);
                    DecimalRaw.Divide(ref raw, DecimalRaw.Powers10[scaleChunk]);
                    scaleDifference -= scaleChunk;
                }
            }

            var result = (long)raw.Mid << 32 | (long)raw.Low;

            if (raw.Negative)
            {
                result = -result;
            }
            buf.WriteInt64(result);
        }
示例#7
0
    /// <inheritdoc />
    public override void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
    {
        var ticksInDay = value.Ticks - TimeSpan.TicksPerDay * value.Days;

        buf.WriteInt64(ticksInDay / 10);
        buf.WriteInt32(value.Days);
        buf.WriteInt32(0);
    }
        public override void Write(Instant value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            if (_convertInfinityDateTime)
            {
                if (value == Instant.MaxValue)
                {
                    buf.WriteInt64(long.MaxValue);
                    return;
                }

                if (value == Instant.MinValue)
                {
                    buf.WriteInt64(long.MinValue);
                    return;
                }
            }
            TimestampHandler.WriteInteger(value, buf);
        }
示例#9
0
        /// <inheritdoc />
        public override void Write(Guid value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            var raw = new GuidRaw(value);

            buf.WriteInt32(raw.Data1);
            buf.WriteInt16(raw.Data2);
            buf.WriteInt16(raw.Data3);
            buf.WriteInt64(raw.Data4, BitConverter.IsLittleEndian);
        }
示例#10
0
    internal static void WriteInstant(Instant value, NpgsqlWriteBuffer buf)
    {
        if (!DisableDateTimeInfinityConversions)
        {
            if (value == Instant.MaxValue)
            {
                buf.WriteInt64(long.MaxValue);
                return;
            }

            if (value == Instant.MinValue)
            {
                buf.WriteInt64(long.MinValue);
                return;
            }
        }

        buf.WriteInt64(EncodeInstant(value));
    }
示例#11
0
        public override void Write(Period value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            var microsecondsInDay =
                (((value.Hours * NodaConstants.MinutesPerHour + value.Minutes) * NodaConstants.SecondsPerMinute + value.Seconds) * NodaConstants.MillisecondsPerSecond + value.Milliseconds) * 1000 +
                value.Nanoseconds / 1000; // Take the microseconds, discard the nanosecond remainder

            buf.WriteInt64(microsecondsInDay);
            buf.WriteInt32(value.Weeks * 7 + value.Days);    // days
            buf.WriteInt32(value.Years * 12 + value.Months); // months
        }
示例#12
0
 public override void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
 {
     if (_integerFormat)
     {
         buf.WriteInt64(value.Ticks / 10);
     }
     else
     {
         buf.WriteDouble(value.TotalSeconds);
     }
 }
        internal static void WriteLocalDateTime(LocalDateTime value, NpgsqlWriteBuffer buf)
        {
            // TODO: Switch to use LocalDateTime.MinMaxValue when available (#4061)
            if (!DisableDateTimeInfinityConversions)
            {
                if (value == LocalDate.MaxIsoValue + LocalTime.MaxValue)
                {
                    buf.WriteInt64(long.MaxValue);
                    return;
                }

                if (value == LocalDate.MinIsoValue + LocalTime.MinValue)
                {
                    buf.WriteInt64(long.MinValue);
                    return;
                }
            }

            buf.WriteInt64(EncodeInstant(value.InUtc().ToInstant()));
        }
示例#14
0
 public override void Write(LocalTime value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
 {
     if (_integerFormat)
     {
         buf.WriteInt64(value.TickOfDay / 10);
     }
     else
     {
         buf.WriteDouble((double)value.TickOfDay / NodaConstants.TicksPerSecond);
     }
 }
示例#15
0
        /// <inheritdoc />
        public override void Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            if (ConvertInfinityDateTime)
            {
                if (value == DateTime.MaxValue)
                {
                    buf.WriteInt64(long.MaxValue);
                    return;
                }

                if (value == DateTime.MinValue)
                {
                    buf.WriteInt64(long.MinValue);
                    return;
                }
            }

            var postgresTimestamp = ToPostgresTimestamp(value);

            buf.WriteInt64(postgresTimestamp);
        }
示例#16
0
        public void Write(Duration value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
        {
            const int microsecondsPerSecond = 1_000_000;

            var microsecondsInDay =
                (((value.Hours * NodaConstants.MinutesPerHour + value.Minutes) * NodaConstants.SecondsPerMinute + value.Seconds) *
                 microsecondsPerSecond + value.SubsecondNanoseconds / 1000);    // Take the microseconds, discard the nanosecond remainder

            buf.WriteInt64(microsecondsInDay);
            buf.WriteInt32(value.Days); // days
            buf.WriteInt32(0);          // months
        }
示例#17
0
        // We need to write the number of microseconds from 2000-01-01T00:00:00.
        internal static void WriteInteger(Instant instant, NpgsqlWriteBuffer buf)
        {
            var since2000 = instant - Instant2000;

            // The nanoseconds may overflow, so fallback to BigInteger where necessary.
            var microseconds =
                since2000 >= Minus292Years &&
                since2000 <= Plus292Years
                    ? since2000.ToInt64Nanoseconds() / 1000
                    : (long)(since2000.ToBigIntegerNanoseconds() / 1000);

            buf.WriteInt64(microseconds);
        }
示例#18
0
        public override void Write(DateTimeOffset value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            if (_integerFormat)
            {
                buf.WriteInt64(value.TimeOfDay.Ticks / 10);
            }
            else
            {
                buf.WriteDouble((double)value.TimeOfDay.Ticks / TimeSpan.TicksPerSecond);
            }

            buf.WriteInt32(-(int)(value.Offset.Ticks / TimeSpan.TicksPerSecond));
        }
示例#19
0
        public void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            if (_integerFormat)
            {
                buf.WriteInt64(value.Ticks / 10);
            }
            else
            {
                buf.WriteDouble((double)value.Ticks / TimeSpan.TicksPerSecond);
            }

            buf.WriteInt32(-(int)(TimeZoneInfo.Local.BaseUtcOffset.Ticks / TimeSpan.TicksPerSecond));
        }
示例#20
0
        public override void Write(NpgsqlTimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            if (_integerFormat)
            {
                buf.WriteInt64(value.Ticks / 10); // TODO: round?
            }
            else
            {
                buf.WriteDouble(value.TotalSeconds - (value.Days * 86400) - (value.Months * NpgsqlTimeSpan.DaysPerMonth * 86400));
            }

            buf.WriteInt32(value.Days);
            buf.WriteInt32(value.Months);
        }
示例#21
0
 public override void Write(Instant value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
 {
     if (_integerFormat)
     {
         if (_convertInfinityDateTime)
         {
             if (value == Instant.MaxValue)
             {
                 buf.WriteInt64(long.MaxValue);
                 return;
             }
             if (value == Instant.MinValue)
             {
                 buf.WriteInt64(long.MinValue);
                 return;
             }
         }
         WriteInteger(value, buf);
     }
     else
     {
         if (_convertInfinityDateTime)
         {
             if (value == Instant.MaxValue)
             {
                 buf.WriteDouble(double.PositiveInfinity);
                 return;
             }
             if (value == Instant.MinValue)
             {
                 buf.WriteDouble(double.NegativeInfinity);
                 return;
             }
         }
         WriteDouble(value, buf);
     }
 }
示例#22
0
        public override void Write(decimal value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
        {
            var raw             = Unsafe.As <decimal, DecimalRaw>(ref value);
            var scaleDifference = MoneyScale - raw.Scale;

            if (scaleDifference > 0)
            {
                DecimalRaw.Multiply(ref raw, DecimalRaw.Powers10[scaleDifference]);
            }
            else
            {
                value = Math.Round(value, MoneyScale, MidpointRounding.AwayFromZero);
                raw   = Unsafe.As <decimal, DecimalRaw>(ref value);
            }

            var result = (long)raw.Mid << 32 | (long)raw.Low;

            if (raw.Negative)
            {
                result = -result;
            }
            buf.WriteInt64(result);
        }
 /// <inheritdoc />
 public override void Write(NpgsqlTimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
 {
     buf.WriteInt64(value.Ticks / 10); // TODO: round?
     buf.WriteInt32(value.Days);
     buf.WriteInt32(value.Months);
 }
示例#24
0
 /// <inheritdoc />
 public void Write(float value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter) => buf.WriteInt64((long)value);
示例#25
0
 /// <inheritdoc />
 public void Write(byte value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter) => buf.WriteInt64(value);
示例#26
0
 /// <inheritdoc />
 public override void Write(long value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter) => buf.WriteInt64(value);
示例#27
0
 public override void Write(decimal value, NpgsqlWriteBuffer buf, NpgsqlParameter parameter)
 => buf.WriteInt64((long)(Math.Round(value, 2, MidpointRounding.AwayFromZero) * 100m));
示例#28
0
 /// <inheritdoc />
 public void Write(double value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter) => buf.WriteInt64(checked ((long)value));
示例#29
0
 /// <inheritdoc />
 public override void Write(DateTimeOffset value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
 {
     buf.WriteInt64(value.TimeOfDay.Ticks / 10);
     buf.WriteInt32(-(int)(value.Offset.Ticks / TimeSpan.TicksPerSecond));
 }
示例#30
0
 /// <inheritdoc />
 public void Write(TimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
 {
     buf.WriteInt64(value.Ticks / 10);
     buf.WriteInt32(-(int)(TimeZoneInfo.Local.BaseUtcOffset.Ticks / TimeSpan.TicksPerSecond));
 }