From 43d2fef523ae16b2f9ad6f31406dea25eb455ad0 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sat, 28 Jan 2017 03:08:38 +0100
Subject: add rddb function to update user-define value of carts


diff --git a/rddb/rddb.go b/rddb/rddb.go
index 7937dce..bbf8a1e 100644
--- a/rddb/rddb.go
+++ b/rddb/rddb.go
@@ -410,39 +410,51 @@ type updateCutCartTitleRequest struct {
 	response   chan<- updateCutCartTitleResult
 }
 
+type updateCartUserDefinedResult struct {
+	err error
+}
+
+type updateCartUserDefinedRequest struct {
+	cart     uint
+	value    string
+	response chan<- updateCartUserDefinedResult
+}
+
 type db struct {
-	conf                     *config
-	readonly                 bool
-	dbh                      *sql.DB
-	passwordCache            map[string]string
-	getPasswordChan          chan getPasswordRequest
-	getPasswordStmt          *sql.Stmt
-	getGroupOfCartChan       chan getGroupOfCartRequest
-	getGroupOfCartStmt       *sql.Stmt
-	getShowInfoChan          chan getShowInfoRequest
-	getShowInfoStmt          *sql.Stmt
-	checkMusicGroupChan      chan checkMusicGroupRequest
-	checkMusicGroupStmt      *sql.Stmt
-	getMusicInfoChan         chan getMusicInfoRequest
-	getMusicInfoStmt         *sql.Stmt
-	getShowListChan          chan getShowListRequest
-	getShowListStmt          *sql.Stmt
-	getShowCartListChan      chan getShowCartListRequest
-	getCartInfoStmt          *sql.Stmt
-	getCutInfoStmt           *sql.Stmt
-	getJingleListChan        chan getJingleListRequest
-	getJingleListStmt        *sql.Stmt
-	getJingleCutListChan     chan getJingleCutListRequest
-	getPoolListChan          chan getPoolListRequest
-	getPoolListStmt          *sql.Stmt
-	getPoolCartListChan      chan getPoolCartListRequest
-	getPoolCartsStmt         *sql.Stmt
-	updateCutCartTitleChan   chan updateCutCartTitleRequest
-	getGroupDefaultTitleStmt *sql.Stmt
-	updateCutTitleStmt       *sql.Stmt
-	updateCartTitleMaybeStmt *sql.Stmt
-	quit                     chan bool
-	done                     chan bool
+	conf                      *config
+	readonly                  bool
+	dbh                       *sql.DB
+	passwordCache             map[string]string
+	getPasswordChan           chan getPasswordRequest
+	getPasswordStmt           *sql.Stmt
+	getGroupOfCartChan        chan getGroupOfCartRequest
+	getGroupOfCartStmt        *sql.Stmt
+	getShowInfoChan           chan getShowInfoRequest
+	getShowInfoStmt           *sql.Stmt
+	checkMusicGroupChan       chan checkMusicGroupRequest
+	checkMusicGroupStmt       *sql.Stmt
+	getMusicInfoChan          chan getMusicInfoRequest
+	getMusicInfoStmt          *sql.Stmt
+	getShowListChan           chan getShowListRequest
+	getShowListStmt           *sql.Stmt
+	getShowCartListChan       chan getShowCartListRequest
+	getCartInfoStmt           *sql.Stmt
+	getCutInfoStmt            *sql.Stmt
+	getJingleListChan         chan getJingleListRequest
+	getJingleListStmt         *sql.Stmt
+	getJingleCutListChan      chan getJingleCutListRequest
+	getPoolListChan           chan getPoolListRequest
+	getPoolListStmt           *sql.Stmt
+	getPoolCartListChan       chan getPoolCartListRequest
+	getPoolCartsStmt          *sql.Stmt
+	updateCutCartTitleChan    chan updateCutCartTitleRequest
+	getGroupDefaultTitleStmt  *sql.Stmt
+	updateCutTitleStmt        *sql.Stmt
+	updateCartTitleMaybeStmt  *sql.Stmt
+	updateCartUserDefinedChan chan updateCartUserDefinedRequest
+	updateCartUserDefinedStmt *sql.Stmt
+	quit                      chan bool
+	done                      chan bool
 }
 
 func (d *db) init() (err error) {
@@ -508,6 +520,9 @@ func (d *db) init() (err error) {
 		if d.updateCartTitleMaybeStmt, err = d.dbh.Prepare("update CART set TITLE = ? where NUMBER = ? and TITLE = ?;"); err != nil {
 			return
 		}
+		if d.updateCartUserDefinedStmt, err = d.dbh.Prepare("update CART set USER_DEFINED = ? where NUMBER = ?;"); err != nil {
+			return
+		}
 	}
 
 	return
@@ -878,6 +893,15 @@ func (d *db) updateCutCartTitle(cart, cut uint, groupName, sourceFile string) (r
 	return
 }
 
+func (d *db) updateCartUserDefined(cart uint, value string) (result updateCartUserDefinedResult) {
+	if d.readonly {
+		result.err = fmt.Errorf("updating Cart User-defined value prohibited: read-only database connection")
+		return
+	}
+	_, result.err = d.updateCartUserDefinedStmt.Exec(value, cart)
+	return
+}
+
 func (d *db) dispatchRequests() {
 	defer func() { d.done <- true }()
 	for {
@@ -908,6 +932,8 @@ func (d *db) dispatchRequests() {
 			req.response <- d.getPoolCartList(req.pool)
 		case req := <-d.updateCutCartTitleChan:
 			req.response <- d.updateCutCartTitle(req.cart, req.cut, req.groupName, req.sourceFile)
+		case req := <-d.updateCartUserDefinedChan:
+			req.response <- d.updateCartUserDefined(req.cart, req.value)
 		}
 	}
 }
@@ -916,18 +942,19 @@ func (d *db) dispatchRequests() {
 // Public Interface
 
 type DB struct {
-	getPasswordChan        chan<- getPasswordRequest
-	getGroupOfCartChan     chan<- getGroupOfCartRequest
-	getShowInfoChan        chan<- getShowInfoRequest
-	checkMusicGroupChan    chan<- checkMusicGroupRequest
-	getMusicInfoChan       chan<- getMusicInfoRequest
-	getShowListChan        chan<- getShowListRequest
-	getShowCartListChan    chan<- getShowCartListRequest
-	getJingleListChan      chan<- getJingleListRequest
-	getJingleCutListChan   chan<- getJingleCutListRequest
-	getPoolListChan        chan<- getPoolListRequest
-	getPoolCartListChan    chan<- getPoolCartListRequest
-	updateCutCartTitleChan chan<- updateCutCartTitleRequest
+	getPasswordChan           chan<- getPasswordRequest
+	getGroupOfCartChan        chan<- getGroupOfCartRequest
+	getShowInfoChan           chan<- getShowInfoRequest
+	checkMusicGroupChan       chan<- checkMusicGroupRequest
+	getMusicInfoChan          chan<- getMusicInfoRequest
+	getShowListChan           chan<- getShowListRequest
+	getShowCartListChan       chan<- getShowCartListRequest
+	getJingleListChan         chan<- getJingleListRequest
+	getJingleCutListChan      chan<- getJingleCutListRequest
+	getPoolListChan           chan<- getPoolListRequest
+	getPoolCartListChan       chan<- getPoolCartListRequest
+	updateCutCartTitleChan    chan<- updateCutCartTitleRequest
+	updateCartUserDefinedChan chan<- updateCartUserDefinedRequest
 }
 
 func (d *DB) GetPassword(username string, cached bool) (string, error) {
@@ -1123,6 +1150,18 @@ func (d *DB) UpdateCutCartTitle(cart, cut uint, groupName, sourceFile string) er
 	return res.err
 }
 
+func (d *DB) UpdateCartUserDefined(cart uint, value string) error {
+	resCh := make(chan updateCartUserDefinedResult)
+	req := updateCartUserDefinedRequest{}
+	req.cart = cart
+	req.value = value
+	req.response = resCh
+	d.updateCartUserDefinedChan <- req
+
+	res := <-resCh
+	return res.err
+}
+
 func (d *db) GetInterface() *DB {
 	ch := &DB{}
 	ch.getPasswordChan = d.getPasswordChan
@@ -1137,6 +1176,7 @@ func (d *db) GetInterface() *DB {
 	ch.getPoolListChan = d.getPoolListChan
 	ch.getPoolCartListChan = d.getPoolCartListChan
 	ch.updateCutCartTitleChan = d.updateCutCartTitleChan
+	ch.updateCartUserDefinedChan = d.updateCartUserDefinedChan
 	return ch
 }
 
@@ -1191,6 +1231,9 @@ func (d *db) Cleanup() {
 	if d.updateCartTitleMaybeStmt != nil {
 		d.updateCartTitleMaybeStmt.Close()
 	}
+	if d.updateCartUserDefinedStmt != nil {
+		d.updateCartUserDefinedStmt.Close()
+	}
 
 }
 
@@ -1215,6 +1258,7 @@ func newDB(configfile string, readonly bool) (d *db, err error) {
 	d.getPoolListChan = make(chan getPoolListRequest, 10)
 	d.getPoolCartListChan = make(chan getPoolCartListRequest, 10)
 	d.updateCutCartTitleChan = make(chan updateCutCartTitleRequest, 10)
+	d.updateCartUserDefinedChan = make(chan updateCartUserDefinedRequest, 10)
 
 	if err = d.init(); err != nil {
 		return
-- 
cgit v0.10.2