-
Notifications
You must be signed in to change notification settings - Fork 12
/
low.go
222 lines (200 loc) · 5.04 KB
/
low.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
// all public low level methods
package db
import (
"errors"
"fmt"
"github.com/alicebob/sqlittle/sql"
)
type Table struct {
db *Database
root int
sql string
}
// Index is either an index, or the table for a non-rowid table
type Index struct {
db *Database
root int
sql string
}
// TableScanCB is the callback for Table.Scan(). It gets the rowid (usually an
// internal number), and the data of the row. It should return true when the
// scan should be terminated.
type TableScanCB func(int64, Record) bool
// RecordCB is passed to Index.Scan(), Index.ScanMin(), and
// Table.ScanWithoutRowid.
// It should return true when the scan should be stopped.
//
// For index scans:
// The callback gets the raw values as stored in the index. For a normal index
// the last value is the rowid value (see ChompRowid()). For a WITHOUT ROWID it
// depends on the table which rows there are.
type RecordCB func(Record) bool
// Def returns the table definition. Not everything SQLite supports is
// supported (yet).
// See Database.Schema() for a friendlier interface.
func (t *Table) Def() (*sql.CreateTableStmt, error) {
c, err := sql.Parse(t.sql)
if err != nil {
return nil, fmt.Errorf("%s SQL: %q", err, t.sql)
}
stmt, ok := c.(sql.CreateTableStmt)
if !ok {
return nil, errors.New("no CREATE TABLE attached")
}
return &stmt, nil
}
// Scan calls cb() for every row in the table. Will be called in 'database
// order'.
// The record is given as sqlite stores it; this means:
// - float64 columns might be stored as int64
// - after an alter table which adds columns a row might miss the new columns
// - an "integer primary key" column will be always be nil, and the rowid is
// the value
// If the callback returns true (done) the scan will be stopped.
func (t *Table) Scan(cb TableScanCB) error {
root, err := t.db.openTable(t.root)
if err != nil {
return err
}
_, err = root.Iter(
maxRecursion,
t.db,
func(rowid int64, pl cellPayload) (bool, error) {
c, err := addOverflow(t.db, pl)
if err != nil {
return false, err
}
rec, err := parseRecord(c)
if err != nil {
return false, err
}
return cb(rowid, rec), nil
},
)
return err
}
// Rowid finds a single row by rowid. Will return nil if it isn't found.
// The rowid is an internal id, but if you have an `integer primary key` column
// that should be the same.
// See Table.Scan comments about the Record
func (t *Table) Rowid(rowid int64) (Record, error) {
root, err := t.db.openTable(t.root)
if err != nil {
return nil, err
}
var recPl *cellPayload
if _, err := root.IterMin(
maxRecursion,
t.db,
rowid,
func(k int64, pl cellPayload) (bool, error) {
if k == rowid {
recPl = &pl
}
return true, nil
},
); err != nil {
return nil, err
}
if recPl == nil {
return nil, nil
}
c, err := addOverflow(t.db, *recPl)
if err != nil {
return nil, err
}
return parseRecord(c)
}
// Def returns the index definition.
func (t *Index) Def() (*sql.CreateIndexStmt, error) {
c, err := sql.Parse(t.sql)
if err != nil {
return nil, fmt.Errorf("%s SQL: %q", err, t.sql)
}
stmt, ok := c.(sql.CreateIndexStmt)
if !ok {
return nil, errors.New("no CREATE INDEX attached")
}
return &stmt, nil
}
// Scan calls cb() for every row in the index. These will be called in the
// index order.
// The callback gets the record as stored in the index. For indexes on a
// non-WITHOUT ROWID table the last value will be the rowid (see ChompRowid()).
// For a WITHOUT ROWID table the columns depend on your table structure.
// If the callback returns true (done) the scan will be stopped.
func (in *Index) Scan(cb RecordCB) error {
root, err := in.db.openIndex(in.root)
if err != nil {
return err
}
_, err = root.Iter(
maxRecursion,
in.db,
func(rec Record) (bool, error) {
return cb(rec), nil
},
)
return err
}
// Scan all record matching key
func (in *Index) ScanEq(key Key, cb RecordCB) error {
root, err := in.db.openIndex(in.root)
if err != nil {
return err
}
_, err = root.IterMin(
maxRecursion,
in.db,
key,
func(rec Record) (bool, error) {
if !Equals(key, rec) {
return true, nil
}
return cb(rec), nil
},
)
return err
}
// ScanMin calls cb() for every row in the index, starting from the first
// record where key is true.
//
// If the callback returns true (done) the scan will be stopped.
// All comments from Index.Scan are valid here as well.
func (in *Index) ScanMin(from Key, cb RecordCB) error {
root, err := in.db.openIndex(in.root)
if err != nil {
return err
}
_, err = root.IterMin(
maxRecursion,
in.db,
from,
func(rec Record) (bool, error) {
return cb(rec), nil
},
)
return err
}
// Find all records where from(index) is true, and to(index) is false.
//
// You'll have to compensate for DESC columns.
//
func (in *Index) ScanRange(from, to Key, cb RecordCB) error {
root, err := in.db.openIndex(in.root)
if err != nil {
return err
}
_, err = root.IterMin(
maxRecursion,
in.db,
from,
func(rec Record) (bool, error) {
if Search(to, rec) {
return true, nil
}
return cb(rec), nil
},
)
return err
}