errors.go 2.5 KB

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