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.

132 lines
3.7 KiB

  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "database/sql/driver"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "log"
  15. "os"
  16. )
  17. // Various errors the driver might return. Can change between driver versions.
  18. var (
  19. ErrInvalidConn = errors.New("invalid connection")
  20. ErrMalformPkt = errors.New("malformed packet")
  21. ErrNoTLS = errors.New("TLS requested but server does not support TLS")
  22. ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
  23. ErrNativePassword = errors.New("this user requires mysql native password authentication.")
  24. ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
  25. ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
  26. ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
  27. ErrPktSync = errors.New("commands out of sync. You can't run this command now")
  28. ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
  29. ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
  30. ErrBusyBuffer = errors.New("busy buffer")
  31. )
  32. var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
  33. // Logger is used to log critical error messages.
  34. type Logger interface {
  35. Print(v ...interface{})
  36. }
  37. // SetLogger is used to set the logger for critical errors.
  38. // The initial logger is os.Stderr.
  39. func SetLogger(logger Logger) error {
  40. if logger == nil {
  41. return errors.New("logger is nil")
  42. }
  43. errLog = logger
  44. return nil
  45. }
  46. // MySQLError is an error type which represents a single MySQL error
  47. type MySQLError struct {
  48. Number uint16
  49. Message string
  50. }
  51. func (me *MySQLError) Error() string {
  52. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  53. }
  54. // MySQLWarnings is an error type which represents a group of one or more MySQL
  55. // warnings
  56. type MySQLWarnings []MySQLWarning
  57. func (mws MySQLWarnings) Error() string {
  58. var msg string
  59. for i, warning := range mws {
  60. if i > 0 {
  61. msg += "\r\n"
  62. }
  63. msg += fmt.Sprintf(
  64. "%s %s: %s",
  65. warning.Level,
  66. warning.Code,
  67. warning.Message,
  68. )
  69. }
  70. return msg
  71. }
  72. // MySQLWarning is an error type which represents a single MySQL warning.
  73. // Warnings are returned in groups only. See MySQLWarnings
  74. type MySQLWarning struct {
  75. Level string
  76. Code string
  77. Message string
  78. }
  79. func (mc *mysqlConn) getWarnings() (err error) {
  80. rows, err := mc.Query("SHOW WARNINGS", nil)
  81. if err != nil {
  82. return
  83. }
  84. var warnings = MySQLWarnings{}
  85. var values = make([]driver.Value, 3)
  86. for {
  87. err = rows.Next(values)
  88. switch err {
  89. case nil:
  90. warning := MySQLWarning{}
  91. if raw, ok := values[0].([]byte); ok {
  92. warning.Level = string(raw)
  93. } else {
  94. warning.Level = fmt.Sprintf("%s", values[0])
  95. }
  96. if raw, ok := values[1].([]byte); ok {
  97. warning.Code = string(raw)
  98. } else {
  99. warning.Code = fmt.Sprintf("%s", values[1])
  100. }
  101. if raw, ok := values[2].([]byte); ok {
  102. warning.Message = string(raw)
  103. } else {
  104. warning.Message = fmt.Sprintf("%s", values[0])
  105. }
  106. warnings = append(warnings, warning)
  107. case io.EOF:
  108. return warnings
  109. default:
  110. rows.Close()
  111. return
  112. }
  113. }
  114. }