-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
70 lines (63 loc) · 1.71 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
package main
import (
"database/sql"
"github.com/go-sql-driver/mysql"
"log"
"time"
)
type note struct {
Id int64 `json:"id"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Title string `json:"title"`
Address string `json:"address"`
Content string `json:"content"`
UpdateTime int64 `json:"updateTime"`
Visible bool `json:"visible"`
UpdateTimeFormatted string `json:"updateTimeFormatted"`
}
var db *sql.DB
func initDB() {
cfg := mysql.Config{
User: "imoonkin",
Passwd: "passwd",
Net: "tcp",
Addr: "hostmysql:3306",
DBName: "sharednotedb",
}
var err error
if db, err = sql.Open("mysql", cfg.FormatDSN()); err != nil {
log.Panicln(err)
}
if err = db.Ping(); err != nil {
log.Panicln(err)
}
log.Println("db connected")
}
func addNote(n note) error {
if _, err := db.Exec(
"insert into sharedNote (latitude,longitude,title,address,content,updateTime,visible) values (?,?,?,?,?,?,?)",
n.Latitude, n.Longitude, n.Title, n.Address, n.Content, time.Now().Unix(), true); err != nil {
return err
}
return nil
}
func rangeFetch(lat1, lng1, lat2, lng2 float64) ([]note, error) {
ret := []note{}
rows, err := db.Query("select id,latitude,longitude,title,address,content,updateTime,visible from sharedNote where latitude between ? and ? and longitude between ? and ? limit 100", lat1, lat2, lng1, lng2)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var n note
if err = rows.Scan(&n.Id, &n.Latitude, &n.Longitude, &n.Title, &n.Address, &n.Content, &n.UpdateTime, &n.Visible); err != nil {
return ret, err
}
ret = append(ret, n)
}
if rows.Err() != nil {
return ret, rows.Err()
}
return ret, nil
}