Skip to content

Commit ca1671a

Browse files
committed
Implement Connector and DriverContext interface
1 parent 1a676ac commit ca1671a

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

connector.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build go1.10
10+
11+
package mysql
12+
13+
import (
14+
"context"
15+
"database/sql/driver"
16+
"net"
17+
)
18+
19+
// Connector is a driver in a fixed configuration.
20+
type Connector struct {
21+
Config *Config
22+
}
23+
24+
// Connect implements driver.Connector interface.
25+
// Connect returns a connection to the database.
26+
func (c Connector) Connect(ctx context.Context) (driver.Conn, error) {
27+
var err error
28+
29+
// New mysqlConn
30+
mc := &mysqlConn{
31+
maxAllowedPacket: maxPacketSize,
32+
maxWriteSize: maxPacketSize - 1,
33+
closech: make(chan struct{}),
34+
cfg: c.Config,
35+
}
36+
mc.parseTime = mc.cfg.ParseTime
37+
38+
// Connect to Server
39+
// TODO: needs RegisterDialContext
40+
if dial, ok := dials[mc.cfg.Net]; ok {
41+
mc.netConn, err = dial(mc.cfg.Addr)
42+
} else {
43+
nd := net.Dialer{Timeout: mc.cfg.Timeout}
44+
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
45+
}
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
// Enable TCP Keepalives on TCP connections
51+
if tc, ok := mc.netConn.(*net.TCPConn); ok {
52+
if err := tc.SetKeepAlive(true); err != nil {
53+
// Don't send COM_QUIT before handshake.
54+
mc.netConn.Close()
55+
mc.netConn = nil
56+
return nil, err
57+
}
58+
}
59+
60+
// Call startWatcher for context support (From Go 1.8)
61+
if s, ok := interface{}(mc).(watcher); ok {
62+
s.startWatcher()
63+
}
64+
if err := mc.watchCancel(ctx); err != nil {
65+
return nil, err
66+
}
67+
defer mc.finish()
68+
69+
mc.buf = newBuffer(mc.netConn)
70+
71+
// Set I/O timeouts
72+
mc.buf.timeout = mc.cfg.ReadTimeout
73+
mc.writeTimeout = mc.cfg.WriteTimeout
74+
75+
// Reading Handshake Initialization Packet
76+
cipher, err := mc.readInitPacket()
77+
if err != nil {
78+
mc.cleanup()
79+
return nil, err
80+
}
81+
82+
// Send Client Authentication Packet
83+
if err = mc.writeAuthPacket(cipher); err != nil {
84+
mc.cleanup()
85+
return nil, err
86+
}
87+
88+
// Handle response to auth packet, switch methods if possible
89+
if err = handleAuthResult(mc, cipher); err != nil {
90+
// Authentication failed and MySQL has already closed the connection
91+
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
92+
// Do not send COM_QUIT, just cleanup and return the error.
93+
mc.cleanup()
94+
return nil, err
95+
}
96+
97+
if mc.cfg.MaxAllowedPacket > 0 {
98+
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
99+
} else {
100+
// Get max allowed packet size
101+
maxap, err := mc.getSystemVar("max_allowed_packet")
102+
if err != nil {
103+
mc.Close()
104+
return nil, err
105+
}
106+
mc.maxAllowedPacket = stringToInt(maxap) - 1
107+
}
108+
if mc.maxAllowedPacket < maxPacketSize {
109+
mc.maxWriteSize = mc.maxAllowedPacket
110+
}
111+
112+
// Handle DSN Params
113+
err = mc.handleParams()
114+
if err != nil {
115+
mc.Close()
116+
return nil, err
117+
}
118+
119+
return mc, nil
120+
}
121+
122+
// Driver implements driver.Connector interface.
123+
// Driver returns &MySQLDriver{}.
124+
func (c Connector) Driver() driver.Driver {
125+
return &MySQLDriver{}
126+
}

driver_go110.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build go1.10
10+
11+
package mysql
12+
13+
import (
14+
"database/sql/driver"
15+
)
16+
17+
// OpenConnector implements driver.DriverContext.
18+
func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
19+
c, err := ParseDSN(dsn)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
return Connector{
25+
Config: c,
26+
}, nil
27+
}

driver_go110_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
package mysql
10+
11+
import (
12+
"database/sql/driver"
13+
)
14+
15+
var _ driver.DriverContext = &MySQLDriver{}

0 commit comments

Comments
 (0)