-
Notifications
You must be signed in to change notification settings - Fork 4
/
driver_go1.10.go
211 lines (195 loc) · 5.24 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
// +build go1.10
package zipkinsql
import (
"context"
"database/sql"
"database/sql/driver"
zipkin "github.com/openzipkin/zipkin-go"
)
var errConnDone = sql.ErrConnDone
// Compile time assertion
var (
_ driver.DriverContext = &zDriver{}
_ driver.Connector = &zDriver{}
)
// WrapConnector allows wrapping a database driver.Connector which eliminates
// the need to register zipkinsql as an available driver.Driver.
func WrapConnector(dc driver.Connector, t *zipkin.Tracer, options ...TraceOption) driver.Connector {
opts := TraceOptions{}
for _, o := range options {
o(&opts)
}
return &zDriver{
parent: dc.Driver(),
connector: dc,
tracer: t,
options: opts,
}
}
// zDriver implements driver.Driver
type zDriver struct {
parent driver.Driver
connector driver.Connector
tracer *zipkin.Tracer
options TraceOptions
}
func wrapDriver(d driver.Driver, t *zipkin.Tracer, o TraceOptions) driver.Driver {
if _, ok := d.(driver.DriverContext); ok {
return zDriver{parent: d, tracer: t, options: o}
}
return struct{ driver.Driver }{zDriver{parent: d, tracer: t, options: o}}
}
func wrapConn(parent driver.Conn, t *zipkin.Tracer, options TraceOptions) driver.Conn {
var (
n, hasNameValueChecker = parent.(driver.NamedValueChecker)
s, hasSessionResetter = parent.(driver.SessionResetter)
)
c := &zConn{parent: parent, tracer: t, 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, tracer *zipkin.Tracer, options TraceOptions) driver.Stmt {
var (
_, hasExeCtx = stmt.(driver.StmtExecContext)
_, hasQryCtx = stmt.(driver.StmtQueryContext)
c, hasColConv = stmt.(driver.ColumnConverter)
n, hasNamValChk = stmt.(driver.NamedValueChecker)
)
s := zStmt{parent: stmt, query: query, tracer: tracer, 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 zDriver) 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 zDriver) Connect(ctx context.Context) (driver.Conn, error) {
c, err := d.connector.Connect(ctx)
if err != nil {
return nil, err
}
return &zConn{parent: c, tracer: d.tracer, options: d.options}, nil
}
func (d zDriver) Driver() driver.Driver {
return d
}