driver.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Package mysql provides a MySQL driver for Go's database/sql package.
  7. //
  8. // The driver should be used via the database/sql package:
  9. //
  10. // import "database/sql"
  11. // import _ "github.com/go-sql-driver/mysql"
  12. //
  13. // db, err := sql.Open("mysql", "user:password@/dbname")
  14. //
  15. // See https://github.com/go-sql-driver/mysql#usage for details
  16. package mysql
  17. import (
  18. "context"
  19. "database/sql"
  20. "database/sql/driver"
  21. "net"
  22. "sync"
  23. )
  24. // MySQLDriver is exported to make the driver directly accessible.
  25. // In general the driver is used via the database/sql package.
  26. type MySQLDriver struct{}
  27. // DialFunc is a function which can be used to establish the network connection.
  28. // Custom dial functions must be registered with RegisterDial
  29. //
  30. // Deprecated: users should register a DialContextFunc instead
  31. type DialFunc func(addr string) (net.Conn, error)
  32. // DialContextFunc is a function which can be used to establish the network connection.
  33. // Custom dial functions must be registered with RegisterDialContext
  34. type DialContextFunc func(ctx context.Context, addr string) (net.Conn, error)
  35. var (
  36. dialsLock sync.RWMutex
  37. dials map[string]DialContextFunc
  38. )
  39. // RegisterDialContext registers a custom dial function. It can then be used by the
  40. // network address mynet(addr), where mynet is the registered new network.
  41. // The current context for the connection and its address is passed to the dial function.
  42. func RegisterDialContext(net string, dial DialContextFunc) {
  43. dialsLock.Lock()
  44. defer dialsLock.Unlock()
  45. if dials == nil {
  46. dials = make(map[string]DialContextFunc)
  47. }
  48. dials[net] = dial
  49. }
  50. // DeregisterDialContext removes the custom dial function registered with the given net.
  51. func DeregisterDialContext(net string) {
  52. dialsLock.Lock()
  53. defer dialsLock.Unlock()
  54. if dials != nil {
  55. delete(dials, net)
  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. // Open new Connection.
  69. // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
  70. // the DSN string is formatted
  71. func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
  72. cfg, err := ParseDSN(dsn)
  73. if err != nil {
  74. return nil, err
  75. }
  76. c := newConnector(cfg)
  77. return c.Connect(context.Background())
  78. }
  79. // This variable can be replaced with -ldflags like below:
  80. // go build "-ldflags=-X github.com/go-sql-driver/mysql.driverName=custom"
  81. var driverName = "mysql"
  82. func init() {
  83. if driverName != "" {
  84. sql.Register(driverName, &MySQLDriver{})
  85. }
  86. }
  87. // NewConnector returns new driver.Connector.
  88. func NewConnector(cfg *Config) (driver.Connector, error) {
  89. cfg = cfg.Clone()
  90. // normalize the contents of cfg so calls to NewConnector have the same
  91. // behavior as MySQLDriver.OpenConnector
  92. if err := cfg.normalize(); err != nil {
  93. return nil, err
  94. }
  95. return newConnector(cfg), nil
  96. }
  97. // OpenConnector implements driver.DriverContext.
  98. func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
  99. cfg, err := ParseDSN(dsn)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return newConnector(cfg), nil
  104. }