-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (57 loc) · 1.44 KB
/
main.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
package main
import (
"fmt"
"os"
"postgis_test/data"
"postgis_test/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
var db *gorm.DB
func main() {
// initalize gorm db
x := Initalize()
if x != nil {
fmt.Println(x)
}
data.CreateTestData(db)
var list []models.Location
// create query strings
q := ComposeQuery(51.990830, 5.967340, 5000)
qPag := ComposeQueryLimit(51.990830, 5.967340, 5000, 10, 0)
db.Raw(q).Scan(&list)
db.Raw(qPag).Scan(&list)
for _, l := range list {
fmt.Println(l)
}
os.Exit(0)
}
func ComposeQuery(x, y float64, radius int) string {
return fmt.Sprintf(`SELECT * FROM locations
WHERE ST_DWithin(ST_MakePoint(geo_x,geo_y)::geography,ST_MakePoint(%f, %f)::geography,%d)
LIMIT 1000
`, x, y, radius)
}
func ComposeQueryLimit(x, y float64, radius, limit, offset int) string {
return fmt.Sprintf(`SELECT * FROM locations
WHERE ST_DWithin(ST_MakePoint(geo_x,geo_y)::geography,ST_MakePoint(%f, %f)::geography,%d)
LIMIT %d
OFFSET %d
`, x, y, radius, limit, offset)
}
func Initalize() error {
username := "test"
password := "test"
dbName := "test"
dbHost := "localhost"
dbURI := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s", dbHost, username, dbName, password)
conn, err := gorm.Open("postgres", dbURI)
if err != nil {
fmt.Println("We cant op open a DATABASE")
return err
}
db = conn
db = db.Debug()
db.AutoMigrate(&models.Location{})
return nil
}