-
Notifications
You must be signed in to change notification settings - Fork 1
/
Base.go
161 lines (153 loc) · 4.42 KB
/
Base.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
package gpa
import (
"database/sql"
"github.com/cihub/seelog"
_ "github.com/go-sql-driver/mysql"
"reflect"
"strings"
)
const (
PrimaryId = "@Id"
AutoIncrement = "AutoIncrement"
)
var nilVf = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
type Gpa struct {
driver, dsn string
Conn *sql.DB
}
func getSqlByMethod(ft reflect.StructField) string {
name := ft.Name
if strings.Index(name, "FindBy") == 0 {
ty := ft.Type.String()
lk := strings.LastIndex(ty, "(")
if lk > 0 {
ty = ty[lk+1:]
}
d := strings.Index(ty, ".")
x := strings.Index(ty, ",")
if d > 0 && x > d {
tb := ty[d+1 : x]
rep := strings.Replace(name[6:], "And", "=? And ", -1)
return "select * from " + tb + " where " + rep + "=?"
} else {
seelog.Error("错误的命令格式:" + name + "," + ty)
}
}
return ""
}
func (dao *Gpa) setMethodImpl(di interface{}) {
toe := reflect.TypeOf(di).Elem()
voe := reflect.ValueOf(di).Elem()
implVO := reflect.ValueOf(&dao).Elem()
for i := 0; i < voe.NumField(); i++ {
ft := toe.Field(i)
runSql := strings.TrimSpace(string(ft.Tag))
if len(runSql) < 1 {
runSql = getSqlByMethod(ft)
if len(runSql) < 1 {
seelog.Error("方法定义错误,没有设置RunSql:", ft.Name, ";", ft.Type.String())
continue
}
}
if strings.Index(ft.Type.String(), "(") < 0 {
seelog.Error("不是函数" + ft.Name)
continue
}
rt := ft.Type.String()
rt = strings.TrimSpace(rt[strings.Index(rt, ")")+1:])
fv := voe.Field(i)
methodName := ""
if len(rt) < 2 {
seelog.Flush()
panic("没有定义返回值:" + ft.Name + ";" + runSql)
}
rts := strings.Replace(rt[1:len(rt)-1], "[]", "array_", -1)
rts = strings.Replace(rts, ", ", "_", -1)
rts = strings.Replace(rts, "_bool", "", -1)
rts = strings.Replace(rts, "_error", "", -1)
rts = strings.Replace(rts, " {}", "", -1)
rts = strings.Replace(rts, "[", "_", -1)
rts = strings.Replace(rts, "]", "_", -1)
rtArray := strings.Split(rts, "_")
obj := false
for _, r := range rtArray {
if len(r) > 1 {
if strings.Index(r, ".") > 0 {
methodName += "Object"
obj = true
} else {
methodName += strings.ToUpper(r[0:1]) + r[1:]
}
}
}
if obj {
if strings.Index(ft.Type.String(), "[]") < 0 {
if strings.Index(runSql, "select * ") == 0 {
runSql = fmtSelectAllSql(runSql, ft.Type.Out(0))
}
fv.Set(reflect.MakeFunc(fv.Type(), func(in []reflect.Value) []reflect.Value {
defer func() {
if err := recover(); err != nil {
seelog.Error("query object fail.methodName=", methodName,
";\n\trunSql=", runSql,
"\n\t", err)
seelog.Flush()
}
}()
v := vti(in)
return dao.QueryObject(runSql, ft.Type.Out(0), v...)
}))
} else {
if strings.Index(runSql, "select * ") == 0 {
runSql = fmtSelectAllSql(runSql, ft.Type.Out(0).Elem())
}
fv.Set(reflect.MakeFunc(fv.Type(), func(in []reflect.Value) []reflect.Value {
defer func() {
if err := recover(); err != nil {
seelog.Error("query object fail.methodName=", methodName,
";\n\trunSql=", runSql,
"\n\t", err)
seelog.Flush()
}
}()
v := vti(in)
return dao.QueryObjectArray(runSql, ft.Type.Out(0), v...)
}))
}
} else {
lowSql := strings.ToLower(runSql)[0:6]
if lowSql == "insert" {
methodName = "Insert"
} else if lowSql == "update" || lowSql == "delete" || lowSql == "replac" {
methodName = "Exec"
} else {
methodName = "Query" + methodName
methodName = strings.Replace(methodName, "QueryArray", "List", -1)
//methodName = strings.Replace(methodName, "Bool", "", -1)
}
implM, b := implVO.Type().MethodByName(methodName)
if b {
fv.Set(reflect.MakeFunc(fv.Type(), func(in []reflect.Value) []reflect.Value {
params := make([]reflect.Value, len(in)+1)
defer func() {
if err := recover(); err != nil {
seelog.Error(dao.Conn == nil, ";", dao.dsn, ";\n\tmethodName=", methodName,
";runSql=", runSql, ",", vti(in),
"\n\t", err)
seelog.Flush()
}
}()
params[0] = reflect.ValueOf(runSql)
for idx, pin := range in {
params[idx+1] = reflect.ValueOf(pin.Interface())
}
return implVO.Method(implM.Index).Call(params)
}))
} else {
msg := "方法没有现实:\nfunc (impl *Impl) " + methodName + "(rows sql.Rows, cols []string) " + rt + "{\n\t\n}\n;" + ft.Name + ";sql=" + runSql
seelog.Error(msg)
panic(msg)
}
}
}
}