forked from alicebob/sqlittle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
128 lines (117 loc) · 3.06 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
package sqlittle
import (
"fmt"
"strings"
sdb "github.com/42wim/sqlittle/db"
)
type columnIndex struct {
col *sdb.TableColumn
rowIndex int
rowid bool
}
// Regroups a Record to a Row, filling in missing columns as needed.
func toRow(rowid int64, cis []columnIndex, r sdb.Record) Row {
row := make(Row, len(cis))
for i, c := range cis {
if c.rowid {
row[i] = rowid
continue
}
if len(r) <= c.rowIndex {
// use 'DEFAULT' when the record is too short
row[i] = c.col.Default
} else {
row[i] = r[c.rowIndex]
}
}
return row
}
// given column names returns the index in a Row this column is expected, and
// the column definition. Allows 'rowid' alias.
func toColumnIndexRowid(s *sdb.Schema, columns []string) ([]columnIndex, error) {
res := make([]columnIndex, 0, len(columns))
for _, c := range columns {
n := s.Column(c)
if n < 0 {
cup := strings.ToUpper(c)
if cup == "ROWID" || cup == "OID" || cup == "_ROWID_" {
res = append(res, columnIndex{nil, n, true})
continue
} else {
return nil, fmt.Errorf("no such column: %q", c)
}
}
c := &s.Columns[n]
if c.Rowid {
res = append(res, columnIndex{nil, n, true})
} else {
res = append(res, columnIndex{c, n, false})
}
}
return res, nil
}
// given column names returns the index of this column in a row in the index (and
// the column definition). For non-rowid tables the database order of the
// columns depends on the primary key.
func toColumnIndexNonRowid(s *sdb.Schema, columns []string) ([]columnIndex, error) {
stored := columnStoreOrder(s) // column indexes in disk order
res := make([]columnIndex, 0, len(columns))
for _, c := range columns {
n := s.Column(c)
if n < 0 {
return nil, fmt.Errorf("no such column: %q", c)
}
res = append(res, columnIndex{&s.Columns[n], stored[n], false})
}
return res, nil
}
// given a non-rowid table, gives the order columns are stored on disk
func columnStoreOrder(schema *sdb.Schema) []int {
if !schema.WithoutRowid {
panic("can't call columnStoreOrder on a rowid table")
}
// all PK columns come first, then all other columns, in order
var cols = make([]string, 0, len(schema.Columns))
for _, c := range schema.PK {
cols = append(cols, strings.ToLower(c.Column))
}
loop:
for _, c := range schema.Columns {
n := strings.ToLower(c.Column)
for _, oc := range cols {
if oc == n {
continue loop
}
}
cols = append(cols, n)
}
res := make([]int, len(cols))
loop2:
for i, c := range schema.Columns {
n := strings.ToLower(c.Column)
for j, oc := range cols {
if oc == n {
res[i] = j
continue loop2
}
}
}
return res
}
// for non-rowid tables only:
// given an index gives back the indexes in a row which form the primary key.
func pkColumns(schema *sdb.Schema, ind *sdb.SchemaIndex) []int {
if !schema.WithoutRowid {
panic("can't call pkColumns on a rowid table")
}
var res []int
for _, c := range schema.PK {
if in := ind.Column(c.Column); in < 0 {
ind.Columns = append(ind.Columns, c)
res = append(res, len(ind.Columns)-1)
} else {
res = append(res, in)
}
}
return res
}