forked from andrewl/wherearetheyflyingto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destinationfinder_cache.go
55 lines (47 loc) · 1.34 KB
/
destinationfinder_cache.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
// Package destinationfinder provides functionality for
// determining where a flight with a given callsign is
// destined.
package destinationfinder
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
/**
* Retrieves the lat long of the destination (as a simple string, we're not interested in doing
* any real processing with this, just using it as an index.
**/
type DestinationFinderCache struct {
// pointer to sqlite3 db
db *sql.DB
}
func (dfc DestinationFinderCache) Open(db *sql.DB) error {
dfc.db = db
create_table_sql := `
create table dest_cache (
callsign text,
destination_lat_long text,
`
_, err := dfc.db.Exec(create_table_sql)
if err != nil {
//display a message?
//logger.Log("msg", "create cache table failed. This probably isn't a problem if the table already exists.", "err", err)
}
return nil
}
func (dfc DestinationFinderCache) Cache_get(callsign string) string {
if dfc.db == nil {
return ""
}
var latlong string
rows, err := dfc.db.Query("select destination_lat_long from dest_cache where callsign = '" + callsign + "'")
if err != nil {
_ = rows.Scan(&latlong)
}
return latlong
}
func (dfc DestinationFinderCache) Cache_set(callsign string, latlong string) {
if dfc.db == nil {
return
}
dfc.db.Exec("insert into dest_cache(callsign,destination_lat_long) values(?,?)", callsign, latlong)
}