-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
341 lines (299 loc) · 9.09 KB
/
connector.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package mysqlx
import (
"bytes"
"context"
"crypto/tls"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"net"
"os"
"runtime"
"strconv"
"strings"
"sync"
"github.com/renthraysk/mysqlx/authentication"
"github.com/renthraysk/mysqlx/authentication/mysql41"
"github.com/renthraysk/mysqlx/errs"
"github.com/renthraysk/mysqlx/msg"
)
// Dialer interface documenting our requirements for dialing a MySQL server.
type Dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
// Connector is the database/sql.Connector implementation
type Connector struct {
network string
addr string
dialer Dialer
tlsConfig *tls.Config
database string
username string
password string
authentication authentication.Starter
bufferSize int
resetBuild sync.Once
resetKeepOpen bool
resetFuncs []func(b *builder) error
reset []byte
connectAttrs map[string]string
}
// Option is a functional option for creating the Connector
type Option func(*Connector) error
const minBufferSize = 4 * 1024
// UserName returns the user name of the account to authenticate with. Part of authentication.Credentials interface.
func (cnn *Connector) UserName() string { return cnn.username }
// Password returns the password of the account to authenticate with. Part of authentication.Credentials interface.
func (cnn *Connector) Password() string { return cnn.password }
// Database returns the database name to authenticate with. Part of authentication.Credentials interface.
func (cnn *Connector) Database() string { return cnn.database }
// New creates a database/sql.Connector
func New(network, addr string, options ...Option) (*Connector, error) {
cnn := &Connector{
dialer: new(net.Dialer),
network: network,
addr: addr,
authentication: mysql41.New(),
bufferSize: minBufferSize,
resetFuncs: make([]func(b *builder) error, 0, 2),
}
for _, opt := range options {
if err := opt(cnn); err != nil {
return nil, err
}
}
return cnn, nil
}
// WithDialer replaces the default net.Dialer for connecting to mysql.
func WithDialer(dialer Dialer) Option {
return func(cnn *Connector) error {
cnn.dialer = dialer
return nil
}
}
// WithDatabase sets the database the connector will be default after successful connection and authentication
func WithDatabase(database string) Option {
return func(cnn *Connector) error {
cnn.database = database
return nil
}
}
// WithAuthentication set the authentication mechanism that will authentication with.
// If authenticating a connection over TLS then either authentication/mysql41 or authentication/sha256.
// If not using a TLS connection then authentication/mysql41 is the only reliable option.
func WithAuthentication(auth authentication.Starter) Option {
return func(cnn *Connector) error {
cnn.authentication = auth
return nil
}
}
// WithUserPassword set the username and password pair of the account to authenticate with.
func WithUserPassword(username, password string) Option {
return func(cnn *Connector) error {
cnn.username = username
cnn.password = password
return nil
}
}
// WithTLSConfig set the TLS configuration to connect to mysqlx with.
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(cnn *Connector) error {
cnn.tlsConfig = tlsConfig
return nil
}
}
// WithBufferSize sets the internal read/write buffer size.
func WithBufferSize(size int) Option {
return func(cnn *Connector) error {
if size > minBufferSize {
cnn.bufferSize = size
}
return nil
}
}
func WithDefaultConnectAttrs() Option {
attrs := map[string]string{
"_client_name": "mysqlx",
"_pid": strconv.Itoa(os.Getpid()),
"_platform": runtime.GOARCH,
"_os": runtime.GOOS,
"program_name": os.Args[0],
}
return WithConnectAttrs(attrs)
}
// WithConnectAttrs sets the connection attributes that will be set on connect
func WithConnectAttrs(attrs map[string]string) Option {
return func(cnn *Connector) error {
if cnn.connectAttrs == nil {
cnn.connectAttrs = make(map[string]string, len(attrs))
}
for k, v := range attrs {
cnn.connectAttrs[k] = v
}
return nil
}
}
// WithSQLMode set the default sql_mode
func WithSQLMode(modes ...string) Option {
return func(cnn *Connector) error {
cnn.resetFuncs = append(cnn.resetFuncs, func(b *builder) error {
return b.WriteStmtExecute("SET SESSION sql_mode = ?", strings.Join(modes, ","))
})
return nil
}
}
type SessionVars map[string]any
func (sv SessionVars) build(b *builder) error {
var s bytes.Buffer
for k, v := range sv {
s.Reset()
s.WriteString("SET SESSION `")
for i := strings.IndexByte(k, '`'); i >= 0; i = strings.IndexByte(k, '`') {
i++
s.WriteString(k[:i])
s.WriteByte('`')
k = k[i:]
}
s.WriteString(k)
s.WriteString("` = ?")
if err := b.WriteStmtExecute(s.String(), v); err != nil {
return err
}
}
return nil
}
// WithSessionVars sets/resets mysql session variables on connect and every reset.
func WithSessionVars(sv SessionVars) Option {
return func(cnn *Connector) error {
cnn.resetFuncs = append(cnn.resetFuncs, sv.build)
return nil
}
}
// WithDefaultTxIsolation sets the default transaction isolation level on connect, and at every reset.
func WithDefaultTxIsolation(isolationLevel sql.IsolationLevel) Option {
set := ""
switch isolationLevel {
case sql.LevelDefault:
return func(cnn *Connector) error { return nil }
case sql.LevelReadUncommitted:
set = "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"
case sql.LevelReadCommitted:
set = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED"
case sql.LevelRepeatableRead:
set = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"
case sql.LevelSerializable:
set = "SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE"
default:
return func(cnn *Connector) error {
return fmt.Errorf("Unsupported default transaction isolation level (%s)", isolationLevel.String())
}
}
return func(cnn *Connector) error {
cnn.resetFuncs = append(cnn.resetFuncs, func(b *builder) error {
return b.WriteStmtExecute(set)
})
return nil
}
}
// Connect is the database/sql.Connector Connect() implementation
func (cnn *Connector) Connect(ctx context.Context) (driver.Conn, error) {
netConn, err := cnn.dialer.DialContext(ctx, cnn.network, cnn.addr)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
}
conn := &conn{
netConn: netConn,
connector: cnn,
buf: make([]byte, cnn.bufferSize),
}
conn.r.Reset(netConn)
// TLS
if _, ok := netConn.(*net.TCPConn); ok && cnn.tlsConfig != nil {
s, err := msg.CapabilitySetTLS(conn.buf, true)
if err != nil {
return nil, fmt.Errorf("failed to marshal TLS enable CapabilitySet: %w", err)
}
if _, err := conn.execMsg(ctx, s); err != nil {
netConn.Close()
return nil, fmt.Errorf("failed to set TLS capability: %w", err)
}
tlsConn := tls.Client(netConn, cnn.tlsConfig)
if err := tlsConn.Handshake(); err != nil {
tlsConn.Close()
return nil, fmt.Errorf("failed TLS handshake: %w", err)
}
conn.netConn = tlsConn
conn.r.Reset(tlsConn)
}
// Connection Attributes
if len(cnn.connectAttrs) > 0 {
cs, err := msg.CapabilitySetSessionConnectAttrs(conn.buf, cnn.connectAttrs)
if err != nil {
return nil, fmt.Errorf("failed to marshal session connection attributes: %w", err)
}
if _, err := conn.execMsg(ctx, cs); err != nil {
return nil, fmt.Errorf("failed to set session connection attributes: %w", err)
}
}
// Authentication
if err := conn.authenticate(ctx); err != nil {
return nil, err
}
// Build a byte slice to reset mysql session state every reset.
// Using err inside this func() to signal err occurring within
cnn.resetBuild.Do(func() {
// Build byteslice to be written/executed on every connect & reset.
const ExpectFieldSessionResetKeepOpen = "6.1"
// See if MySQL supports session-reset's keepOpen field...
b := newBuilder()
b.WriteExpectField(ExpectFieldSessionResetKeepOpen)
b.WriteExpectClose()
cnn.resetKeepOpen = false
if err = conn.sendN(ctx, b.Bytes()); err != nil {
var e *errs.Errors
if !errors.As(err, &e) {
return
}
if !errs.ErXExpectFieldExistsFailed.Is((*e)[0]) {
return
}
// No session-reset(keep-open) support.
err = nil
} else {
cnn.resetKeepOpen = true
}
// Determine if have anything to run per reset
if !cnn.resetKeepOpen && len(cnn.resetFuncs) == 0 {
return
}
b.Reset()
b.WriteExpectOpen(onErrorFail)
// If can perform a session reset without needing to reauthenticate
// then include session-reset(keepOpen=true) for single write reset.
if cnn.resetKeepOpen {
b.WriteSessionReset(true)
} else {
// Using a ping as NOP to ensure error indexes remain consistent for tests.
b.WritePing()
}
for _, f := range cnn.resetFuncs {
if err = f(b); err != nil {
return
}
}
b.WriteExpectClose()
cnn.reset = b.Bytes()
})
if err != nil {
return nil, err
}
if err := conn.sendN(ctx, cnn.reset); err != nil {
return nil, err
}
return conn, nil
}
// Driver is the database/sql.Connector Driver() implementation
func (cnn *Connector) Driver() driver.Driver {
return nil
}