database/sql: add context helper methods and transaction types

Prior to this change, it was implied that transaction properties
would be carried in the context value. However, no such properties
were defined, not even common ones. Define two common properties:
isolation level and read-only. Drivers may choose to support
additional transaction properties. It is not expected any
further transaction properties will be added in the future.

Change-Id: I2f680115a14a1333c65ba6f943d9a1149d412918
Reviewed-on: https://go-review.googlesource.com/31258
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Theophanes 2016-10-16 23:11:55 -07:00 committed by Brad Fitzpatrick
parent 042264ef1b
commit ce6aa2ebda
5 changed files with 96 additions and 10 deletions

View file

@ -232,12 +232,22 @@ func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) {
if ciCtx, is := ci.(driver.ConnBeginContext); is {
return ciCtx.BeginContext(ctx)
}
if ctx.Done() == context.Background().Done() {
return ci.Begin()
}
// TODO(kardianos): check the transaction level in ctx. If set and non-default
// Check the transaction level in ctx. If set and non-default
// then return an error here as the BeginContext driver value is not supported.
if level, ok := driver.IsolationFromContext(ctx); ok && level != driver.IsolationLevel(LevelDefault) {
return nil, errors.New("sql: driver does not support non-default isolation level")
}
// Check for a read-only parameter in ctx. If a read-only transaction is
// requested return an error as the BeginContext driver value is not supported.
if ro := driver.ReadOnlyFromContext(ctx); ro {
return nil, errors.New("sql: driver does not support read-only transactions")
}
type R struct {
err error