diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-28 16:10:06 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-28 16:10:06 (GMT) |
commit | 4fb651e90364cf822007ef16f4190ad853f04166 (patch) | |
tree | 8ea1592612c1eeea7d957a64e855598b3a377e33 | |
parent | e40f502673a130c7d1a9c9b3ffa823399a4cd3cf (diff) |
refactoring of rddb, seperate public from private interface
-rw-r--r-- | rddb.go | 215 |
1 files changed, 109 insertions, 106 deletions
@@ -97,14 +97,6 @@ type getMusicInfoRequest struct { response chan<- getMusicInfoResult } -type RdDbChan struct { - getPasswordChan chan<- getPasswordRequest - getGroupOfCartChan chan<- getGroupOfCartRequest - getShowInfoChan chan<- getShowInfoRequest - checkMusicGroupChan chan<- checkMusicGroupRequest - getMusicInfoChan chan<- getMusicInfoRequest -} - type RdDb struct { dbh *sql.DB password_cache map[string]string @@ -177,48 +169,6 @@ func (self *RdDb) getPassword(username string, cached bool) (result getPasswordR return } -func (self *RdDbChan) GetPassword(username string, cached bool) (string, error) { - res_ch := make(chan getPasswordResult) - req := getPasswordRequest{} - req.username = username - req.cached = cached - req.response = res_ch - self.getPasswordChan <- req - - res := <-res_ch - if res.err != nil { - return "", res.err - } - return res.password, nil -} - -func (self *RdDbChan) CheckPassword(username, password string) (bool, error) { - cached := true - - for { - res_ch := make(chan getPasswordResult) - req := getPasswordRequest{} - req.username = username - req.cached = cached - req.response = res_ch - self.getPasswordChan <- req - - res := <-res_ch - if res.err != nil { - return false, res.err - } - if password == res.password { - return true, nil - } - if cached { - cached = false - } else { - break - } - } - return false, nil -} - func (self *RdDb) getGroupOfCart(cart uint) (result getGroupOfCartResult) { var rows *sql.Rows if rows, result.err = self.getGroupOfCartStmt.Query(cart, cart); result.err != nil { @@ -249,20 +199,6 @@ func (self *RdDb) getGroupOfCart(cart uint) (result getGroupOfCartResult) { return } -func (self *RdDbChan) GetGroupOfCart(cart uint) (string, error) { - res_ch := make(chan getGroupOfCartResult) - req := getGroupOfCartRequest{} - req.cart = cart - req.response = res_ch - self.getGroupOfCartChan <- req - - res := <-res_ch - if res.err != nil { - return "", res.err - } - return res.group, nil -} - func (self *RdDb) getLogTableName(log string) (logtable string, err error) { logtable = strings.Replace(log, " ", "_", -1) + "_LOG" if !mysqlTableNameRe.MatchString(logtable) { @@ -309,20 +245,6 @@ func (self *RdDb) getShowInfo(showid uint) (result getShowInfoResult) { return } -func (self *RdDbChan) GetShowInfo(showid uint) (string, int, int, []uint, error) { - res_ch := make(chan getShowInfoResult) - req := getShowInfoRequest{} - req.showid = showid - req.response = res_ch - self.getShowInfoChan <- req - - res := <-res_ch - if res.err != nil { - return "", 0, 0, nil, res.err - } - return res.group, res.norm_lvl, res.trim_lvl, res.carts, nil -} - func (self *RdDb) checkMusicGroup(group string) (result checkMusicGroupResult) { var cnt int if result.err = self.checkMusicGroupStmt.QueryRow(group).Scan(&cnt); result.err != nil { @@ -336,20 +258,6 @@ func (self *RdDb) checkMusicGroup(group string) (result checkMusicGroupResult) { return } -func (self *RdDbChan) CheckMusicGroup(groupname string) (bool, error) { - res_ch := make(chan checkMusicGroupResult) - req := checkMusicGroupRequest{} - req.group = groupname - req.response = res_ch - self.checkMusicGroupChan <- req - - res := <-res_ch - if res.err != nil { - return false, res.err - } - return res.ismusic, nil -} - func (self *RdDb) getMusicInfo(group string) (result getMusicInfoResult) { result.err = self.getMusicInfoStmt.QueryRow(group).Scan(&result.norm_lvl, &result.trim_lvl) if result.err != nil { @@ -361,20 +269,6 @@ func (self *RdDb) getMusicInfo(group string) (result getMusicInfoResult) { return } -func (self *RdDbChan) GetMusicInfo(groupname string) (int, int, error) { - res_ch := make(chan getMusicInfoResult) - req := getMusicInfoRequest{} - req.group = groupname - req.response = res_ch - self.getMusicInfoChan <- req - - res := <-res_ch - if res.err != nil { - return 0, 0, res.err - } - return res.norm_lvl, res.trim_lvl, nil -} - func (self *RdDb) dispatchRequests() { defer func() { self.done <- true }() for { @@ -395,6 +289,115 @@ func (self *RdDb) dispatchRequests() { } } +// ********************************************************* +// Public Interface + +type RdDbChan struct { + getPasswordChan chan<- getPasswordRequest + getGroupOfCartChan chan<- getGroupOfCartRequest + getShowInfoChan chan<- getShowInfoRequest + checkMusicGroupChan chan<- checkMusicGroupRequest + getMusicInfoChan chan<- getMusicInfoRequest +} + +func (self *RdDbChan) GetPassword(username string, cached bool) (string, error) { + res_ch := make(chan getPasswordResult) + req := getPasswordRequest{} + req.username = username + req.cached = cached + req.response = res_ch + self.getPasswordChan <- req + + res := <-res_ch + if res.err != nil { + return "", res.err + } + return res.password, nil +} + +func (self *RdDbChan) CheckPassword(username, password string) (bool, error) { + cached := true + + for { + res_ch := make(chan getPasswordResult) + req := getPasswordRequest{} + req.username = username + req.cached = cached + req.response = res_ch + self.getPasswordChan <- req + + res := <-res_ch + if res.err != nil { + return false, res.err + } + if password == res.password { + return true, nil + } + if cached { + cached = false + } else { + break + } + } + return false, nil +} + +func (self *RdDbChan) GetGroupOfCart(cart uint) (string, error) { + res_ch := make(chan getGroupOfCartResult) + req := getGroupOfCartRequest{} + req.cart = cart + req.response = res_ch + self.getGroupOfCartChan <- req + + res := <-res_ch + if res.err != nil { + return "", res.err + } + return res.group, nil +} + +func (self *RdDbChan) GetShowInfo(showid uint) (string, int, int, []uint, error) { + res_ch := make(chan getShowInfoResult) + req := getShowInfoRequest{} + req.showid = showid + req.response = res_ch + self.getShowInfoChan <- req + + res := <-res_ch + if res.err != nil { + return "", 0, 0, nil, res.err + } + return res.group, res.norm_lvl, res.trim_lvl, res.carts, nil +} + +func (self *RdDbChan) CheckMusicGroup(groupname string) (bool, error) { + res_ch := make(chan checkMusicGroupResult) + req := checkMusicGroupRequest{} + req.group = groupname + req.response = res_ch + self.checkMusicGroupChan <- req + + res := <-res_ch + if res.err != nil { + return false, res.err + } + return res.ismusic, nil +} + +func (self *RdDbChan) GetMusicInfo(groupname string) (int, int, error) { + res_ch := make(chan getMusicInfoResult) + req := getMusicInfoRequest{} + req.group = groupname + req.response = res_ch + self.getMusicInfoChan <- req + + res := <-res_ch + if res.err != nil { + return 0, 0, res.err + } + return res.norm_lvl, res.trim_lvl, nil +} + func (self *RdDb) GetInterface() *RdDbChan { ch := &RdDbChan{} ch.getPasswordChan = self.getPasswordChan |