gh-ost/vendor/github.com/go-sql-driver/mysql/driver.go

86 lines
2.6 KiB
Go
Raw Normal View History

2016-04-01 14:04:27 +00:00
// Copyright 2012 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/.
2019-05-16 10:29:40 +00:00
// Package mysql provides a MySQL driver for Go's database/sql package.
2016-04-01 14:04:27 +00:00
//
// The driver should be used via the database/sql package:
//
// import "database/sql"
// import _ "github.com/go-sql-driver/mysql"
//
// db, err := sql.Open("mysql", "user:password@/dbname")
//
// See https://github.com/go-sql-driver/mysql#usage for details
package mysql
import (
2019-05-16 10:29:40 +00:00
"context"
2016-04-01 14:04:27 +00:00
"database/sql"
"database/sql/driver"
"net"
2019-05-16 10:29:40 +00:00
"sync"
2016-04-01 14:04:27 +00:00
)
2019-05-16 10:29:40 +00:00
// MySQLDriver is exported to make the driver directly accessible.
2016-04-01 14:04:27 +00:00
// In general the driver is used via the database/sql package.
type MySQLDriver struct{}
// DialFunc is a function which can be used to establish the network connection.
// Custom dial functions must be registered with RegisterDial
2019-05-16 10:29:40 +00:00
//
// Deprecated: users should register a DialContextFunc instead
2016-04-01 14:04:27 +00:00
type DialFunc func(addr string) (net.Conn, error)
2019-05-16 10:29:40 +00:00
// DialContextFunc is a function which can be used to establish the network connection.
// Custom dial functions must be registered with RegisterDialContext
type DialContextFunc func(ctx context.Context, addr string) (net.Conn, error)
2016-04-01 14:04:27 +00:00
2019-05-16 10:29:40 +00:00
var (
dialsLock sync.RWMutex
dials map[string]DialContextFunc
)
// RegisterDialContext registers a custom dial function. It can then be used by the
2016-04-01 14:04:27 +00:00
// network address mynet(addr), where mynet is the registered new network.
2019-05-16 10:29:40 +00:00
// The current context for the connection and its address is passed to the dial function.
func RegisterDialContext(net string, dial DialContextFunc) {
dialsLock.Lock()
defer dialsLock.Unlock()
2016-04-01 14:04:27 +00:00
if dials == nil {
2019-05-16 10:29:40 +00:00
dials = make(map[string]DialContextFunc)
2016-04-01 14:04:27 +00:00
}
dials[net] = dial
}
2019-05-16 10:29:40 +00:00
// 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.
//
// Deprecated: users should call RegisterDialContext instead
func RegisterDial(network string, dial DialFunc) {
RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
return dial(addr)
})
}
2016-04-01 14:04:27 +00:00
// Open new Connection.
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
2019-05-16 10:29:40 +00:00
// the DSN string is formatted
2016-04-01 14:04:27 +00:00
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
2019-05-16 10:29:40 +00:00
cfg, err := ParseDSN(dsn)
2016-04-01 14:04:27 +00:00
if err != nil {
return nil, err
}
2019-05-16 10:29:40 +00:00
c := &connector{
cfg: cfg,
2016-04-01 14:04:27 +00:00
}
2019-05-16 10:29:40 +00:00
return c.Connect(context.Background())
2016-04-01 14:04:27 +00:00
}
func init() {
sql.Register("mysql", &MySQLDriver{})
}