-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
181 lines (160 loc) · 3.93 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package sqlz
import (
"context"
"database/sql"
"io"
)
func Release(conn *sql.Conn) error {
if conn == nil {
return nil
}
err := conn.Raw(func(driverConn interface{}) error {
if c, ok := driverConn.(io.Closer); ok {
return c.Close()
}
return nil
})
if err != nil {
return err
}
if err = conn.Close(); err != nil {
return err
}
return nil
}
func WithConns(ctx context.Context, pool ConnPool, n int, f func(conns ...*sql.Conn) error) error {
conns := make([]*sql.Conn, n)
ctx, cancel := context.WithCancel(ctx)
defer func() {
cancel()
for _, conn := range conns {
Release(conn)
}
}()
for i := 0; i < n; i++ {
conn, err := pool.Conn(ctx)
if err != nil {
return err
}
conns[i] = conn
}
return f(conns...)
}
func WithStmtCache(p PreparerContext) *StmtPool {
return &StmtPool{prepare: p, stmts: map[string]*sql.Stmt{}}
}
type StmtPool struct {
prepare PreparerContext
stmts map[string]*sql.Stmt
}
func (p *StmtPool) Reset() {
for q, s := range p.stmts {
if s != nil {
s.Close()
}
delete(p.stmts, q)
}
}
func (p *StmtPool) GetStmt(ctx context.Context, query string) (StmtContext, error) {
if s, ok := p.stmts[query]; ok {
return s, nil
}
s, err := p.prepare.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
p.stmts[query] = s
return s, nil
}
func (p *StmtPool) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
stmt, err := p.GetStmt(ctx, query)
if err != nil {
return nil, err
}
return stmt.ExecContext(ctx, args...)
}
func (p *StmtPool) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
stmt, err := p.GetStmt(ctx, query)
if err != nil {
return nil, err
}
return stmt.QueryContext(ctx, args...)
}
func Fetch(q Queryer, query string, args ...interface{}) (*ResultSet, error) {
rows, err := q.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return ReadFromRows(rows)
}
func FetchContext(ctx context.Context, q QueryerContext, query string, args ...interface{}) (*ResultSet, error) {
rows, err := q.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return ReadFromRows(rows)
}
func MustFetch(q Queryer, query string, args ...interface{}) *ResultSet {
rs, err := Fetch(q, query, args...)
if err != nil {
panic(err)
}
return rs
}
func MustFetchContext(ctx context.Context, q QueryerContext, query string, args ...interface{}) *ResultSet {
rs, err := FetchContext(ctx, q, query, args...)
if err != nil {
panic(err)
}
return rs
}
func MustQuery(q Queryer, query string, args ...interface{}) *sql.Rows {
rows, err := q.Query(query, args...)
if err != nil {
panic(err)
}
return rows
}
func MustQueryContext(ctx context.Context, q QueryerContext, query string, args ...interface{}) *sql.Rows {
rows, err := q.QueryContext(ctx, query, args...)
if err != nil {
panic(err)
}
return rows
}
func MustExec(e Execer, query string, args ...interface{}) sql.Result {
rows, err := e.Exec(query, args...)
if err != nil {
panic(err)
}
return rows
}
func MustExecContext(ctx context.Context, q ExecerContext, query string, args ...interface{}) sql.Result {
rows, err := q.ExecContext(ctx, query, args...)
if err != nil {
panic(err)
}
return rows
}
func AsyncFetch(q Queryer, query string, args ...interface{}) Future {
return AsyncCall(func() Values {
return Pack(Fetch(q, query, args...))
})
}
func AsyncFetchContext(ctx context.Context, q QueryerContext, query string, args ...interface{}) Future {
return AsyncCall(func() Values {
return Pack(FetchContext(ctx, q, query, args...))
})
}
func AsyncExec(e Execer, query string, args ...interface{}) Future {
return AsyncCall(func() Values {
return Pack(e.Exec(query, args...))
})
}
func AsyncExecContext(ctx context.Context, e ExecerContext, query string, args ...interface{}) Future {
return AsyncCall(func() Values {
return Pack(e.ExecContext(ctx, query, args...))
})
}