/
kdb.go
184 lines (149 loc) · 4.17 KB
/
kdb.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
package kdb
import (
"database/sql"
"fmt"
// "github.com/kisielk/sqlstruct"
// "reflect"
"reflect"
"strings"
)
type Arger interface {
Args() []interface{}
}
func GetMaps(rows *sql.Rows) ([]map[string]interface{}, error) {
var maps []map[string]interface{}
cols, err := rows.Columns()
if err != nil {
return nil, err
}
values := make([]interface{}, len(cols))
scanArgs := make([]interface{}, len(cols))
for x := 0; x < len(cols); x++ {
scanArgs[x] = &values[x]
}
for rows.Next() {
m := make(map[string]interface{})
err := rows.Scan(scanArgs...)
if err == nil {
for n, c := range cols {
m[strings.ToLower(c)] = values[n]
}
maps = append(maps, m)
}
}
return maps, nil
}
func QueryMap(db *sql.DB, query string, args ...interface{}) (map[string]interface{}, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
ret, err := GetMaps(rows)
if len(ret) > 0 {
return ret[0], err
}
return nil, err
}
func QueryMaps(db *sql.DB, query string, args ...interface{}) ([]map[string]interface{}, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
ret, err := GetMaps(rows)
if len(ret) > 0 {
return ret, err
}
return nil, err
}
// Querys the database for one row, and sets the data in arger.
// Usage:
// var account Accounts // implements Arger
// found, err := QueryArger(db, `select * from Accounts where username = ?`, &account, "kevin")
func QueryArger(db *sql.DB, query string, arger Arger, args ...interface{}) (found bool, err error) {
rows, err := db.Query(query, args...)
if err != nil {
return false, err
}
if rows.Next() {
found = true
err := rows.Scan(arger.Args()...)
if err != nil {
return false, err
}
}
return found, nil
}
// Queries database for many rows, and sets the strcts interface as
// the returned rows.
// Usage:
// var strcts []Accounts // Each Accounts implements Arger
// err := helper.QueryArgers(db, `select * from Accounts`, &strcts, reflect.TypeOf(Accounts{}))
func QueryArgers(db *sql.DB, query string, strcts interface{}, typ reflect.Type, args ...interface{}) error {
rows, err := db.Query(query, args...)
if err != nil {
return err
}
vof := reflect.ValueOf(strcts)
for rows.Next() {
strct := reflect.New(typ)
err := rows.Scan(strct.Interface().(Arger).Args()...)
if err != nil {
return err
}
vof.Elem().Set(reflect.Append(vof.Elem(), reflect.Indirect(strct)))
}
return nil
}
// func QueryStruct(db *sql.DB, query string, strct interface{}, args ...interface{}) error {
// rows, err := db.Query(query, args...)
// if err != nil {
// return err
// }
// if rows.Next() {
// err = sqlstruct.Scan(strct, rows)
// if err != nil {
// return err
// }
// }
// return nil
// }
// // use it like:
// // var accounts []*Account
// // var account Account
// // QueryStructs(db, "select * from Accounts", &accounts, &account)
// func QueryStructs(db *sql.DB, query string, strcts interface{}, strct interface{}, args ...interface{}) error {
// rows, err := db.Query(query, args...)
// if err != nil {
// return err
// }
// vof := reflect.ValueOf(strcts)
// //into := reflect.New(vof.Type().Elem().Elem())
// for rows.Next() {
// err := sqlstruct.Scan(strct, rows)
// if err != nil {
// fmt.Println(err)
// return err
// }
// vof.Elem().Set(reflect.Append(vof.Elem(), reflect.ValueOf(strct)))
// }
// return nil
// }
func Fields(names []string) string {
for i, _ := range names {
names[i] = strings.Replace(names[i], `"`, `\"`, -1)
}
return `("` + strings.Join(names, `", "`) + `")`
}
func InsertMap(db *sql.DB, table string, m map[string]interface{}) (sql.Result, error) {
var fields []string
var values []interface{}
var variables []string
for key, value := range m {
fields = append(fields, key)
values = append(values, value)
variables = append(variables, "?")
}
fieldsSql := Fields(fields)
res, err := db.Exec(fmt.Sprintf("INSERT INTO %s %s VALUES (%s)", table, fieldsSql, strings.Join(variables, ",")), values...)
return res, err
}