|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + sqlite3 "github.com/mutecomm/go-sqlcipher/v4" |
| 7 | +) |
| 8 | + |
| 9 | +type seriesModule struct{} |
| 10 | + |
| 11 | +func (m *seriesModule) EponymousOnlyModule() {} |
| 12 | + |
| 13 | +func (m *seriesModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { |
| 14 | + err := c.DeclareVTab(fmt.Sprintf(` |
| 15 | + CREATE TABLE %s ( |
| 16 | + value INT, |
| 17 | + start HIDDEN, |
| 18 | + stop HIDDEN, |
| 19 | + step HIDDEN |
| 20 | + )`, args[0])) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + return &seriesTable{0, 0, 1}, nil |
| 25 | +} |
| 26 | + |
| 27 | +func (m *seriesModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { |
| 28 | + return m.Create(c, args) |
| 29 | +} |
| 30 | + |
| 31 | +func (m *seriesModule) DestroyModule() {} |
| 32 | + |
| 33 | +type seriesTable struct { |
| 34 | + start int64 |
| 35 | + stop int64 |
| 36 | + step int64 |
| 37 | +} |
| 38 | + |
| 39 | +func (v *seriesTable) Open() (sqlite3.VTabCursor, error) { |
| 40 | + return &seriesCursor{v, 0}, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (v *seriesTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) { |
| 44 | + used := make([]bool, len(csts)) |
| 45 | + for c, cst := range csts { |
| 46 | + if cst.Usable && cst.Op == sqlite3.OpEQ { |
| 47 | + used[c] = true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return &sqlite3.IndexResult{ |
| 52 | + IdxNum: 0, |
| 53 | + IdxStr: "default", |
| 54 | + Used: used, |
| 55 | + }, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (v *seriesTable) Disconnect() error { return nil } |
| 59 | +func (v *seriesTable) Destroy() error { return nil } |
| 60 | + |
| 61 | +type seriesCursor struct { |
| 62 | + *seriesTable |
| 63 | + value int64 |
| 64 | +} |
| 65 | + |
| 66 | +func (vc *seriesCursor) Column(c *sqlite3.SQLiteContext, col int) error { |
| 67 | + switch col { |
| 68 | + case 0: |
| 69 | + c.ResultInt64(vc.value) |
| 70 | + case 1: |
| 71 | + c.ResultInt64(vc.seriesTable.start) |
| 72 | + case 2: |
| 73 | + c.ResultInt64(vc.seriesTable.stop) |
| 74 | + case 3: |
| 75 | + c.ResultInt64(vc.seriesTable.step) |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (vc *seriesCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { |
| 81 | + switch { |
| 82 | + case len(vals) < 1: |
| 83 | + vc.seriesTable.start = 0 |
| 84 | + vc.seriesTable.stop = 1000 |
| 85 | + vc.value = vc.seriesTable.start |
| 86 | + case len(vals) < 2: |
| 87 | + vc.seriesTable.start = vals[0].(int64) |
| 88 | + vc.seriesTable.stop = 1000 |
| 89 | + vc.value = vc.seriesTable.start |
| 90 | + case len(vals) < 3: |
| 91 | + vc.seriesTable.start = vals[0].(int64) |
| 92 | + vc.seriesTable.stop = vals[1].(int64) |
| 93 | + vc.value = vc.seriesTable.start |
| 94 | + case len(vals) < 4: |
| 95 | + vc.seriesTable.start = vals[0].(int64) |
| 96 | + vc.seriesTable.stop = vals[1].(int64) |
| 97 | + vc.seriesTable.step = vals[2].(int64) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (vc *seriesCursor) Next() error { |
| 104 | + vc.value += vc.step |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (vc *seriesCursor) EOF() bool { |
| 109 | + return vc.value > vc.stop |
| 110 | +} |
| 111 | + |
| 112 | +func (vc *seriesCursor) Rowid() (int64, error) { |
| 113 | + return int64(vc.value), nil |
| 114 | +} |
| 115 | + |
| 116 | +func (vc *seriesCursor) Close() error { |
| 117 | + return nil |
| 118 | +} |
0 commit comments