-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlite.go
236 lines (210 loc) · 6.49 KB
/
sqlite.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
// Package sqlite provides a thin wrapper over the sqlc-generated SQLite wrapper
// to adhere to our server's DB interface.
package sqlite
import (
"context"
"errors"
"fmt"
"github.com/bcspragu/logseq-sync/db"
"github.com/bcspragu/logseq-sync/db/sqlite/sqlitedb"
"github.com/google/uuid"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type DB struct {
q *sqlitedb.Queries
}
func New(dbPath string) (*DB, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, fmt.Errorf("failed to open SQLite database: %w", err)
}
return &DB{
q: sqlitedb.New(db),
}, nil
}
func (d *DB) IncrementTx(ctx context.Context, id db.GraphID) (db.Tx, error) {
res, err := d.q.IncrementTx(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return 0, db.NotExists("graph", id)
} else if err != nil {
return 0, fmt.Errorf("failed to increment tx: %w", err)
}
return db.Tx(res), nil
}
func (d *DB) SetTx(ctx context.Context, id db.GraphID, tx db.Tx) error {
if err := d.q.SetTx(ctx, sqlitedb.SetTxParams{
CurrentTx: int64(tx),
ID: string(id),
}); errors.Is(err, sql.ErrNoRows) {
return db.NotExists("graph", id)
} else if err != nil {
return fmt.Errorf("failed to set tx: %w", err)
}
return nil
}
func (d *DB) Tx(ctx context.Context, id db.GraphID) (db.Tx, error) {
g, err := d.q.Graph(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return 0, db.NotExists("graph", id)
} else if err != nil {
return 0, fmt.Errorf("failed to load graph: %w", err)
}
return db.Tx(g.CurrentTx), nil
}
func (d *DB) Graph(ctx context.Context, id db.GraphID) (*db.Graph, error) {
g, err := d.q.Graph(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return nil, db.NotExists("graph", id)
} else if err != nil {
return nil, fmt.Errorf("failed to load graph: %w", err)
}
return toGraph(g), nil
}
func toGraph(g sqlitedb.Graph) *db.Graph {
return &db.Graph{
ID: db.GraphID(g.ID),
Name: g.Name,
}
}
func convSlice[I any, O any](in []I, fn func(I) O) []O {
out := make([]O, len(in))
for i, v := range in {
out[i] = fn(v)
}
return out
}
func (d *DB) Graphs(ctx context.Context) ([]*db.Graph, error) {
gs, err := d.q.Graphs(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load graphs: %w", err)
}
return convSlice(gs, toGraph), nil
}
func (d *DB) CreateGraph(ctx context.Context, name string) (db.GraphID, db.Tx, error) {
id := uuid.NewString()
tx, err := d.q.CreateGraph(ctx, sqlitedb.CreateGraphParams{
ID: id,
Name: name,
})
if err != nil {
return "", 0, fmt.Errorf("failed to create graph: %w", err)
}
return db.GraphID(id), db.Tx(tx), nil
}
func (d *DB) DeleteGraph(ctx context.Context, id db.GraphID) error {
if err := d.q.DeleteGraph(ctx, string(id)); errors.Is(err, sql.ErrNoRows) {
return db.NotExists("graph", id)
} else if err != nil {
return fmt.Errorf("failed to delete graph: %w", err)
}
return nil
}
func (d *DB) AddGraphSalt(ctx context.Context, gID db.GraphID, salt *db.GraphSalt) error {
id := uuid.NewString()
if err := d.q.AddGraphSalt(ctx, sqlitedb.AddGraphSaltParams{
ID: id,
GraphID: string(gID),
Value: salt.Value,
ExpiresAt: salt.ExpiredAt,
}); err != nil {
return fmt.Errorf("failed to add graph salt: %w", err)
}
return nil
}
func (d *DB) GraphSalts(ctx context.Context, id db.GraphID) ([]*db.GraphSalt, error) {
gss, err := d.q.GraphSalts(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return nil, db.NotExists("graph", id)
} else if err != nil {
return nil, fmt.Errorf("failed to load graph salts: %w", err)
}
return convSlice(gss, toGraphSalt), nil
}
func toGraphSalt(g sqlitedb.GraphSalt) *db.GraphSalt {
return &db.GraphSalt{
Value: g.Value,
ExpiredAt: g.ExpiresAt,
}
}
func (d *DB) AddGraphEncryptKey(ctx context.Context, gID db.GraphID, gek *db.GraphEncryptKey) error {
id := uuid.NewString()
if err := d.q.AddGraphEncryptKey(ctx, sqlitedb.AddGraphEncryptKeyParams{
ID: id,
GraphID: string(gID),
EncryptedPrivateKey: gek.EncryptedPrivateKey,
PublicKey: gek.PublicKey,
}); err != nil {
return fmt.Errorf("failed to add graph encrypt key: %w", err)
}
return nil
}
func (d *DB) GraphEncryptKeys(ctx context.Context, id db.GraphID) ([]*db.GraphEncryptKey, error) {
geks, err := d.q.GraphEncryptKeys(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return nil, db.NotExists("graph", id)
} else if err != nil {
return nil, fmt.Errorf("failed to load graph encrypt keys: %w", err)
}
return convSlice(geks, toGraphEncryptKey), nil
}
func toGraphEncryptKey(g sqlitedb.GraphEncryptKey) *db.GraphEncryptKey {
return &db.GraphEncryptKey{
EncryptedPrivateKey: g.EncryptedPrivateKey,
PublicKey: g.PublicKey,
}
}
func (d *DB) SetFileMeta(ctx context.Context, gID db.GraphID, md *db.FileMeta) error {
if md.ID == "" {
return errors.New("no file ID set on metadata")
}
id := uuid.NewString()
err := d.q.SetFileMeta(ctx, sqlitedb.SetFileMetaParams{
ID: id,
GraphID: string(gID),
FileID: string(md.ID),
BlobPath: md.BlobPath,
Checksum: md.Checksum,
Size: md.Size,
LastModifiedTx: int64(md.LastModifiedTX),
})
if err != nil {
return fmt.Errorf("failed to set file metadata: %w", err)
}
return nil
}
// Because the Logseq client can/will request files it hasn't uploaded yet, the
// map will contain nil entries for requested files that don't exist
func (d *DB) BatchFileMeta(ctx context.Context, id db.GraphID, fIDs []db.FileID) (map[db.FileID]*db.FileMeta, error) {
fms, err := d.q.BatchFileMetas(ctx, sqlitedb.BatchFileMetasParams{
GraphID: string(id),
FileIds: convSlice(fIDs, func(id db.FileID) string { return string(id) }),
})
if err != nil {
return nil, fmt.Errorf("failed to batch load file metadata: %w", err)
}
out := make(map[db.FileID]*db.FileMeta)
for _, fm := range fms {
out[db.FileID(fm.FileID)] = toFileMeta(fm)
}
return out, nil
}
func toFileMeta(fm sqlitedb.FileMeta) *db.FileMeta {
return &db.FileMeta{
ID: db.FileID(fm.FileID),
BlobPath: fm.BlobPath,
Checksum: fm.Checksum,
Size: fm.Size,
LastModifiedAt: fm.LastModifiedAt,
LastModifiedTX: db.Tx(fm.LastModifiedTx),
}
}
func (d *DB) AllFileMeta(ctx context.Context, id db.GraphID) ([]*db.FileMeta, error) {
fms, err := d.q.AllFileMeta(ctx, string(id))
if errors.Is(err, sql.ErrNoRows) {
return nil, db.NotExists("graph", id)
} else if err != nil {
return nil, fmt.Errorf("failed to load all file metadata for graph: %w", err)
}
return convSlice(fms, toFileMeta), nil
}