driver.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // Go MySQL Driver - 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. "database/sql"
  19. "database/sql/driver"
  20. "net"
  21. )
  22. // This struct is exported to make the driver directly accessible.
  23. // In general the driver is used via the database/sql package.
  24. type MySQLDriver struct{}
  25. // DialFunc is a function which can be used to establish the network connection.
  26. // Custom dial functions must be registered with RegisterDial
  27. type DialFunc func(addr string) (net.Conn, error)
  28. var dials map[string]DialFunc
  29. // RegisterDial registers a custom dial function. It can then be used by the
  30. // network address mynet(addr), where mynet is the registered new network.
  31. // addr is passed as a parameter to the dial function.
  32. func RegisterDial(net string, dial DialFunc) {
  33. if dials == nil {
  34. dials = make(map[string]DialFunc)
  35. }
  36. dials[net] = dial
  37. }
  38. // Open new Connection.
  39. // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
  40. // the DSN string is formated
  41. func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
  42. var err error
  43. // New mysqlConn
  44. mc := &mysqlConn{
  45. maxPacketAllowed: maxPacketSize,
  46. maxWriteSize: maxPacketSize - 1,
  47. }
  48. mc.cfg, err = parseDSN(dsn)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Connect to Server
  53. if dial, ok := dials[mc.cfg.net]; ok {
  54. mc.netConn, err = dial(mc.cfg.addr)
  55. } else {
  56. nd := net.Dialer{Timeout: mc.cfg.timeout}
  57. mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. // Enable TCP Keepalives on TCP connections
  63. if tc, ok := mc.netConn.(*net.TCPConn); ok {
  64. if err := tc.SetKeepAlive(true); err != nil {
  65. mc.Close()
  66. return nil, err
  67. }
  68. }
  69. mc.buf = newBuffer(mc.netConn)
  70. // Reading Handshake Initialization Packet
  71. cipher, err := mc.readInitPacket()
  72. if err != nil {
  73. mc.Close()
  74. return nil, err
  75. }
  76. // Send Client Authentication Packet
  77. if err = mc.writeAuthPacket(cipher); err != nil {
  78. mc.Close()
  79. return nil, err
  80. }
  81. // Read Result Packet
  82. err = mc.readResultOK()
  83. if err != nil {
  84. // Retry with old authentication method, if allowed
  85. if mc.cfg != nil && mc.cfg.allowOldPasswords && err == ErrOldPassword {
  86. if err = mc.writeOldAuthPacket(cipher); err != nil {
  87. mc.Close()
  88. return nil, err
  89. }
  90. if err = mc.readResultOK(); err != nil {
  91. mc.Close()
  92. return nil, err
  93. }
  94. } else {
  95. mc.Close()
  96. return nil, err
  97. }
  98. }
  99. // Get max allowed packet size
  100. maxap, err := mc.getSystemVar("max_allowed_packet")
  101. if err != nil {
  102. mc.Close()
  103. return nil, err
  104. }
  105. mc.maxPacketAllowed = stringToInt(maxap) - 1
  106. if mc.maxPacketAllowed < maxPacketSize {
  107. mc.maxWriteSize = mc.maxPacketAllowed
  108. }
  109. // Handle DSN Params
  110. err = mc.handleParams()
  111. if err != nil {
  112. mc.Close()
  113. return nil, err
  114. }
  115. return mc, nil
  116. }
  117. func init() {
  118. sql.Register("mysql", &MySQLDriver{})
  119. }