2016-09-19 12:19:32 -06:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ctxDriverPrepare(ctx context.Context, ci driver.Conn, query string) (driver.Stmt, error) {
|
|
|
|
if ciCtx, is := ci.(driver.ConnPrepareContext); is {
|
|
|
|
return ciCtx.PrepareContext(ctx, query)
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
si, err := ci.Prepare(query)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
si.Close()
|
|
|
|
return nil, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return si, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-03 10:49:25 -06:00
|
|
|
func ctxDriverExec(ctx context.Context, execer driver.Execer, query string, nvdargs []driver.NamedValue) (driver.Result, error) {
|
2016-09-19 12:19:32 -06:00
|
|
|
if execerCtx, is := execer.(driver.ExecerContext); is {
|
2016-10-03 10:49:25 -06:00
|
|
|
return execerCtx.ExecContext(ctx, query, nvdargs)
|
|
|
|
}
|
|
|
|
dargs, err := namedValueToValue(nvdargs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-28 11:10:46 -06:00
|
|
|
resi, err := execer.Exec(query, dargs)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return resi, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return resi, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-03 10:49:25 -06:00
|
|
|
func ctxDriverQuery(ctx context.Context, queryer driver.Queryer, query string, nvdargs []driver.NamedValue) (driver.Rows, error) {
|
2016-09-19 12:19:32 -06:00
|
|
|
if queryerCtx, is := queryer.(driver.QueryerContext); is {
|
2016-10-28 11:10:46 -06:00
|
|
|
ret, err := queryerCtx.QueryContext(ctx, query, nvdargs)
|
|
|
|
return ret, err
|
2016-10-03 10:49:25 -06:00
|
|
|
}
|
|
|
|
dargs, err := namedValueToValue(nvdargs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
2016-09-28 13:51:39 -06:00
|
|
|
|
2016-10-28 11:10:46 -06:00
|
|
|
rowsi, err := queryer.Query(query, dargs)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
rowsi.Close()
|
|
|
|
return nil, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return rowsi, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-03 10:49:25 -06:00
|
|
|
func ctxDriverStmtExec(ctx context.Context, si driver.Stmt, nvdargs []driver.NamedValue) (driver.Result, error) {
|
2016-09-19 12:19:32 -06:00
|
|
|
if siCtx, is := si.(driver.StmtExecContext); is {
|
2016-10-03 10:49:25 -06:00
|
|
|
return siCtx.ExecContext(ctx, nvdargs)
|
|
|
|
}
|
|
|
|
dargs, err := namedValueToValue(nvdargs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-28 11:10:46 -06:00
|
|
|
resi, err := si.Exec(dargs)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return resi, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return resi, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-03 10:49:25 -06:00
|
|
|
func ctxDriverStmtQuery(ctx context.Context, si driver.Stmt, nvdargs []driver.NamedValue) (driver.Rows, error) {
|
2016-09-19 12:19:32 -06:00
|
|
|
if siCtx, is := si.(driver.StmtQueryContext); is {
|
2016-10-03 10:49:25 -06:00
|
|
|
return siCtx.QueryContext(ctx, nvdargs)
|
|
|
|
}
|
|
|
|
dargs, err := namedValueToValue(nvdargs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
2016-09-28 13:51:39 -06:00
|
|
|
|
2016-10-28 11:10:46 -06:00
|
|
|
rowsi, err := si.Query(dargs)
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
rowsi.Close()
|
|
|
|
return nil, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return rowsi, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var errLevelNotSupported = errors.New("sql: selected isolation level is not supported")
|
|
|
|
|
2016-12-13 08:55:12 -07:00
|
|
|
func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (driver.Tx, error) {
|
|
|
|
if ciCtx, is := ci.(driver.ConnBeginTx); is {
|
|
|
|
dopts := driver.TxOptions{}
|
|
|
|
if opts != nil {
|
|
|
|
dopts.Isolation = driver.IsolationLevel(opts.Isolation)
|
|
|
|
dopts.ReadOnly = opts.ReadOnly
|
|
|
|
}
|
|
|
|
return ciCtx.BeginTx(ctx, dopts)
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
2016-10-17 00:11:55 -06:00
|
|
|
|
2016-09-28 13:51:39 -06:00
|
|
|
if ctx.Done() == context.Background().Done() {
|
|
|
|
return ci.Begin()
|
|
|
|
}
|
|
|
|
|
2016-12-13 08:55:12 -07:00
|
|
|
if opts != nil {
|
|
|
|
// Check the transaction level. If the transaction level is non-default
|
|
|
|
// then return an error here as the BeginTx driver value is not supported.
|
|
|
|
if opts.Isolation != LevelDefault {
|
|
|
|
return nil, errors.New("sql: driver does not support non-default isolation level")
|
|
|
|
}
|
2016-10-17 00:11:55 -06:00
|
|
|
|
2016-12-13 08:55:12 -07:00
|
|
|
// If a read-only transaction is requested return an error as the
|
|
|
|
// BeginTx driver value is not supported.
|
|
|
|
if opts.ReadOnly {
|
|
|
|
return nil, errors.New("sql: driver does not support read-only transactions")
|
|
|
|
}
|
2016-10-17 00:11:55 -06:00
|
|
|
}
|
2016-09-19 12:19:32 -06:00
|
|
|
|
2016-10-28 11:10:46 -06:00
|
|
|
txi, err := ci.Begin()
|
|
|
|
if err == nil {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
txi.Rollback()
|
|
|
|
return nil, ctx.Err()
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:10:46 -06:00
|
|
|
return txi, err
|
2016-09-19 12:19:32 -06:00
|
|
|
}
|
2016-10-03 10:49:25 -06:00
|
|
|
|
|
|
|
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
|
|
|
|
dargs := make([]driver.Value, len(named))
|
|
|
|
for n, param := range named {
|
|
|
|
if len(param.Name) > 0 {
|
|
|
|
return nil, errors.New("sql: driver does not support the use of Named Parameters")
|
|
|
|
}
|
|
|
|
dargs[n] = param.Value
|
|
|
|
}
|
|
|
|
return dargs, nil
|
|
|
|
}
|