-
Notifications
You must be signed in to change notification settings - Fork 27
/
stmt.go
151 lines (134 loc) · 4.03 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
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
package proxy
import (
"context"
"database/sql/driver"
)
// Stmt adds hook points into "database/sql/driver".Stmt.
type Stmt struct {
// Stmt is the original statement.
// It may be nil because some sql drivers support skipping Prepare.
Stmt driver.Stmt
QueryString string
Proxy *Proxy
Conn *Conn
}
// Close closes the statement.
// It just calls the original Close method.
func (stmt *Stmt) Close() error {
return stmt.Stmt.Close()
}
// NumInput returns the number of placeholder parameters.
// It just calls the original NumInput method.
func (stmt *Stmt) NumInput() int {
return stmt.Stmt.NumInput()
}
// Exec executes a query that doesn't return rows.
// It will trigger PreExec, Exec, PostExec hooks.
// NOT SUPPORTED: use ExecContext instead
func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error) {
panic("not supported")
}
// ExecContext executes a query that doesn't return rows.
// It will trigger PreExec, Exec, PostExec hooks.
func (stmt *Stmt) ExecContext(c context.Context, args []driver.NamedValue) (driver.Result, error) {
var ctx interface{}
var err error
var result driver.Result
hooks := stmt.Proxy.getHooks(c)
if hooks != nil {
defer func() { hooks.postExec(c, ctx, stmt, args, result, err) }()
if ctx, err = hooks.preExec(c, stmt, args); err != nil {
return nil, err
}
}
if execerContext, ok := stmt.Stmt.(driver.StmtExecContext); ok {
result, err = execerContext.ExecContext(c, args)
} else {
select {
default:
case <-c.Done():
return nil, c.Err()
}
dargs, err0 := namedValuesToValues(args)
if err0 != nil {
return nil, err0
}
result, err = stmt.Stmt.Exec(dargs)
}
if err != nil {
return result, err
}
if hooks != nil {
if err = hooks.exec(c, ctx, stmt, args, result); err != nil {
return result, err
}
}
return result, nil
}
// Query executes a query that may return rows.
// It wil trigger PreQuery, Query, PostQuery hooks.
// NOT SUPPORTED: use QueryContext instead
func (stmt *Stmt) Query(args []driver.Value) (driver.Rows, error) {
panic("not supported")
}
// QueryContext executes a query that may return rows.
// It wil trigger PreQuery, Query, PostQuery hooks.
func (stmt *Stmt) QueryContext(c context.Context, args []driver.NamedValue) (driver.Rows, error) {
var ctx interface{}
var err error
var rows driver.Rows
hooks := stmt.Proxy.getHooks(c)
if hooks != nil {
defer func() { hooks.postQuery(c, ctx, stmt, args, rows, err) }()
if ctx, err = hooks.preQuery(c, stmt, args); err != nil {
return nil, err
}
}
if queryCtx, ok := stmt.Stmt.(driver.StmtQueryContext); ok {
rows, err = queryCtx.QueryContext(c, args)
} else {
select {
default:
case <-c.Done():
return nil, c.Err()
}
dargs, err0 := namedValuesToValues(args)
if err0 != nil {
return nil, err0
}
rows, err = stmt.Stmt.Query(dargs)
}
if err != nil {
return nil, err
}
if hooks != nil {
if err = hooks.query(c, ctx, stmt, args, rows); err != nil {
return nil, err
}
}
return rows, nil
}
// ColumnConverter returns a ValueConverter for the provided column index.
// If the original statement does not satisfy ColumnConverter,
// it returns driver.DefaultParameterConverter.
func (stmt *Stmt) ColumnConverter(idx int) driver.ValueConverter {
if conv, ok := stmt.Stmt.(driver.ColumnConverter); ok {
return conv.ColumnConverter(idx)
}
return driver.DefaultParameterConverter
}
// CheckNamedValue for implementing NamedValueChecker
func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) (err error) {
if nvc, ok := stmt.Stmt.(namedValueChecker); ok {
return nvc.CheckNamedValue(nv)
}
// When converting data in sql/driver/convert.go, it is checked first whether the `stmt`
// implements `NamedValueChecker`, and then checks if `conn` implements NamedValueChecker.
// In the case of "go-sql-proxy", the `proxy.Stmt` "implements" `CheckNamedValue` here,
// so we also check both `stmt` and `conn` inside here.
if nvc, ok := stmt.Conn.Conn.(namedValueChecker); ok {
return nvc.CheckNamedValue(nv)
}
// fallback to default
return defaultCheckNamedValue(nv)
}