-
Notifications
You must be signed in to change notification settings - Fork 0
/
runs.go
105 lines (86 loc) · 1.5 KB
/
runs.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
97
98
99
100
101
102
103
104
105
package models
const (
StatusGood = iota
StatusWait
StatusFail
)
type Run struct {
RunID int64
JobID int64
Status int
}
// GetRuns returns all Runs.
func (c *Client) GetRuns() ([]Run, error) {
s := `select * from runs order by runid desc`
rows, err := c.DB.Query(s)
if err != nil {
return []Run{}, err
}
defer rows.Close()
runs := make([]Run, 0, 16)
for rows.Next() {
run := Run{}
err = rows.Scan(
&run.RunID,
&run.JobID,
&run.Status)
if err != nil {
return []Run{}, err
}
runs = append(runs, run)
}
return runs, nil
}
func (c *Client) GetLastFullRunID(sid int64, dir string) (int64, error) {
s := `
SELECT
*
FROM runs
INNER JOIN jobs ON
runs.jobid=jobs.jobid,
runs.status=?,
jobs.serverid=?,
jobs.directory=?,
jobs.style=?
ORDER BY
runs.runid
DESC
LIMIT 1
`
rows, err := c.DB.Query(s,
StatusGood,
sid,
dir,
Full)
if err != nil {
return 0, err
}
r := Run{}
err = rows.Scan(&r.RunID, &r.JobID, &r.Status)
if err != nil {
return 0, err
}
return r.RunID, nil
}
// InsertRun inserts Run r.
func (c *Client) InsertRun(r Run) (int64, error) {
s := `insert into runs values (?, ?, ?)`
res, err := c.DB.Exec(s, nil,
r.JobID,
r.Status)
if err != nil {
return 0, err
}
return res.LastInsertId()
}
// UpdateRun updates a Run r identified by r.RunID.
func (c *Client) UpdateRun(r Run) error {
s := `update runs
set jobid=?,status=?
where runid=?`
_, err := c.DB.Exec(s,
r.JobID,
r.Status,
r.RunID)
return err
}