/
db.go
333 lines (303 loc) · 7.6 KB
/
db.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Package dbutil provides helper functions for database queries.
//
// It has no external dependencies, just stdlib.
package dbutil
import (
"database/sql"
"encoding/csv"
"fmt"
"io"
"strings"
)
func strVal(in interface{}) string {
switch v := in.(type) {
case nil:
return ""
case string:
return v
case sql.RawBytes:
return string(v)
case []uint8:
return string(v)
default:
return fmt.Sprint(v)
}
}
func toString(in []interface{}) []string {
out := make([]string, len(in))
for i, col := range in {
out[i] = strVal(col)
}
return out
}
// Row returns one row of the results of a query
func Row(db *sql.DB, dest []interface{}, query string, args ...interface{}) error {
return db.QueryRow(query, args...).Scan(dest...)
}
// Get returns a row results
func Get(db *sql.DB, query string, args ...interface{}) ([]string, []interface{}, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, nil, err
}
if !rows.Next() {
return nil, nil, sql.ErrNoRows
}
columns, _ := Columns(rows)
buff := make([]interface{}, len(columns))
dest := make([]interface{}, len(columns))
for k := 0; k < len(dest); k++ {
dest[k] = &buff[k]
}
return columns, buff, rows.Scan(dest...)
}
// RowStrings returns the row results as a slice of strings
func RowStrings(db *sql.DB, query string, args ...interface{}) ([]string, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, sql.ErrNoRows
}
columns, _ := Columns(rows)
dest := make([]interface{}, len(columns))
// recycle columns slice as values buffer
for k := 0; k < len(dest); k++ {
dest[k] = &columns[k]
}
return columns, rows.Scan(dest...)
}
// Update runs an update query and returns the count of records updated, if any
func Update(db *sql.DB, query string, args ...interface{}) (int64, error) {
mods, _, err := Exec(db, query, args...)
return mods, err
}
// Insert runs an insert query and returns the id of the last records inserted
func Insert(db *sql.DB, query string, args ...interface{}) (int64, error) {
_, last, err := Exec(db, query, args...)
return last, err
}
// InsertMany inserts multiple records as a single transaction
func InsertMany(db *sql.DB, query string, args ...[]interface{}) error {
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(query)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
for _, arg := range args {
if _, err = stmt.Exec(arg...); err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
return nil
}
// Exec executes a query and returns the effected records info
func Exec(db *sql.DB, query string, args ...interface{}) (affected, last int64, err error) {
query = strings.TrimSpace(query)
if query == "" {
return 0, 0, fmt.Errorf("empty query")
}
r, err := db.Exec(query, args...)
if err != nil {
return 0, 0, err
}
affected, _ = r.RowsAffected()
last, _ = r.LastInsertId()
return affected, last, nil
}
// Columns returns a slice of column names that respects aliases in the query
func Columns(row *sql.Rows) ([]string, error) {
ctypes, err := row.ColumnTypes()
if err != nil {
return nil, err
}
columns := make([]string, len(ctypes))
for i, c := range ctypes {
columns[i] = c.Name()
}
return columns, nil
}
// StreamFunc is a function called for each row by Stream (columns, row number, values).
//
// Row numbering starts at 1.
type StreamFunc func([]string, int, []interface{}) error
// Streamer streams rows from query results to be formatted or processed
type Streamer struct {
db *sql.DB
query string
args []interface{}
}
// NewStreamer returns a Streamer
func NewStreamer(db *sql.DB, query string, args ...interface{}) *Streamer {
return &Streamer{db: db, query: query, args: args}
}
// Stream sends each row the query results to a StreamFunc
func (s *Streamer) Stream(fn StreamFunc) error {
return stream(s.db, fn, s.query, s.args...)
}
// stream streams the query results to function fn
func stream(db *sql.DB, fn StreamFunc, query string, args ...interface{}) error {
rows, err := db.Query(query, args...)
if err != nil {
return err
}
defer rows.Close()
columns, err := Columns(rows)
if err != nil {
return err
}
buffer := make([]interface{}, len(columns))
dest := make([]interface{}, len(columns))
for k := 0; k < len(buffer); k++ {
dest[k] = &buffer[k]
}
i := 1
for rows.Next() {
if err := rows.Scan(dest...); err != nil {
return err
}
if err := fn(columns, i, buffer); err != nil {
return err
}
i++
}
return err
}
// CSV streams the query results as a comma separated file
func (s *Streamer) CSV(w io.Writer, header bool) error {
cw := csv.NewWriter(w)
fn := func(columns []string, count int, buffer []interface{}) error {
if header && count == 1 {
cw.Write(columns)
}
return cw.Write(toString(buffer))
}
defer cw.Flush()
return s.Stream(fn)
}
// TSV streams the query results as a tab separated values
func (s *Streamer) TSV(w io.Writer, header bool) error {
fn := func(columns []string, count int, buffer []interface{}) error {
if header && count == 1 {
fmt.Fprintln(w, strings.Join(columns, "\t"))
}
for i, col := range buffer {
if i > 0 {
fmt.Fprint(w, "\t")
}
fmt.Fprint(w, strVal(col))
}
fmt.Fprintln(w)
return nil
}
return s.Stream(fn)
}
// JSON streams the query results as an array of JSON objects to the writer
func (s *Streamer) JSON(w io.Writer) error {
fn := func(columns []string, count int, buffer []interface{}) error {
if count > 1 {
fmt.Fprint(w, ",")
}
fmt.Fprint(w, "\n{")
for i, col := range columns {
if i > 0 {
fmt.Fprint(w, ", ")
}
fmt.Fprintf(w, `"%s": `, col)
switch v := buffer[i].(type) {
case nil:
fmt.Fprint(w, "null")
case bool, int, int32, int64, float32, float64:
fmt.Fprint(w, v)
case []byte:
fmt.Fprintf(w, `"%v"`, string(v))
default:
fmt.Fprintf(w, `"%v"`, v)
}
}
fmt.Fprint(w, "}")
return nil
}
fmt.Fprint(w, "[")
defer fmt.Fprintln(w, "\n]")
return s.Stream(fn)
}
// RowMap returns the results of a query as a map
func RowMap(db *sql.DB, query string, args ...interface{}) (map[string]interface{}, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, sql.ErrNoRows
}
columns, _ := Columns(rows)
buffer := make([]interface{}, len(columns))
dest := make([]interface{}, len(columns))
for k := 0; k < len(dest); k++ {
dest[k] = &buffer[k]
}
if err := rows.Scan(dest...); err != nil {
return nil, err
}
reply := make(map[string]interface{})
for i, col := range columns {
reply[col] = buffer[i]
}
return reply, nil
}
type inserted struct {
args []interface{}
err chan error
}
// Inserter enables inserting multiple records in a single transaction
type Inserter struct {
c chan inserted
err chan error
}
// Insert inserts a record in a transaction
func (i Inserter) Insert(args ...interface{}) error {
err := make(chan error)
i.c <- inserted{args, err}
return <-err
}
// Close closes the insert transaction
func (i Inserter) Close() error {
close(i.c)
return <-i.err
}
// NewInserter returns an Inserter that allows inserting multiple records as a single transaction
func NewInserter(db *sql.DB, query string) (*Inserter, error) {
tx, err := db.Begin()
if err != nil {
return nil, err
}
stmt, err := tx.Prepare(query)
if err != nil {
tx.Rollback()
return nil, err
}
c := make(chan inserted)
e := make(chan error)
inserter := Inserter{c, e}
go func() {
for i := range c {
if _, err = stmt.Exec(i.args...); err != nil {
tx.Rollback()
i.err <- err
return
}
i.err <- nil
}
e <- tx.Commit()
}()
return &inserter, nil
}