MySQL Drive 驱动代码- GO 语言- 立哥开发

该代码展示了如何创建一个自定义的MySQL驱动程序,包括使用`DialFunc`和`DialContextFunc`来建立网络连接,并通过`RegisterDialContext`进行注册。驱动程序实现了`database/sql`包所需的接口,如`Open`和`OpenConnector`,用于建立数据库连接。此外,还提供了初始化函数用于在`sql`包中注册驱动。
摘要由CSDN通过智能技术生成

// Copyright  2020 Jacky Zong. All rights reserved.
package mysql

import (
    "context"
    "database/sql"
    "database/sql/driver"
    "net"
    "sync"
)

// MySQLDriver is exported to make the driver directly accessible.
// 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
//
// Deprecated: users should register a DialContextFunc instead
type DialFunc func(addr string) (net.Conn, error)

// 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)

var (
    dialsLock sync.RWMutex
    dials     map[string]DialContextFunc
)

// RegisterDialContext registers a custom dial function. It can then be used by the
// network address mynet(addr), where mynet is the registered new network.
// 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()
    if dials == nil {
        dials = make(map[string]DialContextFunc)
    }
    dials[net] = dial
}

// 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)
    })
}

// Open new Connection.
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
// the DSN string is formatted
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
    cfg, err := ParseDSN(dsn)
    if err != nil {
        return nil, err
    }
    c := &connector{
        cfg: cfg,
    }
    return c.Connect(context.Background())
}

func init() {
    sql.Register("mysql", &MySQLDriver{})
}

// NewConnector returns new driver.Connector.
func NewConnector(cfg *Config) (driver.Connector, error) {
    cfg = cfg.Clone()
    // normalize the contents of cfg so calls to NewConnector have the same
    // behavior as MySQLDriver.OpenConnector
    if err := cfg.normalize(); err != nil {
        return nil, err
    }
    return &connector{cfg: cfg}, nil
}

// 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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值