-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdriver.go
62 lines (51 loc) · 1.65 KB
/
driver.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
// Go driver for MySQL X Protocol
//
// Copyright 2016 Simon J Mudd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// MySQL X protocol authentication using MYSQL41 method
package mysql
import (
"database/sql"
"database/sql/driver"
"net"
"github.com/sjmudd/go-mysqlx-driver/capability"
)
// This struct is exported to make the driver directly accessible.
// In general the driver is used via the database/sql package.
type XDriver struct{}
// DialFunc is a function which can be used to establish the network connection.
// Custom dial functions must be registered with RegisterDial
type DialFunc func(addr string) (net.Conn, error)
var dials map[string]DialFunc
// RegisterDial registers a custom dial function. It can then be used by the
// network address mynet(addr), where mynet is the registered new network.
// addr is passed as a parameter to the dial function.
func RegisterDial(net string, dial DialFunc) {
if dials == nil {
dials = make(map[string]DialFunc)
}
dials[net] = dial
}
func (d XDriver) Open(dsn string) (driver.Conn, error) {
var err error
cfg, err := parseDSN(dsn)
if err != nil {
return nil, err
}
cfg.useXProtocol = true // force X protocol as this driver was called explicitly
// New mysqlConn
mc := &mysqlXConn{
capabilities: capability.NewServerCapabilities(),
cfg: NewXconfigFromConfig(cfg),
maxPacketAllowed: maxPacketSize,
maxWriteSize: maxPacketSize - 1,
}
return mc.Open2()
}
func init() {
sql.Register("mysql/xprotocol", &XDriver{})
}