-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
113 lines (100 loc) · 2.79 KB
/
db.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
106
107
108
109
110
111
112
113
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
func startDb() *sql.DB {
log.Println(" => Remove ./data/gtfs.db")
os.Remove("data/gtfs.db")
os.Remove("data/gtfs.db-shm")
os.Remove("data/gtfs.db-wal")
file, err := os.Create("data/gtfs.db")
log.Println(" => Creating ./data/gtfs.db")
if err != nil {
log.Fatal(err.Error())
}
defer file.Close()
log.Println("./data/gtfs.db created")
sqliteDatabase, _ := sql.Open("sqlite3", "./data/gtfs.db")
configure(sqliteDatabase)
createTableRoute(sqliteDatabase)
createTableStopTime(sqliteDatabase)
return sqliteDatabase
}
func configure(db *sql.DB) {
ddl := `
PRAGMA automatic_index = ON;
PRAGMA cache_size = 32768;
PRAGMA cache_spill = OFF;
PRAGMA foreign_keys = ON;
PRAGMA journal_size_limit = 67110000;
PRAGMA locking_mode = NORMAL;
PRAGMA page_size = 4096;
PRAGMA recursive_triggers = ON;
PRAGMA secure_delete = ON;
PRAGMA synchronous = NORMAL;
PRAGMA temp_store = MEMORY;
PRAGMA journal_mode = WAL;
PRAGMA wal_autocheckpoint = 16384;
`
db.Exec(ddl)
log.Println(" => Configure DB")
}
func createTableRoute(db *sql.DB) {
createTable := `
CREATE TABLE TABLE_ROUTE (
_ID INTEGER PRIMARY KEY,
COLUMN_ROUTE_ID TEXT,
COLUMN_ROUTE_SHORT_NAME TEXT
);`
log.Println(" => Creating TABLE_ROUTE...")
statement, err := db.Prepare(createTable)
if err != nil {
log.Fatal(err.Error())
}
statement.Exec()
log.Println("TABLE_ROUTE created")
}
func createTableStopTime(db *sql.DB) {
createTable := `
CREATE TABLE TABLE_STOP_TIME (
_ID INTEGER PRIMARY KEY,
COLUMN_TRIP_ID TEXT,
COLUMN_ROUTE_ID TEXT,
COLUMN_ARRIVAL_TIME TEXT,
COLUMN_DEPARTURE_TIME TEXT,
COLUMN_STOP_ID TEXT,
COLUMN_STOP_HEADSIGN TEXT
);`
log.Println(" => Creating TABLE_STOP_TIME...")
statement, err := db.Prepare(createTable)
if err != nil {
log.Fatal(err.Error())
}
statement.Exec()
log.Println("TABLE_STOP_TIME created")
}
func insertRoute(db *sql.DB, routeID string, routeShortName string) {
insertSQL := `INSERT INTO TABLE_ROUTE(COLUMN_ROUTE_ID, COLUMN_ROUTE_SHORT_NAME) VALUES (?, ?)`
statement, err := db.Prepare(insertSQL)
if err != nil {
log.Fatalln(err.Error())
}
_, err = statement.Exec(&routeID, &routeShortName)
if err != nil {
log.Fatalln(err.Error())
}
}
func insertStopTime(db *sql.DB, tripID string, routeID string, arrivalTime string, departureTime string, stopID string, stopHeadsign string) {
insertSQL := `INSERT INTO TABLE_STOP_TIME(COLUMN_TRIP_ID, COLUMN_ROUTE_ID, COLUMN_ARRIVAL_TIME, COLUMN_DEPARTURE_TIME, COLUMN_STOP_ID, COLUMN_STOP_HEADSIGN) VALUES (?, ?, ?, ?, ?, ?)`
statement, err := db.Prepare(insertSQL)
if err != nil {
log.Fatalln(err.Error())
}
_, err = statement.Exec(&tripID, &routeID, &arrivalTime, &departureTime, &stopID, &stopHeadsign)
if err != nil {
log.Fatalln(err.Error())
}
}