-
Notifications
You must be signed in to change notification settings - Fork 2
/
orm.go
101 lines (96 loc) · 1.98 KB
/
orm.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
package xmysql
import (
"errors"
"reflect"
"strconv"
)
var (
MYSQL_TAG = "mysql"
)
func convertType(ft reflect.StructField, fv reflect.Value, value string) error {
var v interface{}
switch ft.Type.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32:
tmp_v, err := strconv.Atoi(value)
if err != nil {
return err
}
v = tmp_v
case reflect.Int64:
tmp_v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
v = tmp_v
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
tmp_v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
v = tmp_v
case reflect.Float32, reflect.Float64:
tmp_v, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
v = tmp_v
case reflect.String:
v = value
case reflect.Bool:
v = (value == "1")
}
fv.Set(reflect.ValueOf(v))
return nil
}
func mapToStruct(result map[string]string, output interface{}) error {
var (
err error
)
// output is ptr to the struct
ov := reflect.Indirect(reflect.ValueOf(output))
ot := ov.Type()
if ot.Kind() != reflect.Struct {
return errors.New("output is not struct type")
}
for i := 0; i < ot.NumField(); i++ {
ft := ot.Field(i)
fv := ov.Field(i)
if _, ok := ft.Tag.Lookup(MYSQL_TAG); !ok {
continue
}
tag_name := ft.Tag.Get(MYSQL_TAG)
if _, ok := result[tag_name]; !ok {
continue
}
v := result[tag_name]
err = convertType(ft, fv, v)
if err != nil {
return err
}
}
return nil
}
func Find(service string, output interface{}, sql string, args ...interface{}) error {
var (
result = make([]map[string]string, 0)
err error
)
GMysqlProxy.mux.RLock()
defer GMysqlProxy.mux.RUnlock()
if conn, ok := GMysqlProxy.mysqlConnPool[service]; ok {
result, err = conn.Select(sql, args...)
} else {
err = errors.New("not found db instance")
}
if err != nil {
return err
}
if len(result) == 0 {
return nil
}
err = mapToStruct(result[0], output)
if err != nil {
return err
}
return nil
}