PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/go-sql-driver/mysql/errors.go

https://gitlab.com/billyprice1/boulder
Go | 129 lines | 95 code | 18 blank | 16 comment | 14 complexity | dfddd9de3cb353f73e0790bf1c3ccc91 MD5 | raw file
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "database/sql/driver"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "log"
  15. "os"
  16. )
  17. // Various errors the driver might return. Can change between driver versions.
  18. var (
  19. ErrInvalidConn = errors.New("Invalid Connection")
  20. ErrMalformPkt = errors.New("Malformed Packet")
  21. ErrNoTLS = errors.New("TLS encryption requested but server does not support TLS")
  22. ErrOldPassword = errors.New("This server only supports the insecure old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
  23. ErrOldProtocol = errors.New("MySQL-Server does not support required Protocol 41+")
  24. ErrPktSync = errors.New("Commands out of sync. You can't run this command now")
  25. ErrPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
  26. ErrPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
  27. ErrBusyBuffer = errors.New("Busy buffer")
  28. )
  29. var errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
  30. // Logger is used to log critical error messages.
  31. type Logger interface {
  32. Print(v ...interface{})
  33. }
  34. // SetLogger is used to set the logger for critical errors.
  35. // The initial logger is os.Stderr.
  36. func SetLogger(logger Logger) error {
  37. if logger == nil {
  38. return errors.New("logger is nil")
  39. }
  40. errLog = logger
  41. return nil
  42. }
  43. // MySQLError is an error type which represents a single MySQL error
  44. type MySQLError struct {
  45. Number uint16
  46. Message string
  47. }
  48. func (me *MySQLError) Error() string {
  49. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  50. }
  51. // MySQLWarnings is an error type which represents a group of one or more MySQL
  52. // warnings
  53. type MySQLWarnings []MySQLWarning
  54. func (mws MySQLWarnings) Error() string {
  55. var msg string
  56. for i, warning := range mws {
  57. if i > 0 {
  58. msg += "\r\n"
  59. }
  60. msg += fmt.Sprintf(
  61. "%s %s: %s",
  62. warning.Level,
  63. warning.Code,
  64. warning.Message,
  65. )
  66. }
  67. return msg
  68. }
  69. // MySQLWarning is an error type which represents a single MySQL warning.
  70. // Warnings are returned in groups only. See MySQLWarnings
  71. type MySQLWarning struct {
  72. Level string
  73. Code string
  74. Message string
  75. }
  76. func (mc *mysqlConn) getWarnings() (err error) {
  77. rows, err := mc.Query("SHOW WARNINGS", nil)
  78. if err != nil {
  79. return
  80. }
  81. var warnings = MySQLWarnings{}
  82. var values = make([]driver.Value, 3)
  83. for {
  84. err = rows.Next(values)
  85. switch err {
  86. case nil:
  87. warning := MySQLWarning{}
  88. if raw, ok := values[0].([]byte); ok {
  89. warning.Level = string(raw)
  90. } else {
  91. warning.Level = fmt.Sprintf("%s", values[0])
  92. }
  93. if raw, ok := values[1].([]byte); ok {
  94. warning.Code = string(raw)
  95. } else {
  96. warning.Code = fmt.Sprintf("%s", values[1])
  97. }
  98. if raw, ok := values[2].([]byte); ok {
  99. warning.Message = string(raw)
  100. } else {
  101. warning.Message = fmt.Sprintf("%s", values[0])
  102. }
  103. warnings = append(warnings, warning)
  104. case io.EOF:
  105. return warnings
  106. default:
  107. rows.Close()
  108. return
  109. }
  110. }
  111. }