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
|
package database
import (
"git.adyxax.org/adyxax/trains/pkg/model"
)
func (env *DBEnv) CountStops() (i int, err error) {
query := `SELECT count(*) from stops;`
err = env.db.QueryRow(query).Scan(&i)
if err != nil {
return 0, newQueryError("Could not run database query: most likely the schema is corrupted", err)
}
return
}
func (env *DBEnv) GetStop(id string) (*model.Stop, error) {
query := `SELECT name FROM stops WHERE id = $1;`
stop := model.Stop{Id: id}
err := env.db.QueryRow(
query,
id,
).Scan(
&stop.Name,
)
if err != nil {
return nil, newQueryError("Could not run database query", err)
}
return &stop, nil
}
func (env *DBEnv) GetStops() (stops []model.Stop, err error) {
query := `SELECT id, name FROM stops;`
rows, err := env.db.Query(query)
if err != nil {
return nil, newQueryError("Could not run database query", err)
}
defer rows.Close()
for rows.Next() {
var stop model.Stop
if err := rows.Scan(&stop.Id, &stop.Name); err != nil {
return nil, newQueryError("Could not run database query", err)
}
stops = append(stops, stop)
}
return
}
func (env *DBEnv) ReplaceAndImportStops(stops []model.Stop) error {
pre_query := `DELETE FROM stops;`
query := `
INSERT INTO stops
(id, name)
VALUES
($1, $2);`
tx, err := env.db.Begin()
if err != nil {
return newTransactionError("Could not Begin()", err)
}
_, err = tx.Exec(pre_query)
if err != nil {
tx.Rollback()
return newQueryError("Could not run database query: most likely the schema is corrupted", err)
}
for i := 0; i < len(stops); i++ {
_, err = tx.Exec(
query,
stops[i].Id,
stops[i].Name,
)
if err != nil {
tx.Rollback()
return newQueryError("Could not run database query", err)
}
}
if err := tx.Commit(); err != nil {
return newTransactionError("Could not commit transaction", err)
}
return nil
}
|