forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/driver.go

180 lines
3.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
// Package mysql provides a MySQL driver for Go's database/sql package.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The driver should be used via the database/sql package:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// import "database/sql"
2024-06-14 08:41:36 +00:00
// import _ "github.com/go-sql-driver/mysql"
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// db, err := sql.Open("mysql", "user:password@/dbname")
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See https://github.com/go-sql-driver/mysql#usage for details
2024-02-18 10:42:21 +00:00
package mysql
import (
"context"
"database/sql"
"database/sql/driver"
"net"
"sync"
)
// MySQLDriver is exported to make the driver directly accessible.
2024-02-18 10:42:21 +00:00
// In general the driver is used via the database/sql package.
2024-02-18 10:42:21 +00:00
type MySQLDriver struct{}
// DialFunc is a function which can be used to establish the network connection.
2024-02-18 10:42:21 +00:00
// Custom dial functions must be registered with RegisterDial
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: users should register a DialContextFunc instead
2024-02-18 10:42:21 +00:00
type DialFunc func(addr string) (net.Conn, error)
// DialContextFunc is a function which can be used to establish the network connection.
2024-02-18 10:42:21 +00:00
// Custom dial functions must be registered with RegisterDialContext
2024-02-18 10:42:21 +00:00
type DialContextFunc func(ctx context.Context, addr string) (net.Conn, error)
var (
dialsLock sync.RWMutex
dials map[string]DialContextFunc
2024-02-18 10:42:21 +00:00
)
// RegisterDialContext registers a custom dial function. It can then be used by the
2024-02-18 10:42:21 +00:00
// network address mynet(addr), where mynet is the registered new network.
2024-02-18 10:42:21 +00:00
// The current context for the connection and its address is passed to the dial function.
2024-02-18 10:42:21 +00:00
func RegisterDialContext(net string, dial DialContextFunc) {
2024-02-18 10:42:21 +00:00
dialsLock.Lock()
2024-02-18 10:42:21 +00:00
defer dialsLock.Unlock()
2024-02-18 10:42:21 +00:00
if dials == nil {
2024-02-18 10:42:21 +00:00
dials = make(map[string]DialContextFunc)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dials[net] = dial
2024-02-18 10:42:21 +00:00
}
// RegisterDial registers a custom dial function. It can then be used by the
2024-02-18 10:42:21 +00:00
// network address mynet(addr), where mynet is the registered new network.
2024-02-18 10:42:21 +00:00
// addr is passed as a parameter to the dial function.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: users should call RegisterDialContext instead
2024-02-18 10:42:21 +00:00
func RegisterDial(network string, dial DialFunc) {
2024-02-18 10:42:21 +00:00
RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
2024-02-18 10:42:21 +00:00
return dial(addr)
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
// Open new Connection.
2024-02-18 10:42:21 +00:00
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
2024-02-18 10:42:21 +00:00
// the DSN string is formatted
2024-02-18 10:42:21 +00:00
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
2024-02-18 10:42:21 +00:00
cfg, err := ParseDSN(dsn)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c := &connector{
2024-02-18 10:42:21 +00:00
cfg: cfg,
}
2024-02-18 10:42:21 +00:00
return c.Connect(context.Background())
2024-02-18 10:42:21 +00:00
}
func init() {
2024-02-18 10:42:21 +00:00
sql.Register("mysql", &MySQLDriver{})
2024-02-18 10:42:21 +00:00
}
// NewConnector returns new driver.Connector.
2024-02-18 10:42:21 +00:00
func NewConnector(cfg *Config) (driver.Connector, error) {
2024-02-18 10:42:21 +00:00
cfg = cfg.Clone()
2024-02-18 10:42:21 +00:00
// normalize the contents of cfg so calls to NewConnector have the same
2024-02-18 10:42:21 +00:00
// behavior as MySQLDriver.OpenConnector
2024-02-18 10:42:21 +00:00
if err := cfg.normalize(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &connector{cfg: cfg}, nil
2024-02-18 10:42:21 +00:00
}
// OpenConnector implements driver.DriverContext.
2024-02-18 10:42:21 +00:00
func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
2024-02-18 10:42:21 +00:00
cfg, err := ParseDSN(dsn)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &connector{
2024-02-18 10:42:21 +00:00
cfg: cfg,
}, nil
2024-02-18 10:42:21 +00:00
}