-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from amnuts/location-lookup
Location lookup added
- Loading branch information
Showing
9 changed files
with
335 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
func NewCacheDatabase(dbFile string) (*CacheDatabase, error) { | ||
sqliteDb, err := sql.Open("sqlite", dbFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cache := &CacheDatabase{DB: sqliteDb} | ||
err = cache.initCache() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cache, nil | ||
} | ||
|
||
func (cache *CacheDatabase) initCache() error { | ||
// Create table if it doesn't exist | ||
createTableSql := `CREATE TABLE IF NOT EXISTS locations ( | ||
geohash TEXT PRIMARY KEY, | ||
latitude REAL NOT NULL, | ||
longitude REAL NOT NULL, | ||
country TEXT NOT NULL, | ||
division TEXT NOT NULL, | ||
city TEXT NOT NULL, | ||
place TEXT NOT NULL, | ||
url TEXT NOT NULL | ||
);` | ||
|
||
_, err := cache.DB.Exec(createTableSql) | ||
checkErr(err, "Failed to create locations table") | ||
|
||
return err | ||
} | ||
|
||
func (cache *CacheDatabase) InsertLocation(url string, location GeoLocationMetadata) error { | ||
stmt, err := cache.DB.Prepare("INSERT INTO locations(geohash, latitude, longitude, country, division, city, place, url) VALUES(?, ?, ?, ?, ?, ?, ?, ?)") | ||
_, err = stmt.Exec(location.Geohash, location.Latitude, location.Longitude, location.Country, location.Division, location.City, location.Place, url) | ||
return err | ||
} | ||
|
||
func (cache *CacheDatabase) GetLocation(geohash string) (*GeoLocationMetadata, error) { | ||
|
||
var rowGeohash string | ||
var rowLatitude float64 | ||
var rowLongitude float64 | ||
var rowCountry string | ||
var rowDivision string | ||
var rowCity string | ||
var rowPlace string | ||
var rowUrl string | ||
|
||
if err := cache.DB.QueryRow("SELECT * FROM locations WHERE geohash = ?", geohash).Scan(&rowGeohash, &rowLatitude, &rowLongitude, &rowCountry, &rowDivision, &rowCity, &rowPlace, &rowUrl); err != nil { | ||
return nil, err | ||
} | ||
|
||
var location *GeoLocationMetadata | ||
location = &GeoLocationMetadata{ | ||
Geohash: rowGeohash, | ||
Latitude: rowLatitude, | ||
Longitude: rowLongitude, | ||
Country: rowCountry, | ||
Division: rowDivision, | ||
City: rowCity, | ||
Place: rowPlace, | ||
} | ||
|
||
return location, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.