Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user-defined functions to accept pointers #729

Open
rittneje opened this issue Jun 25, 2019 · 4 comments
Open

Allow user-defined functions to accept pointers #729

rittneje opened this issue Jun 25, 2019 · 4 comments

Comments

@rittneje
Copy link
Collaborator

rittneje commented Jun 25, 2019

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.

@gjrtimmer
Copy link
Collaborator

@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 added a commit that referenced this issue Aug 22, 2019
@gjrtimmer
Copy link
Collaborator

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

@clarkmcc
Copy link

Any updates on this?

@rittneje
Copy link
Collaborator Author

rittneje commented Feb 4, 2022

@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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants