/
sql.go
92 lines (77 loc) · 2.42 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
package main
import (
"database/sql"
_ "github.com/lib/pq"
)
func insertDataSQL(n int) {
// Connect to the database.
db, err := sql.Open(dialect, getDSN())
if err != nil {
fatal("can't connect to the database", err)
}
defer func() {
fatal("can't close the connection to the database", db.Close())
}()
// Get the last index.
last := getLastBusStopID(db)
// Randomly generate and write fake data to the database.
for i := last + 1; i <= n + last; i++ {
busStop := getBusStop(i)
_, err := db.Exec("INSERT INTO bus_stops(id, updated_at, number, latitude, longitude, siteats, city_site) VALUES($1, $2, $3, $4, $5, $6, $7)",
busStop.ID, busStop.UpdatedAt, busStop.Number, busStop.Latitude, busStop.Longitude, busStop.SiteATS, busStop.CitySite)
if err != nil {
fatal("can't insert bus stops", err)
}
}
}
func updateDataSQL(n int) {
// Connect to the database.
db, err := sql.Open(dialect, getDSN())
if err != nil {
fatal("can't connect to the database", err)
}
defer func() {
fatal("can't close the connection to the database", db.Close())
}()
// Get the first index.
first := getFirstBusStopID(db)
// Randomly generate and write fake data to the database.
for i := first; i < n; i++ {
busStop := getBusStopForUpdate(i)
_, err := db.Exec("UPDATE bus_stops SET updated_at = $1, number = $2, latitude = $3, longitude = $4, siteats = $5, city_site = $6 WHERE id = $7",
busStop.UpdatedAt,
busStop.Number,
busStop.Latitude,
busStop.Longitude,
busStop.SiteATS,
busStop.CitySite,
i)
if err != nil {
fatal("can't update bus stops", err)
}
}
}
func selectDataSQL(n int) {
// Connect to the database.
db, err := sql.Open(dialect, getDSN())
if err != nil {
fatal("can't connect to the database", err)
}
defer func() {
fatal("can't close the connection to the database", db.Close())
}()
// Get the first index.
first := getFirstBusStopID(db)
// Randomly generate and write fake data to the database.
for i := first; i < n; i++ {
rows, err := db.Query("SELECT id, updated_at, number, latitude, longitude, siteats, city_site FROM bus_stops WHERE id = $1", i)
if err != nil {
fatal("can't query from bus stops", err)
}
var busStop BusStop
for rows.Next() {
fatal("can't scan from a row", rows.Scan(&busStop.ID, &busStop.UpdatedAt, &busStop.Number, &busStop.Latitude, &busStop.Longitude, &busStop.SiteATS, &busStop.CitySite))
}
fatal("can't close row", rows.Close())
}
}