// 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 (
	
	
	
)

func ctxDriverPrepare( context.Context,  driver.Conn,  string) (driver.Stmt, error) {
	if ,  := .(driver.ConnPrepareContext);  {
		return .PrepareContext(, )
	}
	,  := .Prepare()
	if  == nil {
		select {
		default:
		case <-.Done():
			.Close()
			return nil, .Err()
		}
	}
	return , 
}

func ctxDriverExec( context.Context,  driver.ExecerContext,  driver.Execer,  string,  []driver.NamedValue) (driver.Result, error) {
	if  != nil {
		return .ExecContext(, , )
	}
	,  := namedValueToValue()
	if  != nil {
		return nil, 
	}

	select {
	default:
	case <-.Done():
		return nil, .Err()
	}
	return .Exec(, )
}

func ctxDriverQuery( context.Context,  driver.QueryerContext,  driver.Queryer,  string,  []driver.NamedValue) (driver.Rows, error) {
	if  != nil {
		return .QueryContext(, , )
	}
	,  := namedValueToValue()
	if  != nil {
		return nil, 
	}

	select {
	default:
	case <-.Done():
		return nil, .Err()
	}
	return .Query(, )
}

func ctxDriverStmtExec( context.Context,  driver.Stmt,  []driver.NamedValue) (driver.Result, error) {
	if ,  := .(driver.StmtExecContext);  {
		return .ExecContext(, )
	}
	,  := namedValueToValue()
	if  != nil {
		return nil, 
	}

	select {
	default:
	case <-.Done():
		return nil, .Err()
	}
	return .Exec()
}

func ctxDriverStmtQuery( context.Context,  driver.Stmt,  []driver.NamedValue) (driver.Rows, error) {
	if ,  := .(driver.StmtQueryContext);  {
		return .QueryContext(, )
	}
	,  := namedValueToValue()
	if  != nil {
		return nil, 
	}

	select {
	default:
	case <-.Done():
		return nil, .Err()
	}
	return .Query()
}

func ctxDriverBegin( context.Context,  *TxOptions,  driver.Conn) (driver.Tx, error) {
	if ,  := .(driver.ConnBeginTx);  {
		 := driver.TxOptions{}
		if  != nil {
			.Isolation = driver.IsolationLevel(.Isolation)
			.ReadOnly = .ReadOnly
		}
		return .BeginTx(, )
	}

	if  != 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 .Isolation != LevelDefault {
			return nil, errors.New("sql: driver does not support non-default isolation level")
		}

		// If a read-only transaction is requested return an error as the
		// BeginTx driver value is not supported.
		if .ReadOnly {
			return nil, errors.New("sql: driver does not support read-only transactions")
		}
	}

	if .Done() == nil {
		return .Begin()
	}

	,  := .Begin()
	if  == nil {
		select {
		default:
		case <-.Done():
			.Rollback()
			return nil, .Err()
		}
	}
	return , 
}

func namedValueToValue( []driver.NamedValue) ([]driver.Value, error) {
	 := make([]driver.Value, len())
	for ,  := range  {
		if len(.Name) > 0 {
			return nil, errors.New("sql: driver does not support the use of Named Parameters")
		}
		[] = .Value
	}
	return , nil
}