diff options
Diffstat (limited to 'src/helsinki.at/rhimport/rddb.go')
-rw-r--r-- | src/helsinki.at/rhimport/rddb.go | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index 52d6e18..e945483 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -28,6 +28,12 @@ import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" + "regexp" + "strings" +) + +var ( + showMacroRe = regexp.MustCompile(`^LL 1 ([^ ]+) 0\!$`) ) type getPasswordResult struct { @@ -52,9 +58,11 @@ type getGroupOfCartRequest struct { } type getShowInfoResult struct { - title string - carts map[int]int - err error + title string + carts []int + norm_lvl int + trim_lvl int + err error } type getShowInfoRequest struct { @@ -92,13 +100,15 @@ func (self *RdDb) init(conf *Config) (err error) { if self.dbh, err = sql.Open("mysql", dsn); err != nil { return } + //TODO: check if we have a compatible DB version + if self.getPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?;"); err != nil { return } if self.getGroupOfCartStmt, err = self.dbh.Prepare("select NAME,DEFAULT_LOW_CART,DEFAULT_HIGH_CART from GROUPS where DEFAULT_LOW_CART <= ? and DEFAULT_HIGH_CART >= ?;"); err != nil { return } - if self.getShowInfoStmt, err = self.dbh.Prepare("select TITLE,MACROS from CART where NUMBER = ?;"); err != nil { + if self.getShowInfoStmt, err = self.dbh.Prepare("select CART.TITLE,CART.MACROS,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART from CART, DROPBOXES, GROUPS where CART.NUMBER = DROPBOXES.TO_CART and GROUPS.NAME = DROPBOXES.GROUP_NAME and CART.NUMBER = ?;"); err != nil { return } if self.checkMusicGroupStmt, err = self.dbh.Prepare("select count(*) from DROPBOXES where GROUP_NAME = ? and SET_USER_DEFINED like \"M;%\";"); err != nil { @@ -156,19 +166,41 @@ func (self *RdDb) getGroupOfCart(cart uint) (group string, err error) { return } -func (self *RdDb) getShowInfo(showid int) (title string, carts map[int]int, err error) { +func (self *RdDb) getLogTableName(log string) string { + return strings.Replace(log, " ", "_", -1) + "_LOG" // TODO: this should get escaped fir mySQL but golang doesn't support it!!! +} + +func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []int, err error) { + q := fmt.Sprintf("select CART_NUMBER from %s where CART_NUMBER >= %d and CART_NUMBER <= %d order by COUNT;", self.getLogTableName(log), low_cart, high_cart) + var rows *sql.Rows + if rows, err = self.dbh.Query(q); err != nil { + return + } + defer rows.Close() + for rows.Next() { + var cart int + if err = rows.Scan(&cart); err != nil { + return + } + carts = append(carts, cart) + } + err = rows.Err() + return +} + +func (self *RdDb) getShowInfo(showid int) (title string, carts []int, norm_lvl int, trim_lvl int, err error) { var macros string - err = self.getShowInfoStmt.QueryRow(showid).Scan(&title, ¯os) + 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) } return } - - rhdl.Printf("RdDb: macros for showid '%d' are set to '%s'", showid, macros) - // TODO: also fetch cart list and import params (norm_level, ...) - carts = make(map[int]int) + norm_lvl /= 100 + trim_lvl /= 100 + carts, err = self.getShowCarts(showMacroRe.FindStringSubmatch(macros)[1], low_cart, high_cart) return } @@ -193,23 +225,15 @@ func (self *RdDb) dispatchRequests() { case <-self.quit: return case req := <-self.getPasswordChan: - if req.cached { - rhdl.Println("RdDb: got getPassword request for", req.username, "(cached)") - } else { - rhdl.Println("RdDb: got getPassword request for", req.username, "(not cached)") - } pwd, err := self.getPassword(req.username, req.cached) req.response <- getPasswordResult{pwd, err} case req := <-self.getGroupOfCartChan: - rhdl.Println("RdDb: got getGroupOfCart request for", req.cart) group, err := self.getGroupOfCart(req.cart) req.response <- getGroupOfCartResult{group, err} case req := <-self.getShowInfoChan: - rhdl.Println("RdDb: got getShowInfo request for", req.showid) - title, carts, err := self.getShowInfo(req.showid) - req.response <- getShowInfoResult{title, carts, err} + title, carts, norm_lvl, trim_lvl, err := self.getShowInfo(req.showid) + req.response <- getShowInfoResult{title, carts, norm_lvl, trim_lvl, err} case req := <-self.checkMusicGroupChan: - rhdl.Println("RdDb: got checkMusicGroup request for", req.group) ismusic, err := self.checkMusicGroup(req.group) req.response <- checkMusicGroupResult{ismusic, err} } |