aboutsummaryrefslogtreecommitdiff
path: root/sqlite3_test.go
diff options
context:
space:
mode:
authorDavid Anderson <dave@natulte.net>2015-08-21 17:12:18 -0700
committerDavid Anderson <dave@natulte.net>2015-08-21 17:12:18 -0700
commitb037a616903746de8e647f53503d4edca29192ec (patch)
tree3045643b1c3243ab76ed54348f1cc6c0fc64965b /sqlite3_test.go
parentImplement support for variadic functions. (diff)
downloadgolite-b037a616903746de8e647f53503d4edca29192ec.tar.gz
golite-b037a616903746de8e647f53503d4edca29192ec.tar.xz
Add support for interface{} arguments in Go SQLite functions.
This enabled support for functions like Foo(a interface{}) and Bar(a ...interface{}).
Diffstat (limited to '')
-rw-r--r--sqlite3_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/sqlite3_test.go b/sqlite3_test.go
index a563c08..62db05b 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -1071,6 +1071,20 @@ func TestFunctionRegistration(t *testing.T) {
regex := func(re, s string) (bool, error) {
return regexp.MatchString(re, s)
}
+ generic := func(a interface{}) int64 {
+ switch a.(type) {
+ case int64:
+ return 1
+ case float64:
+ return 2
+ case []byte:
+ return 3
+ case string:
+ return 4
+ default:
+ panic("unreachable")
+ }
+ }
variadic := func(a, b int64, c ...int64) int64 {
ret := a + b
for _, d := range c {
@@ -1078,6 +1092,9 @@ func TestFunctionRegistration(t *testing.T) {
}
return ret
}
+ variadicGeneric := func(a ...interface{}) int64 {
+ return int64(len(a))
+ }
sql.Register("sqlite3_FunctionRegistration", &SQLiteDriver{
ConnectHook: func(conn *SQLiteConn) error {
@@ -1105,9 +1122,15 @@ func TestFunctionRegistration(t *testing.T) {
if err := conn.RegisterFunc("regex", regex, true); err != nil {
return err
}
+ if err := conn.RegisterFunc("generic", generic, true); err != nil {
+ return err
+ }
if err := conn.RegisterFunc("variadic", variadic, true); err != nil {
return err
}
+ if err := conn.RegisterFunc("variadicGeneric", variadicGeneric, true); err != nil {
+ return err
+ }
return nil
},
})
@@ -1131,9 +1154,14 @@ func TestFunctionRegistration(t *testing.T) {
{"SELECT not(0)", true},
{`SELECT regex("^foo.*", "foobar")`, true},
{`SELECT regex("^foo.*", "barfoobar")`, false},
+ {"SELECT generic(1)", int64(1)},
+ {"SELECT generic(1.1)", int64(2)},
+ {`SELECT generic(NULL)`, int64(3)},
+ {`SELECT generic("foo")`, int64(4)},
{"SELECT variadic(1,2)", int64(3)},
{"SELECT variadic(1,2,3,4)", int64(10)},
{"SELECT variadic(1,1,1,1,1,1,1,1,1,1)", int64(10)},
+ {`SELECT variadicGeneric(1,"foo",2.3, NULL)`, int64(4)},
}
for _, op := range ops {