forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutor.go
97 lines (79 loc) · 2.03 KB
/
executor.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
package gobatis
import (
"errors"
"log"
)
type executor struct {
*runner
}
func (exec *executor) update(ms *mappedStmt, params map[string]interface{}) (lastInsertId int64, affected int64, err error) {
boundSql, paramArr, err := paramProc(ms, params)
if nil != err {
return 0, 0, err
}
if debug {
log.Println("SQL:", boundSql.sqlStr)
log.Println("ParamMappings:", boundSql.paramMappings)
log.Println("Params:", paramArr)
}
stmt, err := exec.executor.Prepare(boundSql.sqlStr)
if nil != err {
return 0, 0, err
}
result, err := stmt.Exec(paramArr...)
if nil != err {
return 0, 0, err
}
lastInsertId, err = result.LastInsertId()
if nil != err {
return 0, 0, err
}
affected, err = result.RowsAffected()
if nil != err {
return 0, 0, err
}
return lastInsertId, affected, nil
}
func (exec *executor) query(ms *mappedStmt, params map[string]interface{}, res interface{}) error {
boundSql, paramArr, err := paramProc(ms, params)
if nil != err {
return err
}
if debug {
log.Println("SQL:", boundSql.sqlStr)
log.Println("ParamMappings:", boundSql.paramMappings)
log.Println("Params:", paramArr)
}
rows, err := exec.executor.Query(boundSql.sqlStr, paramArr...)
if nil != err {
return err
}
resProc, ok := resSetProcMap[ms.resultType]
if !ok {
return errors.New("No exec result type proc, result type:" + string(ms.resultType))
}
// func(rows *sql.Rows, res interface{}) error
err = resProc(rows, res)
if nil != err {
return err
}
return nil
}
func paramProc(ms *mappedStmt, params map[string]interface{}) (boundSql *boundSql, paramArr []interface{}, err error) {
boundSql = ms.sqlSource.getBoundSql(params)
if nil == boundSql {
err = errors.New("get boundSql err: boundSql == nil")
return
}
paramArr = make([]interface{}, 0)
for i := 0; i < len(boundSql.paramMappings); i++ {
paramName := boundSql.paramMappings[i]
param, ok := boundSql.extParams[paramName]
if !ok {
err = errors.New("param:" + paramName + " not exists")
return
}
paramArr = append(paramArr, param)
}
return
}