-
Notifications
You must be signed in to change notification settings - Fork 27
/
driver_go1.10.go
214 lines (198 loc) · 5.23 KB
/
driver_go1.10.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// +build go1.10
package ocsql
import (
"context"
"database/sql"
"database/sql/driver"
"go.opencensus.io/trace"
)
var errConnDone = sql.ErrConnDone
// Compile time assertion
var (
_ driver.DriverContext = &ocDriver{}
_ driver.Connector = &ocDriver{}
)
// WrapConnector allows wrapping a database driver.Connector which eliminates
// the need to register ocsql as an available driver.Driver.
func WrapConnector(dc driver.Connector, options ...TraceOption) driver.Connector {
o := TraceOptions{}
for _, option := range options {
option(&o)
}
if o.InstanceName == "" {
o.InstanceName = defaultInstanceName
} else {
o.DefaultAttributes = append(o.DefaultAttributes, trace.StringAttribute("sql.instance", o.InstanceName))
}
return &ocDriver{
parent: dc.Driver(),
connector: dc,
options: o,
}
}
// ocDriver implements driver.Driver
type ocDriver struct {
parent driver.Driver
connector driver.Connector
options TraceOptions
}
func wrapDriver(d driver.Driver, o TraceOptions) driver.Driver {
if _, ok := d.(driver.DriverContext); ok {
return ocDriver{parent: d, options: o}
}
return struct{ driver.Driver }{ocDriver{parent: d, options: o}}
}
func wrapConn(parent driver.Conn, options TraceOptions) driver.Conn {
var (
n, hasNameValueChecker = parent.(driver.NamedValueChecker)
s, hasSessionResetter = parent.(driver.SessionResetter)
)
c := &ocConn{parent: parent, options: options}
switch {
case !hasNameValueChecker && !hasSessionResetter:
return c
case hasNameValueChecker && !hasSessionResetter:
return struct {
conn
driver.NamedValueChecker
}{c, n}
case !hasNameValueChecker && hasSessionResetter:
return struct {
conn
driver.SessionResetter
}{c, s}
case hasNameValueChecker && hasSessionResetter:
return struct {
conn
driver.NamedValueChecker
driver.SessionResetter
}{c, n, s}
}
panic("unreachable")
}
func wrapStmt(stmt driver.Stmt, query string, options TraceOptions) driver.Stmt {
var (
_, hasExeCtx = stmt.(driver.StmtExecContext)
_, hasQryCtx = stmt.(driver.StmtQueryContext)
c, hasColConv = stmt.(driver.ColumnConverter)
n, hasNamValChk = stmt.(driver.NamedValueChecker)
)
s := ocStmt{parent: stmt, query: query, options: options}
switch {
case !hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
}{s}
case !hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
}{s, s}
case hasExeCtx && !hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
}{s, s}
case hasExeCtx && hasQryCtx && !hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
}{s, s, s}
case !hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.ColumnConverter
}{s, c}
case !hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.ColumnConverter
}{s, s, c}
case hasExeCtx && !hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.ColumnConverter
}{s, s, c}
case hasExeCtx && hasQryCtx && hasColConv && !hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.ColumnConverter
}{s, s, s, c}
case !hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.NamedValueChecker
}{s, n}
case !hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, n}
case hasExeCtx && !hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.NamedValueChecker
}{s, s, n}
case hasExeCtx && hasQryCtx && !hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, s, n}
case !hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.ColumnConverter
driver.NamedValueChecker
}{s, c, n}
case !hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, c, n}
case hasExeCtx && !hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, c, n}
case hasExeCtx && hasQryCtx && hasColConv && hasNamValChk:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, s, c, n}
}
panic("unreachable")
}
func (d ocDriver) OpenConnector(name string) (driver.Connector, error) {
var err error
d.connector, err = d.parent.(driver.DriverContext).OpenConnector(name)
if err != nil {
return nil, err
}
return d, err
}
func (d ocDriver) Connect(ctx context.Context) (driver.Conn, error) {
c, err := d.connector.Connect(ctx)
if err != nil {
return nil, err
}
return &ocConn{parent: c, options: d.options}, nil
}
func (d ocDriver) Driver() driver.Driver {
return d
}