forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offinemessages.go
49 lines (45 loc) · 858 Bytes
/
offinemessages.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
package db
import (
"database/sql"
"sync"
"time"
)
type OfflineMessagesDB struct {
db *sql.DB
lock sync.RWMutex
}
func (o *OfflineMessagesDB) Put(url string) error {
o.lock.Lock()
defer o.lock.Unlock()
tx, err := o.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("insert into offlinemessages(url, timestamp) values(?,?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(url, int(time.Now().Unix()))
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (o *OfflineMessagesDB) Has(url string) bool {
o.lock.RLock()
defer o.lock.RUnlock()
stmt, err := o.db.Prepare("select url from offlinemessages where url=?")
if err != nil {
return false
}
defer stmt.Close()
var ret string
err = stmt.QueryRow(url).Scan(&ret)
if err != nil {
return false
}
return true
}