-
Notifications
You must be signed in to change notification settings - Fork 0
/
dql.go
49 lines (46 loc) · 1007 Bytes
/
dql.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
package skt
import (
"fmt"
"time"
)
// Select data
func (db *DB) Select(query string, args ...interface{}) ([]map[string]string, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
var field []string
field = append(field, columns...)
values := make([]interface{}, len(columns))
scanArgs := make([]interface{}, len(columns))
var vmapSlice []map[string]string
for i := range values {
scanArgs[i] = &values[i]
}
for rows.Next() {
err = rows.Scan(scanArgs...)
if err != nil {
return nil, err
}
vmap := make(map[string]string, len(columns))
for i, v := range values {
switch x := v.(type) {
case nil:
vmap[field[i]] = ""
case []uint8:
vmap[field[i]] = string(x)
case time.Time:
vmap[field[i]] = x.Format(DATETIME)
default:
vmap[field[i]] = fmt.Sprint(x)
}
}
vmapSlice = append(vmapSlice, vmap)
}
return vmapSlice, nil
}