Commit d7fe5817 authored by gwenn's avatar gwenn

Tests pass with minimalist vtab impl.

parent 690673ed
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
package sqlite_test package sqlite_test
import ( import (
"fmt"
. "github.com/gwenn/gosqlite" . "github.com/gwenn/gosqlite"
"strconv"
"testing" "testing"
) )
...@@ -14,9 +16,12 @@ type testModule struct { ...@@ -14,9 +16,12 @@ type testModule struct {
} }
type testVTab struct { type testVTab struct {
eof bool
} }
type testVTabCursor struct { type testVTabCursor struct {
vTab *testVTab
pos int64
} }
func (m testModule) Create(c *Conn, args []string) (VTab, error) { func (m testModule) Create(c *Conn, args []string) (VTab, error) {
...@@ -29,10 +34,10 @@ func (m testModule) Create(c *Conn, args []string) (VTab, error) { ...@@ -29,10 +34,10 @@ func (m testModule) Create(c *Conn, args []string) (VTab, error) {
assertEquals(m.t, "Expected '%s' but got '%s' as first arg", "2", args[4]) assertEquals(m.t, "Expected '%s' but got '%s' as first arg", "2", args[4])
assertEquals(m.t, "Expected '%s' but got '%s' as first arg", "three", args[5]) assertEquals(m.t, "Expected '%s' but got '%s' as first arg", "three", args[5])
c.DeclareVTab("CREATE TABLE x(test TEXT)") c.DeclareVTab("CREATE TABLE x(test TEXT)")
return testVTab{}, nil return &testVTab{}, nil
} }
func (m testModule) Connect(c *Conn, args []string) (VTab, error) { func (m testModule) Connect(c *Conn, args []string) (VTab, error) {
println("testVTab.Connect") //println("testVTab.Connect")
return m.Create(c, args) return m.Create(c, args)
} }
...@@ -40,46 +45,58 @@ func (m testModule) Destroy() { ...@@ -40,46 +45,58 @@ func (m testModule) Destroy() {
//println("testModule.Destroy") //println("testModule.Destroy")
} }
func (v testVTab) BestIndex() error { func (v *testVTab) BestIndex() error {
println("testVTab.BestIndex") //fmt.Printf("testVTab.BestIndex: %v\n", v)
return nil return nil
} }
func (v testVTab) Disconnect() error { func (v *testVTab) Disconnect() error {
//println("testVTab.Disconnect") //fmt.Printf("testVTab.Disconnect: %v\n", v)
return nil return nil
} }
func (v testVTab) Destroy() error { func (v *testVTab) Destroy() error {
//println("testVTab.Destroy") //fmt.Printf("testVTab.Destroy: %v\n", v)
return nil return nil
} }
func (v testVTab) Open() (VTabCursor, error) { func (v *testVTab) Open() (VTabCursor, error) {
//println("testVTab.Open") //fmt.Printf("testVTab.Open: %v\n", v)
return testVTabCursor{}, nil return &testVTabCursor{v, 0}, nil
} }
func (v testVTabCursor) Close() error { func (vc *testVTabCursor) Close() error {
//println("testVTabCursor.Close") //fmt.Printf("testVTabCursor.Close: %v\n", vc)
return nil return nil
} }
func (v testVTabCursor) Filter( /*idxNum int, idxStr string, int argc, sqlite3_value **argv*/) error { func (vc *testVTabCursor) Filter( /*idxNum int, idxStr string, int argc, sqlite3_value **argv*/) error {
println("testVTabCursor.Filter") //fmt.Printf("testVTabCursor.Filter: %v\n", vc)
vc.vTab.eof = false
return vc.Next()
}
func (vc *testVTabCursor) Next() error {
//fmt.Printf("testVTabCursor.Next: %v\n", vc)
if vc.vTab.eof {
return fmt.Errorf("Next() called after EOF!")
}
if vc.pos == 1 {
vc.vTab.eof = true
}
vc.pos++
return nil return nil
} }
func (v testVTabCursor) Next() error { func (vc *testVTabCursor) Eof() bool {
println("testVTabCursor.Next") //fmt.Printf("testVTabCursor.Eof: %v\n", vc)
return nil return vc.vTab.eof
}
func (v testVTabCursor) Eof() bool {
println("testVTabCursor.Eof")
return true
} }
func (v testVTabCursor) Column(c *Context, col int) error { func (vc *testVTabCursor) Column(c *Context, col int) error {
println("testVTabCursor.Column") //fmt.Printf("testVTabCursor.Column(%d): %v\n", col, vc)
if col != 0 {
return fmt.Errorf("Column index out of bounds: %d", col)
}
c.ResultText(strconv.FormatInt(vc.pos, 10))
return nil return nil
} }
func (v testVTabCursor) Rowid() (int64, error) { func (vc *testVTabCursor) Rowid() (int64, error) {
println("testVTabCursor.Rowid") //fmt.Printf("testVTabCursor.Rowid: %v\n", vc)
return 1, nil return vc.pos, nil
} }
func TestCreateModule(t *testing.T) { func TestCreateModule(t *testing.T) {
...@@ -89,11 +106,10 @@ func TestCreateModule(t *testing.T) { ...@@ -89,11 +106,10 @@ func TestCreateModule(t *testing.T) {
checkNoError(t, err, "couldn't create module: %s") checkNoError(t, err, "couldn't create module: %s")
err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)") err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)")
checkNoError(t, err, "couldn't create virtual table: %s") checkNoError(t, err, "couldn't create virtual table: %s")
//var value *string var value string
//err = db.OneValue("SELECT * from vtab", &value) err = db.OneValue("SELECT * from vtab", &value)
//checkNoError(t, err, "couldn't select from virtual table: %s") checkNoError(t, err, "couldn't select from virtual table: %s")
//assert(t, "Not null value expected", value != nil) assertEquals(t, "Expected '%s' but got '%s'", "1", value)
//assertEquals(t, "Expected '%s' but got '%s'", "test", *value)
err = db.Exec("DROP TABLE vtab") err = db.Exec("DROP TABLE vtab")
checkNoError(t, err, "couldn't drop virtual table: %s") checkNoError(t, err, "couldn't drop virtual table: %s")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment