diff options
Diffstat (limited to 'src/helsinki.at/rhimport')
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 30 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/rddb.go | 79 |
2 files changed, 85 insertions, 24 deletions
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 5eea054..2b512ed 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -35,10 +35,11 @@ import ( type ImportContext struct { *Config - *RDDB + *RdDb UserName string Password string Trusted bool + ShowId int GroupName string Cart int Cut int @@ -51,13 +52,14 @@ type ImportContext struct { DeleteSourceFile bool } -func NewImportContext(conf *Config, rddb *RDDB, user string, group string) *ImportContext { +func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *ImportContext { ctx := new(ImportContext) ctx.Config = conf - ctx.RDDB = rddb + ctx.RdDb = rddb ctx.UserName = user ctx.Password = "" ctx.Trusted = false + ctx.ShowId = 0 ctx.GroupName = group ctx.Cart = 0 ctx.Cut = 0 @@ -76,7 +78,7 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { req.username = ctx.UserName req.cached = cached req.response = make(chan getPasswordResult) - ctx.RDDB.getPasswordChan <- req + ctx.RdDb.getPasswordChan <- req res := <-req.response if res.err != nil { @@ -87,6 +89,21 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { return } +func (ctx *ImportContext) getShowInfo() (err error) { + req := getShowInfoRequest{} + req.showid = ctx.ShowId + req.response = make(chan getShowInfoResult) + ctx.RdDb.getShowInfoChan <- req + + res := <-req.response + if res.err != nil { + err = res.err + return + } + rhdl.Printf("Title of show %d is '%s'", ctx.ShowId, res.title) + return +} + // func import_audio(url, file string) (err error) { // var b bytes.Buffer @@ -142,6 +159,11 @@ func ImportFile(ctx *ImportContext) (err error) { return } } + + if err = ctx.getShowInfo(); err != nil { + return + } + rhdl.Printf("%+v", ctx) return 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 |