1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//
// pool-import
//
// Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
// This file is part of pool-import.
//
// pool-import is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// pool-import is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pool-import. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/ziutek/mymysql/godrv"
)
var (
mysqlTableNameRe = regexp.MustCompile(`^[_0-9a-zA-Z-]+$`)
)
const (
DB_VERSION = 182
defaultCartTitle = "[new cart]"
)
type CutListEntry struct {
Number uint
Evergreen bool
Description string
Duration time.Duration
Imported NullTime
NumPlayed uint
LastPlayed NullTime
}
type CartListEntry struct {
Number uint
Exists bool
Artist string
Title string
Album string
Cuts []CutListEntry
}
type PoolListEntry struct {
Group string
Description string
}
type getPoolCartListResult struct {
carts map[uint]CartListEntry
err error
}
type getPoolCartListRequest struct {
pool PoolListEntry
response chan<- getPoolCartListResult
}
type db struct {
conf *config
dbh *sql.DB
getPoolCartListChan chan getPoolCartListRequest
getPoolCartsStmt *sql.Stmt
quit chan bool
done chan bool
}
func (d *db) init() (err error) {
godrv.Register("SET CHARACTER SET utf8;")
dsn := fmt.Sprintf("tcp:%s:3306*%s/%s/%s", d.conf.dbHost, d.conf.dbDb, d.conf.dbUser, d.conf.dbPasswd)
if d.dbh, err = sql.Open("mymysql", dsn); err != nil {
return
}
var dbver int
err = d.dbh.QueryRow("select DB from VERSION;").Scan(&dbver)
if err != nil {
err = fmt.Errorf("fetching version: %s", err)
return
}
if dbver != DB_VERSION {
err = fmt.Errorf("version mismatch is %d, should be %d", dbver, DB_VERSION)
return
}
if d.getPoolCartsStmt, err = d.dbh.Prepare("select CART.NUMBER,CART.ARTIST,CART.TITLE,CART.ALBUM,CUTS.CUT_NAME,CUTS.EVERGREEN,CUTS.DESCRIPTION,CUTS.LENGTH,CUTS.ORIGIN_DATETIME,CUTS.PLAY_COUNTER,CUTS.LAST_PLAY_DATETIME from CUTS,CART where CUTS.CART_NUMBER = CART.NUMBER and CART.GROUP_NAME = ?;"); err != nil {
return
}
return
}
func (d *db) getPoolCartList(pool PoolListEntry) (result getPoolCartListResult) {
var rows *sql.Rows
if rows, result.err = d.getPoolCartsStmt.Query(pool.Group); result.err != nil {
return
}
defer rows.Close()
result.carts = make(map[uint]CartListEntry)
for rows.Next() {
var cut CutListEntry
var cart, length uint
var cutName, evergreen string
var artist, title, album sql.NullString
if result.err = rows.Scan(&cart, &artist, &title, &album, &cutName, &evergreen, &cut.Description, &length, &cut.Imported, &cut.NumPlayed, &cut.LastPlayed); result.err != nil {
return
}
parts := strings.Split(cutName, "_")
if len(parts) == 2 {
if cn, converr := strconv.ParseUint(parts[1], 10, 32); converr != nil {
continue
} else {
cut.Number = uint(cn)
}
} else {
continue
}
switch evergreen {
case "Y":
cut.Evergreen = true
default:
cut.Evergreen = false
}
cut.Duration = time.Duration(length) * time.Millisecond
if c, exists := result.carts[cart]; !exists {
c = CartListEntry{cart, true, artist.String, title.String, album.String, nil}
c.Cuts = append(c.Cuts, cut)
result.carts[cart] = c
} else {
c.Cuts = append(c.Cuts, cut)
}
}
result.err = rows.Err()
return
}
func (d *db) dispatchRequests() {
defer func() { d.done <- true }()
for {
select {
case <-d.quit:
return
case req := <-d.getPoolCartListChan:
req.response <- d.getPoolCartList(req.pool)
}
}
}
// *********************************************************
// Public Interface
type DB struct {
getPoolCartListChan chan<- getPoolCartListRequest
}
func (d *DB) GetPoolCartList(pool PoolListEntry) (map[uint]CartListEntry, error) {
resCh := make(chan getPoolCartListResult)
req := getPoolCartListRequest{}
req.pool = pool
req.response = resCh
d.getPoolCartListChan <- req
res := <-resCh
if res.err != nil {
return nil, res.err
}
return res.carts, nil
}
func (d *db) GetInterface() *DB {
ch := &DB{}
ch.getPoolCartListChan = d.getPoolCartListChan
return ch
}
func (d *db) Cleanup() {
d.quit <- true
<-d.done
close(d.quit)
close(d.done)
if d.dbh != nil {
d.dbh.Close()
}
if d.getPoolCartsStmt != nil {
d.getPoolCartsStmt.Close()
}
}
func NewDB(configfile string) (d *db, err error) {
d = new(db)
if d.conf, err = newConfig(configfile); err != nil {
return
}
d.quit = make(chan bool)
d.done = make(chan bool)
d.getPoolCartListChan = make(chan getPoolCartListRequest, 10)
if err = d.init(); err != nil {
return
}
go d.dispatchRequests()
return
}
|