-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.go
More file actions
220 lines (188 loc) · 5.07 KB
/
deck.go
File metadata and controls
220 lines (188 loc) · 5.07 KB
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
package anki
import (
"database/sql"
"errors"
"iter"
"strings"
"time"
"github.com/lftk/anki/pb"
"google.golang.org/protobuf/proto"
)
// Deck represents a deck in Anki.
type Deck struct {
ID int64
Name DeckName
Modified time.Time
USN int64
Common *pb.DeckCommon
Kind *pb.DeckKind
}
// NormalDeckKind creates a normal deck kind with the given configuration ID.
func NormalDeckKind(configID int64) *pb.DeckKind {
return &pb.DeckKind{
Kind: &pb.DeckKind_Normal{
Normal: &pb.DeckNormal{
ConfigId: configID,
},
},
}
}
// DefaultDeckCommon returns the default deck common settings.
func DefaultDeckCommon() *pb.DeckCommon {
return &pb.DeckCommon{
StudyCollapsed: true,
BrowserCollapsed: true,
}
}
// DeckName is the name of a deck.
type DeckName string
// JoinDeckName joins deck name components into a single DeckName.
// In Anki, deck names are hierarchical, separated by "::".
// Internally, they are stored with the U+001F INFORMATION SEPARATOR ONE character.
func JoinDeckName(components ...string) DeckName {
return DeckName(strings.Join(components, deckNameSeparator))
}
// Parent returns the parent deck's name.
// If the deck is a top-level deck, it returns an empty string.
func (dn DeckName) Parent() DeckName {
i := strings.LastIndex(string(dn), deckNameSeparator)
if i != -1 {
return dn[:i]
}
return ""
}
// Components returns the individual components of the deck name.
func (dn DeckName) Components() []string {
return strings.Split(string(dn), deckNameSeparator)
}
// HumanString returns the deck name in a human-readable format,
// with "::" as the separator.
func (dn DeckName) HumanString() string {
return strings.ReplaceAll(string(dn), deckNameSeparator, "::")
}
const deckNameSeparator = "\x1f"
// AddDeck adds a new deck to the collection.
// If the parent decks do not exist, they will be created automatically.
func (c *Collection) AddDeck(deck *Deck) error {
return sqlTransact(c.db, func(tx *sql.Tx) error {
var query = getDeckQuery + " WHERE name = ?"
// Ensure all parent decks exist.
for name := deck.Name.Parent(); name != ""; name = name.Parent() {
_, err := sqlGet(tx, scanDeck, query, name)
if err == nil {
// Parent deck already exists.
continue
}
if !errors.Is(err, sql.ErrNoRows) {
return err
}
// Create the parent deck if it doesn't exist.
parent := &Deck{
ID: 0, // Let the database assign an ID.
Name: name,
Modified: time.Now(),
USN: deck.USN,
Common: deck.Common,
Kind: deck.Kind,
}
if err := addDeck(tx, parent); err != nil {
return err
}
}
return addDeck(tx, deck)
})
}
// addDeck is a helper function to add a deck to the database.
func addDeck(e sqlExecer, deck *Deck) error {
id := deck.ID
if id == 0 {
id = time.Now().UnixMilli()
}
if deck.Common == nil {
deck.Common = DefaultDeckCommon()
}
common, err := proto.Marshal(deck.Common)
if err != nil {
return err
}
if deck.Kind == nil {
deck.Kind = NormalDeckKind(1) // Use default deck config ID.
}
kind, err := proto.Marshal(deck.Kind)
if err != nil {
return err
}
args := []any{
id,
deck.Name,
timeUnix(deck.Modified),
deck.USN,
common,
kind,
}
id, err = sqlInsert(e, addDeckQuery, args...)
if err == nil {
deck.ID = id
}
return err
}
// GetDeck gets a deck by its ID.
func (c *Collection) GetDeck(id int64) (*Deck, error) {
return getDeck(c.db, id)
}
// getDeck gets a deck by its ID.
func getDeck(q sqlQueryer, id int64) (*Deck, error) {
return sqlGet(q, scanDeck, getDeckQuery+" WHERE id = ?", id)
}
// ListDecksOptions specifies options for listing decks.
type ListDecksOptions struct {
ParentName *DeckName
}
// ListDecks lists all decks.
func (c *Collection) ListDecks(opts *ListDecksOptions) iter.Seq2[*Deck, error] {
var args []any
var conds []string
if opts != nil {
if opts.ParentName != nil && *opts.ParentName != "" {
conds = append(conds, "name LIKE ? AND name NOT LIKE ?")
pattern := string(*opts.ParentName) + deckNameSeparator + "%"
args = append(args, pattern, pattern+deckNameSeparator+"%")
}
}
query := getDeckQuery
if len(conds) > 0 {
query += " WHERE " + strings.Join(conds, " AND ")
}
return sqlSelectSeq(c.db, scanDeck, query, args...)
}
// scanDeck scans a deck from a database row.
func scanDeck(_ sqlQueryer, row sqlRow) (*Deck, error) {
var deck Deck
var mod int64
var common []byte
var kind []byte
if err := row.Scan(&deck.ID, &deck.Name, &mod, &deck.USN, &common, &kind); err != nil {
return nil, err
}
deck.Common = new(pb.DeckCommon)
if err := proto.Unmarshal(common, deck.Common); err != nil {
return nil, err
}
deck.Kind = new(pb.DeckKind)
if err := proto.Unmarshal(kind, deck.Kind); err != nil {
return nil, err
}
deck.Modified = time.Unix(mod, 0)
return &deck, nil
}
// addDefaultDeck adds the default deck to the database.
func addDefaultDeck(e sqlExecer) error {
return addDeck(e, &Deck{
ID: 1,
Name: "Default",
Modified: timeZero(),
USN: 0,
Common: DefaultDeckCommon(),
Kind: NormalDeckKind(1), // Use default deck config ID.
})
}