aboutsummaryrefslogtreecommitdiff
path: root/vtable_test.go
diff options
context:
space:
mode:
authorConor Branagan <conor.branagan@gmail.com>2016-11-10 10:27:20 -0500
committerConor Branagan <conor.branagan@gmail.com>2017-03-04 18:10:02 -0500
commitbba480975b7136da2f4016b2ba6133e68aa2a878 (patch)
tree43196ee7701499dbc840250d33f06688d553f81c /vtable_test.go
parentfix test (diff)
downloadgolite-bba480975b7136da2f4016b2ba6133e68aa2a878.tar.gz
golite-bba480975b7136da2f4016b2ba6133e68aa2a878.tar.xz
Add Go API for virtual tables
See https://www.sqlite.org/vtab.html for more details. This work was started from https://github.com/gwenn/gosqlite/blob/master/vtab.{c,go} and adds: - Porting the API to go-sqlite3 APIs. - Support for >= Go 1.6 without requiring the `cgocheck` flag to be changed. - Filling out the unfinished callback functions for the `Vtable` struct. - A simple `Context` API layer for ease of use when adding modules. Tests are included.
Diffstat (limited to 'vtable_test.go')
-rw-r--r--vtable_test.go132
1 files changed, 132 insertions, 0 deletions
diff --git a/vtable_test.go b/vtable_test.go
new file mode 100644
index 0000000..4c7efcb
--- /dev/null
+++ b/vtable_test.go
@@ -0,0 +1,132 @@
+// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
+//
+// Use of this source code is governed by an MIT-style
+// license that can be found in the LICENSE file.
+
+package sqlite3
+
+import (
+ "database/sql"
+ "fmt"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+type testModule struct {
+ t *testing.T
+ intarray []int
+}
+
+type testVTab struct {
+ intarray []int
+}
+
+type testVTabCursor struct {
+ vTab *testVTab
+ index int
+}
+
+func (m testModule) Create(c *SQLiteConn, args []string) (VTab, error) {
+ assert.True(m.t, len(args) == 6, "six arguments expected")
+ assert.Equal(m.t, "test", args[0], "module name")
+ assert.Equal(m.t, "main", args[1], "db name")
+ assert.Equal(m.t, "vtab", args[2], "table name")
+ assert.Equal(m.t, "'1'", args[3], "first arg")
+ assert.Equal(m.t, "2", args[4], "second arg")
+ assert.Equal(m.t, "three", args[5], "third arg")
+ err := c.DeclareVTab("CREATE TABLE x(test TEXT)")
+ if err != nil {
+ return nil, err
+ }
+ return &testVTab{m.intarray}, nil
+}
+
+func (m testModule) Connect(c *SQLiteConn, args []string) (VTab, error) {
+ return m.Create(c, args)
+}
+
+func (m testModule) DestroyModule() {}
+
+func (v *testVTab) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) {
+ used := make([]bool, 0, len(cst))
+ for range cst {
+ used = append(used, false)
+ }
+ return &IndexResult{
+ Used: used,
+ IdxNum: 0,
+ IdxStr: "test-index",
+ AlreadyOrdered: true,
+ EstimatedCost: 100,
+ EstimatedRows: 200,
+ }, nil
+}
+
+func (v *testVTab) Disconnect() error {
+ return nil
+}
+
+func (v *testVTab) Destroy() error {
+ return nil
+}
+
+func (v *testVTab) Open() (VTabCursor, error) {
+ return &testVTabCursor{v, 0}, nil
+}
+
+func (vc *testVTabCursor) Close() error {
+ return nil
+}
+
+func (vc *testVTabCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
+ vc.index = 0
+ return nil
+}
+
+func (vc *testVTabCursor) Next() error {
+ vc.index++
+ return nil
+}
+
+func (vc *testVTabCursor) EOF() bool {
+ return vc.index >= len(vc.vTab.intarray)
+}
+
+func (vc *testVTabCursor) Column(c *Context, col int) error {
+ if col != 0 {
+ return fmt.Errorf("column index out of bounds: %d", col)
+ }
+ c.ResultInt(vc.vTab.intarray[vc.index])
+ return nil
+}
+
+func (vc *testVTabCursor) Rowid() (int64, error) {
+ return int64(vc.index), nil
+}
+
+func TestCreateModule(t *testing.T) {
+ tempFilename := TempFilename(t)
+ defer os.Remove(tempFilename)
+ intarray := []int{1, 2, 3}
+ sql.Register("sqlite3_TestCreateModule", &SQLiteDriver{
+ ConnectHook: func(conn *SQLiteConn) error {
+ return conn.CreateModule("test", testModule{t, intarray})
+ },
+ })
+ db, err := sql.Open("sqlite3_TestCreateModule", tempFilename)
+ assert.Nil(t, err, "could not open db")
+ _, err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)")
+ assert.Nil(t, err, "could not create vtable")
+
+ var i, value int
+ rows, err := db.Query("SELECT rowid, * FROM vtab WHERE test = '3'")
+ assert.Nil(t, err, "couldn't select from virtual table")
+ for rows.Next() {
+ rows.Scan(&i, &value)
+ assert.Equal(t, intarray[i], value)
+ }
+ _, err = db.Exec("DROP TABLE vtab")
+ assert.Nil(t, err, "couldn't drop virtual table")
+}