forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
238 lines (203 loc) · 8.56 KB
/
conn.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// +build go1.9
package sqlx
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx/reflectx"
)
// Return a single connection using this DB.
// Queries run on the same Conn will be run in the same database session.
// Every Conn must be returned to the database pool after use by calling Conn.Close.
func (db *DB) Connx() (*Conn, error) {
return db.ConnxContext(context.Background())
}
// Return a single connection using this DB.
// Queries run on the same Conn will be run in the same database session.
// Every Conn must be returned to the database pool after use by calling Conn.Close.
func (db *DB) ConnxContext(ctx context.Context) (*Conn, error) {
conn, err := db.DB.Conn(ctx)
if err != nil {
return nil, err
}
return &Conn{Conn: conn, driverName: db.driverName, unsafe: db.unsafe, Mapper: db.Mapper}, err
}
// Conn is an sqlx wrapper around sql.Conn with extra functionality
type Conn struct {
*sql.Conn
driverName string
unsafe bool
Mapper *reflectx.Mapper
}
// DriverName returns the driverName used by the DB which opened this connection.
func (conn *Conn) DriverName() string {
return conn.driverName
}
// Rebind a query within a connection's bindvar type.
func (conn *Conn) Rebind(query string) string {
return Rebind(BindType(conn.driverName), query)
}
// Unsafe returns a version of Conn which will silently succeed to scan when
// columns in the SQL result have no fields in the destination struct.
func (conn *Conn) Unsafe() *Conn {
return &Conn{Conn: conn.Conn, driverName: conn.driverName, unsafe: true, Mapper: conn.Mapper}
}
// BindNamed binds a query within a connection's bindvar type.
func (conn *Conn) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
return bindNamedMapper(BindType(conn.driverName), query, arg, conn.Mapper)
}
// NamedQuery within a connection.
// Any named placeholder parameters are replaced with fields from arg.
func (conn *Conn) NamedQuery(query string, arg interface{}) (*Rows, error) {
return NamedQuery(conn, query, arg)
}
// NamedQueryContext using this connection.
// Any named placeholder parameters are replaced with fields from arg.
func (conn *Conn) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) {
return NamedQueryContext(ctx, conn, query, arg)
}
// NamedExec a named query within a connection.
// Any named placeholder parameters are replaced with fields from arg.
func (conn *Conn) NamedExec(query string, arg interface{}) (sql.Result, error) {
return NamedExec(conn, query, arg)
}
// NamedExecContext using this connection.
// Any named placeholder parameters are replaced with fields from arg.
func (conn *Conn) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return NamedExecContext(ctx, conn, query, arg)
}
// Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.
func (conn *Conn) Beginx() (*Tx, error) {
return conn.BeginTxx(context.Background(), nil)
}
// MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead
// of an *sql.Tx.
func (conn *Conn) MustBegin() *Tx {
tx, err := conn.Beginx()
if err != nil {
panic(err)
}
return tx
}
// BeginTxx begins a transaction and returns an *sqlx.Tx instead of an
// *sql.Tx.
//
// The provided context is used until the transaction is committed or rolled
// back. If the context is canceled, the sql package will roll back the
// transaction. Tx.Commit will return an error if the context provided to
// BeginxContext is canceled.
func (conn *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
tx, err := conn.Conn.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &Tx{Tx: tx, driverName: conn.driverName, unsafe: conn.unsafe, Mapper: conn.Mapper}, err
}
// MustBeginTx starts a transaction, and panics on error. Returns an *sqlx.Tx instead
// of an *sql.Tx.
//
// The provided context is used until the transaction is committed or rolled
// back. If the context is canceled, the sql package will roll back the
// transaction. Tx.Commit will return an error if the context provided to
// MustBeginContext is canceled.
func (conn *Conn) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx {
tx, err := conn.BeginTxx(ctx, opts)
if err != nil {
panic(err)
}
return tx
}
// Select using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) Select(dest interface{}, query string, args ...interface{}) error {
return Select(conn, dest, query, args...)
}
// SelectContext using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return SelectContext(ctx, conn, dest, query, args...)
}
// Query using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) Query(query string, args ...interface{}) (*sql.Rows, error) {
return conn.Conn.QueryContext(context.Background(), query, args...)
}
// Queryx using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) Queryx(query string, args ...interface{}) (*Rows, error) {
r, err := conn.Query(query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: conn.unsafe, Mapper: conn.Mapper}, err
}
// QueryxContext queries the database and returns an *sqlx.Rows.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
r, err := conn.Conn.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: conn.unsafe, Mapper: conn.Mapper}, err
}
// QueryRowx using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) QueryRowx(query string, args ...interface{}) *Row {
rows, err := conn.Query(query, args...)
return &Row{rows: rows, err: err, unsafe: conn.unsafe, Mapper: conn.Mapper}
}
// QueryRowxContext queries the database and returns an *sqlx.Row.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row {
rows, err := conn.Conn.QueryContext(ctx, query, args...)
return &Row{rows: rows, err: err, unsafe: conn.unsafe, Mapper: conn.Mapper}
}
// Get using this connection.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (conn *Conn) Get(dest interface{}, query string, args ...interface{}) error {
return Get(conn, dest, query, args...)
}
// GetContext using this connection.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (conn *Conn) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return GetContext(ctx, conn, dest, query, args...)
}
// Exec using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) Exec(query string, args ...interface{}) (sql.Result, error) {
return conn.Conn.ExecContext(context.Background(), query, args...)
}
// MustExec (panic) using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) MustExec(query string, args ...interface{}) sql.Result {
return MustExec(conn, query, args...)
}
// MustExecContext (panic) runs MustExecContext using this connection.
// Any placeholder parameters are replaced with supplied args.
func (conn *Conn) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result {
return MustExecContext(ctx, conn, query, args...)
}
// Prepare a statement using this connection.
func (conn *Conn) Prepare(query string) (*sql.Stmt, error) {
return conn.PrepareContext(context.Background(), query)
}
// Preparex a statement using this connection.
func (conn *Conn) Preparex(query string) (*Stmt, error) {
return Preparex(conn, query)
}
// PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
//
// The provided context is used for the preparation of the statement, not for
// the execution of the statement.
func (conn *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error) {
return PreparexContext(ctx, conn, query)
}
// PrepareNamed returns an sqlx.NamedStmt
func (conn *Conn) PrepareNamed(query string) (*NamedStmt, error) {
return prepareNamed(conn, query)
}
// PrepareNamedContext returns an sqlx.NamedStmt
func (conn *Conn) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) {
return prepareNamedContext(ctx, conn, query)
}