-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
conn.go
73 lines (59 loc) · 1.97 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
package dbresolver
import (
"context"
"database/sql"
"strings"
)
// Conn is a *sql.Conn wrapper.
// Its main purpose is to be able to return the internal Tx and Stmt interfaces.
type Conn interface {
Close() error
BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PingContext(ctx context.Context) error
PrepareContext(ctx context.Context, query string) (Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Raw(f func(driverConn interface{}) error) (err error)
}
type conn struct {
sourceDB *sql.DB
conn *sql.Conn
}
func (c *conn) Close() error {
return c.conn.Close()
}
func (c *conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) {
stx, err := c.conn.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &tx{
sourceDB: c.sourceDB,
tx: stx,
}, nil
}
func (c *conn) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return c.conn.ExecContext(ctx, query, args...)
}
func (c *conn) PingContext(ctx context.Context) error {
return c.conn.PingContext(ctx)
}
func (c *conn) PrepareContext(ctx context.Context, query string) (Stmt, error) {
pstmt, err := c.conn.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
_query := strings.ToUpper(query)
writeFlag := strings.Contains(_query, "RETURNING")
return newSingleDBStmt(c.sourceDB, pstmt, writeFlag), nil
}
func (c *conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return c.conn.QueryContext(ctx, query, args...)
}
func (c *conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return c.conn.QueryRowContext(ctx, query, args...)
}
func (c *conn) Raw(f func(driverConn interface{}) error) (err error) {
return c.conn.Raw(f)
}