-
Notifications
You must be signed in to change notification settings - Fork 11
/
datastore.go
70 lines (55 loc) · 1.15 KB
/
datastore.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 pkg
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"syreclabs.com/go/faker"
)
type DataStore struct {
db *sql.DB
}
func (ds *DataStore) GetCities() ([]*City, error) {
var cities []*City
rows, err := ds.db.Query("SELECT id, name FROM cities")
if err != nil {
return cities, err
}
defer rows.Close()
for rows.Next() {
var (
id int
name string
)
err := rows.Scan(&id, &name)
if err != nil {
return cities, err
}
city := NewCity(id, name)
cities = append(cities, city)
}
return cities, nil
}
func (ds *DataStore) init() error {
_, err := ds.db.Exec("CREATE TABLE cities (id integer, name varchar(64))")
if err != nil {
return err
}
sqlStmt := `INSERT INTO cities (id, name) VALUES ($1, $2)`
for i := 1; i < 4; i++ {
_, err := ds.db.Exec(sqlStmt, i, faker.Address().City())
if err != nil {
return err
}
}
return nil
}
func NewDataStore(db *sql.DB) (*DataStore, error) {
datastore := DataStore{db: db}
err := datastore.init()
if err != nil {
return nil, err
}
return &datastore, nil
}
func OpenConnection(settings *Settings) (*sql.DB, error) {
return sql.Open("sqlite3", settings.DSN)
}