-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_finders.go
103 lines (74 loc) · 1.75 KB
/
database_finders.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
// note: "package global" db object
var db *sql.DB
func InitDb() {
var err error
db, err = sql.Open( "sqlite3", "./football.db" )
checkErr( err )
}
func FetchTeamsByEvent( event Event ) []Team {
query :=
`SELECT
t.[key],
t.title,
t.code
FROM teams t
INNER JOIN events_teams et ON et.team_id = t.id
INNER JOIN events e ON e.id = et.event_id
WHERE e.[key] = ?`
rows, err := db.Query( query, event.Key )
checkErr( err )
defer rows.Close()
teams := []Team{}
for rows.Next() {
var r Team
err = rows.Scan( &r.Key, &r.Title, &r.Code )
checkErr( err )
teams = append( teams, r ) // add new row
}
return teams
}
func FetchEventByKey( key string ) Event {
query :=
`SELECT
e.[key] AS event_key,
l.title || ' ' || s.title AS event_name
FROM events e
INNER JOIN seasons s ON s.id = e.season_id
INNER JOIN leagues l ON l.id = e.league_id
WHERE e.[key] = ?`
var r Event
err := db.QueryRow( query, key ).Scan( &r.Key, &r.Title )
checkErr( err )
return r
}
func FetchEvents() []Event {
query :=
`SELECT
e.[key] AS event_key,
l.title || ' ' || s.title AS event_name
FROM events e
INNER JOIN seasons s ON s.id = e.season_id
INNER JOIN leagues l ON l.id = e.league_id`
rows, err := db.Query( query )
checkErr( err )
defer rows.Close()
events := []Event{}
for rows.Next() {
var r Event
err = rows.Scan( &r.Key, &r.Title )
checkErr( err )
events = append( events, r ) // add new row
}
return events
}