diff options
Diffstat (limited to 'src/helsinki.at/rhimport/rddb.go')
-rw-r--r-- | src/helsinki.at/rhimport/rddb.go | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index dd1dfec..85a3177 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -41,6 +41,16 @@ type getPasswordRequest struct { response chan getPasswordResult } +type getGroupOfCartResult struct { + group string + err error +} + +type getGroupOfCartRequest struct { + cart uint + response chan getGroupOfCartResult +} + type getShowInfoResult struct { title string carts map[int]int @@ -53,14 +63,16 @@ type getShowInfoRequest struct { } type RdDb struct { - dbh *sql.DB - password_cache map[string]string - getPasswordChan chan getPasswordRequest - getPasswordStmt *sql.Stmt - getShowInfoChan chan getShowInfoRequest - getShowInfoStmt *sql.Stmt - quit chan bool - done chan bool + dbh *sql.DB + password_cache map[string]string + getPasswordChan chan getPasswordRequest + getPasswordStmt *sql.Stmt + getGroupOfCartChan chan getGroupOfCartRequest + getGroupOfCartStmt *sql.Stmt + getShowInfoChan chan getShowInfoRequest + getShowInfoStmt *sql.Stmt + quit chan bool + done chan bool } func (self *RdDb) init(conf *Config) (err error) { @@ -71,6 +83,9 @@ func (self *RdDb) init(conf *Config) (err error) { 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 { return } @@ -78,7 +93,6 @@ func (self *RdDb) init(conf *Config) (err error) { } func (self *RdDb) getPassword(username string, cached bool) (pwd string, err error) { - if cached { pwd = self.password_cache[username] } @@ -97,6 +111,36 @@ func (self *RdDb) getPassword(username string, cached bool) (pwd string, err err return } +func (self *RdDb) getGroupOfCart(cart uint) (group string, err error) { + var rows *sql.Rows + if rows, err = self.getGroupOfCartStmt.Query(cart, cart); err != nil { + return + } + defer rows.Close() + size_min := ^uint(0) + for rows.Next() { + var name string + var low_cart, high_cart uint + if err = rows.Scan(&name, &low_cart, &high_cart); err != nil { + return + } + if high_cart >= low_cart { + size := (high_cart - low_cart) + 1 + if size_min > size { + group = name + size_min = size + } + } + } + if err = rows.Err(); err != nil { + return + } + if group == "" { + err = fmt.Errorf("cart is outside of all group cart ranges") + } + return +} + func (self *RdDb) getShowInfo(showid int) (title string, carts map[int]int, err error) { var macros string err = self.getShowInfoStmt.QueryRow(showid).Scan(&title, ¯os) @@ -127,6 +171,10 @@ func (self *RdDb) dispatchRequests() { } 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) @@ -147,6 +195,9 @@ func (self *RdDb) Cleanup() { if self.getPasswordStmt != nil { self.getPasswordStmt.Close() } + if self.getGroupOfCartStmt != nil { + self.getGroupOfCartStmt.Close() + } if self.getShowInfoStmt != nil { self.getPasswordStmt.Close() } @@ -159,6 +210,7 @@ func NewRdDb(conf *Config) (db *RdDb, err error) { db.done = make(chan bool) db.password_cache = make(map[string]string) db.getPasswordChan = make(chan getPasswordRequest) + db.getGroupOfCartChan = make(chan getGroupOfCartRequest) db.getShowInfoChan = make(chan getShowInfoRequest) if err = db.init(conf); err != nil { |