From f0d8a54f49c091db8f10efef4825681f79e2f856 Mon Sep 17 00:00:00 2001 From: Zach Moazeni Date: Sun, 4 Aug 2019 21:26:33 -0400 Subject: [PATCH] fix returned error values in defer functions This is copied from https://github.com/github/orchestrator/pull/946 with a slight tweak to the an indent. Originates from the PR https://github.com/github/orchestrator/issues/943 that applies to this codebase too. When we define the variable and attempt to set it in a defer function, the value we're returning isn't what we expect. Instead we can name the return values as variables and set that instead. --- .../outbrain/golib/sqlutils/sqlutils.go | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/vendor/github.com/outbrain/golib/sqlutils/sqlutils.go b/vendor/github.com/outbrain/golib/sqlutils/sqlutils.go index 8d9869078..9cfe0311b 100644 --- a/vendor/github.com/outbrain/golib/sqlutils/sqlutils.go +++ b/vendor/github.com/outbrain/golib/sqlutils/sqlutils.go @@ -21,11 +21,12 @@ import ( "encoding/json" "errors" "fmt" - _ "github.com/go-sql-driver/mysql" - "github.com/outbrain/golib/log" "strconv" "strings" "sync" + + _ "github.com/go-sql-driver/mysql" + "github.com/outbrain/golib/log" ) // RowMap represents one row in a result set. Its objective is to allow @@ -195,34 +196,33 @@ func ScanRowsToMaps(rows *sql.Rows, on_row func(RowMap) error) error { // QueryRowsMap is a convenience function allowing querying a result set while poviding a callback // function activated per read row. -func QueryRowsMap(db *sql.DB, query string, on_row func(RowMap) error, args ...interface{}) error { - var err error +func QueryRowsMap(db *sql.DB, query string, on_row func(RowMap) error, args ...interface{}) (err error) { defer func() { if derr := recover(); derr != nil { err = errors.New(fmt.Sprintf("QueryRowsMap unexpected error: %+v", derr)) } }() - rows, err := db.Query(query, args...) + var rows *sql.Rows + rows, err = db.Query(query, args...) defer rows.Close() if err != nil && err != sql.ErrNoRows { return log.Errore(err) } err = ScanRowsToMaps(rows, on_row) - return err + return } // queryResultData returns a raw array of rows for a given query, optionally reading and returning column names -func queryResultData(db *sql.DB, query string, retrieveColumns bool, args ...interface{}) (ResultData, []string, error) { - var err error +func queryResultData(db *sql.DB, query string, retrieveColumns bool, args ...interface{}) (resultData ResultData, columns []string, err error) { defer func() { if derr := recover(); derr != nil { err = errors.New(fmt.Sprintf("QueryRowsMap unexpected error: %+v", derr)) } }() - columns := []string{} - rows, err := db.Query(query, args...) + var rows *sql.Rows + rows, err = db.Query(query, args...) defer rows.Close() if err != nil && err != sql.ErrNoRows { return EmptyResultData, columns, log.Errore(err) @@ -231,7 +231,7 @@ func queryResultData(db *sql.DB, query string, retrieveColumns bool, args ...int // Don't pay if you don't want to columns, _ = rows.Columns() } - resultData := ResultData{} + resultData = ResultData{} err = ScanRowsToArrays(rows, func(rowData []CellData) error { resultData = append(resultData, rowData) return nil @@ -269,15 +269,13 @@ func QueryRowsMapBuffered(db *sql.DB, query string, on_row func(RowMap) error, a } // ExecNoPrepare executes given query using given args on given DB, without using prepared statements. -func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (sql.Result, error) { - var err error +func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (res sql.Result, err error) { defer func() { if derr := recover(); derr != nil { err = errors.New(fmt.Sprintf("ExecNoPrepare unexpected error: %+v", derr)) } }() - var res sql.Result res, err = db.Exec(query, args...) if err != nil { log.Errore(err) @@ -287,20 +285,18 @@ func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (sql.Result, e // ExecQuery executes given query using given args on given DB. It will safele prepare, execute and close // the statement. -func execInternal(silent bool, db *sql.DB, query string, args ...interface{}) (sql.Result, error) { - var err error +func execInternal(silent bool, db *sql.DB, query string, args ...interface{}) (res sql.Result, err error) { defer func() { if derr := recover(); derr != nil { err = errors.New(fmt.Sprintf("execInternal unexpected error: %+v", derr)) } }() - - stmt, err := db.Prepare(query) + var stmt *sql.Stmt + stmt, err = db.Prepare(query) if err != nil { return nil, err } defer stmt.Close() - var res sql.Result res, err = stmt.Exec(args...) if err != nil && !silent { log.Errore(err)