1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func ctxDriverPrepare(ctx context
.Context
, ci driver
.Conn
, query
string) (driver
.Stmt
, error
) {
14 if ciCtx
, is
:= ci
.(driver
.ConnPrepareContext
); is
{
15 return ciCtx
.PrepareContext(ctx
, query
)
17 si
, err
:= ci
.Prepare(query
)
29 func ctxDriverExec(ctx context
.Context
, execerCtx driver
.ExecerContext
, execer driver
.Execer
, query
string, nvdargs
[]driver
.NamedValue
) (driver
.Result
, error
) {
31 return execerCtx
.ExecContext(ctx
, query
, nvdargs
)
33 dargs
, err
:= namedValueToValue(nvdargs
)
43 return execer
.Exec(query
, dargs
)
46 func ctxDriverQuery(ctx context
.Context
, queryerCtx driver
.QueryerContext
, queryer driver
.Queryer
, query
string, nvdargs
[]driver
.NamedValue
) (driver
.Rows
, error
) {
47 if queryerCtx
!= nil {
48 return queryerCtx
.QueryContext(ctx
, query
, nvdargs
)
50 dargs
, err
:= namedValueToValue(nvdargs
)
60 return queryer
.Query(query
, dargs
)
63 func ctxDriverStmtExec(ctx context
.Context
, si driver
.Stmt
, nvdargs
[]driver
.NamedValue
) (driver
.Result
, error
) {
64 if siCtx
, is
:= si
.(driver
.StmtExecContext
); is
{
65 return siCtx
.ExecContext(ctx
, nvdargs
)
67 dargs
, err
:= namedValueToValue(nvdargs
)
80 func ctxDriverStmtQuery(ctx context
.Context
, si driver
.Stmt
, nvdargs
[]driver
.NamedValue
) (driver
.Rows
, error
) {
81 if siCtx
, is
:= si
.(driver
.StmtQueryContext
); is
{
82 return siCtx
.QueryContext(ctx
, nvdargs
)
84 dargs
, err
:= namedValueToValue(nvdargs
)
94 return si
.Query(dargs
)
97 func ctxDriverBegin(ctx context
.Context
, opts
*TxOptions
, ci driver
.Conn
) (driver
.Tx
, error
) {
98 if ciCtx
, is
:= ci
.(driver
.ConnBeginTx
); is
{
99 dopts
:= driver
.TxOptions
{}
101 dopts
.Isolation
= driver
.IsolationLevel(opts
.Isolation
)
102 dopts
.ReadOnly
= opts
.ReadOnly
104 return ciCtx
.BeginTx(ctx
, dopts
)
108 // Check the transaction level. If the transaction level is non-default
109 // then return an error here as the BeginTx driver value is not supported.
110 if opts
.Isolation
!= LevelDefault
{
111 return nil, errors
.New("sql: driver does not support non-default isolation level")
114 // If a read-only transaction is requested return an error as the
115 // BeginTx driver value is not supported.
117 return nil, errors
.New("sql: driver does not support read-only transactions")
121 if ctx
.Done() == nil {
125 txi
, err
:= ci
.Begin()
131 return nil, ctx
.Err()
137 func namedValueToValue(named
[]driver
.NamedValue
) ([]driver
.Value
, error
) {
138 dargs
:= make([]driver
.Value
, len(named
))
139 for n
, param
:= range named
{
140 if len(param
.Name
) > 0 {
141 return nil, errors
.New("sql: driver does not support the use of Named Parameters")
143 dargs
[n
] = param
.Value