While the query had succeeded, the context can expire right after. In
this situation, iterating the rows will fail. The test need to check for
sql.Rows.Err() too.
Also, don't forget to close sql.Rows when done to avoid memory leak.
Passing an already canceled/expired context to QueryContext/ExecContext
would return empty results without error because interruptOnDone would
immediately set the "done" flag to one so the calling function (query or
exec) would not enter the for loop doing the meaningful work.
Similarly, if the context is expired/canceled at the "wrong time", the
same behavior could happen.
This patch first check the context is still valid when entering the
function (before launching interruptOnDone), then if the funxtion is
about to return no values and no error, it checks if the "done" flag is
set and return the context error if it makes sense.
This allows registering custom aggregate and window functions (which
provide Step, Inverse, Value, and Final callbacks). It uses basically
the same interface as the zombiezen.com/go/sqlite package, which already
handles some trickier cases around multiple or concurrent invocations of
the same function.