|
3 | 3 |
|
4 | 4 | namespace Npgsql.Internal.Converters; |
5 | 5 |
|
6 | | -sealed class MoneyConverter<T> : PgBufferedConverter<T> |
7 | | -#if NET7_0_OR_GREATER |
8 | | - where T : INumberBase<T> |
9 | | -#endif |
| 6 | +sealed class MoneyConverter<T> : PgBufferedConverter<T> where T : INumberBase<T> |
10 | 7 | { |
11 | 8 | public override bool CanConvert(DataFormat format, out BufferRequirements bufferRequirements) |
12 | 9 | { |
13 | 10 | bufferRequirements = BufferRequirements.CreateFixedSize(sizeof(long)); |
14 | 11 | return format is DataFormat.Binary; |
15 | 12 | } |
| 13 | + |
16 | 14 | protected override T ReadCore(PgReader reader) => ConvertTo(new PgMoney(reader.ReadInt64())); |
17 | 15 | protected override void WriteCore(PgWriter writer, T value) => writer.WriteInt64(ConvertFrom(value).GetValue()); |
18 | 16 |
|
19 | | - static PgMoney ConvertFrom(T value) |
20 | | - { |
21 | | -#if !NET7_0_OR_GREATER |
22 | | - if (typeof(short) == typeof(T)) |
23 | | - return new PgMoney((decimal)(short)(object)value!); |
24 | | - if (typeof(int) == typeof(T)) |
25 | | - return new PgMoney((decimal)(int)(object)value!); |
26 | | - if (typeof(long) == typeof(T)) |
27 | | - return new PgMoney((decimal)(long)(object)value!); |
28 | | - |
29 | | - if (typeof(byte) == typeof(T)) |
30 | | - return new PgMoney((decimal)(byte)(object)value!); |
31 | | - if (typeof(sbyte) == typeof(T)) |
32 | | - return new PgMoney((decimal)(sbyte)(object)value!); |
33 | | - |
34 | | - if (typeof(float) == typeof(T)) |
35 | | - return new PgMoney((decimal)(float)(object)value!); |
36 | | - if (typeof(double) == typeof(T)) |
37 | | - return new PgMoney((decimal)(double)(object)value!); |
38 | | - if (typeof(decimal) == typeof(T)) |
39 | | - return new PgMoney((decimal)(object)value!); |
40 | | - |
41 | | - throw new NotSupportedException(); |
42 | | -#else |
43 | | - return new PgMoney(decimal.CreateChecked(value)); |
44 | | -#endif |
45 | | - } |
46 | | - |
47 | | - static T ConvertTo(PgMoney money) |
48 | | - { |
49 | | -#if !NET7_0_OR_GREATER |
50 | | - if (typeof(short) == typeof(T)) |
51 | | - return (T)(object)(short)money.ToDecimal(); |
52 | | - if (typeof(int) == typeof(T)) |
53 | | - return (T)(object)(int)money.ToDecimal(); |
54 | | - if (typeof(long) == typeof(T)) |
55 | | - return (T)(object)(long)money.ToDecimal(); |
56 | | - |
57 | | - if (typeof(byte) == typeof(T)) |
58 | | - return (T)(object)(byte)money.ToDecimal(); |
59 | | - if (typeof(sbyte) == typeof(T)) |
60 | | - return (T)(object)(sbyte)money.ToDecimal(); |
61 | | - |
62 | | - if (typeof(float) == typeof(T)) |
63 | | - return (T)(object)(float)money.ToDecimal(); |
64 | | - if (typeof(double) == typeof(T)) |
65 | | - return (T)(object)(double)money.ToDecimal(); |
66 | | - if (typeof(decimal) == typeof(T)) |
67 | | - return (T)(object)money.ToDecimal(); |
68 | | - |
69 | | - throw new NotSupportedException(); |
70 | | -#else |
71 | | - return T.CreateChecked(money.ToDecimal()); |
72 | | -#endif |
73 | | - } |
| 17 | + static PgMoney ConvertFrom(T value) => new(decimal.CreateChecked(value)); |
| 18 | + static T ConvertTo(PgMoney money) => T.CreateChecked(money.ToDecimal()); |
74 | 19 | } |
0 commit comments