github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/go-sql-driver/mysql/driver.go (about)

     1  // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  // You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7  // Package mysql provides a MySQL driver for Go's database/sql package.
     8  //
     9  // The driver should be used via the database/sql package:
    10  //
    11  //  import "database/sql"
    12  //  import _ "github.com/go-sql-driver/mysql"
    13  //
    14  //  db, err := sql.Open("mysql", "user:password@/dbname")
    15  //
    16  // See https://github.com/go-sql-driver/mysql#usage for details
    17  package mysql
    18  
    19  import (
    20  	"context"
    21  	"database/sql"
    22  	"database/sql/driver"
    23  	"net"
    24  	"sync"
    25  )
    26  
    27  // MySQLDriver is exported to make the driver directly accessible.
    28  // In general the driver is used via the database/sql package.
    29  type MySQLDriver struct{}
    30  
    31  // DialFunc is a function which can be used to establish the network connection.
    32  // Custom dial functions must be registered with RegisterDial
    33  //
    34  // Deprecated: users should register a DialContextFunc instead
    35  type DialFunc func(addr string) (net.Conn, error)
    36  
    37  // DialContextFunc is a function which can be used to establish the network connection.
    38  // Custom dial functions must be registered with RegisterDialContext
    39  type DialContextFunc func(ctx context.Context, addr string) (net.Conn, error)
    40  
    41  var (
    42  	dialsLock sync.RWMutex
    43  	dials     map[string]DialContextFunc
    44  )
    45  
    46  // RegisterDialContext registers a custom dial function. It can then be used by the
    47  // network address mynet(addr), where mynet is the registered new network.
    48  // The current context for the connection and its address is passed to the dial function.
    49  func RegisterDialContext(net string, dial DialContextFunc) {
    50  	dialsLock.Lock()
    51  	defer dialsLock.Unlock()
    52  	if dials == nil {
    53  		dials = make(map[string]DialContextFunc)
    54  	}
    55  	dials[net] = dial
    56  }
    57  
    58  // RegisterDial registers a custom dial function. It can then be used by the
    59  // network address mynet(addr), where mynet is the registered new network.
    60  // addr is passed as a parameter to the dial function.
    61  //
    62  // Deprecated: users should call RegisterDialContext instead
    63  func RegisterDial(network string, dial DialFunc) {
    64  	RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
    65  		return dial(addr)
    66  	})
    67  }
    68  
    69  // Open new Connection.
    70  // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
    71  // the DSN string is formatted
    72  func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
    73  	cfg, err := ParseDSN(dsn)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	c := &connector{
    78  		cfg: cfg,
    79  	}
    80  	return c.Connect(context.Background())
    81  }
    82  
    83  func init() {
    84  	sql.Register("mysql", &MySQLDriver{})
    85  }