driver.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }