errors.go 2.7 KB

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