errors.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 Julien Schmidt. All rights reserved.
  4. // http://www.julienschmidt.com
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public
  7. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  8. // You can obtain one at http://mozilla.org/MPL/2.0/.
  9. package mysql
  10. import (
  11. "database/sql/driver"
  12. "errors"
  13. "fmt"
  14. "io"
  15. )
  16. var (
  17. errMalformPkt = errors.New("Malformed Packet")
  18. errPktSync = errors.New("Commands out of sync. You can't run this command now")
  19. errPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
  20. errOldPassword = errors.New("It seems like you are using old_passwords, which is unsupported. See https://github.com/go-sql-driver/mysql/wiki/old_passwords")
  21. errPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
  22. )
  23. // error type which represents a single MySQL error
  24. type MySQLError struct {
  25. Number uint16
  26. Message string
  27. }
  28. func (me *MySQLError) Error() string {
  29. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  30. }
  31. // error type which represents a group of one or more MySQL warnings
  32. type MySQLWarnings []mysqlWarning
  33. func (mws MySQLWarnings) Error() string {
  34. var msg string
  35. for i, warning := range mws {
  36. if i > 0 {
  37. msg += "\r\n"
  38. }
  39. msg += fmt.Sprintf("%s %s: %s", warning.Level, warning.Code, warning.Message)
  40. }
  41. return msg
  42. }
  43. // error type which represents a single MySQL warning
  44. type mysqlWarning struct {
  45. Level string
  46. Code string
  47. Message string
  48. }
  49. func (mc *mysqlConn) getWarnings() (err error) {
  50. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  51. if err != nil {
  52. return
  53. }
  54. var warnings = MySQLWarnings{}
  55. var values = make([]driver.Value, 3)
  56. var warning mysqlWarning
  57. var raw []byte
  58. var ok bool
  59. for {
  60. err = rows.Next(values)
  61. switch err {
  62. case nil:
  63. warning = mysqlWarning{}
  64. if raw, ok = values[0].([]byte); ok {
  65. warning.Level = string(raw)
  66. } else {
  67. warning.Level = fmt.Sprintf("%s", values[0])
  68. }
  69. if raw, ok = values[1].([]byte); ok {
  70. warning.Code = string(raw)
  71. } else {
  72. warning.Code = fmt.Sprintf("%s", values[1])
  73. }
  74. if raw, ok = values[2].([]byte); ok {
  75. warning.Message = string(raw)
  76. } else {
  77. warning.Message = fmt.Sprintf("%s", values[0])
  78. }
  79. warnings = append(warnings, warning)
  80. case io.EOF:
  81. return warnings
  82. default:
  83. rows.Close()
  84. return
  85. }
  86. }
  87. return
  88. }