/
score.go
76 lines (59 loc) · 1.33 KB
/
score.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
/**
* @file score.go
* @author Mikhail Klementyev jollheef<AT>riseup.net
* @license GNU AGPLv3
* @date November, 2015
* @brief queries for score table
*/
package db
import (
"database/sql"
"time"
)
// Score row
type Score struct {
ID int
TeamID int
Score int
Timestamp time.Time
}
func createScoreTable(db *sql.DB) (err error) {
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS "score" (
id SERIAL PRIMARY KEY,
team_id INTEGER NOT NULL,
score INTEGER NOT NULL,
timestamp TIMESTAMP with time zone DEFAULT now()
)`)
return
}
// AddScore add last result for team and fill id
func AddScore(db *sql.DB, score *Score) (err error) {
stmt, err := db.Prepare("INSERT INTO score (team_id, score) " +
"VALUES ($1, $2) RETURNING id")
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(score.TeamID, score.Score).Scan(&score.ID)
if err != nil {
return
}
return
}
// GetLastScore get last result for team id
func GetLastScore(db *sql.DB, teamID int) (s Score, err error) {
stmt, err := db.Prepare("SELECT id, score, timestamp FROM score " +
" WHERE team_id=$1 AND id = " +
"(SELECT MAX(id) FROM score WHERE team_id=$1)")
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(teamID).Scan(&s.ID, &s.Score, &s.Timestamp)
if err != nil {
return
}
s.TeamID = teamID
return
}