-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement Connector and DriverContext interface #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
shogo82148
wants to merge
19
commits into
go-sql-driver:master
from
shogo82148:connector-interface-support
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ca1671a
Implement Connector and DriverContext interface
shogo82148 ccd2622
no need to check the driver implements the watcher interface.
shogo82148 cc53a93
add missing build tag.
shogo82148 f1a63a4
Merge branch 'master' into connector-interface-support
shogo82148 877ae16
Rename new files.
shogo82148 0aca26b
move Connect to Config from Connector.
shogo82148 cfaf043
rename driver_go110.go
shogo82148 a1d847f
Merge branch 'master' into connector-interface-support
shogo82148 49e2480
fix broken build.
shogo82148 82898e3
simplify OpenConnector
shogo82148 ee66ae6
make `MySQLDriver.Open` more simple.
shogo82148 069b529
fix broken build with Go 1.7
shogo82148 0891474
Merge branch 'master' into connector-interface-support
shogo82148 d081e4e
remove the watcher interface
shogo82148 8982810
Merge branch 'master' into connector-interface-support
shogo82148 40d78d5
apply the chages of 7ac0064e
shogo82148 1b00bed
add the connetor struct
shogo82148 255f988
add NewConnector
shogo82148 af9889e
Merge branch 'master' into connector-interface-support
shogo82148 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// 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/. | ||
|
||
package mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"net" | ||
) | ||
|
||
type connector struct { | ||
cfg *Config // immutable private copy. | ||
} | ||
|
||
// Connect implements driver.Connector interface. | ||
// Connect returns a connection to the database. | ||
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { | ||
var err error | ||
|
||
// New mysqlConn | ||
mc := &mysqlConn{ | ||
maxAllowedPacket: maxPacketSize, | ||
maxWriteSize: maxPacketSize - 1, | ||
closech: make(chan struct{}), | ||
cfg: c.cfg, | ||
} | ||
mc.parseTime = mc.cfg.ParseTime | ||
|
||
// Connect to Server | ||
// TODO: needs RegisterDialContext | ||
dialsLock.RLock() | ||
dial, ok := dials[mc.cfg.Net] | ||
dialsLock.RUnlock() | ||
if ok { | ||
mc.netConn, err = dial(mc.cfg.Addr) | ||
} else { | ||
nd := net.Dialer{Timeout: mc.cfg.Timeout} | ||
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr) | ||
} | ||
if err != nil { | ||
if nerr, ok := err.(net.Error); ok && nerr.Temporary() { | ||
errLog.Print("net.Error from Dial()': ", nerr.Error()) | ||
return nil, driver.ErrBadConn | ||
} | ||
return nil, err | ||
} | ||
|
||
// Enable TCP Keepalives on TCP connections | ||
if tc, ok := mc.netConn.(*net.TCPConn); ok { | ||
if err := tc.SetKeepAlive(true); err != nil { | ||
// Don't send COM_QUIT before handshake. | ||
mc.netConn.Close() | ||
mc.netConn = nil | ||
return nil, err | ||
} | ||
} | ||
|
||
// Call startWatcher for context support (From Go 1.8) | ||
mc.startWatcher() | ||
if err := mc.watchCancel(ctx); err != nil { | ||
return nil, err | ||
} | ||
defer mc.finish() | ||
|
||
mc.buf = newBuffer(mc.netConn) | ||
|
||
// Set I/O timeouts | ||
mc.buf.timeout = mc.cfg.ReadTimeout | ||
mc.writeTimeout = mc.cfg.WriteTimeout | ||
|
||
// Reading Handshake Initialization Packet | ||
authData, plugin, err := mc.readHandshakePacket() | ||
if err != nil { | ||
mc.cleanup() | ||
return nil, err | ||
} | ||
|
||
if plugin == "" { | ||
plugin = defaultAuthPlugin | ||
} | ||
|
||
// Send Client Authentication Packet | ||
authResp, err := mc.auth(authData, plugin) | ||
if err != nil { | ||
// try the default auth plugin, if using the requested plugin failed | ||
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error()) | ||
plugin = defaultAuthPlugin | ||
authResp, err = mc.auth(authData, plugin) | ||
if err != nil { | ||
mc.cleanup() | ||
return nil, err | ||
} | ||
} | ||
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil { | ||
mc.cleanup() | ||
return nil, err | ||
} | ||
|
||
// Handle response to auth packet, switch methods if possible | ||
if err = mc.handleAuthResult(authData, plugin); err != nil { | ||
// Authentication failed and MySQL has already closed the connection | ||
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html). | ||
// Do not send COM_QUIT, just cleanup and return the error. | ||
mc.cleanup() | ||
return nil, err | ||
} | ||
|
||
if mc.cfg.MaxAllowedPacket > 0 { | ||
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket | ||
} else { | ||
// Get max allowed packet size | ||
maxap, err := mc.getSystemVar("max_allowed_packet") | ||
if err != nil { | ||
mc.Close() | ||
return nil, err | ||
} | ||
mc.maxAllowedPacket = stringToInt(maxap) - 1 | ||
} | ||
if mc.maxAllowedPacket < maxPacketSize { | ||
mc.maxWriteSize = mc.maxAllowedPacket | ||
} | ||
|
||
// Handle DSN Params | ||
err = mc.handleParams() | ||
if err != nil { | ||
mc.Close() | ||
return nil, err | ||
} | ||
|
||
return mc, nil | ||
} | ||
|
||
// Driver implements driver.Connector interface. | ||
// Driver returns &MySQLDriver{}. | ||
func (c *connector) Driver() driver.Driver { | ||
return &MySQLDriver{} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// 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/. | ||
|
||
// +build go1.10 | ||
|
||
package mysql | ||
|
||
import ( | ||
"crypto/rsa" | ||
"database/sql/driver" | ||
"math/big" | ||
) | ||
|
||
// NewConnector returns new driver.Connector. | ||
func NewConnector(cfg *Config) driver.Connector { | ||
copyCfg := *cfg | ||
copyCfg.tls = cfg.tls.Clone() | ||
copyCfg.Params = make(map[string]string, len(cfg.Params)) | ||
for k, v := range cfg.Params { | ||
copyCfg.Params[k] = v | ||
} | ||
if cfg.pubKey != nil { | ||
copyCfg.pubKey = &rsa.PublicKey{ | ||
N: new(big.Int).Set(cfg.pubKey.N), | ||
E: cfg.pubKey.E, | ||
} | ||
} | ||
return &connector{cfg: ©Cfg} | ||
} | ||
|
||
// OpenConnector implements driver.DriverContext. | ||
func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) { | ||
cfg, err := ParseDSN(dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &connector{ | ||
cfg: cfg, | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// 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/. | ||
|
||
// +build go1.10 | ||
|
||
package mysql | ||
|
||
import ( | ||
"database/sql/driver" | ||
) | ||
|
||
var _ driver.DriverContext = &MySQLDriver{} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.