-
Notifications
You must be signed in to change notification settings - Fork 14
/
executor.go
133 lines (106 loc) · 3.12 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
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
package gobatis
import (
"context"
"errors"
"fmt"
)
type executor struct {
gb *gbBase
}
func (exec *executor) updateContext(ctx context.Context, 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 conf.dbConf.ShowSQL {
LOG.Info("SQL:%s\nParamMappings:%s\nParams:%v", boundSql.sqlStr, boundSql.paramMappings, paramArr)
}
stmt, err := exec.gb.db.PrepareContext(ctx, boundSql.sqlStr)
if nil != err {
return 0, 0, err
}
defer stmt.Close()
result, err := stmt.ExecContext(ctx, 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) update(ms *mappedStmt, params map[string]interface{}) (lastInsertId int64, affected int64, err error) {
return exec.updateContext(context.Background(), ms, params)
}
func (exec *executor) queryContext(ctx context.Context, ms *mappedStmt, params map[string]interface{}, res interface{}, rowBound ...*rowBounds) (int64, error) {
boundSql, paramArr, err := paramProc(ms, params)
if nil != err {
return 0, err
}
sqlStr := boundSql.sqlStr
if conf.dbConf.ShowSQL {
LOG.Info("SQL:%s\nParamMappings:%s\nParams:%v", sqlStr, boundSql.paramMappings, paramArr)
}
count := int64(0)
if len(rowBound) > 0 {
countSql := "SELECT COUNT(1) cnt FROM (" + sqlStr + ") AS t"
rows, err := exec.gb.db.QueryContext(ctx, countSql, paramArr...)
if nil != err {
return 0, err
}
resProc, err := rowsToMaps(rows)
if nil != err {
return 0, err
}
c, err := valToInt64(resProc[0].(map[string]interface{})["cnt"])
if nil != err {
return 0, err
}
count = c
if count <= 0 {
return 0, nil
}
sqlStr += " LIMIT " + fmt.Sprint(rowBound[0].offset) + "," + fmt.Sprint(rowBound[0].limit)
}
rows, err := exec.gb.db.QueryContext(ctx, sqlStr, paramArr...)
if nil != err {
return 0, err
}
defer rows.Close()
resProc, ok := resSetProcMap[ms.resultType]
if !ok {
return 0, 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 0, err
}
return count, nil
}
func (exec *executor) query(ms *mappedStmt, params map[string]interface{}, res interface{}, rowBound ...*rowBounds) (int64, error) {
return exec.queryContext(context.Background(), ms, params, res, rowBound...)
}
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
}