errors.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var (
  18. ErrInvalidConn = errors.New("Invalid Connection")
  19. ErrMalformPkt = errors.New("Malformed Packet")
  20. ErrNoTLS = errors.New("TLS encryption requested but server does not support TLS")
  21. ErrOldPassword = errors.New("This server only supports the insecure 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")
  22. ErrOldProtocol = errors.New("MySQL-Server does not support required Protocol 41+")
  23. ErrPktSync = errors.New("Commands out of sync. You can't run this command now")
  24. ErrPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
  25. ErrPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
  26. ErrBusyBuffer = errors.New("Busy buffer")
  27. errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
  28. )
  29. // Logger is used to log critical error messages.
  30. type Logger interface {
  31. Print(v ...interface{})
  32. }
  33. // SetLogger is used to set the logger for critical errors.
  34. // The initial logger is stderr.
  35. func SetLogger(logger Logger) error {
  36. if logger == nil {
  37. return errors.New("logger is nil")
  38. }
  39. errLog = logger
  40. return nil
  41. }
  42. // MySQLError is an error type which represents a single MySQL error
  43. type MySQLError struct {
  44. Number uint16
  45. Message string
  46. }
  47. func (me *MySQLError) Error() string {
  48. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  49. }
  50. // MySQLWarnings is an error type which represents a group of one or more MySQL
  51. // warnings
  52. type MySQLWarnings []MysqlWarning
  53. func (mws MySQLWarnings) Error() string {
  54. var msg string
  55. for i, warning := range mws {
  56. if i > 0 {
  57. msg += "\r\n"
  58. }
  59. msg += fmt.Sprintf(
  60. "%s %s: %s",
  61. warning.Level,
  62. warning.Code,
  63. warning.Message,
  64. )
  65. }
  66. return msg
  67. }
  68. // MysqlWarning is an error type which represents a single MySQL warning.
  69. // Warnings are returned in groups only. See MySQLWarnings
  70. type MysqlWarning struct {
  71. Level string
  72. Code string
  73. Message string
  74. }
  75. func (mc *mysqlConn) getWarnings() (err error) {
  76. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  77. if err != nil {
  78. return
  79. }
  80. var warnings = MySQLWarnings{}
  81. var values = make([]driver.Value, 3)
  82. var warning MysqlWarning
  83. var raw []byte
  84. var ok bool
  85. for {
  86. err = rows.Next(values)
  87. switch err {
  88. case nil:
  89. warning = MysqlWarning{}
  90. if raw, ok = values[0].([]byte); ok {
  91. warning.Level = string(raw)
  92. } else {
  93. warning.Level = fmt.Sprintf("%s", values[0])
  94. }
  95. if raw, ok = values[1].([]byte); ok {
  96. warning.Code = string(raw)
  97. } else {
  98. warning.Code = fmt.Sprintf("%s", values[1])
  99. }
  100. if raw, ok = values[2].([]byte); ok {
  101. warning.Message = string(raw)
  102. } else {
  103. warning.Message = fmt.Sprintf("%s", values[0])
  104. }
  105. warnings = append(warnings, warning)
  106. case io.EOF:
  107. return warnings
  108. default:
  109. rows.Close()
  110. return
  111. }
  112. }
  113. }