Commit bc59e46a authored by gwenn's avatar gwenn

Add a simple test with a user's defined function in Go.

parent dee9b21f
package sqlite_test
import (
. "github.com/gwenn/gosqlite"
"testing"
)
func half(ctx *Context, nArg int) {
ctx.ResultDouble(ctx.Double(0) / 2)
}
func TestScalarFunction(t *testing.T) {
db, err := Open("")
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
defer db.Close()
err = db.CreateScalarFunction("half", 1, nil, half, nil)
if err != nil {
t.Fatalf("couldn't create function: %s", err)
}
s, err := db.Prepare("select half(6)")
if err != nil {
t.Fatalf("couldn't prepare statement: %s", err)
}
b, err := s.Next()
if err != nil {
t.Fatalf("couldn't step statement: %s", err)
}
if !b {
t.Fatalf("No result")
}
d, _, err := s.ScanDouble(0)
if err != nil {
t.Fatalf("couldn't scan result: %s", err)
}
if d != 3 {
t.Errorf("Expected %f but got %f", 3, d)
}
err = s.Finalize()
if err != nil {
t.Fatalf("couldn't finalize statement: %s", err)
}
err = db.CreateScalarFunction("half", 1, nil, nil, nil)
if err != nil {
t.Errorf("couldn't destroy function: %s", err)
}
}
\ No newline at end of file
......@@ -899,7 +899,7 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
case *bool:
*value, isNull, err = s.ScanBool(index)
case *float64:
*value, isNull, err = s.ScanFloat64(index)
*value, isNull, err = s.ScanDouble(index)
case *[]byte:
*value, isNull = s.ScanBlob(index)
case *interface{}:
......@@ -1050,7 +1050,7 @@ func (s *Stmt) ScanBool(index int) (value bool, isNull bool, err error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_double.
// http://sqlite.org/c3ref/column_blob.html
func (s *Stmt) ScanFloat64(index int) (value float64, isNull bool, err error) { // TODO Rename in ScanDouble
func (s *Stmt) ScanDouble(index int) (value float64, isNull bool, err error) {
var ctype Type
if s.CheckNull || s.CheckTypeMismatch {
ctype = s.ColumnType(index)
......
......@@ -50,6 +50,7 @@ func TestNoTrace(t *testing.T) {
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
defer db.Close()
db.Trace(nil, nil)
db.SetAuthorizer(nil, nil)
db.Profile(nil, nil)
......@@ -58,11 +59,14 @@ func TestNoTrace(t *testing.T) {
db.CommitHook(nil, nil)
db.RollbackHook(nil, nil)
db.UpdateHook(nil, nil)
db.Close()
}
func TestTrace(t *testing.T) {
db, err := Open("")
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
defer db.Close()
db.Trace(trace, "TRACE")
err = db.SetAuthorizer(authorizer, "AUTH")
if err != nil {
......
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