From 9c73ef52be70b2558f9b382b652d7b9f236c6bd7 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Sun, 4 Aug 2019 09:37:30 +0300 Subject: [PATCH 1/2] fix returned error values in defer functions --- .../openark/golib/sqlutils/sqlutils.go | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/vendor/github.com/openark/golib/sqlutils/sqlutils.go b/vendor/github.com/openark/golib/sqlutils/sqlutils.go index fd09689dc..af78b3fb3 100644 --- a/vendor/github.com/openark/golib/sqlutils/sqlutils.go +++ b/vendor/github.com/openark/golib/sqlutils/sqlutils.go @@ -242,34 +242,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) @@ -278,7 +277,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 @@ -317,15 +316,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) @@ -335,20 +332,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) From 0b6f8a600e2e0472508c28e43f9a71b36b7c3815 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Mon, 5 Aug 2019 09:07:58 +0300 Subject: [PATCH 2/2] gofmt --- vendor/github.com/openark/golib/sqlutils/sqlutils.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vendor/github.com/openark/golib/sqlutils/sqlutils.go b/vendor/github.com/openark/golib/sqlutils/sqlutils.go index af78b3fb3..e1a34a98f 100644 --- a/vendor/github.com/openark/golib/sqlutils/sqlutils.go +++ b/vendor/github.com/openark/golib/sqlutils/sqlutils.go @@ -260,7 +260,7 @@ func QueryRowsMap(db *sql.DB, query string, on_row func(RowMap) error, args ...i } // 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 ResultData, columns []string,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)) @@ -316,7 +316,7 @@ 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{}) (res sql.Result,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)) @@ -332,13 +332,13 @@ func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (res sql.Resul // 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{}) (res sql.Result,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)) } }() -var stmt *sql.Stmt + var stmt *sql.Stmt stmt, err = db.Prepare(query) if err != nil { return nil, err