Esempio n. 1
0
        public override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter)
        {
            var num = (decimal)(parameter?.ConvertedValue ?? value);

            if (num == 0M)
            {
                buf.WriteInt64(0);
            }
            else
            {
                bool negative = num < 0;
                if (negative)
                {
                    num = -num;
                }

                int numGroups, weight, fractionDigits;
                GetNumericHeader(num, 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)(num / Decimals[pos]));
                    num %= Decimals[pos];
                }
            }
        }
Esempio n. 2
0
 public override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter)
 {
     if (parameter?.ConvertedValue != null)
     {
         value = parameter.ConvertedValue;
     }
     buf.WriteInt64((long)value);
 }
Esempio n. 3
0
        public override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter)
        {
            if (value is DateTimeOffset)
            {
                var dto = (DateTimeOffset)value;
                buf.WriteInt64(dto.TimeOfDay.Ticks / 10);
                buf.WriteInt32(-(int)(dto.Offset.Ticks / TimeSpan.TicksPerSecond));
                return;
            }

            if (value is DateTime)
            {
                var dt = (DateTime)value;

                buf.WriteInt64(dt.TimeOfDay.Ticks / 10);

                switch (dt.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 PGUtil.ThrowIfReached();
                }

                return;
            }

            if (value is TimeSpan)
            {
                var ts = (TimeSpan)value;
                buf.WriteInt64(ts.Ticks / 10);
                buf.WriteInt32(-(int)(TimeZoneInfo.Local.BaseUtcOffset.Ticks / TimeSpan.TicksPerSecond));
                return;
            }

            throw PGUtil.ThrowIfReached();
        }
Esempio n. 4
0
        protected override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter = null)
        {
            if (value is DateTimeOffset)
            {
                var dto = (DateTimeOffset)value;
                buf.WriteInt64(dto.TimeOfDay.Ticks / 10);
                buf.WriteInt32(-(int)(dto.Offset.Ticks / TimeSpan.TicksPerSecond));
                return;
            }

            if (value is DateTime)
            {
                var dt = (DateTime)value;

                buf.WriteInt64(dt.TimeOfDay.Ticks / 10);

                switch (dt.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 {dt.Kind} of enum {nameof(DateTimeKind)}. Please file a bug.");
                }

                return;
            }

            if (value is TimeSpan)
            {
                var ts = (TimeSpan)value;
                buf.WriteInt64(ts.Ticks / 10);
                buf.WriteInt32(-(int)(TimeZoneInfo.Local.BaseUtcOffset.Ticks / TimeSpan.TicksPerSecond));
                return;
            }

            throw new InvalidOperationException("Internal Npgsql bug, please report.");
        }
Esempio n. 5
0
        protected override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter = null)
        {
            if (parameter?.ConvertedValue != null)
            {
                value = parameter.ConvertedValue;
            }

            buf.WriteInt64(((TimeSpan)value).Ticks / 10);
        }
Esempio n. 6
0
        protected override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter = null)
        {
            if (parameter?.ConvertedValue != null)
            {
                value = parameter.ConvertedValue;
            }

            var interval = value as TimeSpan? ?? (NpgsqlTimeSpan)value;

            buf.WriteInt64(interval.Ticks / 10); // TODO: round?
            buf.WriteInt32(interval.Days);
            buf.WriteInt32(interval.Months);
        }
Esempio n. 7
0
        public override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter)
        {
            if (parameter != null && parameter.ConvertedValue != null)
            {
                value = parameter.ConvertedValue;
            }

            var interval = (value is TimeSpan)
                ? ((NpgsqlTimeSpan)(TimeSpan)value)
                : ((NpgsqlTimeSpan)value);

            buf.WriteInt64(interval.Ticks / 10); // TODO: round?
            buf.WriteInt32(interval.Days);
            buf.WriteInt32(interval.Months);
        }
Esempio n. 8
0
        protected override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter = null)
        {
            if (parameter?.ConvertedValue != null)
            {
                value = parameter.ConvertedValue;
            }

            NpgsqlDateTime ts;

            if (value is NpgsqlDateTime)
            {
                ts = (NpgsqlDateTime)value;
                if (!ts.IsFinite)
                {
                    if (ts.IsInfinity)
                    {
                        buf.WriteInt64(long.MaxValue);
                        return;
                    }

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

                    throw new InvalidOperationException("Internal Npgsql bug, please report.");
                }
            }
            else if (value is DateTime)
            {
                var dt = (DateTime)value;
                if (ConvertInfinityDateTime)
                {
                    if (dt == DateTime.MaxValue)
                    {
                        buf.WriteInt64(long.MaxValue);
                        return;
                    }
                    if (dt == DateTime.MinValue)
                    {
                        buf.WriteInt64(long.MinValue);
                        return;
                    }
                }
                ts = new NpgsqlDateTime(dt);
            }
            else if (value is DateTimeOffset)
            {
                ts = new NpgsqlDateTime(((DateTimeOffset)value).DateTime);
            }
            else
            {
                throw new InvalidOperationException("Internal Npgsql bug, please report.");
            }

            var uSecsTime = ts.Time.Ticks / 10;

            if (ts >= new NpgsqlDateTime(2000, 1, 1, 0, 0, 0))
            {
                var uSecsDate = (ts.Date.DaysSinceEra - 730119) * 86400000000L;
                buf.WriteInt64(uSecsDate + uSecsTime);
            }
            else
            {
                var uSecsDate = (730119 - ts.Date.DaysSinceEra) * 86400000000L;
                buf.WriteInt64(-(uSecsDate - uSecsTime));
            }
        }
Esempio n. 9
0
        protected override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter = null)
        {
            var v = (decimal)(parameter?.ConvertedValue ?? value);

            buf.WriteInt64((long)(Math.Round(v, 2, MidpointRounding.AwayFromZero) * 100m));
        }
Esempio n. 10
0
        public override void Write(object value, WriteBuffer buf, NpgsqlParameter parameter)
        {
            if (parameter != null && parameter.ConvertedValue != null)
            {
                value = parameter.ConvertedValue;
            }

            NpgsqlDateTime ts;

            if (value is NpgsqlDateTime)
            {
                ts = (NpgsqlDateTime)value;
                if (!ts.IsFinite)
                {
                    if (ts.IsInfinity)
                    {
                        buf.WriteInt64(Int64.MaxValue);
                        return;
                    }

                    if (ts.IsNegativeInfinity)
                    {
                        buf.WriteInt64(Int64.MinValue);
                        return;
                    }

                    throw PGUtil.ThrowIfReached();
                }
            }
            else if (value is DateTime)
            {
                var dt = (DateTime)value;
                if (_convertInfinityDateTime)
                {
                    if (dt == DateTime.MaxValue)
                    {
                        buf.WriteInt64(Int64.MaxValue);
                        return;
                    }
                    else if (dt == DateTime.MinValue)
                    {
                        buf.WriteInt64(Int64.MinValue);
                        return;
                    }
                }
                ts = new NpgsqlDateTime(dt);
            }
            else if (value is DateTimeOffset)
            {
                ts = new NpgsqlDateTime(((DateTimeOffset)value).DateTime);
            }
            else
            {
                throw PGUtil.ThrowIfReached();
            }

            var uSecsTime = ts.Time.Ticks / 10;

            if (ts >= new NpgsqlDateTime(2000, 1, 1, 0, 0, 0))
            {
                var uSecsDate = (ts.Date.DaysSinceEra - 730119) * 86400000000L;
                buf.WriteInt64(uSecsDate + uSecsTime);
            }
            else
            {
                var uSecsDate = (730119 - ts.Date.DaysSinceEra) * 86400000000L;
                buf.WriteInt64(-(uSecsDate - uSecsTime));
            }
        }