diff options
Diffstat (limited to 'src/helsinki.at/rhimport/rddb.go')
-rw-r--r-- | src/helsinki.at/rhimport/rddb.go | 79 |
1 files changed, 59 insertions, 20 deletions
diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index 2a87388..dd1dfec 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -41,35 +41,50 @@ type getPasswordRequest struct { response chan getPasswordResult } -type RDDB struct { - dbh *sql.DB - password_cache map[string]string - getPasswordChan chan getPasswordRequest - dbGetPasswordStmt *sql.Stmt - quit chan bool - done chan bool +type getShowInfoResult struct { + title string + carts map[int]int + err error } -func (self *RDDB) init(conf *Config) (err error) { +type getShowInfoRequest struct { + showid int + response chan getShowInfoResult +} + +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 +} + +func (self *RdDb) init(conf *Config) (err error) { dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", conf.db_user, conf.db_passwd, conf.db_host, conf.db_db) if self.dbh, err = sql.Open("mysql", dsn); err != nil { return } - if self.dbGetPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?"); err != nil { + if self.getPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?;"); err != nil { + return + } + if self.getShowInfoStmt, err = self.dbh.Prepare("select TITLE,MACROS from CART where NUMBER = ?;"); err != nil { return } - return } -func (self *RDDB) getPassword(username string, cached bool) (pwd string, err error) { +func (self *RdDb) getPassword(username string, cached bool) (pwd string, err error) { if cached { pwd = self.password_cache[username] } if pwd == "" { - err = self.dbGetPasswordStmt.QueryRow(username).Scan(&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) @@ -82,7 +97,23 @@ func (self *RDDB) getPassword(username string, cached bool) (pwd string, err err return } -func (self *RDDB) dispatchRequests() { +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) + 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 + carts = make(map[int]int) + return +} + +func (self *RdDb) dispatchRequests() { defer func() { self.done <- true }() for { select { @@ -90,17 +121,21 @@ func (self *RDDB) dispatchRequests() { return case req := <-self.getPasswordChan: if req.cached { - rhdl.Println("RDDB: got getPassword request for", req.username, "(cached)") + rhdl.Println("RdDb: got getPassword request for", req.username, "(cached)") } else { - rhdl.Println("RDDB: got getPassword request for", req.username, "(not cached)") + 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.getShowInfoChan: + rhdl.Println("RdDb: got getShowInfo request for", req.showid) + title, carts, err := self.getShowInfo(req.showid) + req.response <- getShowInfoResult{title, carts, err} } } } -func (self *RDDB) Cleanup() { +func (self *RdDb) Cleanup() { self.quit <- true <-self.done close(self.quit) @@ -109,18 +144,22 @@ func (self *RDDB) Cleanup() { if self.dbh != nil { self.dbh.Close() } - if self.dbGetPasswordStmt != nil { - self.dbGetPasswordStmt.Close() + if self.getPasswordStmt != nil { + self.getPasswordStmt.Close() + } + if self.getShowInfoStmt != nil { + self.getPasswordStmt.Close() } } -func NewRDDB(conf *Config) (db *RDDB, err error) { - db = new(RDDB) +func NewRdDb(conf *Config) (db *RdDb, err error) { + db = new(RdDb) db.quit = make(chan bool) db.done = make(chan bool) db.password_cache = make(map[string]string) db.getPasswordChan = make(chan getPasswordRequest) + db.getShowInfoChan = make(chan getShowInfoRequest) if err = db.init(conf); err != nil { return |