forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
60 lines (47 loc) · 1.12 KB
/
nodes.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
package datastore
import (
"database/sql"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type nodestore struct {
*sql.DB
}
func (db *nodestore) Get(id int64) (*model.Node, error) {
var node = new(model.Node)
var err = meddler.Load(db, nodeTable, node, id)
return node, err
}
func (db *nodestore) GetList() ([]*model.Node, error) {
var nodes = []*model.Node{}
var err = meddler.QueryAll(db, &nodes, rebind(nodeListQuery))
return nodes, err
}
func (db *nodestore) Count() (int, error) {
var count int
var err = db.QueryRow(rebind(nodeCountQuery)).Scan(&count)
return count, err
}
func (db *nodestore) Create(node *model.Node) error {
return meddler.Insert(db, nodeTable, node)
}
func (db *nodestore) Update(node *model.Node) error {
return meddler.Update(db, nodeTable, node)
}
func (db *nodestore) Delete(node *model.Node) error {
var _, err = db.Exec(rebind(nodeDeleteStmt), node.ID)
return err
}
const nodeTable = "nodes"
const nodeListQuery = `
SELECT *
FROM nodes
ORDER BY node_addr
`
const nodeCountQuery = `
SELECT COUNT(*) FROM nodes
`
const nodeDeleteStmt = `
DELETE FROM nodes
WHERE node_id=?
`