This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
183 lines (154 loc) · 4.11 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
package types
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"github.com/ericlagergren/decimal"
)
var (
_ driver.Valuer = Decimal{}
_ driver.Valuer = NullDecimal{}
_ sql.Scanner = &Decimal{}
_ sql.Scanner = &NullDecimal{}
)
// 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
}
// UnmarshalJSON allows marshalling JSON into a null pointer
func (d *Decimal) UnmarshalJSON(data []byte) error {
if d.Big == nil {
d.Big = new(decimal.Big)
}
return d.Big.UnmarshalJSON(data)
}
// 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
}
// UnmarshalJSON allows marshalling JSON into a null pointer
func (n *NullDecimal) UnmarshalJSON(data []byte) error {
if n.Big == nil {
n.Big = new(decimal.Big)
}
return n.Big.UnmarshalJSON(data)
}
// IsZero implements qmhelper.Nullable
func (n NullDecimal) IsZero() bool {
return n.Big == 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
}
switch t := val.(type) {
case float64:
if d == nil {
d = new(decimal.Big)
}
d.SetFloat64(t)
return d, nil
case int64:
return decimal.New(t, 0), nil
case string:
if d == nil {
d = new(decimal.Big)
}
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 d == nil {
d = new(decimal.Big)
}
if err := d.UnmarshalText(t); err != nil {
return nil, err
}
return d, nil
default:
return nil, fmt.Errorf("cannot scan decimal value: %#v", val)
}
}