You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // RegisterDial registers a custom dial function. It can then be used by the
  51. // network address mynet(addr), where mynet is the registered new network.
  52. // addr is passed as a parameter to the dial function.
  53. //
  54. // Deprecated: users should call RegisterDialContext instead
  55. func RegisterDial(network string, dial DialFunc) {
  56. RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
  57. return dial(addr)
  58. })
  59. }
  60. // Open new Connection.
  61. // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
  62. // the DSN string is formatted
  63. func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
  64. cfg, err := ParseDSN(dsn)
  65. if err != nil {
  66. return nil, err
  67. }
  68. c := &connector{
  69. cfg: cfg,
  70. }
  71. return c.Connect(context.Background())
  72. }
  73. func init() {
  74. sql.Register("mysql", &MySQLDriver{})
  75. }
  76. // NewConnector returns new driver.Connector.
  77. func NewConnector(cfg *Config) (driver.Connector, error) {
  78. cfg = cfg.Clone()
  79. // normalize the contents of cfg so calls to NewConnector have the same
  80. // behavior as MySQLDriver.OpenConnector
  81. if err := cfg.normalize(); err != nil {
  82. return nil, err
  83. }
  84. return &connector{cfg: cfg}, nil
  85. }
  86. // OpenConnector implements driver.DriverContext.
  87. func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
  88. cfg, err := ParseDSN(dsn)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return &connector{
  93. cfg: cfg,
  94. }, nil
  95. }