-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
385 lines (333 loc) · 8.15 KB
/
store.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package guide
import (
"database/sql"
"errors"
_ "modernc.org/sqlite"
)
type Storage interface {
CreateGuide(*guide) error
GetGuidebyID(int64) (*guide, error)
UpdateGuide(*guide) error
DeleteGuide(int64) error
GetAllGuides() []guide
Search(string) ([]guide, error)
CountGuides() int
GetPoi(int64, int64) (*pointOfInterest, error)
CreatePoi(*pointOfInterest) error
UpdatePoi(*pointOfInterest) error
DeletePoi(int64, int64) error
GetAllPois(int64) []pointOfInterest
}
type sqliteStore struct {
db *sql.DB
}
func OpenSQLiteStorage(dbPath string) (Storage, error) {
if dbPath == "" {
return &sqliteStore{}, errors.New("db source cannot be empty")
}
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return &sqliteStore{}, err
}
for _, stmt := range []string{pragmaWALEnabled, pragma500BusyTimeout, pragmaForeignKeysON} {
_, err = db.Exec(stmt, nil)
if err != nil {
return &sqliteStore{}, err
}
}
_, err = db.Exec(createGuideTable)
if err != nil {
return &sqliteStore{}, err
}
_, err = db.Exec(createPoiTable)
if err != nil {
return &sqliteStore{}, err
}
store := sqliteStore{
db: db,
}
return &store, nil
}
func (s *sqliteStore) CreateGuide(guide *guide) error {
stmt, err := s.db.Prepare(insertGuide)
if err != nil {
return err
}
defer stmt.Close()
rs, err := stmt.Exec(guide.Name, guide.Description, guide.Coordinate.Latitude, guide.Coordinate.Longitude)
if err != nil {
return err
}
lastInsertID, err := rs.LastInsertId()
if err != nil {
return err
}
guide.Id = lastInsertID
return nil
}
func (s *sqliteStore) GetGuidebyID(id int64) (*guide, error) {
var (
name string
description string
latitude float64
longitude float64
)
err := s.db.QueryRow(getGuide, id).Scan(&name, &description, &latitude, &longitude)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
default:
g := guide{
Id: id,
Name: name,
Description: description,
Coordinate: coordinate{
Latitude: latitude,
Longitude: longitude,
},
Pois: nil,
}
return &g, nil
}
}
func (s *sqliteStore) UpdateGuide(g *guide) error {
stmt, err := s.db.Prepare(updateGuide)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(g.Name, g.Description, g.Coordinate.Latitude, g.Coordinate.Longitude, g.Id)
if err != nil {
return err
}
return nil
}
func (s *sqliteStore) DeleteGuide(id int64) error {
stmt, err := s.db.Prepare(deleteGuide)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(id)
if err != nil {
return err
}
return nil
}
func (s *sqliteStore) GetAllGuides() []guide {
rows, err := s.db.Query(getAllGuides)
if err != nil {
return []guide{}
}
guides := make([]guide, 0)
for rows.Next() {
var (
id int64
name string
description string
latitude float64
longitude float64
)
err = rows.Scan(&id, &name, &description, &latitude, &longitude)
if err != nil {
return []guide{}
}
g := guide{
Id: id,
Name: name,
Description: description,
Coordinate: coordinate{Latitude: latitude, Longitude: longitude},
}
guides = append(guides, g)
}
if err = rows.Err(); err != nil {
return []guide{}
}
return guides
}
func (s *sqliteStore) CreatePoi(poi *pointOfInterest) error {
stmt, err := s.db.Prepare(insertPoi)
if err != nil {
return err
}
defer stmt.Close()
rs, err := stmt.Exec(poi.Name, poi.Description, poi.Coordinate.Latitude, poi.Coordinate.Longitude, poi.GuideID)
if err != nil {
return err
}
lastInsertID, err := rs.LastInsertId()
if err != nil {
return err
}
poi.Id = lastInsertID
return nil
}
func (s *sqliteStore) UpdatePoi(poi *pointOfInterest) error {
stmt, err := s.db.Prepare(updatePoi)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(poi.Name, poi.Description, poi.Coordinate.Latitude, poi.Coordinate.Longitude, poi.Id)
if err != nil {
return err
}
return nil
}
func (s *sqliteStore) GetPoi(guideID, poiID int64) (*pointOfInterest, error) {
var (
name string
description string
latitude float64
longitude float64
)
err := s.db.QueryRow(getPoi, guideID, poiID).Scan(&name, &description, &latitude, &longitude)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
default:
p := pointOfInterest{
Id: poiID,
GuideID: guideID,
Coordinate: coordinate{
Latitude: latitude,
Longitude: longitude,
},
Name: name,
Description: description,
}
return &p, nil
}
}
func (s *sqliteStore) DeletePoi(guideId, poiID int64) error {
stmt, err := s.db.Prepare(deletePoi)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(guideId, poiID)
if err != nil {
return err
}
return nil
}
func (s *sqliteStore) GetAllPois(guideId int64) []pointOfInterest {
rows, err := s.db.Query(getAllPois, guideId)
if err != nil {
return []pointOfInterest{}
}
pois := make([]pointOfInterest, 0)
for rows.Next() {
var (
id int64
name string
description string
latitude float64
longitude float64
)
err = rows.Scan(&id, &name, &description, &latitude, &longitude)
if err != nil {
return []pointOfInterest{}
}
p := pointOfInterest{
Id: id,
Name: name,
Description: description,
Coordinate: coordinate{Latitude: latitude, Longitude: longitude},
GuideID: guideId,
}
pois = append(pois, p)
}
if err = rows.Err(); err != nil {
return []pointOfInterest{}
}
return pois
}
func (s *sqliteStore) Search(query string) ([]guide, error) {
rows, err := s.db.Query(searchGuides, "%"+query+"%")
if err != nil {
return nil, err
}
results := make([]guide, 0)
for rows.Next() {
var (
id int64
name string
description string
latitude float64
longitude float64
)
err = rows.Scan(&id, &name, &description, &latitude, &longitude)
if err != nil {
return nil, err
}
g := guide{
Id: id,
Name: name,
Description: description,
Coordinate: coordinate{
Latitude: latitude,
Longitude: longitude,
},
}
results = append(results, g)
}
if err = rows.Err(); err != nil {
return nil, err
}
return results, nil
}
func (s *sqliteStore) CountGuides() int {
rows, err := s.db.Query(countGuides)
if err != nil {
return 0
}
var count int
for rows.Next() {
err = rows.Scan(&count)
if err != nil {
return 0
}
}
if err = rows.Err(); err != nil {
return 0
}
return count
}
const pragmaWALEnabled = `PRAGMA journal_mode = WAL;`
const pragma500BusyTimeout = `PRAGMA busy_timeout = 5000;`
const pragmaForeignKeysON = `PRAGMA foreign_keys = on;`
const createGuideTable = `
CREATE TABLE IF NOT EXISTS guide(
Id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
CHECK (name <> ''));`
const createPoiTable = `
CREATE TABLE IF NOT EXISTS poi(
Id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
guideId INTEGER NOT NULL,
FOREIGN KEY(guideId) REFERENCES guide(Id),
CHECK (name <> ''));`
const insertGuide = `INSERT INTO guide(name, description, latitude, longitude ) VALUES (?, ?, ?, ?);`
const insertPoi = `INSERT INTO poi(name, description, latitude, longitude, guideId ) VALUES (?, ?, ?, ?, ?);`
const getGuide = `SELECT name, description, latitude, longitude FROM guide WHERE Id = ?`
const getPoi = `SELECT name, description, latitude, longitude FROM poi WHERE guideid = ? AND Id = ?`
const updateGuide = `UPDATE guide SET name = ?, description = ?, latitude = ?, longitude = ? WHERE Id = ?`
const updatePoi = `UPDATE poi SET name = ?, description = ?, latitude = ?, longitude = ? WHERE Id = ?`
const deleteGuide = `DELETE FROM guide WHERE Id = ?`
const deletePoi = `DELETE FROM poi WHERE guideid =? AND Id = ?`
const getAllGuides = `SELECT Id,name, description, latitude, longitude FROM guide`
const getAllPois = `SELECT Id, name, description, latitude, longitude FROM poi WHERE guideid = ?`
const searchGuides = `SELECT Id,name, description, latitude, longitude FROM guide WHERE name LIKE ?`
const countGuides = `SELECT COUNT (*) FROM guide`