-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
42 lines (35 loc) · 802 Bytes
/
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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"reflect"
)
func OpenDB(name string) (*sql.DB, error) {
var db *sql.DB
var err error
db, err = sql.Open("sqlite3", name)
if err != nil {
return nil, err
}
if _, err = db.Exec(CreateStmt); err != nil {
db.Close()
return nil, err
}
return db, nil
}
func getFields(i interface{}) map[string]interface{} {
v := reflect.Indirect(reflect.ValueOf(i))
q := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
valueField := v.Field(i)
typeField := v.Type().Field(i)
if valueField.Kind() == reflect.Ptr && !valueField.IsNil() {
if tag, ok := typeField.Tag.Lookup("db"); ok {
if tag != "" && tag != "id" {
q[tag] = reflect.Indirect(valueField).Interface()
}
}
}
}
return q
}