diff options
| author | Daniel Theophanes <kardianos@gmail.com> | 2016-09-19 11:19:32 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-09-27 19:41:09 +0000 |
| commit | e13df02e5fbf2c0cd8811b826a8c8567efa882dd (patch) | |
| tree | d07465f0472a1fdd7b8384a1dba32f8fea326e64 /src/database/sql/driver/driver.go | |
| parent | 54a72d90f62030034f03cacbac1d1ec02c0444c6 (diff) | |
| download | go-e13df02e5fbf2c0cd8811b826a8c8567efa882dd.tar.xz | |
database/sql: add context methods
Add context methods to sql and sql/driver methods. If
the driver doesn't implement context methods the connection
pool will still handle timeouts when a query fails to return
in time or when a connection is not available from the pool
in time.
There will be a follow-up CL that will add support for
context values that specify transaction levels and modes
that a driver can use.
Fixes #15123
Change-Id: Ia99f3957aa3f177b23044dd99d4ec217491a30a7
Reviewed-on: https://go-review.googlesource.com/29381
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/database/sql/driver/driver.go')
| -rw-r--r-- | src/database/sql/driver/driver.go | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/database/sql/driver/driver.go b/src/database/sql/driver/driver.go index 4dba85a6d3..ccc283d373 100644 --- a/src/database/sql/driver/driver.go +++ b/src/database/sql/driver/driver.go @@ -8,7 +8,10 @@ // Most code should use package sql. package driver -import "errors" +import ( + "context" + "errors" +) // Value is a value that drivers must be able to handle. // It is either nil or an instance of one of these types: @@ -65,6 +68,12 @@ type Execer interface { Exec(query string, args []Value) (Result, error) } +// ExecerContext is like execer, but must honor the context timeout and return +// when the context is cancelled. +type ExecerContext interface { + ExecContext(ctx context.Context, query string, args []Value) (Result, error) +} + // 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 @@ -76,6 +85,12 @@ type Queryer interface { Query(query string, args []Value) (Rows, error) } +// QueryerContext is like Queryer, but most honor the context timeout and return +// when the context is cancelled. +type QueryerContext interface { + QueryContext(ctx context.Context, query string, args []Value) (Rows, error) +} + // Conn is a connection to a database. It is not used concurrently // by multiple goroutines. // @@ -98,6 +113,23 @@ type Conn interface { Begin() (Tx, error) } +// ConnPrepareContext enhances the Conn interface with context. +type ConnPrepareContext interface { + // PrepareContext returns a prepared statement, bound to this connection. + // context is for the preparation of the statement, + // it must not store the context within the statement itself. + PrepareContext(ctx context.Context, query string) (Stmt, error) +} + +// ConnBeginContext enhances the Conn interface with context. +type ConnBeginContext interface { + // BeginContext starts and returns a new transaction. + // the provided context should be used to roll the transaction back + // if it is cancelled. If there is an isolation level in context + // that is not supported by the driver an error must be returned. + BeginContext(ctx context.Context) (Tx, error) +} + // Result is the result of a query execution. type Result interface { // LastInsertId returns the database's auto-generated ID @@ -139,6 +171,18 @@ type Stmt interface { Query(args []Value) (Rows, error) } +// StmtExecContext enhances the Stmt interface by providing Exec with context. +type StmtExecContext interface { + // ExecContext must honor the context timeout and return when it is cancelled. + ExecContext(ctx context.Context, args []Value) (Result, error) +} + +// StmtQueryContext enhances the Stmt interface by providing Query with context. +type StmtQueryContext interface { + // QueryContext must honor the context timeout and return when it is cancelled. + QueryContext(ctx context.Context, args []Value) (Rows, error) +} + // ColumnConverter may be optionally implemented by Stmt if the // statement is aware of its own columns' types and can convert from // any type to a driver Value. |
