-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
124 lines (99 loc) · 2.72 KB
/
util.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
package sqlt
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/twiglab/sqlt/internal/mapper"
)
var DefaultTxOptions *sql.TxOptions = NewTxOptions(sql.LevelDefault, false)
func NewTxOptions(level sql.IsolationLevel, readonly bool) *sql.TxOptions {
return &sql.TxOptions{Isolation: level, ReadOnly: readonly}
}
type Param = map[string]interface{}
var null = make(Param)
func dummy(p interface{}) interface{} {
if p == nil {
return null
}
return p
}
type sqltExecer interface {
PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error)
Maker
}
func query(ctx context.Context, ext sqltExecer, id string, data interface{}, h RowsExtractor) error {
param := dummy(data)
sql := MustSql(ext, id, param)
stmt, err := ext.PrepareNamedContext(ctx, sql)
if err != nil {
return err
}
defer stmt.Close()
rows, err := stmt.QueryxContext(ctx, param)
if err != nil {
return err
}
defer rows.Close()
return h.Extract(&Rs{Rows: rows})
}
func exec(ctx context.Context, ext sqltExecer, id string, data interface{}) (r sql.Result, e error) {
param := dummy(data)
sql := MustSql(ext, id, param)
stmt, err := ext.PrepareNamedContext(ctx, sql)
if err != nil {
return nil, err
}
defer stmt.Close()
r, e = stmt.ExecContext(ctx, param)
return
}
type TExecer interface {
TQuery(context.Context, string, interface{}, RowsExtractor) error
TExec(context.Context, string, interface{}) (sql.Result, error)
TExecRtn(context.Context, string, interface{}, RowsExtractor) error
}
func Query(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) (err error) {
err = execer.TQuery(ctx, id, param, h)
return
}
func MustQuery(execer TExecer, ctx context.Context, id string, param interface{}, h RowsExtractor) {
if err := Query(execer, ctx, id, param, h); err != nil {
panic(err)
}
}
func Exec(execer TExecer, ctx context.Context, id string, param interface{}) (r sql.Result, err error) {
r, err = execer.TExec(ctx, id, param)
return
}
func MustExec(execer TExecer, ctx context.Context, id string, param interface{}) (r sql.Result) {
var err error
if r, err = Exec(execer, ctx, id, param); err != nil {
panic(err)
}
return
}
type TxBegin interface {
TBegin(context.Context, *sql.TxOptions) (*Txop, error)
}
func Begin(b TxBegin, ctx context.Context, opt *sql.TxOptions) (*Txop, error) {
return b.TBegin(ctx, opt)
}
type TxEnd interface {
TCommit() error
TRollback() error
}
func Commit(t TxEnd) error {
return t.TCommit()
}
func Rollback(t TxEnd) error {
return t.TRollback()
}
type Rs struct {
*sqlx.Rows
}
func (r *Rs) MapScan(m map[string]interface{}) error {
return mapper.MapScan(r, m)
}
func (r *Rs) StructScan(dist interface{}) error {
return mapper.StructScan(r, dist)
}