forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txmetadata.go
101 lines (94 loc) · 2.35 KB
/
txmetadata.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
package db
import (
"database/sql"
"github.com/OpenBazaar/openbazaar-go/repo"
"sync"
)
type TxMetadataDB struct {
modelStore
}
func NewTransactionMetadataStore(db *sql.DB, lock *sync.Mutex) repo.TransactionMetadataStore {
return &TxMetadataDB{modelStore{db, lock}}
}
func (t *TxMetadataDB) Put(m repo.Metadata) error {
t.lock.Lock()
defer t.lock.Unlock()
tx, _ := t.db.Begin()
stmt, err := tx.Prepare("insert or replace into txmetadata(txid, address, memo, orderID, thumbnail, canBumpFee) values(?,?,?,?,?,?)")
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
bumpable := 0
if m.CanBumpFee {
bumpable = 1
}
_, err = stmt.Exec(m.Txid, m.Address, m.Memo, m.OrderId, m.Thumbnail, bumpable)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (t *TxMetadataDB) Get(txid string) (repo.Metadata, error) {
t.lock.Lock()
defer t.lock.Unlock()
var m repo.Metadata
stmt, err := t.db.Prepare("select txid, address, memo, orderID, thumbnail, canBumpFee from txmetadata where txid=?")
defer stmt.Close()
var id, address, memo, orderId, thumbnail string
var canBumpFee int
err = stmt.QueryRow(txid).Scan(&id, &address, &memo, &orderId, &thumbnail, &canBumpFee)
if err != nil {
return m, err
}
bumpable := false
if canBumpFee > 0 {
bumpable = true
}
m = repo.Metadata{id, address, memo, orderId, thumbnail, bumpable}
return m, nil
}
func (t *TxMetadataDB) GetAll() (map[string]repo.Metadata, error) {
t.lock.Lock()
defer t.lock.Unlock()
ret := make(map[string]repo.Metadata)
stm := "select txid, address, memo, orderID, thumbnail, canBumpFee from txmetadata"
rows, err := t.db.Query(stm)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var txid, address, memo, orderId, thumbnail string
var canBumpFee int
if err := rows.Scan(&txid, &address, &memo, &orderId, &thumbnail, &canBumpFee); err != nil {
return ret, err
}
bumpable := false
if canBumpFee > 0 {
bumpable = true
}
m := repo.Metadata{
Txid: txid,
Address: address,
Memo: memo,
OrderId: orderId,
Thumbnail: thumbnail,
CanBumpFee: bumpable,
}
ret[txid] = m
}
return ret, nil
}
func (t *TxMetadataDB) Delete(txid string) error {
t.lock.Lock()
defer t.lock.Unlock()
_, err := t.db.Exec("delete from txmetadata where txid=?", txid)
if err != nil {
return err
}
return nil
}