-
Notifications
You must be signed in to change notification settings - Fork 0
/
spots.go
91 lines (77 loc) · 1.52 KB
/
spots.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
package db
import (
"log"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // sqlite driver
)
var SpotDb *sqlx.DB
// OpenSpotDb creates the connection to the spot database
func OpenSpotDb() error {
var err error
// if already open, close it
if SpotDb != nil {
err = CloseSpotDb()
if err != nil {
log.Printf("%+v", err)
return err
}
}
// make connection
SpotDb, err = sqlx.Connect("sqlite3", "file:spot.db?mode=memory&cache=shared&_loc=UTC")
if err != nil {
log.Printf("%+v", err)
return err
}
return nil
}
// CloseSpotDb closes the connection to the spot database
func CloseSpotDb() error {
if SpotDb != nil {
err := SpotDb.Close()
if err != nil {
log.Printf("%+v", err)
return err
}
SpotDb = nil
}
return nil
}
// NewSpotDb initializes a new spot database
func NewSpotDb() error {
err := CloseSpotDb()
if err != nil {
log.Printf("%+v", err)
return err
}
// open new database
err = OpenSpotDb()
if err != nil {
log.Printf("%+v", err)
return err
}
// create spots table
_, err = SpotDb.Exec(`
create table spots (
id integer primary key asc not null,
timestamp text null,
call text null,
band text null,
frequency text null,
comments text null,
spotter text null
)
`)
if err != nil {
log.Printf("%+v", err)
return err
}
// index for what uniquely defines a spot
_, err = SpotDb.Exec(`
create unique index unique_spots on spots(timestamp, call, band, spotter)
`)
if err != nil {
log.Printf("%+v", err)
return err
}
return nil
}