forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
145 lines (121 loc) · 3.34 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
package types
import (
"database/sql/driver"
"errors"
"fmt"
"github.com/ericlagergren/decimal"
)
// Decimal is a DECIMAL in sql. Its zero value is valid for use with both
// Value and Scan.
//
// Although decimal can represent NaN and Infinity it will return an error
// if an attempt to store these values in the database is made.
//
// Because it cannot be nil, when Big is nil Value() will return "0"
// It will error if an attempt to Scan() a "null" value into it.
type Decimal struct {
*decimal.Big
}
// NullDecimal is the same as Decimal, but allows the Big pointer to be nil.
// See docmentation for Decimal for more details.
//
// When going into a database, if Big is nil it's value will be "null".
type NullDecimal struct {
*decimal.Big
}
// NewDecimal creates a new decimal from a decimal
func NewDecimal(d *decimal.Big) Decimal {
return Decimal{Big: d}
}
// NewNullDecimal creates a new null decimal from a decimal
func NewNullDecimal(d *decimal.Big) NullDecimal {
return NullDecimal{Big: d}
}
// Value implements driver.Valuer.
func (d Decimal) Value() (driver.Value, error) {
return decimalValue(d.Big, false)
}
// Scan implements sql.Scanner.
func (d *Decimal) Scan(val interface{}) error {
newD, err := decimalScan(d.Big, val, false)
if err != nil {
return err
}
d.Big = newD
return nil
}
// Randomize implements sqlboiler's randomize interface
func (d *Decimal) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
d.Big = randomDecimal(nextInt, fieldType, false)
}
// Value implements driver.Valuer.
func (n NullDecimal) Value() (driver.Value, error) {
return decimalValue(n.Big, true)
}
// Scan implements sql.Scanner.
func (n *NullDecimal) Scan(val interface{}) error {
newD, err := decimalScan(n.Big, val, true)
if err != nil {
return err
}
n.Big = newD
return nil
}
// Randomize implements sqlboiler's randomize interface
func (n *NullDecimal) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
n.Big = randomDecimal(nextInt, fieldType, shouldBeNull)
}
func randomDecimal(nextInt func() int64, fieldType string, shouldBeNull bool) *decimal.Big {
if shouldBeNull {
return nil
}
randVal := fmt.Sprintf("%d.%d", nextInt()%10, nextInt()%10)
random, success := new(decimal.Big).SetString(randVal)
if !success {
panic("randVal could not be turned into a decimal")
}
return random
}
func decimalValue(d *decimal.Big, canNull bool) (driver.Value, error) {
if canNull && d == nil {
return nil, nil
}
if d.IsNaN(0) {
return nil, errors.New("refusing to allow NaN into database")
}
if d.IsInf(0) {
return nil, errors.New("refusing to allow infinity into database")
}
return d.String(), nil
}
func decimalScan(d *decimal.Big, val interface{}, canNull bool) (*decimal.Big, error) {
if val == nil {
if !canNull {
return nil, errors.New("null cannot be scanned into decimal")
}
return nil, nil
}
if d == nil {
d = new(decimal.Big)
}
switch t := val.(type) {
case float64:
d.SetFloat64(t)
return d, nil
case string:
if _, ok := d.SetString(t); !ok {
if err := d.Context.Err(); err != nil {
return nil, err
}
return nil, fmt.Errorf("invalid decimal syntax: %q", t)
}
return d, nil
case []byte:
if err := d.UnmarshalText(t); err != nil {
return nil, err
}
return d, nil
default:
return nil, fmt.Errorf("cannot scan decimal value: %#v", val)
}
}