-
Notifications
You must be signed in to change notification settings - Fork 31
/
stmt.go
executable file
·110 lines (96 loc) · 2.76 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
106
107
108
109
110
package sqlmw
import (
"context"
"database/sql/driver"
)
type wrappedStmt struct {
intr Interceptor
ctx context.Context
query string
parent driver.Stmt
conn wrappedConn
}
// Compile time validation that our types implement the expected interfaces
var (
_ driver.Stmt = wrappedStmt{}
_ driver.StmtExecContext = wrappedStmt{}
_ driver.StmtQueryContext = wrappedStmt{}
_ driver.ColumnConverter = wrappedStmt{}
)
func (s wrappedStmt) Close() (err error) {
return s.intr.StmtClose(s.ctx, s.parent)
}
func (s wrappedStmt) NumInput() int {
return s.parent.NumInput()
}
func (s wrappedStmt) Exec(args []driver.Value) (res driver.Result, err error) {
res, err = s.parent.Exec(args)
if err != nil {
return nil, err
}
return wrappedResult{intr: s.intr, ctx: s.ctx, parent: res}, nil
}
func (s wrappedStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
rows, err = s.parent.Query(args)
if err != nil {
return nil, err
}
return wrapRows(s.ctx, s.intr, rows), nil
}
func (s wrappedStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
wrappedParent := wrappedParentStmt{Stmt: s.parent}
res, err = s.intr.StmtExecContext(ctx, wrappedParent, s.query, args)
if err != nil {
return nil, err
}
return wrappedResult{intr: s.intr, ctx: ctx, parent: res}, nil
}
func (s wrappedStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
wrappedParent := wrappedParentStmt{Stmt: s.parent}
ctx, rows, err = s.intr.StmtQueryContext(ctx, wrappedParent, s.query, args)
if err != nil {
return nil, err
}
return wrapRows(ctx, s.intr, rows), nil
}
func (s wrappedStmt) ColumnConverter(idx int) driver.ValueConverter {
if converter, ok := s.parent.(driver.ColumnConverter); ok {
return converter.ColumnConverter(idx)
}
return driver.DefaultParameterConverter
}
type wrappedParentStmt struct {
driver.Stmt
}
func (s wrappedParentStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
if stmtQueryContext, ok := s.Stmt.(driver.StmtQueryContext); ok {
return stmtQueryContext.QueryContext(ctx, args)
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return s.Stmt.Query(dargs)
}
func (s wrappedParentStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
if stmtExecContext, ok := s.Stmt.(driver.StmtExecContext); ok {
return stmtExecContext.ExecContext(ctx, args)
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return s.Stmt.Exec(dargs)
}