forked from textileio/go-textile
/
threads.go
128 lines (116 loc) · 2.65 KB
/
threads.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
package db
import (
"database/sql"
"sync"
"github.com/textileio/textile-go/repo"
)
type ThreadDB struct {
modelStore
}
func NewThreadStore(db *sql.DB, lock *sync.Mutex) repo.ThreadStore {
return &ThreadDB{modelStore{db, lock}}
}
func (c *ThreadDB) Add(thread *repo.Thread) error {
c.lock.Lock()
defer c.lock.Unlock()
tx, err := c.db.Begin()
if err != nil {
return err
}
stm := `insert into threads(id, key, sk, name, schema, initiator, type, state, head) values(?,?,?,?,?,?,?,?,?)`
stmt, err := tx.Prepare(stm)
if err != nil {
log.Errorf("error in tx prepare: %s", err)
return err
}
defer stmt.Close()
_, err = stmt.Exec(
thread.Id,
thread.Key,
thread.PrivKey,
thread.Name,
thread.Schema,
thread.Initiator,
int(thread.Type),
int(thread.State),
thread.Head,
)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (c *ThreadDB) Get(id string) *repo.Thread {
c.lock.Lock()
defer c.lock.Unlock()
ret := c.handleQuery("select * from threads where id='" + id + "';")
if len(ret) == 0 {
return nil
}
return &ret[0]
}
func (c *ThreadDB) GetByKey(key string) *repo.Thread {
c.lock.Lock()
defer c.lock.Unlock()
ret := c.handleQuery("select * from threads where key='" + key + "';")
if len(ret) == 0 {
return nil
}
return &ret[0]
}
func (c *ThreadDB) List() []repo.Thread {
c.lock.Lock()
defer c.lock.Unlock()
return c.handleQuery("select * from threads;")
}
func (c *ThreadDB) Count() int {
c.lock.Lock()
defer c.lock.Unlock()
row := c.db.QueryRow("select Count(*) from threads;")
var count int
row.Scan(&count)
return count
}
func (c *ThreadDB) UpdateHead(id string, head string) error {
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.db.Exec("update threads set head=? where id=?", head, id)
return err
}
func (c *ThreadDB) Delete(id string) error {
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.db.Exec("delete from threads where id=?", id)
return err
}
func (c *ThreadDB) handleQuery(stm string) []repo.Thread {
var ret []repo.Thread
rows, err := c.db.Query(stm)
if err != nil {
log.Errorf("error in db query: %s", err)
return nil
}
for rows.Next() {
var id, key, name, schema, initiator, head string
var skb []byte
var typeInt, stateInt int
if err := rows.Scan(&id, &key, &skb, &name, &schema, &initiator, &typeInt, &stateInt, &head); err != nil {
log.Errorf("error in db scan: %s", err)
continue
}
ret = append(ret, repo.Thread{
Id: id,
Key: key,
PrivKey: skb,
Name: name,
Schema: schema,
Initiator: initiator,
Type: repo.ThreadType(typeInt),
State: repo.ThreadState(stateInt),
Head: head,
})
}
return ret
}