-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstmt.go
105 lines (89 loc) · 2.11 KB
/
stmt.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
package sqlplus
import (
"context"
"database/sql/driver"
)
var (
_ driver.Stmt = (*stmt)(nil)
_ driver.StmtExecContext = (*stmt)(nil)
_ driver.StmtQueryContext = (*stmt)(nil)
)
type (
stmt struct {
driver.Stmt
query string
StmtHook
prepareContext context.Context
}
prepareContextKey struct{}
stmtKey struct{}
)
func PrepareContextFromContext(ctx context.Context) context.Context {
value := ctx.Value(prepareContextKey{})
if value != nil {
return value.(context.Context)
}
return nil
}
func StmtFromContext(ctx context.Context) interface {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
} {
value := ctx.Value(stmtKey{})
if value != nil {
return nil
}
return value.(interface {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
})
}
// -----------------
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
query := s.query
ctx = s.newStmtContext(ctx)
ctx, args, err = s.BeforeStmtQueryContext(ctx, query, args, nil)
defer func() {
_, rows, err = s.AfterStmtQueryContext(ctx, query, args, rows, err)
}()
if err != nil {
return nil, err
}
switch ss := s.Stmt.(type) {
case driver.StmtQueryContext:
return ss.QueryContext(ctx, args)
default:
value, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return s.Query(value)
}
}
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) {
query := s.query
ctx = s.newStmtContext(ctx)
ctx, args, err = s.BeforeStmtExecContext(ctx, query, args, nil)
defer func() {
_, r, err = s.AfterStmtExecContext(ctx, query, args, r, err)
}()
if err != nil {
return nil, err
}
switch ss := s.Stmt.(type) {
case driver.StmtExecContext:
return ss.ExecContext(ctx, args)
default:
value, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return s.Exec(value)
}
}
func (s *stmt) newStmtContext(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, prepareContextKey{}, s.prepareContext)
return context.WithValue(ctx, stmtKey{}, s)
}