From cd522b26ded18ef1972647ac8353f6549468e903 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 14 Dec 2015 03:20:59 +0100 Subject: minor refactoring at RdDb diff --git a/rddb.go b/rddb.go index 87ac17a..0a9e21e 100644 --- a/rddb.go +++ b/rddb.go @@ -147,28 +147,27 @@ func (self *RdDb) init(conf *Config) (err error) { return } -func (self *RdDb) getPassword(username string, cached bool) (pwd string, err error) { +func (self *RdDb) getPassword(username string, cached bool) (result getPasswordResult) { if cached { - pwd = self.password_cache[username] + result.password = self.password_cache[username] } - if pwd == "" { - err = self.getPasswordStmt.QueryRow(username).Scan(&pwd) - if err != nil { - if err == sql.ErrNoRows { - err = fmt.Errorf("user '%s' not known by rivendell", username) + if result.password == "" { + if result.err = self.getPasswordStmt.QueryRow(username).Scan(&result.password); result.err != nil { + if result.err == sql.ErrNoRows { + result.err = fmt.Errorf("user '%s' not known by rivendell", username) } return } - self.password_cache[username] = pwd + self.password_cache[username] = result.password } return } -func (self *RdDb) getGroupOfCart(cart uint) (group string, err error) { +func (self *RdDb) getGroupOfCart(cart uint) (result getGroupOfCartResult) { var rows *sql.Rows - if rows, err = self.getGroupOfCartStmt.Query(cart, cart); err != nil { + if rows, result.err = self.getGroupOfCartStmt.Query(cart, cart); result.err != nil { return } defer rows.Close() @@ -176,22 +175,22 @@ func (self *RdDb) getGroupOfCart(cart uint) (group string, err error) { for rows.Next() { var name string var low_cart, high_cart uint - if err = rows.Scan(&name, &low_cart, &high_cart); err != nil { + if result.err = rows.Scan(&name, &low_cart, &high_cart); result.err != nil { return } if high_cart >= low_cart { size := (high_cart - low_cart) + 1 if size_min > size { - group = name + result.group = name size_min = size } } } - if err = rows.Err(); err != nil { + if result.err = rows.Err(); result.err != nil { return } - if group == "" { - err = fmt.Errorf("cart is outside of all group cart ranges") + if result.group == "" { + result.err = fmt.Errorf("cart is outside of all group cart ranges") } return } @@ -218,41 +217,40 @@ func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []int return } -func (self *RdDb) getShowInfo(showid int) (title string, carts []int, norm_lvl int, trim_lvl int, err error) { +func (self *RdDb) getShowInfo(showid int) (result getShowInfoResult) { var macros string var low_cart, high_cart int - err = self.getShowInfoStmt.QueryRow(showid).Scan(&title, ¯os, &norm_lvl, &trim_lvl, &low_cart, &high_cart) - if err != nil { - if err == sql.ErrNoRows { - err = fmt.Errorf("show '%d' not found", showid) + result.err = self.getShowInfoStmt.QueryRow(showid).Scan(&result.title, ¯os, &result.norm_lvl, &result.trim_lvl, &low_cart, &high_cart) + if result.err != nil { + if result.err == sql.ErrNoRows { + result.err = fmt.Errorf("show '%d' not found", showid) } return } - norm_lvl /= 100 - trim_lvl /= 100 - carts, err = self.getShowCarts(showMacroRe.FindStringSubmatch(macros)[1], low_cart, high_cart) + result.norm_lvl /= 100 + result.trim_lvl /= 100 + result.carts, result.err = self.getShowCarts(showMacroRe.FindStringSubmatch(macros)[1], low_cart, high_cart) return } -func (self *RdDb) checkMusicGroup(group string) (ismusic bool, err error) { +func (self *RdDb) checkMusicGroup(group string) (result checkMusicGroupResult) { var cnt int - err = self.getMusicInfoStmt.QueryRow(group).Scan(&cnt) - if err != nil { - if err == sql.ErrNoRows { - err = nil - ismusic = false + if result.err = self.getMusicInfoStmt.QueryRow(group).Scan(&cnt); result.err != nil { + if result.err == sql.ErrNoRows { + result.err = nil + result.ismusic = false } return } - ismusic = cnt > 0 + result.ismusic = cnt > 0 return } -func (self *RdDb) getMusicInfo(group string) (norm_lvl, trim_lvl int, err error) { - err = self.checkMusicGroupStmt.QueryRow(group).Scan(&norm_lvl, &trim_lvl) - if err != nil { - if err == sql.ErrNoRows { - err = fmt.Errorf("music pool '%d' not found", group) +func (self *RdDb) getMusicInfo(group string) (result getMusicInfoResult) { + result.err = self.checkMusicGroupStmt.QueryRow(group).Scan(&result.norm_lvl, &result.trim_lvl) + if result.err != nil { + if result.err == sql.ErrNoRows { + result.err = fmt.Errorf("music pool '%d' not found", group) } return } @@ -266,20 +264,15 @@ func (self *RdDb) dispatchRequests() { case <-self.quit: return case req := <-self.getPasswordChan: - pwd, err := self.getPassword(req.username, req.cached) - req.response <- getPasswordResult{pwd, err} + req.response <- self.getPassword(req.username, req.cached) case req := <-self.getGroupOfCartChan: - group, err := self.getGroupOfCart(req.cart) - req.response <- getGroupOfCartResult{group, err} + req.response <- self.getGroupOfCart(req.cart) case req := <-self.getShowInfoChan: - title, carts, norm_lvl, trim_lvl, err := self.getShowInfo(req.showid) - req.response <- getShowInfoResult{title, carts, norm_lvl, trim_lvl, err} + req.response <- self.getShowInfo(req.showid) case req := <-self.checkMusicGroupChan: - ismusic, err := self.checkMusicGroup(req.group) - req.response <- checkMusicGroupResult{ismusic, err} + req.response <- self.checkMusicGroup(req.group) case req := <-self.getMusicInfoChan: - norm_lvl, trim_lvl, err := self.getMusicInfo(req.group) - req.response <- getMusicInfoResult{norm_lvl, trim_lvl, err} + req.response <- self.getMusicInfo(req.group) } } } -- cgit v0.10.2