errors.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. )
  15. var (
  16. errInvalidConn = errors.New("Invalid Connection")
  17. errMalformPkt = errors.New("Malformed Packet")
  18. errNoTLS = errors.New("TLS encryption requested but server does not support TLS")
  19. 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")
  20. errOldProtocol = errors.New("MySQL-Server does not support required Protocol 41+")
  21. errPktSync = errors.New("Commands out of sync. You can't run this command now")
  22. errPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
  23. errPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
  24. )
  25. // error type which represents a single MySQL error
  26. type MySQLError struct {
  27. Number uint16
  28. Message string
  29. }
  30. func (me *MySQLError) Error() string {
  31. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  32. }
  33. // error type which represents a group of one or more MySQL warnings
  34. type MySQLWarnings []mysqlWarning
  35. func (mws MySQLWarnings) Error() string {
  36. var msg string
  37. for i, warning := range mws {
  38. if i > 0 {
  39. msg += "\r\n"
  40. }
  41. msg += fmt.Sprintf("%s %s: %s", warning.Level, warning.Code, warning.Message)
  42. }
  43. return msg
  44. }
  45. // error type which represents a single MySQL warning
  46. type mysqlWarning struct {
  47. Level string
  48. Code string
  49. Message string
  50. }
  51. func (mc *mysqlConn) getWarnings() (err error) {
  52. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  53. if err != nil {
  54. return
  55. }
  56. var warnings = MySQLWarnings{}
  57. var values = make([]driver.Value, 3)
  58. var warning mysqlWarning
  59. var raw []byte
  60. var ok bool
  61. for {
  62. err = rows.Next(values)
  63. switch err {
  64. case nil:
  65. warning = mysqlWarning{}
  66. if raw, ok = values[0].([]byte); ok {
  67. warning.Level = string(raw)
  68. } else {
  69. warning.Level = fmt.Sprintf("%s", values[0])
  70. }
  71. if raw, ok = values[1].([]byte); ok {
  72. warning.Code = string(raw)
  73. } else {
  74. warning.Code = fmt.Sprintf("%s", values[1])
  75. }
  76. if raw, ok = values[2].([]byte); ok {
  77. warning.Message = string(raw)
  78. } else {
  79. warning.Message = fmt.Sprintf("%s", values[0])
  80. }
  81. warnings = append(warnings, warning)
  82. case io.EOF:
  83. return warnings
  84. default:
  85. rows.Close()
  86. return
  87. }
  88. }
  89. }