errors.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 one or more MySQL warnings
  24. type MySQLWarnings []MySQLWarning
  25. func (mws MySQLWarnings) Error() string {
  26. var msg string
  27. for i := range mws {
  28. if i > 0 {
  29. msg += "\r\n"
  30. }
  31. msg += mws[i].Error()
  32. }
  33. return msg
  34. }
  35. // error type which represents a single MySQL warning
  36. type MySQLWarning struct {
  37. Level string
  38. Code string
  39. Message string
  40. }
  41. func (mw MySQLWarning) Error() string {
  42. return fmt.Sprintf("%s %s: %s", mw.Level, mw.Code, mw.Message)
  43. }
  44. func (mc *mysqlConn) getWarnings() (err error) {
  45. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  46. if err != nil {
  47. return
  48. }
  49. var warnings = MySQLWarnings{}
  50. var values = make([]driver.Value, 3)
  51. var warning MySQLWarning
  52. var raw []byte
  53. var ok bool
  54. for {
  55. err = rows.Next(values)
  56. switch err {
  57. case nil:
  58. warning = MySQLWarning{}
  59. if raw, ok = values[0].([]byte); ok {
  60. warning.Level = string(raw)
  61. } else {
  62. warning.Level = fmt.Sprintf("%s", values[0])
  63. }
  64. if raw, ok = values[1].([]byte); ok {
  65. warning.Code = string(raw)
  66. } else {
  67. warning.Code = fmt.Sprintf("%s", values[1])
  68. }
  69. if raw, ok = values[2].([]byte); ok {
  70. warning.Message = string(raw)
  71. } else {
  72. warning.Message = fmt.Sprintf("%s", values[0])
  73. }
  74. warnings = append(warnings, warning)
  75. case io.EOF:
  76. return warnings
  77. default:
  78. rows.Close()
  79. return
  80. }
  81. }
  82. return
  83. }