-
Notifications
You must be signed in to change notification settings - Fork 12
/
select.go
119 lines (105 loc) · 2.19 KB
/
select.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
package sqlittle
import (
"errors"
sdb "github.com/alicebob/sqlittle/db"
)
func select_(db *sdb.Database, s *sdb.Schema, cb RowDoneCB, columns []string) error {
ci, err := toColumnIndexRowid(s, columns)
if err != nil {
return err
}
t, err := db.Table(s.Table)
if err != nil {
return err
}
return t.Scan(func(rowid int64, r sdb.Record) bool {
return cb(toRow(rowid, ci, r))
})
}
func selectNonRowid(db *sdb.Database, s *sdb.Schema, cb RowDoneCB, columns []string) error {
ci, err := toColumnIndexNonRowid(s, columns)
if err != nil {
return err
}
t, err := db.NonRowidTable(s.Table)
if err != nil {
return err
}
return t.Scan(func(r sdb.Record) bool {
return cb(toRow(0, ci, r))
})
}
func selectRowid(db *sdb.Database, s *sdb.Schema, rowid int64, columns []string) (Row, error) {
ci, err := toColumnIndexRowid(s, columns)
if err != nil {
return nil, err
}
t, err := db.Table(s.Table)
if err != nil {
return nil, err
}
r, err := t.Rowid(rowid)
if err != nil || r == nil {
return nil, err
}
// TODO: decide what to do with shared []byte pointers
return toRow(rowid, ci, r), nil
}
func pkSelect(db *sdb.Database, s *sdb.Schema, key Key, cb RowCB, columns []string) error {
if s.RowidPK {
// `integer primary key` table.
var rowid int64
if len(key) == 0 {
return errors.New("invalid key")
}
rowid, ok := key[0].(int64)
if !ok {
return errors.New("invalid key")
}
row, err := selectRowid(db, s, rowid, columns)
if err != nil {
return err
}
if row != nil {
cb(row)
}
return nil
}
ind := s.NamedIndex(s.PrimaryKey)
if ind == nil {
return errors.New("table has no primary key")
}
dbkey, err := asDbKey(key, ind.Columns)
if err != nil {
return err
}
return indexedSelectEq(
db,
s,
ind,
dbkey,
cb,
columns,
)
}
func pkSelectNonRowid(db *sdb.Database, s *sdb.Schema, key Key, cb RowCB, columns []string) error {
ci, err := toColumnIndexNonRowid(s, columns)
if err != nil {
return err
}
t, err := db.NonRowidTable(s.Table)
if err != nil {
return err
}
dbkey, err := asDbKey(key, s.PK)
if err != nil {
return err
}
return t.ScanEq(
dbkey,
func(r sdb.Record) bool {
cb(toRow(0, ci, r))
return false
},
)
}