-
Notifications
You must be signed in to change notification settings - Fork 18k
database/sql: different handling of underlying types in DefaultParameterConverter #15174
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
Comments
Is there a reason to convert underlying strings to []byte instead of just string (it is a Value after all)? We could then go ahead and support nearly all of the Value types consistently: int64, float64, bool, []byte, string. |
I've got a change ready to go once 1.7 is out. |
@220kts The change I had in mind here is quite straightforward,
|
Thanks @sctb The current implementation is a bit inconsistent in its handling of nil pointers: a nil pointer to a basic type returns I would suggest to handle nil pointers consistently between basic types and types that implement the Valuer interface. In the case of a type that implements the Valuer interface, this would allow the caller to decide whether to pass a pointer or a value to the ConvertValue function (which logically tends to correspond to database columns with or without NOT NULL constraint). Just thought it might make sense to consider that along with the change you propose. |
CL https://golang.org/cl/27812 mentions this issue. |
Original testcase still fails on tip: https://travis-ci.org/AlekSi/go-sql-bugs/jobs/169501389. |
@AlekSi, closed bugs aren't tracked. File a new bug if you must. |
@AlekSi Your bug tests for Scan types which should use valuers if custom. This issue talked about query parameters. |
It wasn't my intention to limit this issue only to query parameters, although my analysis was incorrect. Basically, I wanted to be able to insert a value into a database and read it back into the same variable. And I wanted this behavior to be consistent for both predeclared types and named types with predeclared underlying types. As testcase shows, I can do it for The reason why my analysis was incorrect is this comment:
I will create a new issue. |
Please add the following code to your test:
|
That works, unsurprisingly. But custom |
Filed #18101 |
After e405c29 (#11489)
DefaultParameterConverter
documentation says:// DefaultParameterConverter returns its argument directly if
// IsValue(arg). Otherwise, if the argument implements Valuer, its
// Value method is used to return a Value. As a fallback, the provided
// argument's underlying type is used to convert it to a Value:
// underlying integer types are converted to int64, floats to float64,
// and strings to []byte. If the argument is a nil pointer,
// ConvertValue returns a nil Value. If the argument is a non-nil
// pointer, it is dereferenced and ConvertValue is called
// recursively. Other types are an error.
Underlying integers are really converted to int64, but underlying strings are not in fact converted – there is no code for this.
Testcase: https://github.com/AlekSi/go-sql-bugs/blob/master/issue15174_test.go
Test output: https://travis-ci.org/AlekSi/go-sql-bugs/builds/121408329 (see 1.6 and tip)
I propose to do what documentation says and convert the underlying string to []byte, it will help with custom string-like types with constants (enums).
Also, can we do the same thing for bools?
/cc @bradfitz
The text was updated successfully, but these errors were encountered: