forked from leonliu315/sqlt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecer.go
85 lines (68 loc) · 1.63 KB
/
execer.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
package sqlt
import (
"context"
"github.com/it512/dsds"
"github.com/jmoiron/sqlx"
)
var (
defData = struct{}{}
)
func dataIsNilWithDef(data interface{}) interface{} {
if data == nil {
return defData
}
return data
}
func processSqlDescriberWithDbManager(m dsds.DbManager, assembler SqlAssembler, id string, data interface{}) (*sqlx.DB, SqlDescriber, error) {
desc, e := assembler.AssembleSql(id, data)
if e != nil {
return nil, nil, e
}
p, e := m.GetDb(desc)
if e != nil {
return nil, nil, e
}
return p, desc, nil
}
func processSqlDescriberWithTx(tx *sqlx.Tx, assembler SqlAssembler, id string, param interface{}) (*sqlx.Tx, SqlDescriber, error) {
desc, e := assembler.AssembleSql(id, param)
if e != nil {
return nil, nil, e
}
return tx, desc, nil
}
func processMultiRowsHander(rows *sqlx.Rows, mrh MultiRowsHandler) {
mrh.AddResultSet()
for rows.Next() {
mrh.HandleRow(rows)
}
for rows.NextResultSet() {
mrh.AddResultSet()
for rows.Next() {
mrh.HandleRow(rows)
}
}
}
func query(c context.Context, ext sqlx.ExtContext, descr SqlDescriber, data interface{}, mrh MultiRowsHandler) error {
sql, ctx, e := descr.GetSql(c)
if e != nil {
return e
}
rows, e := sqlx.NamedQueryContext(ctx, ext, sql, dataIsNilWithDef(data))
if e != nil {
return e
}
processMultiRowsHander(rows, mrh)
return rows.Close()
}
func exec(c context.Context, ext sqlx.ExtContext, descr SqlDescriber, data interface{}) (int64, error) {
sql, ctx, e := descr.GetSql(c)
if e != nil {
return -1, e
}
r, e := sqlx.NamedExecContext(ctx, ext, sql, dataIsNilWithDef(data))
if e != nil {
return -1, e
}
return r.RowsAffected()
}