-
Notifications
You must be signed in to change notification settings - Fork 5
/
decimal.go
296 lines (221 loc) · 7.56 KB
/
decimal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package decimal
import (
"fmt"
"math"
"reflect"
"github.com/jackc/pgx/v5/pgtype"
"github.com/shopspring/decimal"
)
type Decimal decimal.Decimal
func (d *Decimal) ScanNumeric(v pgtype.Numeric) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into *decimal.Decimal")
}
if v.NaN {
return fmt.Errorf("cannot scan NaN into *decimal.Decimal")
}
if v.InfinityModifier != pgtype.Finite {
return fmt.Errorf("cannot scan %v into *decimal.Decimal", v.InfinityModifier)
}
*d = Decimal(decimal.NewFromBigInt(v.Int, v.Exp))
return nil
}
func (d Decimal) NumericValue() (pgtype.Numeric, error) {
dd := decimal.Decimal(d)
return pgtype.Numeric{Int: dd.Coefficient(), Exp: dd.Exponent(), Valid: true}, nil
}
func (d *Decimal) ScanFloat64(v pgtype.Float8) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into *decimal.Decimal")
}
if math.IsNaN(v.Float64) {
return fmt.Errorf("cannot scan NaN into *decimal.Decimal")
}
if math.IsInf(v.Float64, 0) {
return fmt.Errorf("cannot scan %v into *decimal.Decimal", v.Float64)
}
*d = Decimal(decimal.NewFromFloat(v.Float64))
return nil
}
func (d Decimal) Float64Value() (pgtype.Float8, error) {
dd := decimal.Decimal(d)
return pgtype.Float8{Float64: dd.InexactFloat64(), Valid: true}, nil
}
func (d *Decimal) ScanInt64(v pgtype.Int8) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into *decimal.Decimal")
}
*d = Decimal(decimal.NewFromInt(v.Int64))
return nil
}
func (d Decimal) Int64Value() (pgtype.Int8, error) {
dd := decimal.Decimal(d)
if !dd.IsInteger() {
return pgtype.Int8{}, fmt.Errorf("cannot convert %v to int64", dd)
}
bi := dd.BigInt()
if !bi.IsInt64() {
return pgtype.Int8{}, fmt.Errorf("cannot convert %v to int64", dd)
}
return pgtype.Int8{Int64: bi.Int64(), Valid: true}, nil
}
type NullDecimal decimal.NullDecimal
func (d *NullDecimal) ScanNumeric(v pgtype.Numeric) error {
if !v.Valid {
*d = NullDecimal{}
return nil
}
if v.NaN {
return fmt.Errorf("cannot scan NaN into *decimal.NullDecimal")
}
if v.InfinityModifier != pgtype.Finite {
return fmt.Errorf("cannot scan %v into *decimal.NullDecimal", v.InfinityModifier)
}
*d = NullDecimal(decimal.NullDecimal{Decimal: decimal.NewFromBigInt(v.Int, v.Exp), Valid: true})
return nil
}
func (d NullDecimal) NumericValue() (pgtype.Numeric, error) {
if !d.Valid {
return pgtype.Numeric{}, nil
}
dd := decimal.Decimal(d.Decimal)
return pgtype.Numeric{Int: dd.Coefficient(), Exp: dd.Exponent(), Valid: true}, nil
}
func (d *NullDecimal) ScanFloat64(v pgtype.Float8) error {
if !v.Valid {
*d = NullDecimal{}
return nil
}
if math.IsNaN(v.Float64) {
return fmt.Errorf("cannot scan NaN into *decimal.NullDecimal")
}
if math.IsInf(v.Float64, 0) {
return fmt.Errorf("cannot scan %v into *decimal.NullDecimal", v.Float64)
}
*d = NullDecimal(decimal.NullDecimal{Decimal: decimal.NewFromFloat(v.Float64), Valid: true})
return nil
}
func (d NullDecimal) Float64Value() (pgtype.Float8, error) {
if !d.Valid {
return pgtype.Float8{}, nil
}
dd := decimal.NullDecimal(d)
return pgtype.Float8{Float64: dd.Decimal.InexactFloat64(), Valid: true}, nil
}
func (d *NullDecimal) ScanInt64(v pgtype.Int8) error {
if !v.Valid {
*d = NullDecimal{}
return nil
}
*d = NullDecimal(decimal.NullDecimal{Decimal: decimal.NewFromInt(v.Int64), Valid: true})
return nil
}
func (d NullDecimal) Int64Value() (pgtype.Int8, error) {
if !d.Valid {
return pgtype.Int8{}, nil
}
dd := decimal.NullDecimal(d).Decimal
if !dd.IsInteger() {
return pgtype.Int8{}, fmt.Errorf("cannot convert %v to int64", dd)
}
bi := dd.BigInt()
if !bi.IsInt64() {
return pgtype.Int8{}, fmt.Errorf("cannot convert %v to int64", dd)
}
return pgtype.Int8{Int64: bi.Int64(), Valid: true}, nil
}
func TryWrapNumericEncodePlan(value interface{}) (plan pgtype.WrappedEncodePlanNextSetter, nextValue interface{}, ok bool) {
switch value := value.(type) {
case decimal.Decimal:
return &wrapDecimalEncodePlan{}, Decimal(value), true
case decimal.NullDecimal:
return &wrapNullDecimalEncodePlan{}, NullDecimal(value), true
}
return nil, nil, false
}
type wrapDecimalEncodePlan struct {
next pgtype.EncodePlan
}
func (plan *wrapDecimalEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
func (plan *wrapDecimalEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(Decimal(value.(decimal.Decimal)), buf)
}
type wrapNullDecimalEncodePlan struct {
next pgtype.EncodePlan
}
func (plan *wrapNullDecimalEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
func (plan *wrapNullDecimalEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(NullDecimal(value.(decimal.NullDecimal)), buf)
}
func TryWrapNumericScanPlan(target interface{}) (plan pgtype.WrappedScanPlanNextSetter, nextDst interface{}, ok bool) {
switch target := target.(type) {
case *decimal.Decimal:
return &wrapDecimalScanPlan{}, (*Decimal)(target), true
case *decimal.NullDecimal:
return &wrapNullDecimalScanPlan{}, (*NullDecimal)(target), true
}
return nil, nil, false
}
type wrapDecimalScanPlan struct {
next pgtype.ScanPlan
}
func (plan *wrapDecimalScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
func (plan *wrapDecimalScanPlan) Scan(src []byte, dst interface{}) error {
return plan.next.Scan(src, (*Decimal)(dst.(*decimal.Decimal)))
}
type wrapNullDecimalScanPlan struct {
next pgtype.ScanPlan
}
func (plan *wrapNullDecimalScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
func (plan *wrapNullDecimalScanPlan) Scan(src []byte, dst interface{}) error {
return plan.next.Scan(src, (*NullDecimal)(dst.(*decimal.NullDecimal)))
}
type NumericCodec struct {
pgtype.NumericCodec
}
func (NumericCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (interface{}, error) {
if src == nil {
return nil, nil
}
var target decimal.Decimal
scanPlan := tm.PlanScan(oid, format, &target)
if scanPlan == nil {
return nil, fmt.Errorf("PlanScan did not find a plan")
}
err := scanPlan.Scan(src, &target)
if err != nil {
return nil, err
}
return target, nil
}
// Register registers the shopspring/decimal integration with a pgtype.ConnInfo.
func Register(m *pgtype.Map) {
m.TryWrapEncodePlanFuncs = append([]pgtype.TryWrapEncodePlanFunc{TryWrapNumericEncodePlan}, m.TryWrapEncodePlanFuncs...)
m.TryWrapScanPlanFuncs = append([]pgtype.TryWrapScanPlanFunc{TryWrapNumericScanPlan}, m.TryWrapScanPlanFuncs...)
m.RegisterType(&pgtype.Type{
Name: "numeric",
OID: pgtype.NumericOID,
Codec: NumericCodec{},
})
registerDefaultPgTypeVariants := func(name, arrayName string, value interface{}) {
// T
m.RegisterDefaultPgType(value, name)
// *T
valueType := reflect.TypeOf(value)
m.RegisterDefaultPgType(reflect.New(valueType).Interface(), name)
// []T
sliceType := reflect.SliceOf(valueType)
m.RegisterDefaultPgType(reflect.MakeSlice(sliceType, 0, 0).Interface(), arrayName)
// *[]T
m.RegisterDefaultPgType(reflect.New(sliceType).Interface(), arrayName)
// []*T
sliceOfPointerType := reflect.SliceOf(reflect.TypeOf(reflect.New(valueType).Interface()))
m.RegisterDefaultPgType(reflect.MakeSlice(sliceOfPointerType, 0, 0).Interface(), arrayName)
// *[]*T
m.RegisterDefaultPgType(reflect.New(sliceOfPointerType).Interface(), arrayName)
}
registerDefaultPgTypeVariants("numeric", "_numeric", decimal.Decimal{})
registerDefaultPgTypeVariants("numeric", "_numeric", decimal.NullDecimal{})
registerDefaultPgTypeVariants("numeric", "_numeric", Decimal{})
registerDefaultPgTypeVariants("numeric", "_numeric", NullDecimal{})
}