diff options
author | Patrick DeVivo <patrick.devivo@gmail.com> | 2020-12-26 09:11:17 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-26 23:11:17 +0900 |
commit | 92d23714a87355a0b7087795126bc0e0e2b06098 (patch) | |
tree | 45b6e467fb85afe01fe258e87743cc5539cb6017 /_example/vtable_eponymous_only/vtable.go | |
parent | RowsColumnTypeNullable not implemented (#848) (diff) | |
download | golite-92d23714a87355a0b7087795126bc0e0e2b06098.tar.gz golite-92d23714a87355a0b7087795126bc0e0e2b06098.tar.xz |
add support for defining an "eponymous only" virtual table (#885)
* add support for defining an "eponymous only" virtual table
As suggested here: https://github.com/mattn/go-sqlite3/issues/846#issuecomment-736206222
* add an example of an eponymous only vtab module
* add a test case for an eponymous only vtab module
Diffstat (limited to '_example/vtable_eponymous_only/vtable.go')
-rw-r--r-- | _example/vtable_eponymous_only/vtable.go | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/_example/vtable_eponymous_only/vtable.go b/_example/vtable_eponymous_only/vtable.go new file mode 100644 index 0000000..49fc0b7 --- /dev/null +++ b/_example/vtable_eponymous_only/vtable.go @@ -0,0 +1,118 @@ +package main + +import ( + "fmt" + + "github.com/mattn/go-sqlite3" +) + +type seriesModule struct{} + +func (m *seriesModule) EponymousOnlyModule() {} + +func (m *seriesModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { + err := c.DeclareVTab(fmt.Sprintf(` + CREATE TABLE %s ( + value INT, + start HIDDEN, + stop HIDDEN, + step HIDDEN + )`, args[0])) + if err != nil { + return nil, err + } + return &seriesTable{0, 0, 1}, nil +} + +func (m *seriesModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { + return m.Create(c, args) +} + +func (m *seriesModule) DestroyModule() {} + +type seriesTable struct { + start int64 + stop int64 + step int64 +} + +func (v *seriesTable) Open() (sqlite3.VTabCursor, error) { + return &seriesCursor{v, 0}, nil +} + +func (v *seriesTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) { + used := make([]bool, len(csts)) + for c, cst := range csts { + if cst.Usable && cst.Op == sqlite3.OpEQ { + used[c] = true + } + } + + return &sqlite3.IndexResult{ + IdxNum: 0, + IdxStr: "default", + Used: used, + }, nil +} + +func (v *seriesTable) Disconnect() error { return nil } +func (v *seriesTable) Destroy() error { return nil } + +type seriesCursor struct { + *seriesTable + value int64 +} + +func (vc *seriesCursor) Column(c *sqlite3.SQLiteContext, col int) error { + switch col { + case 0: + c.ResultInt64(vc.value) + case 1: + c.ResultInt64(vc.seriesTable.start) + case 2: + c.ResultInt64(vc.seriesTable.stop) + case 3: + c.ResultInt64(vc.seriesTable.step) + } + return nil +} + +func (vc *seriesCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { + switch { + case len(vals) < 1: + vc.seriesTable.start = 0 + vc.seriesTable.stop = 1000 + vc.value = vc.seriesTable.start + case len(vals) < 2: + vc.seriesTable.start = vals[0].(int64) + vc.seriesTable.stop = 1000 + vc.value = vc.seriesTable.start + case len(vals) < 3: + vc.seriesTable.start = vals[0].(int64) + vc.seriesTable.stop = vals[1].(int64) + vc.value = vc.seriesTable.start + case len(vals) < 4: + vc.seriesTable.start = vals[0].(int64) + vc.seriesTable.stop = vals[1].(int64) + vc.seriesTable.step = vals[2].(int64) + } + + return nil +} + +func (vc *seriesCursor) Next() error { + vc.value += vc.step + return nil +} + +func (vc *seriesCursor) EOF() bool { + return vc.value > vc.stop +} + +func (vc *seriesCursor) Rowid() (int64, error) { + return int64(vc.value), nil +} + +func (vc *seriesCursor) Close() error { + return nil +} |