-
Notifications
You must be signed in to change notification settings - Fork 246
/
database.go
244 lines (208 loc) · 6.72 KB
/
database.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package multiaccounts
import (
"context"
"database/sql"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/migrations"
"github.com/status-im/status-go/sqlite"
)
// Account stores public information about account.
type Account struct {
Name string `json:"name"`
Timestamp int64 `json:"timestamp"`
Identicon string `json:"identicon"`
KeycardPairing string `json:"keycard-pairing"`
KeyUID string `json:"key-uid"`
Images []images.IdentityImage `json:"images"`
}
type MultiAccountMarshaller interface {
ToMultiAccount() *Account
}
type Database struct {
db *sql.DB
identityImageSubscriptions []chan struct{}
}
// InitializeDB creates db file at a given path and applies migrations.
func InitializeDB(path string) (*Database, error) {
db, err := sqlite.OpenUnecryptedDB(path)
if err != nil {
return nil, err
}
err = migrations.Migrate(db)
if err != nil {
return nil, err
}
return &Database{db: db}, nil
}
func (db *Database) Close() error {
return db.db.Close()
}
func (db *Database) GetAccounts() (rst []Account, err error) {
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.keycardPairing, a.keyUid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC")
if err != nil {
return nil, err
}
defer func() {
err = rows.Close()
}()
for rows.Next() {
acc := Account{}
accLoginTimestamp := sql.NullInt64{}
accIdenticon := sql.NullString{}
ii := &images.IdentityImage{}
iiName := sql.NullString{}
iiWidth := sql.NullInt64{}
iiHeight := sql.NullInt64{}
iiFileSize := sql.NullInt64{}
iiResizeTarget := sql.NullInt64{}
err = rows.Scan(
&acc.Name,
&accLoginTimestamp,
&accIdenticon,
&acc.KeycardPairing,
&acc.KeyUID,
&iiName,
&ii.Payload,
&iiWidth,
&iiHeight,
&iiFileSize,
&iiResizeTarget,
)
if err != nil {
return nil, err
}
acc.Timestamp = accLoginTimestamp.Int64
acc.Identicon = accIdenticon.String
ii.KeyUID = acc.KeyUID
ii.Name = iiName.String
ii.Width = int(iiWidth.Int64)
ii.Height = int(iiHeight.Int64)
ii.FileSize = int(iiFileSize.Int64)
ii.ResizeTarget = int(iiResizeTarget.Int64)
if ii.Name == "" && len(ii.Payload) == 0 && ii.Width == 0 && ii.Height == 0 && ii.FileSize == 0 && ii.ResizeTarget == 0 {
ii = nil
}
// Last index
li := len(rst) - 1
// Don't process nil identity images
if ii != nil {
// attach the identity image to a previously created account if present, check keyUID matches
if len(rst) > 0 && rst[li].KeyUID == acc.KeyUID {
rst[li].Images = append(rst[li].Images, *ii)
// else attach the identity image to the newly created account
} else {
acc.Images = append(acc.Images, *ii)
}
}
// Append newly created account only if this is the first loop or the keyUID doesn't match
if len(rst) == 0 || rst[li].KeyUID != acc.KeyUID {
rst = append(rst, acc)
}
}
return rst, nil
}
func (db *Database) SaveAccount(account Account) error {
_, err := db.db.Exec("INSERT OR REPLACE INTO accounts (name, identicon, keycardPairing, keyUid) VALUES (?, ?, ?, ?)", account.Name, account.Identicon, account.KeycardPairing, account.KeyUID)
return err
}
func (db *Database) UpdateAccount(account Account) error {
_, err := db.db.Exec("UPDATE accounts SET name = ?, identicon = ?, keycardPairing = ? WHERE keyUid = ?", account.Name, account.Identicon, account.KeycardPairing, account.KeyUID)
return err
}
func (db *Database) UpdateAccountKeycardPairing(account Account) error {
_, err := db.db.Exec("UPDATE accounts SET keycardPairing = ? WHERE keyUid = ?", account.KeycardPairing, account.KeyUID)
return err
}
func (db *Database) UpdateAccountTimestamp(keyUID string, loginTimestamp int64) error {
_, err := db.db.Exec("UPDATE accounts SET loginTimestamp = ? WHERE keyUid = ?", loginTimestamp, keyUID)
return err
}
func (db *Database) DeleteAccount(keyUID string) error {
_, err := db.db.Exec("DELETE FROM accounts WHERE keyUid = ?", keyUID)
return err
}
// Account images
func (db *Database) GetIdentityImages(keyUID string) (iis []*images.IdentityImage, err error) {
rows, err := db.db.Query(`SELECT key_uid, name, image_payload, width, height, file_size, resize_target FROM identity_images WHERE key_uid = ?`, keyUID)
if err != nil {
return nil, err
}
defer func() {
err = rows.Close()
}()
for rows.Next() {
ii := &images.IdentityImage{}
err = rows.Scan(&ii.KeyUID, &ii.Name, &ii.Payload, &ii.Width, &ii.Height, &ii.FileSize, &ii.ResizeTarget)
if err != nil {
return nil, err
}
iis = append(iis, ii)
}
return iis, nil
}
func (db *Database) GetIdentityImage(keyUID, it string) (*images.IdentityImage, error) {
var ii images.IdentityImage
err := db.db.QueryRow("SELECT key_uid, name, image_payload, width, height, file_size, resize_target FROM identity_images WHERE key_uid = ? AND name = ?", keyUID, it).Scan(&ii.KeyUID, &ii.Name, &ii.Payload, &ii.Width, &ii.Height, &ii.FileSize, &ii.ResizeTarget)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
return &ii, nil
}
func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityImage) (err error) {
// Because SQL INSERTs are triggered in a loop use a tx to ensure a single call to the DB.
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()
for _, ii := range iis {
if ii == nil {
continue
}
ii.KeyUID = keyUID
_, err := tx.Exec(
"INSERT INTO identity_images (key_uid, name, image_payload, width, height, file_size, resize_target) VALUES (?, ?, ?, ?, ?, ?, ?)",
ii.KeyUID,
ii.Name,
ii.Payload,
ii.Width,
ii.Height,
ii.FileSize,
ii.ResizeTarget,
)
if err != nil {
return err
}
}
db.publishOnIdentityImageSubscriptions()
return nil
}
func (db *Database) SubscribeToIdentityImageChanges() chan struct{} {
s := make(chan struct{}, 100)
db.identityImageSubscriptions = append(db.identityImageSubscriptions, s)
return s
}
func (db *Database) publishOnIdentityImageSubscriptions() {
// Publish on channels, drop if buffer is full
for _, s := range db.identityImageSubscriptions {
select {
case s <- struct{}{}:
default:
log.Warn("subscription channel full, dropping message")
}
}
}
func (db *Database) DeleteIdentityImage(keyUID string) error {
_, err := db.db.Exec(`DELETE FROM identity_images WHERE key_uid = ?`, keyUID)
return err
}