-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsqlutil.go
More file actions
196 lines (166 loc) · 4.61 KB
/
sqlutil.go
File metadata and controls
196 lines (166 loc) · 4.61 KB
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
// Package sqlutil provides some helpers for SQL databases.
package sqlutil // import "github.com/teamwork/utils/v2/sqlutil"
import (
"database/sql/driver"
"fmt"
"html/template"
"strconv"
"strings"
"github.com/teamwork/utils/v2/sliceutil"
)
// IntList expands comma-separated values from a column to []int64, and stores
// []int64 as a comma-separated string.
//
// This is safe for NULL values, in which case it will scan in to IntList(nil).
type IntList []int64
// Value implements the SQL Value function to determine what to store in the DB.
func (l IntList) Value() (driver.Value, error) {
return sliceutil.Join(l), nil
}
// Scan converts the data returned from the DB into the struct.
func (l *IntList) Scan(v interface{}) error {
if v == nil {
return nil
}
ints := []int64{}
for _, i := range strings.Split(fmt.Sprintf("%s", v), ",") {
i = strings.TrimSpace(i)
if i == "" {
continue
}
in, err := strconv.ParseInt(i, 10, 64)
if err != nil {
return err
}
ints = append(ints, in)
}
*l = ints
return nil
}
// StringList expands comma-separated values from a column to []string, and
// stores []string as a comma-separated string.
//
// Note that this only works for simple strings (e.g. enums), we DO NOT escape
// commas in strings and you will run in to problems.
//
// This is safe for NULL values, in which case it will scan in to
// StringList(nil).
type StringList []string
// Value implements the SQL Value function to determine what to store in the DB.
func (l StringList) Value() (driver.Value, error) {
return strings.Join(sliceutil.Filter(l, sliceutil.FilterEmpty[string]), ","), nil
}
// Scan converts the data returned from the DB into the struct.
func (l *StringList) Scan(v interface{}) error {
if v == nil {
return nil
}
strs := []string{}
for _, s := range strings.Split(fmt.Sprintf("%s", v), ",") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
strs = append(strs, s)
}
*l = strs
return nil
}
// Bool add the capability to handle more column types than the usual sql
// driver. The following types are supported when reading the data:
//
// - int64 and float64 - 0 for false, true otherwise
// - bool
// - []byte and string - "1" or "true" for true, and "0" or "false" for false. Also handles the 1 bit cases.
// - nil - defaults to false
//
// It is also prepared to be encoded and decoded to a human readable format.
type Bool bool
// Scan converts the different types of representation of a boolean in the
// database into a bool type.
func (b *Bool) Scan(src interface{}) error {
if b == nil {
return fmt.Errorf("boolean not initialized")
}
switch v := src.(type) {
case int64:
*b = v != 0
case float64:
*b = v != 0
case bool:
*b = Bool(v)
case []byte, string:
var text string
if raw, ok := v.([]byte); ok {
// handle the bit(1) column type
if len(raw) == 1 {
switch raw[0] {
case 0x1:
*b = true
return nil
case 0x0:
*b = false
return nil
}
}
text = string(raw)
} else {
text = v.(string)
}
text = strings.TrimSpace(strings.ToLower(text))
switch text {
case "true", "1":
*b = true
case "false", "0":
*b = false
default:
return fmt.Errorf("invalid value '%s'", text)
}
case nil:
// nil will be considered false
*b = false
default:
return fmt.Errorf("unsupported format %T", src)
}
return nil
}
// Value converts a bool type into a number to persist it in the database.
func (b Bool) Value() (driver.Value, error) {
return bool(b), nil
}
// MarshalText converts the bool to a human readable representation, that is
// also compatible with the JSON format.
func (b Bool) MarshalText() ([]byte, error) {
if b {
return []byte("true"), nil
}
return []byte("false"), nil
}
// UnmarshalText parse different types of human representation of the boolean
// and convert it to the bool type. It is also compatible with the JSON format.
func (b *Bool) UnmarshalText(text []byte) error {
if b == nil {
return fmt.Errorf("boolean not initialized")
}
normalized := strings.TrimSpace(strings.ToLower(string(text)))
switch normalized {
case "true", "1", `"true"`:
*b = true
case "false", "0", `"false"`:
*b = false
default:
return fmt.Errorf("invalid value '%s'", normalized)
}
return nil
}
// HTML is a string which indicates that the string has been HTML-escaped.
type HTML template.HTML
// Value implements the SQL Value function to determine what to store in the DB.
func (h HTML) Value() (driver.Value, error) {
return string(h), nil
}
// Scan converts the data returned from the DB into the struct.
func (h *HTML) Scan(v interface{}) error {
*h = HTML(v.([]byte))
return nil
}