go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

Allow user-defined functions to accept pointers

Open rittneje opened this issue 5 years ago • 4 comments

See #728. There the user had defined a function that took a string and SQLite tried to pass NULL to it. That resulted in an error from this library stating argument must be BLOB or TEXT. Changing the function to take a *string instead didn't work and resulted in an error stating don't know how to convert to *string. Please update the private callbackArg func used by RegisterFunc to properly handle an input type of kind reflect.Ptr.

rittneje avatar Jun 25 '19 10:06 rittneje

@rittneje I've implemented this feature request tested it only briefly. Could you add a test case for this to TestFunctionRegistration ?

About #728 This does not fix the issue with case #728 it only fixes "don't know how to convert to <reflect.Ptr>.

I hope you can write some nice RegisterFunc which uses Pointers as arguments.

gjrtimmer avatar Aug 22 '19 13:08 gjrtimmer

@rittneje Code available in branch: feature-pointer-callback

gjrtimmer avatar Aug 22 '19 13:08 gjrtimmer

Any updates on this?

clarkmcc avatar Jan 31 '22 21:01 clarkmcc

@clarkmcc I don't believe a PR was ever raised. And I don't think the branch from @gjrtimmer will work without further changes. It needs to be something along these lines:


func callbackArg(typ reflect.Type) (callbackArgConverter, error) {
    switch typ.Kind() {
    case reflect.Ptr:
        f := callbackArg(typ.Elem())
        return func(v *C.sqlite3_value) (reflect.Value, error) {
            if C.sqlite3_value_type(v) == C.SQLITE_NULL {
                return reflect.Zero(typ), nil
            }
            rv, err := f(v)
            if err != nil {
                return reflect.Value{}, err
            }
            ptr := reflect.New(rv.Type())
            ptr.Elem().Set(rv)
            return ptr, nil
        }
    ...

(Note: I have not tested or compiled this.)

rittneje avatar Feb 04 '22 05:02 rittneje