errors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // MySQLError is an 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. // MySQLWarnings is an error type which represents a group of one or more MySQL
  34. // warnings
  35. type MySQLWarnings []MysqlWarning
  36. func (mws MySQLWarnings) Error() string {
  37. var msg string
  38. for i, warning := range mws {
  39. if i > 0 {
  40. msg += "\r\n"
  41. }
  42. msg += fmt.Sprintf(
  43. "%s %s: %s",
  44. warning.Level,
  45. warning.Code,
  46. warning.Message,
  47. )
  48. }
  49. return msg
  50. }
  51. // MysqlWarning is an error type which represents a single MySQL warning.
  52. // Warnings are returned in groups only. See MySQLWarnings
  53. type MysqlWarning struct {
  54. Level string
  55. Code string
  56. Message string
  57. }
  58. func (mc *mysqlConn) getWarnings() (err error) {
  59. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  60. if err != nil {
  61. return
  62. }
  63. var warnings = MySQLWarnings{}
  64. var values = make([]driver.Value, 3)
  65. var warning MysqlWarning
  66. var raw []byte
  67. var ok bool
  68. for {
  69. err = rows.Next(values)
  70. switch err {
  71. case nil:
  72. warning = MysqlWarning{}
  73. if raw, ok = values[0].([]byte); ok {
  74. warning.Level = string(raw)
  75. } else {
  76. warning.Level = fmt.Sprintf("%s", values[0])
  77. }
  78. if raw, ok = values[1].([]byte); ok {
  79. warning.Code = string(raw)
  80. } else {
  81. warning.Code = fmt.Sprintf("%s", values[1])
  82. }
  83. if raw, ok = values[2].([]byte); ok {
  84. warning.Message = string(raw)
  85. } else {
  86. warning.Message = fmt.Sprintf("%s", values[0])
  87. }
  88. warnings = append(warnings, warning)
  89. case io.EOF:
  90. return warnings
  91. default:
  92. rows.Close()
  93. return
  94. }
  95. }
  96. }