github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-sql-driver/mysql/errors.go (about)

     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  
     9  package mysql
    10  
    11  import (
    12  	"database/sql/driver"
    13  	"errors"
    14  	"fmt"
    15  	"io"
    16  	"log"
    17  	"os"
    18  )
    19  
    20  // Various errors the driver might return. Can change between driver versions.
    21  var (
    22  	ErrInvalidConn       = errors.New("invalid connection")
    23  	ErrMalformPkt        = errors.New("malformed packet")
    24  	ErrNoTLS             = errors.New("TLS requested but server does not support TLS")
    25  	ErrOldPassword       = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://yougam/libraries/go-sql-driver/mysql/wiki/old_passwords")
    26  	ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
    27  	ErrUnknownPlugin     = errors.New("this authentication plugin is not supported")
    28  	ErrOldProtocol       = errors.New("MySQL server does not support required protocol 41+")
    29  	ErrPktSync           = errors.New("commands out of sync. You can't run this command now")
    30  	ErrPktSyncMul        = errors.New("commands out of sync. Did you run multiple statements at once?")
    31  	ErrPktTooLarge       = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
    32  	ErrBusyBuffer        = errors.New("busy buffer")
    33  )
    34  
    35  var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
    36  
    37  // Logger is used to log critical error messages.
    38  type Logger interface {
    39  	Print(v ...interface{})
    40  }
    41  
    42  // SetLogger is used to set the logger for critical errors.
    43  // The initial logger is os.Stderr.
    44  func SetLogger(logger Logger) error {
    45  	if logger == nil {
    46  		return errors.New("logger is nil")
    47  	}
    48  	errLog = logger
    49  	return nil
    50  }
    51  
    52  // MySQLError is an error type which represents a single MySQL error
    53  type MySQLError struct {
    54  	Number  uint16
    55  	Message string
    56  }
    57  
    58  func (me *MySQLError) Error() string {
    59  	return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
    60  }
    61  
    62  // MySQLWarnings is an error type which represents a group of one or more MySQL
    63  // warnings
    64  type MySQLWarnings []MySQLWarning
    65  
    66  func (mws MySQLWarnings) Error() string {
    67  	var msg string
    68  	for i, warning := range mws {
    69  		if i > 0 {
    70  			msg += "\r\n"
    71  		}
    72  		msg += fmt.Sprintf(
    73  			"%s %s: %s",
    74  			warning.Level,
    75  			warning.Code,
    76  			warning.Message,
    77  		)
    78  	}
    79  	return msg
    80  }
    81  
    82  // MySQLWarning is an error type which represents a single MySQL warning.
    83  // Warnings are returned in groups only. See MySQLWarnings
    84  type MySQLWarning struct {
    85  	Level   string
    86  	Code    string
    87  	Message string
    88  }
    89  
    90  func (mc *mysqlConn) getWarnings() (err error) {
    91  	rows, err := mc.Query("SHOW WARNINGS", nil)
    92  	if err != nil {
    93  		return
    94  	}
    95  
    96  	var warnings = MySQLWarnings{}
    97  	var values = make([]driver.Value, 3)
    98  
    99  	for {
   100  		err = rows.Next(values)
   101  		switch err {
   102  		case nil:
   103  			warning := MySQLWarning{}
   104  
   105  			if raw, ok := values[0].([]byte); ok {
   106  				warning.Level = string(raw)
   107  			} else {
   108  				warning.Level = fmt.Sprintf("%s", values[0])
   109  			}
   110  			if raw, ok := values[1].([]byte); ok {
   111  				warning.Code = string(raw)
   112  			} else {
   113  				warning.Code = fmt.Sprintf("%s", values[1])
   114  			}
   115  			if raw, ok := values[2].([]byte); ok {
   116  				warning.Message = string(raw)
   117  			} else {
   118  				warning.Message = fmt.Sprintf("%s", values[0])
   119  			}
   120  
   121  			warnings = append(warnings, warning)
   122  
   123  		case io.EOF:
   124  			return warnings
   125  
   126  		default:
   127  			rows.Close()
   128  			return
   129  		}
   130  	}
   131  }