-
Notifications
You must be signed in to change notification settings - Fork 4
/
sql.go
96 lines (91 loc) · 2.08 KB
/
sql.go
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
package main
import (
"database/sql"
"fmt"
"github.com/irlndts/go-discogs"
"github.com/pkg/errors"
)
func (client *App) UpdateLabelWithThumbnail(label *discogs.Label) error {
if len(label.Images) > 0 {
_, err := client.SQLDriver.Exec(fmt.Sprintf(`
UPDATE dg_labels
SET thumbnail_medium = ?
WHERE label_id = ?
`), label.Images[0].ResourceURL, label.ID)
return errors.Wrap(err, "failed to update label with thumbnail")
}
return nil
}
func (client *App) GetLabelInfo(labelId int) (LocalLabel, error) {
l := LocalLabel{
MasterReleasesCache: map[int]discogs.Release{},
}
selDB := client.SQLDriver.QueryRow(fmt.Sprintf(`
SELECT
label_id,
highest_dg_release,
label_releases,
label_tracks,
last_page,
did_init
FROM dg_labels
WHERE label_id = ?
`), labelId)
err := selDB.Scan(
&l.LabelID,
&l.HighestReleaseID,
&l.LabelReleases,
&l.LabelTracks,
&l.LastPage,
&l.DidInit,
)
return l, errors.Wrap(err, "failed to get label info")
}
func (client *App) UpdateLabelWithStats(localLabel LocalLabel, isLastPage bool) error {
if !localLabel.DidInit.Bool {
didInit := localLabel.LastPage == localLabel.MaxPages
nb := sql.NullBool{}
err := nb.Scan(didInit)
if err != nil {
return err
}
localLabel.DidInit = nb
}
if isLastPage {
localLabel.LastPage = 1
}
_, err := client.SQLDriver.Exec(fmt.Sprintf(`
UPDATE dg_labels
SET
highest_dg_release = ?,
label_releases = ?,
label_tracks = ?,
last_page = ?,
did_init = ?
WHERE label_id = ?;
`),
localLabel.HighestReleaseID,
localLabel.LabelReleases,
localLabel.LabelTracks,
localLabel.LastPage,
localLabel.DidInit,
localLabel.LabelID)
return errors.Wrap(err, "failed to update label stats")
}
func (client *App) GetNextLabel(lastSuccessChannel int) (LocalLabel, error) {
l := LocalLabel{}
selDB := client.SQLDriver.QueryRow(fmt.Sprintf(`
SELECT
id,
label_id
FROM dg_labels
WHERE (id > ? or id = 1)
ORDER BY id = 1, id
LIMIT 1
`), lastSuccessChannel)
err := selDB.Scan(
&l.ID,
&l.LabelID,
)
return l, errors.Wrap(err, "failed to get next label LabelID")
}