-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
add support for time.Duration #1217
Comments
The driver can't support
I am also surprised by this fact, however it is a MySQL specification.
MySQL doesn't have time duration types. https://dev.mysql.com/doc/refman/8.0/en/data-types.html
The driver doesn't know which unit you use, so you need to convert the unit yourself. d := time.Hour
db.QueryContext(ctx, "SELECT NOW() + INTERVAL ? MICROSECOND", int64(d / time.Microsecond))
db.QueryContext(ctx, "SELECT NOW() + INTERVAL ? SECOND", d.Seconds()) |
Thanks for clarifying. If the driver can't support it, then please consider the alternative of specifically rejecting time.Duration from the generic conversion applied to other integer data types. The error message could suggest how to use either the INTERVAL syntax or an explicit integer number of nanoseconds (for the vanishing minority who might want the current behavior). BTW, you can use |
This kind of error in your code should be caught by a static analysis tool that would target |
I understand that Duration does not implement the SQL value interface, but that does not mean we cannot treat it specially, given how important it is (though I recognize there are pros and cons to doing so). A static analysis might have caught the problem, but that requires users to know that the analysis exists, to know how to run it, and to remember to run it---and if I knew that, I wouldn't have made the mistake. :) If the library itself detects the mistake at little cost, so much the better. |
I don't think that's a task for the MySQL driver. The same mistake could happen when using |
As I pointed out in my first note, my original report against the standard I don't see why the objection "the same mistake could happen when using database/sql with any other SQL driver" should present an obstacle to preventing the mistake in this MySQL driver. The worst case outcome is that both layers perform the check, making the driver's check redundant. |
It's a car-driver's responsibility to drive safely, but cars still come with seatbelts to reduce the cost of mistakes. |
Well, SQL expression
I prefer static analysis solution, reported a warning alert like: |
Today I learned two fun facts:
now()
is a datetime,now() + 0
is an integer formed from all of the decimal digits in the date. e.g. 2021-05-27 17:00:26 vs 20210527170026. (I'm not sure why one would ever want that.)database/sql
API for prepared statements, "?" applied to a duration value treats the duration (thanks to reflection on the underlying type) as an integer number of nanoseconds, not an interval. I don't know why one would ever want that either, since nanoseconds are not really useful in SQL.The combination of these two facts means that the behavior of
query("now() + ?", 1*time.Hour)
isn't remotely close to what one might expect:Perhaps go-sql-driver could support time.Duration by mapping it to
interval 1 hour
, thereby avoiding this pitfall.Alternatively, simply rejecting it with a clear error ("you cannot possibly have wanted this behavior") would be an improvement.
(See also golang/go#46427: I reported this initially against database/sql, and it was closed as a driver-specific issue.)
The text was updated successfully, but these errors were encountered: