From 2a5b4d6ca2679be6a388cb2ef927708b567b044e Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sat, 31 May 2014 11:08:43 +0200 Subject: [PATCH 1/2] small errors refactoring --- errors.go | 21 +++++++++------------ errors_test.go | 8 +++++++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/errors.go b/errors.go index c79c606fc..ea5634d9c 100644 --- a/errors.go +++ b/errors.go @@ -17,6 +17,7 @@ import ( "os" ) +// Various errors the driver might return. Can change between driver versions. var ( ErrInvalidConn = errors.New("Invalid Connection") ErrMalformPkt = errors.New("Malformed Packet") @@ -27,17 +28,17 @@ var ( ErrPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?") ErrPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.") ErrBusyBuffer = errors.New("Busy buffer") - - errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile) ) +var errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile) + // Logger is used to log critical error messages. type Logger interface { Print(v ...interface{}) } // SetLogger is used to set the logger for critical errors. -// The initial logger is stderr. +// The initial logger is os.Stderr. func SetLogger(logger Logger) error { if logger == nil { return errors.New("logger is nil") @@ -85,7 +86,7 @@ type MysqlWarning struct { } func (mc *mysqlConn) getWarnings() (err error) { - rows, err := mc.Query("SHOW WARNINGS", []driver.Value{}) + rows, err := mc.Query("SHOW WARNINGS", nil) if err != nil { return } @@ -93,27 +94,23 @@ func (mc *mysqlConn) getWarnings() (err error) { var warnings = MySQLWarnings{} var values = make([]driver.Value, 3) - var warning MysqlWarning - var raw []byte - var ok bool - for { err = rows.Next(values) switch err { case nil: - warning = MysqlWarning{} + warning := MysqlWarning{} - if raw, ok = values[0].([]byte); ok { + if raw, ok := values[0].([]byte); ok { warning.Level = string(raw) } else { warning.Level = fmt.Sprintf("%s", values[0]) } - if raw, ok = values[1].([]byte); ok { + if raw, ok := values[1].([]byte); ok { warning.Code = string(raw) } else { warning.Code = fmt.Sprintf("%s", values[1]) } - if raw, ok = values[2].([]byte); ok { + if raw, ok := values[2].([]byte); ok { warning.Message = string(raw) } else { warning.Message = fmt.Sprintf("%s", values[0]) diff --git a/errors_test.go b/errors_test.go index b83d38211..117450e84 100644 --- a/errors_test.go +++ b/errors_test.go @@ -14,16 +14,22 @@ import ( "testing" ) -func TestSetLogger(t *testing.T) { +func TestErrorsSetLogger(t *testing.T) { previous := errLog defer func() { errLog = previous }() + + // set up logger const expected = "prefix: test\n" buffer := bytes.NewBuffer(make([]byte, 0, 64)) logger := log.New(buffer, "prefix: ", 0) + + // print SetLogger(logger) errLog.Print("test") + + // check result if actual := buffer.String(); actual != expected { t.Errorf("expected %q, got %q", expected, actual) } From 6b302cc76eab96500d58e6cebe271887e0234229 Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sun, 1 Jun 2014 18:17:57 +0200 Subject: [PATCH 2/2] Rename MysqlWarning to MySQLWarning for consistency --- errors.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index ea5634d9c..97d7b3996 100644 --- a/errors.go +++ b/errors.go @@ -59,7 +59,7 @@ func (me *MySQLError) Error() string { // MySQLWarnings is an error type which represents a group of one or more MySQL // warnings -type MySQLWarnings []MysqlWarning +type MySQLWarnings []MySQLWarning func (mws MySQLWarnings) Error() string { var msg string @@ -77,9 +77,9 @@ func (mws MySQLWarnings) Error() string { return msg } -// MysqlWarning is an error type which represents a single MySQL warning. +// MySQLWarning is an error type which represents a single MySQL warning. // Warnings are returned in groups only. See MySQLWarnings -type MysqlWarning struct { +type MySQLWarning struct { Level string Code string Message string @@ -98,7 +98,7 @@ func (mc *mysqlConn) getWarnings() (err error) { err = rows.Next(values) switch err { case nil: - warning := MysqlWarning{} + warning := MySQLWarning{} if raw, ok := values[0].([]byte); ok { warning.Level = string(raw)