errors.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 []string
  25. func (mw MySQLWarnings) Error() string {
  26. var msg string
  27. for i := range mw {
  28. if i > 0 {
  29. msg += "\r\n"
  30. }
  31. msg += mw[i]
  32. }
  33. return msg
  34. }
  35. func (mc *mysqlConn) getWarnings() (err error) {
  36. rows, err := mc.Query("SHOW WARNINGS", []driver.Value{})
  37. if err != nil {
  38. return
  39. }
  40. var warnings = MySQLWarnings{}
  41. var values = make([]driver.Value, 3)
  42. for {
  43. if err = rows.Next(values); err == nil {
  44. warnings = append(warnings,
  45. fmt.Sprintf("%s %s: %s", values[0], values[1], values[2]),
  46. )
  47. } else if err == io.EOF {
  48. return warnings
  49. } else {
  50. rows.Close()
  51. return
  52. }
  53. }
  54. return
  55. }