You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the docs in database/sql/driver, the Queryer interface for SQL drivers is deprecated and QueryContext should be implemented instead:
Queryer is an optional interface that may be implemented by a Conn.
If a Conn does not implement Queryer, the sql package's DB.Query will first prepare a query, execute the statement, and then close the statement.
Query may return ErrSkip.
Deprecated: Drivers should implement QueryerContext instead (or additionally).
I created a dummy SQL driver that implements QueryerContext, but notQueryer. I also implemented an identical dummy driver that implements both Queryer and QueryerContext.
I then ran a simple SELECT query using both drivers.
I expected the database/sql library to call QueryContext() on both my dummy drivers, and to print Called QueryContext with 'SELECT 'foobar'' twice.
What did you see instead?
The dummy driver that implements Queryer and QueryerContext works just fine, and calls QueryContext() to resolve the query.
The dummy driver that only implements QueryerContext does not work. It panics because database/sql doesn't attempt to use QueryContext(), and instead falls back to preparing a statement and discarding it immediately.
Further notes
I came across this bug when attempting to write a "wrapper" driver to instrument SQL queries in an app. The very first version showed a 100% slowdown when performing simple queries with a wrapped MySQL driver.
After some investigation, I noticed that the implementation for both Query and Exec in database/sql does not match the provided documentation.
The docs state that the Queryer and Execer interfaces are now deprecated, and new drivers should implement their Context-aware counterparts "instead". But this is not the case: if the original deprecated interfaces are not implemented, the following type checks will fail and the library will never call the new interfaces:
The end result is that any new SQL driver that implements only the new QueryContext() and ExecContext() methods will not really work: these methods will never be called, and database/sql will fallback to the slow path of preparing and discarding a statement for the query.
The same situation will happen if any of the existing SQL drivers decide to truly deprecate Query and Exec in any future release by removing the two APIs altogether.
The text was updated successfully, but these errors were encountered:
I agree that this should be done and will target go1.10. I haven't looked at the code in detail with this in mind, but I think it would be reasonable for anyone to do this.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, all releases since 1.8
What operating system and processor architecture are you using (
go env
)?What did you do?
According to the docs in
database/sql/driver
, theQueryer
interface for SQL drivers is deprecated andQueryContext
should be implemented instead:I created a dummy SQL driver that implements
QueryerContext
, but notQueryer
. I also implemented an identical dummy driver that implements bothQueryer
andQueryerContext
.I then ran a simple
SELECT
query using both drivers.Go Playground link
What did you expect to see?
I expected the
database/sql
library to callQueryContext()
on both my dummy drivers, and to printCalled QueryContext with 'SELECT 'foobar''
twice.What did you see instead?
The dummy driver that implements
Queryer
andQueryerContext
works just fine, and callsQueryContext()
to resolve the query.The dummy driver that only implements
QueryerContext
does not work. Itpanic
s becausedatabase/sql
doesn't attempt to useQueryContext()
, and instead falls back to preparing a statement and discarding it immediately.Further notes
I came across this bug when attempting to write a "wrapper" driver to instrument SQL queries in an app. The very first version showed a 100% slowdown when performing simple queries with a wrapped MySQL driver.
After some investigation, I noticed that the implementation for both
Query
andExec
indatabase/sql
does not match the provided documentation.The docs state that the
Queryer
andExecer
interfaces are now deprecated, and new drivers should implement their Context-aware counterparts "instead". But this is not the case: if the original deprecated interfaces are not implemented, the following type checks will fail and the library will never call the new interfaces:go/src/database/sql/sql.go
Lines 1311 to 1312 in 5d39af9
go/src/database/sql/sql.go
Lines 1241 to 1245 in 5d39af9
The end result is that any new SQL driver that implements only the new
QueryContext()
andExecContext()
methods will not really work: these methods will never be called, anddatabase/sql
will fallback to the slow path of preparing and discarding a statement for the query.The same situation will happen if any of the existing SQL drivers decide to truly deprecate
Query
andExec
in any future release by removing the two APIs altogether.The text was updated successfully, but these errors were encountered: