-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.go
More file actions
97 lines (82 loc) · 2.21 KB
/
base.go
File metadata and controls
97 lines (82 loc) · 2.21 KB
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
package sql
import (
"context"
"database/sql"
)
var (
_ DB = (*BaseDB)(nil)
_ Stmt = (*BaseStmt)(nil)
_ Tx = (*BaseTx)(nil)
_ Conn = (*BaseConn)(nil)
)
// BaseDB is the most inner middleware, which implements the DB interface. Other
// middlewares can wrap each other casually, but this one is the base.
type BaseDB struct {
*sql.DB
}
func (b *BaseDB) PrepareContext(ctx context.Context, query string) (Stmt, error) {
stmt, err := b.DB.PrepareContext(ctx, query)
return &BaseStmt{stmt}, err
}
func (b *BaseDB) Prepare(query string) (Stmt, error) {
stmt, err := b.DB.Prepare(query)
return &BaseStmt{stmt}, err
}
func (b *BaseDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) {
tx, err := b.DB.BeginTx(ctx, opts)
return &BaseTx{tx}, err
}
func (b *BaseDB) Begin() (Tx, error) {
tx, err := b.DB.Begin()
return &BaseTx{tx}, err
}
func (b *BaseDB) Conn(ctx context.Context) (Conn, error) {
conn, err := b.DB.Conn(ctx)
return &BaseConn{conn}, err
}
func (b *BaseDB) OriginDB() *sql.DB {
return b.DB
}
// BaseStmt implements the Stmt interface.
type BaseStmt struct {
*sql.Stmt
}
func (b *BaseStmt) OriginStmt() *sql.Stmt {
return b.Stmt
}
// BaseTx implements the Tx interface.
type BaseTx struct {
*sql.Tx
}
func (b *BaseTx) PrepareContext(ctx context.Context, query string) (Stmt, error) {
stmt, err := b.Tx.PrepareContext(ctx, query)
return &BaseStmt{stmt}, err
}
func (b *BaseTx) Prepare(query string) (Stmt, error) {
stmt, err := b.Tx.Prepare(query)
return &BaseStmt{stmt}, err
}
func (b *BaseTx) StmtContext(ctx context.Context, stmt Stmt) Stmt {
return &BaseStmt{b.Tx.StmtContext(ctx, stmt.OriginStmt())}
}
func (b *BaseTx) Stmt(stmt Stmt) Stmt {
return &BaseStmt{b.Tx.Stmt(stmt.OriginStmt())}
}
func (b *BaseTx) OriginTx() *sql.Tx {
return b.Tx
}
// BaseConn implements the Conn interface.
type BaseConn struct {
*sql.Conn
}
func (b *BaseConn) PrepareContext(ctx context.Context, query string) (Stmt, error) {
stmt, err := b.Conn.PrepareContext(ctx, query)
return &BaseStmt{stmt}, err
}
func (b *BaseConn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) {
tx, err := b.Conn.BeginTx(ctx, opts)
return &BaseTx{tx}, err
}
func (b *BaseConn) OriginConn() *sql.Conn {
return b.Conn
}