forked from zyguan/sqlz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
194 lines (166 loc) · 4.63 KB
/
sql.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
182
183
184
185
186
187
188
189
190
191
192
193
194
package sqlz
import (
"context"
"database/sql"
"github.com/zyguan/sqlz/resultset"
. "github.com/zyguan/just"
)
func Connect(ctx context.Context, db *sql.DB) (*sql.Conn, error) {
for {
conn, err := db.Conn(ctx)
if err != nil {
return nil, err
}
if err := conn.PingContext(ctx); err == nil {
return conn, nil
}
conn.Close()
}
}
type DB struct {
*sql.DB
ctx context.Context
}
func WrapDB(ctx context.Context, db *sql.DB) *DB { return &DB{db, ctx} }
func (db *DB) Connect() (*Conn, error) {
return db.ConnectContext(db.ctx)
}
func (db *DB) ConnectContext(ctx context.Context) (*Conn, error) {
conn, err := Connect(ctx, db.DB)
if err != nil {
return nil, err
}
return WrapConn(ctx, conn), nil
}
func (db *DB) WithConns(n int, do func(conns ...*Conn) error) error {
conns := make([]*Conn, n)
ctx, cancel := context.WithCancel(db.ctx)
defer func() {
cancel()
for _, conn := range conns {
if conn != nil {
conn.Close()
}
}
}()
for i := 0; i < n; i++ {
conn, err := db.ConnectContext(ctx)
if err != nil {
return err
}
conns[i] = conn
}
return do(conns...)
}
func (db *DB) MustFetch(query string, args ...interface{}) *resultset.ResultSet {
rows := db.MustQuery(query, args...)
defer rows.Close()
return Try(resultset.ReadFromRows(rows)).(*resultset.ResultSet)
}
func (db *DB) MustQuery(query string, args ...interface{}) *sql.Rows {
return Try(db.QueryContext(db.ctx, query, args...)).(*sql.Rows)
}
func (db *DB) MustExec(query string, args ...interface{}) sql.Result {
return Try(db.ExecContext(db.ctx, query, args...)).(sql.Result)
}
func (db *DB) AsyncFetch(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
rows, err := db.QueryContext(db.ctx, query, args...)
if err != nil {
future <- Pack(nil, err)
return
}
defer rows.Close()
future <- Pack(resultset.ReadFromRows(rows))
}()
return future
}
func (db *DB) AsyncQuery(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(db.QueryContext(db.ctx, query, args...))
}()
return future
}
func (db *DB) AsyncQueryRow(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(db.QueryRowContext(db.ctx, query, args...))
}()
return future
}
func (db *DB) AsyncExec(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(db.ExecContext(db.ctx, query, args...))
}()
return future
}
type Conn struct {
*sql.Conn
ctx context.Context
}
func WrapConn(ctx context.Context, conn *sql.Conn) *Conn { return &Conn{conn, ctx} }
func (conn *Conn) MustFetch(query string, args ...interface{}) *resultset.ResultSet {
rows := conn.MustQuery(query, args...)
defer rows.Close()
return Try(resultset.ReadFromRows(rows)).(*resultset.ResultSet)
}
func (conn *Conn) MustQuery(query string, args ...interface{}) *sql.Rows {
return Try(conn.QueryContext(conn.ctx, query, args...)).(*sql.Rows)
}
func (conn *Conn) MustExec(query string, args ...interface{}) sql.Result {
return Try(conn.ExecContext(conn.ctx, query, args...)).(sql.Result)
}
func (conn *Conn) Query(query string, args ...interface{}) (*sql.Rows, error) {
return conn.QueryContext(conn.ctx, query, args...)
}
func (conn *Conn) QueryRow(query string, args ...interface{}) *sql.Row {
return conn.QueryRowContext(conn.ctx, query, args...)
}
func (conn *Conn) Exec(query string, args ...interface{}) (sql.Result, error) {
return conn.ExecContext(conn.ctx, query, args...)
}
func (conn *Conn) AsyncFetch(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
rows, err := conn.QueryContext(conn.ctx, query, args...)
if err != nil {
future <- Pack(nil, err)
return
}
defer rows.Close()
future <- Pack(resultset.ReadFromRows(rows))
}()
return future
}
func (conn *Conn) AsyncQuery(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(conn.QueryContext(conn.ctx, query, args...))
}()
return future
}
func (conn *Conn) AsyncQueryRow(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(conn.QueryRowContext(conn.ctx, query, args...))
}()
return future
}
func (conn *Conn) AsyncExec(query string, args ...interface{}) <-chan Values {
future := make(chan Values, 1)
go func() {
defer close(future)
future <- Pack(conn.ExecContext(conn.ctx, query, args...))
}()
return future
}