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
|
package db
func (p *Phlox) CreateEndpoint(name, path, address string) (Endpoint, error) {
var id int
row := p.db.QueryRow("select max(endpointid) from endpoints;")
err := row.Scan(&id)
if err != nil {
return Endpoint{}, err
}
id += 1
_, err = p.db.Exec(
"insert into endpoints values (?, ?, ?, ?)",
id, name, path, address,
)
if err != nil {
return Endpoint{}, err
}
endpoint := Endpoint{
Id: id,
Name: name,
Path: path,
Address: address,
}
return endpoint, nil
}
func (p *Phlox) DeleteEndpoint(endpoint Endpoint) error {
_, err := p.db.Exec("delete from endpoints where endpointid = ?;", endpoint.Id)
return err
}
func extractEndpoint(s Scanner) (Endpoint, error) {
var endpoint Endpoint
err := s.Scan(
&endpoint.Id,
&endpoint.Name,
&endpoint.Path,
&endpoint.Address,
)
return endpoint, err
}
func (p *Phlox) SetEndpointPath(endpoint Endpoint, path string) error {
_, err := p.db.Exec("update endpoints set path=? where endpointid=?", path, endpoint.Id)
return err
}
func (p *Phlox) SetEndpointAddress(endpoint Endpoint, address string) error {
_, err := p.db.Exec("update endpoints set address=? where endpointid=?", address, endpoint.Id)
return err
}
func queryEndpoint(p *Phlox, query, param string) (Endpoint, error) {
row := p.db.QueryRow(query, param)
endpoint, err := extractEndpoint(row)
return endpoint, err
}
func (p *Phlox) GetEndpointByName(name string) (Endpoint, error) {
return queryEndpoint(p, "select * from endpoints where name = ?;", name)
}
func (p *Phlox) GetEndpointByPath(path string) (Endpoint, error) {
return queryEndpoint(p, "select * from endpoints where path = ?;", path)
}
func (p *Phlox) GetEndpointByAddress(address string) (Endpoint, error) {
return queryEndpoint(p, "select * from endpoints where address = ?;", address)
}
func (p *Phlox) AllEndpoints() ([]Endpoint, error) {
endpoints := make([]Endpoint, 0)
rows, err := p.db.Query("select * from endpoints;")
if err != nil {
return endpoints, err
}
defer rows.Close()
for rows.Next() {
endpoint, err := extractEndpoint(rows)
if err != nil {
return endpoints, err
}
endpoints = append(endpoints, endpoint)
}
return endpoints, nil
}
|