Debian Codegraph @

_example/vtable_eponymous_only/vtable.go

raw
1 package main
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/mattn/go-sqlite3"
9 )
10
11 type seriesModule struct{}
12
13 func (m *seriesModule) EponymousOnlyModule() {}
14
15 func (m *seriesModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
16 err := c.DeclareVTab(fmt.Sprintf(`
17 CREATE TABLE %s (
18 value INT,
19 start HIDDEN,
20 stop HIDDEN,
21 step HIDDEN
22 )`, args[0]))
23 if err != nil {
24 return nil, err
25 }
26 return &seriesTable{}, nil
27 }
28
29 func (m *seriesModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
30 return m.Create(c, args)
31 }
32
33 func (m *seriesModule) DestroyModule() {}
34
35 type seriesTable struct{}
36
37 func (v *seriesTable) Open() (sqlite3.VTabCursor, error) {
38 return &seriesCursor{}, nil
39 }
40
41 func (v *seriesTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
42 // Consume equality constraints on the hidden parameter columns
43 // (start, stop, step) only. A constraint on the value column must be
44 // left to SQLite, otherwise it would be dropped from the WHERE clause
45 // and the query would return wrong results.
46 used := make([]bool, len(csts))
47 var params []byte
48 for c, cst := range csts {
49 if !cst.Usable || cst.Op != sqlite3.OpEQ {
50 continue
51 }
52 var p byte
53 switch cst.Column {
54 case 1:
55 p = 's' // start
56 case 2:
57 p = 'e' // stop
58 case 3:
59 p = 't' // step
60 default:
61 continue
62 }
63 if strings.IndexByte(string(params), p) >= 0 {
64 continue
65 }
66 used[c] = true
67 params = append(params, p)
68 }
69
70 // IdxStr records which parameter each Filter argument holds, in
71 // argument order.
72 return &sqlite3.IndexResult{
73 IdxNum: 0,
74 IdxStr: string(params),
75 Used: used,
76 }, nil
77 }
78
79 func (v *seriesTable) Disconnect() error { return nil }
80 func (v *seriesTable) Destroy() error { return nil }
81
82 type seriesCursor struct {
83 start int64
84 stop int64
85 step int64
86 value int64
87 }
88
89 func (vc *seriesCursor) Column(c *sqlite3.SQLiteContext, col int) error {
90 switch col {
91 case 0:
92 c.ResultInt64(vc.value)
93 case 1:
94 c.ResultInt64(vc.start)
95 case 2:
96 c.ResultInt64(vc.stop)
97 case 3:
98 c.ResultInt64(vc.step)
99 }
100 return nil
101 }
102
103 func (vc *seriesCursor) Filter(idxNum int, idxStr string, vals []any) error {
104 start, stop, step := int64(0), int64(1000), int64(1)
105 for i := 0; i < len(idxStr) && i < len(vals); i++ {
106 n, ok := vals[i].(int64)
107 if !ok {
108 return fmt.Errorf("series: argument %d must be an integer", i+1)
109 }
110 switch idxStr[i] {
111 case 's':
112 start = n
113 case 'e':
114 stop = n
115 case 't':
116 step = n
117 }
118 }
119 if step == 0 {
120 return errors.New("series: step must not be zero")
121 }
122 vc.start = start
123 vc.stop = stop
124 vc.step = step
125 vc.value = start
126 return nil
127 }
128
129 func (vc *seriesCursor) Next() error {
130 vc.value += vc.step
131 return nil
132 }
133
134 func (vc *seriesCursor) EOF() bool {
135 if vc.step < 0 {
136 return vc.value < vc.stop
137 }
138 return vc.value > vc.stop
139 }
140
141 func (vc *seriesCursor) Rowid() (int64, error) {
142 return int64(vc.value), nil
143 }
144
145 func (vc *seriesCursor) Close() error {
146 return nil
147 }