github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/database/sql/sql.go (about)

     1  // Copyright 2011 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.
     4  
     5  // Package sql provides a generic interface around SQL (or SQL-like)
     6  // databases.
     7  //
     8  // The sql package must be used in conjunction with a database driver.
     9  // See https://golang.org/s/sqldrivers for a list of drivers.
    10  //
    11  // Drivers that do not support context cancelation will not return until
    12  // after the query is completed.
    13  //
    14  // For usage examples, see the wiki page at
    15  // https://golang.org/s/sqlwiki.
    16  package sql
    17  
    18  import (
    19  	"context"
    20  	"database/sql/driver"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"reflect"
    25  	"runtime"
    26  	"sort"
    27  	"sync"
    28  	"sync/atomic"
    29  	"time"
    30  )
    31  
    32  var (
    33  	driversMu sync.RWMutex
    34  	drivers   = make(map[string]driver.Driver)
    35  )
    36  
    37  // nowFunc returns the current time; it's overridden in tests.
    38  var nowFunc = time.Now
    39  
    40  // Register makes a database driver available by the provided name.
    41  // If Register is called twice with the same name or if driver is nil,
    42  // it panics.
    43  func Register(name string, driver driver.Driver) {
    44  	driversMu.Lock()
    45  	defer driversMu.Unlock()
    46  	if driver == nil {
    47  		panic("sql: Register driver is nil")
    48  	}
    49  	if _, dup := drivers[name]; dup {
    50  		panic("sql: Register called twice for driver " + name)
    51  	}
    52  	drivers[name] = driver
    53  }
    54  
    55  func unregisterAllDrivers() {
    56  	driversMu.Lock()
    57  	defer driversMu.Unlock()
    58  	// For tests.
    59  	drivers = make(map[string]driver.Driver)
    60  }
    61  
    62  // Drivers returns a sorted list of the names of the registered drivers.
    63  func Drivers() []string {
    64  	driversMu.RLock()
    65  	defer driversMu.RUnlock()
    66  	var list []string
    67  	for name := range drivers {
    68  		list = append(list, name)
    69  	}
    70  	sort.Strings(list)
    71  	return list
    72  }
    73  
    74  // A NamedArg is a named argument. NamedArg values may be used as
    75  // arguments to Query or Exec and bind to the corresponding named
    76  // parameter in the SQL statement.
    77  //
    78  // For a more concise way to create NamedArg values, see
    79  // the Named function.
    80  type NamedArg struct {
    81  	_Named_Fields_Required struct{}
    82  
    83  	// Name is the name of the parameter placeholder.
    84  	//
    85  	// If empty, the ordinal position in the argument list will be
    86  	// used.
    87  	//
    88  	// Name must omit any symbol prefix.
    89  	Name string
    90  
    91  	// Value is the value of the parameter.
    92  	// It may be assigned the same value types as the query
    93  	// arguments.
    94  	Value interface{}
    95  }
    96  
    97  // Named provides a more concise way to create NamedArg values.
    98  //
    99  // Example usage:
   100  //
   101  //     db.ExecContext(ctx, `
   102  //         delete from Invoice
   103  //         where
   104  //             TimeCreated < @end
   105  //             and TimeCreated >= @start;`,
   106  //         sql.Named("start", startTime),
   107  //         sql.Named("end", endTime),
   108  //     )
   109  func Named(name string, value interface{}) NamedArg {
   110  	// This method exists because the go1compat promise
   111  	// doesn't guarantee that structs don't grow more fields,
   112  	// so unkeyed struct literals are a vet error. Thus, we don't
   113  	// want to allow sql.NamedArg{name, value}.
   114  	return NamedArg{Name: name, Value: value}
   115  }
   116  
   117  // IsolationLevel is the transaction isolation level used in TxOptions.
   118  type IsolationLevel int
   119  
   120  // Various isolation levels that drivers may support in BeginTx.
   121  // If a driver does not support a given isolation level an error may be returned.
   122  //
   123  // See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
   124  const (
   125  	LevelDefault IsolationLevel = iota
   126  	LevelReadUncommitted
   127  	LevelReadCommitted
   128  	LevelWriteCommitted
   129  	LevelRepeatableRead
   130  	LevelSnapshot
   131  	LevelSerializable
   132  	LevelLinearizable
   133  )
   134  
   135  // TxOptions holds the transaction options to be used in DB.BeginTx.
   136  type TxOptions struct {
   137  	// Isolation is the transaction isolation level.
   138  	// If zero, the driver or database's default level is used.
   139  	Isolation IsolationLevel
   140  	ReadOnly  bool
   141  }
   142  
   143  // RawBytes is a byte slice that holds a reference to memory owned by
   144  // the database itself. After a Scan into a RawBytes, the slice is only
   145  // valid until the next call to Next, Scan, or Close.
   146  type RawBytes []byte
   147  
   148  // NullString represents a string that may be null.
   149  // NullString implements the Scanner interface so
   150  // it can be used as a scan destination:
   151  //
   152  //  var s NullString
   153  //  err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
   154  //  ...
   155  //  if s.Valid {
   156  //     // use s.String
   157  //  } else {
   158  //     // NULL value
   159  //  }
   160  //
   161  type NullString struct {
   162  	String string
   163  	Valid  bool // Valid is true if String is not NULL
   164  }
   165  
   166  // Scan implements the Scanner interface.
   167  func (ns *NullString) Scan(value interface{}) error {
   168  	if value == nil {
   169  		ns.String, ns.Valid = "", false
   170  		return nil
   171  	}
   172  	ns.Valid = true
   173  	return convertAssign(&ns.String, value)
   174  }
   175  
   176  // Value implements the driver Valuer interface.
   177  func (ns NullString) Value() (driver.Value, error) {
   178  	if !ns.Valid {
   179  		return nil, nil
   180  	}
   181  	return ns.String, nil
   182  }
   183  
   184  // NullInt64 represents an int64 that may be null.
   185  // NullInt64 implements the Scanner interface so
   186  // it can be used as a scan destination, similar to NullString.
   187  type NullInt64 struct {
   188  	Int64 int64
   189  	Valid bool // Valid is true if Int64 is not NULL
   190  }
   191  
   192  // Scan implements the Scanner interface.
   193  func (n *NullInt64) Scan(value interface{}) error {
   194  	if value == nil {
   195  		n.Int64, n.Valid = 0, false
   196  		return nil
   197  	}
   198  	n.Valid = true
   199  	return convertAssign(&n.Int64, value)
   200  }
   201  
   202  // Value implements the driver Valuer interface.
   203  func (n NullInt64) Value() (driver.Value, error) {
   204  	if !n.Valid {
   205  		return nil, nil
   206  	}
   207  	return n.Int64, nil
   208  }
   209  
   210  // NullFloat64 represents a float64 that may be null.
   211  // NullFloat64 implements the Scanner interface so
   212  // it can be used as a scan destination, similar to NullString.
   213  type NullFloat64 struct {
   214  	Float64 float64
   215  	Valid   bool // Valid is true if Float64 is not NULL
   216  }
   217  
   218  // Scan implements the Scanner interface.
   219  func (n *NullFloat64) Scan(value interface{}) error {
   220  	if value == nil {
   221  		n.Float64, n.Valid = 0, false
   222  		return nil
   223  	}
   224  	n.Valid = true
   225  	return convertAssign(&n.Float64, value)
   226  }
   227  
   228  // Value implements the driver Valuer interface.
   229  func (n NullFloat64) Value() (driver.Value, error) {
   230  	if !n.Valid {
   231  		return nil, nil
   232  	}
   233  	return n.Float64, nil
   234  }
   235  
   236  // NullBool represents a bool that may be null.
   237  // NullBool implements the Scanner interface so
   238  // it can be used as a scan destination, similar to NullString.
   239  type NullBool struct {
   240  	Bool  bool
   241  	Valid bool // Valid is true if Bool is not NULL
   242  }
   243  
   244  // Scan implements the Scanner interface.
   245  func (n *NullBool) Scan(value interface{}) error {
   246  	if value == nil {
   247  		n.Bool, n.Valid = false, false
   248  		return nil
   249  	}
   250  	n.Valid = true
   251  	return convertAssign(&n.Bool, value)
   252  }
   253  
   254  // Value implements the driver Valuer interface.
   255  func (n NullBool) Value() (driver.Value, error) {
   256  	if !n.Valid {
   257  		return nil, nil
   258  	}
   259  	return n.Bool, nil
   260  }
   261  
   262  // Scanner is an interface used by Scan.
   263  type Scanner interface {
   264  	// Scan assigns a value from a database driver.
   265  	//
   266  	// The src value will be of one of the following types:
   267  	//
   268  	//    int64
   269  	//    float64
   270  	//    bool
   271  	//    []byte
   272  	//    string
   273  	//    time.Time
   274  	//    nil - for NULL values
   275  	//
   276  	// An error should be returned if the value cannot be stored
   277  	// without loss of information.
   278  	Scan(src interface{}) error
   279  }
   280  
   281  // ErrNoRows is returned by Scan when QueryRow doesn't return a
   282  // row. In such a case, QueryRow returns a placeholder *Row value that
   283  // defers this error until a Scan.
   284  var ErrNoRows = errors.New("sql: no rows in result set")
   285  
   286  // DB is a database handle representing a pool of zero or more
   287  // underlying connections. It's safe for concurrent use by multiple
   288  // goroutines.
   289  //
   290  // The sql package creates and frees connections automatically; it
   291  // also maintains a free pool of idle connections. If the database has
   292  // a concept of per-connection state, such state can only be reliably
   293  // observed within a transaction. Once DB.Begin is called, the
   294  // returned Tx is bound to a single connection. Once Commit or
   295  // Rollback is called on the transaction, that transaction's
   296  // connection is returned to DB's idle connection pool. The pool size
   297  // can be controlled with SetMaxIdleConns.
   298  type DB struct {
   299  	driver driver.Driver
   300  	dsn    string
   301  	// numClosed is an atomic counter which represents a total number of
   302  	// closed connections. Stmt.openStmt checks it before cleaning closed
   303  	// connections in Stmt.css.
   304  	numClosed uint64
   305  
   306  	mu           sync.Mutex // protects following fields
   307  	freeConn     []*driverConn
   308  	connRequests map[uint64]chan connRequest
   309  	nextRequest  uint64 // Next key to use in connRequests.
   310  	numOpen      int    // number of opened and pending open connections
   311  	// Used to signal the need for new connections
   312  	// a goroutine running connectionOpener() reads on this chan and
   313  	// maybeOpenNewConnections sends on the chan (one send per needed connection)
   314  	// It is closed during db.Close(). The close tells the connectionOpener
   315  	// goroutine to exit.
   316  	openerCh    chan struct{}
   317  	closed      bool
   318  	dep         map[finalCloser]depSet
   319  	lastPut     map[*driverConn]string // stacktrace of last conn's put; debug only
   320  	maxIdle     int                    // zero means defaultMaxIdleConns; negative means 0
   321  	maxOpen     int                    // <= 0 means unlimited
   322  	maxLifetime time.Duration          // maximum amount of time a connection may be reused
   323  	cleanerCh   chan struct{}
   324  }
   325  
   326  // connReuseStrategy determines how (*DB).conn returns database connections.
   327  type connReuseStrategy uint8
   328  
   329  const (
   330  	// alwaysNewConn forces a new connection to the database.
   331  	alwaysNewConn connReuseStrategy = iota
   332  	// cachedOrNewConn returns a cached connection, if available, else waits
   333  	// for one to become available (if MaxOpenConns has been reached) or
   334  	// creates a new database connection.
   335  	cachedOrNewConn
   336  )
   337  
   338  // driverConn wraps a driver.Conn with a mutex, to
   339  // be held during all calls into the Conn. (including any calls onto
   340  // interfaces returned via that Conn, such as calls on Tx, Stmt,
   341  // Result, Rows)
   342  type driverConn struct {
   343  	db        *DB
   344  	createdAt time.Time
   345  
   346  	sync.Mutex  // guards following
   347  	ci          driver.Conn
   348  	closed      bool
   349  	finalClosed bool // ci.Close has been called
   350  	openStmt    map[*driverStmt]bool
   351  
   352  	// guarded by db.mu
   353  	inUse      bool
   354  	onPut      []func() // code (with db.mu held) run when conn is next returned
   355  	dbmuClosed bool     // same as closed, but guarded by db.mu, for removeClosedStmtLocked
   356  }
   357  
   358  func (dc *driverConn) releaseConn(err error) {
   359  	dc.db.putConn(dc, err)
   360  }
   361  
   362  func (dc *driverConn) removeOpenStmt(ds *driverStmt) {
   363  	dc.Lock()
   364  	defer dc.Unlock()
   365  	delete(dc.openStmt, ds)
   366  }
   367  
   368  func (dc *driverConn) expired(timeout time.Duration) bool {
   369  	if timeout <= 0 {
   370  		return false
   371  	}
   372  	return dc.createdAt.Add(timeout).Before(nowFunc())
   373  }
   374  
   375  func (dc *driverConn) prepareLocked(ctx context.Context, query string) (*driverStmt, error) {
   376  	si, err := ctxDriverPrepare(ctx, dc.ci, query)
   377  	if err != nil {
   378  		return nil, err
   379  	}
   380  
   381  	// Track each driverConn's open statements, so we can close them
   382  	// before closing the conn.
   383  	//
   384  	// Wrap all driver.Stmt is *driverStmt to ensure they are only closed once.
   385  	if dc.openStmt == nil {
   386  		dc.openStmt = make(map[*driverStmt]bool)
   387  	}
   388  	ds := &driverStmt{Locker: dc, si: si}
   389  	dc.openStmt[ds] = true
   390  
   391  	return ds, nil
   392  }
   393  
   394  // the dc.db's Mutex is held.
   395  func (dc *driverConn) closeDBLocked() func() error {
   396  	dc.Lock()
   397  	defer dc.Unlock()
   398  	if dc.closed {
   399  		return func() error { return errors.New("sql: duplicate driverConn close") }
   400  	}
   401  	dc.closed = true
   402  	return dc.db.removeDepLocked(dc, dc)
   403  }
   404  
   405  func (dc *driverConn) Close() error {
   406  	dc.Lock()
   407  	if dc.closed {
   408  		dc.Unlock()
   409  		return errors.New("sql: duplicate driverConn close")
   410  	}
   411  	dc.closed = true
   412  	dc.Unlock() // not defer; removeDep finalClose calls may need to lock
   413  
   414  	// And now updates that require holding dc.mu.Lock.
   415  	dc.db.mu.Lock()
   416  	dc.dbmuClosed = true
   417  	fn := dc.db.removeDepLocked(dc, dc)
   418  	dc.db.mu.Unlock()
   419  	return fn()
   420  }
   421  
   422  func (dc *driverConn) finalClose() error {
   423  	var err error
   424  
   425  	// Each *driverStmt has a lock to the dc. Copy the list out of the dc
   426  	// before calling close on each stmt.
   427  	var openStmt []*driverStmt
   428  	withLock(dc, func() {
   429  		openStmt = make([]*driverStmt, 0, len(dc.openStmt))
   430  		for ds := range dc.openStmt {
   431  			openStmt = append(openStmt, ds)
   432  		}
   433  		dc.openStmt = nil
   434  	})
   435  	for _, ds := range openStmt {
   436  		ds.Close()
   437  	}
   438  	withLock(dc, func() {
   439  		dc.finalClosed = true
   440  		err = dc.ci.Close()
   441  		dc.ci = nil
   442  	})
   443  
   444  	dc.db.mu.Lock()
   445  	dc.db.numOpen--
   446  	dc.db.maybeOpenNewConnections()
   447  	dc.db.mu.Unlock()
   448  
   449  	atomic.AddUint64(&dc.db.numClosed, 1)
   450  	return err
   451  }
   452  
   453  // driverStmt associates a driver.Stmt with the
   454  // *driverConn from which it came, so the driverConn's lock can be
   455  // held during calls.
   456  type driverStmt struct {
   457  	sync.Locker // the *driverConn
   458  	si          driver.Stmt
   459  	closed      bool
   460  	closeErr    error // return value of previous Close call
   461  }
   462  
   463  // Close ensures dirver.Stmt is only closed once any always returns the same
   464  // result.
   465  func (ds *driverStmt) Close() error {
   466  	ds.Lock()
   467  	defer ds.Unlock()
   468  	if ds.closed {
   469  		return ds.closeErr
   470  	}
   471  	ds.closed = true
   472  	ds.closeErr = ds.si.Close()
   473  	return ds.closeErr
   474  }
   475  
   476  // depSet is a finalCloser's outstanding dependencies
   477  type depSet map[interface{}]bool // set of true bools
   478  
   479  // The finalCloser interface is used by (*DB).addDep and related
   480  // dependency reference counting.
   481  type finalCloser interface {
   482  	// finalClose is called when the reference count of an object
   483  	// goes to zero. (*DB).mu is not held while calling it.
   484  	finalClose() error
   485  }
   486  
   487  // addDep notes that x now depends on dep, and x's finalClose won't be
   488  // called until all of x's dependencies are removed with removeDep.
   489  func (db *DB) addDep(x finalCloser, dep interface{}) {
   490  	//println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep))
   491  	db.mu.Lock()
   492  	defer db.mu.Unlock()
   493  	db.addDepLocked(x, dep)
   494  }
   495  
   496  func (db *DB) addDepLocked(x finalCloser, dep interface{}) {
   497  	if db.dep == nil {
   498  		db.dep = make(map[finalCloser]depSet)
   499  	}
   500  	xdep := db.dep[x]
   501  	if xdep == nil {
   502  		xdep = make(depSet)
   503  		db.dep[x] = xdep
   504  	}
   505  	xdep[dep] = true
   506  }
   507  
   508  // removeDep notes that x no longer depends on dep.
   509  // If x still has dependencies, nil is returned.
   510  // If x no longer has any dependencies, its finalClose method will be
   511  // called and its error value will be returned.
   512  func (db *DB) removeDep(x finalCloser, dep interface{}) error {
   513  	db.mu.Lock()
   514  	fn := db.removeDepLocked(x, dep)
   515  	db.mu.Unlock()
   516  	return fn()
   517  }
   518  
   519  func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error {
   520  	//println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep))
   521  
   522  	xdep, ok := db.dep[x]
   523  	if !ok {
   524  		panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
   525  	}
   526  
   527  	l0 := len(xdep)
   528  	delete(xdep, dep)
   529  
   530  	switch len(xdep) {
   531  	case l0:
   532  		// Nothing removed. Shouldn't happen.
   533  		panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
   534  	case 0:
   535  		// No more dependencies.
   536  		delete(db.dep, x)
   537  		return x.finalClose
   538  	default:
   539  		// Dependencies remain.
   540  		return func() error { return nil }
   541  	}
   542  }
   543  
   544  // This is the size of the connectionOpener request chan (DB.openerCh).
   545  // This value should be larger than the maximum typical value
   546  // used for db.maxOpen. If maxOpen is significantly larger than
   547  // connectionRequestQueueSize then it is possible for ALL calls into the *DB
   548  // to block until the connectionOpener can satisfy the backlog of requests.
   549  var connectionRequestQueueSize = 1000000
   550  
   551  // Open opens a database specified by its database driver name and a
   552  // driver-specific data source name, usually consisting of at least a
   553  // database name and connection information.
   554  //
   555  // Most users will open a database via a driver-specific connection
   556  // helper function that returns a *DB. No database drivers are included
   557  // in the Go standard library. See https://golang.org/s/sqldrivers for
   558  // a list of third-party drivers.
   559  //
   560  // Open may just validate its arguments without creating a connection
   561  // to the database. To verify that the data source name is valid, call
   562  // Ping.
   563  //
   564  // The returned DB is safe for concurrent use by multiple goroutines
   565  // and maintains its own pool of idle connections. Thus, the Open
   566  // function should be called just once. It is rarely necessary to
   567  // close a DB.
   568  func Open(driverName, dataSourceName string) (*DB, error) {
   569  	driversMu.RLock()
   570  	driveri, ok := drivers[driverName]
   571  	driversMu.RUnlock()
   572  	if !ok {
   573  		return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
   574  	}
   575  	db := &DB{
   576  		driver:       driveri,
   577  		dsn:          dataSourceName,
   578  		openerCh:     make(chan struct{}, connectionRequestQueueSize),
   579  		lastPut:      make(map[*driverConn]string),
   580  		connRequests: make(map[uint64]chan connRequest),
   581  	}
   582  	go db.connectionOpener()
   583  	return db, nil
   584  }
   585  
   586  func (db *DB) pingDC(ctx context.Context, dc *driverConn, release func(error)) error {
   587  	var err error
   588  	if pinger, ok := dc.ci.(driver.Pinger); ok {
   589  		withLock(dc, func() {
   590  			err = pinger.Ping(ctx)
   591  		})
   592  	}
   593  	release(err)
   594  	return err
   595  }
   596  
   597  // PingContext verifies a connection to the database is still alive,
   598  // establishing a connection if necessary.
   599  func (db *DB) PingContext(ctx context.Context) error {
   600  	var dc *driverConn
   601  	var err error
   602  
   603  	for i := 0; i < maxBadConnRetries; i++ {
   604  		dc, err = db.conn(ctx, cachedOrNewConn)
   605  		if err != driver.ErrBadConn {
   606  			break
   607  		}
   608  	}
   609  	if err == driver.ErrBadConn {
   610  		dc, err = db.conn(ctx, alwaysNewConn)
   611  	}
   612  	if err != nil {
   613  		return err
   614  	}
   615  
   616  	return db.pingDC(ctx, dc, dc.releaseConn)
   617  }
   618  
   619  // Ping verifies a connection to the database is still alive,
   620  // establishing a connection if necessary.
   621  func (db *DB) Ping() error {
   622  	return db.PingContext(context.Background())
   623  }
   624  
   625  // Close closes the database, releasing any open resources.
   626  //
   627  // It is rare to Close a DB, as the DB handle is meant to be
   628  // long-lived and shared between many goroutines.
   629  func (db *DB) Close() error {
   630  	db.mu.Lock()
   631  	if db.closed { // Make DB.Close idempotent
   632  		db.mu.Unlock()
   633  		return nil
   634  	}
   635  	close(db.openerCh)
   636  	if db.cleanerCh != nil {
   637  		close(db.cleanerCh)
   638  	}
   639  	var err error
   640  	fns := make([]func() error, 0, len(db.freeConn))
   641  	for _, dc := range db.freeConn {
   642  		fns = append(fns, dc.closeDBLocked())
   643  	}
   644  	db.freeConn = nil
   645  	db.closed = true
   646  	for _, req := range db.connRequests {
   647  		close(req)
   648  	}
   649  	db.mu.Unlock()
   650  	for _, fn := range fns {
   651  		err1 := fn()
   652  		if err1 != nil {
   653  			err = err1
   654  		}
   655  	}
   656  	return err
   657  }
   658  
   659  const defaultMaxIdleConns = 2
   660  
   661  func (db *DB) maxIdleConnsLocked() int {
   662  	n := db.maxIdle
   663  	switch {
   664  	case n == 0:
   665  		// TODO(bradfitz): ask driver, if supported, for its default preference
   666  		return defaultMaxIdleConns
   667  	case n < 0:
   668  		return 0
   669  	default:
   670  		return n
   671  	}
   672  }
   673  
   674  // SetMaxIdleConns sets the maximum number of connections in the idle
   675  // connection pool.
   676  //
   677  // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
   678  // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
   679  //
   680  // If n <= 0, no idle connections are retained.
   681  func (db *DB) SetMaxIdleConns(n int) {
   682  	db.mu.Lock()
   683  	if n > 0 {
   684  		db.maxIdle = n
   685  	} else {
   686  		// No idle connections.
   687  		db.maxIdle = -1
   688  	}
   689  	// Make sure maxIdle doesn't exceed maxOpen
   690  	if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
   691  		db.maxIdle = db.maxOpen
   692  	}
   693  	var closing []*driverConn
   694  	idleCount := len(db.freeConn)
   695  	maxIdle := db.maxIdleConnsLocked()
   696  	if idleCount > maxIdle {
   697  		closing = db.freeConn[maxIdle:]
   698  		db.freeConn = db.freeConn[:maxIdle]
   699  	}
   700  	db.mu.Unlock()
   701  	for _, c := range closing {
   702  		c.Close()
   703  	}
   704  }
   705  
   706  // SetMaxOpenConns sets the maximum number of open connections to the database.
   707  //
   708  // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
   709  // MaxIdleConns, then MaxIdleConns will be reduced to match the new
   710  // MaxOpenConns limit
   711  //
   712  // If n <= 0, then there is no limit on the number of open connections.
   713  // The default is 0 (unlimited).
   714  func (db *DB) SetMaxOpenConns(n int) {
   715  	db.mu.Lock()
   716  	db.maxOpen = n
   717  	if n < 0 {
   718  		db.maxOpen = 0
   719  	}
   720  	syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
   721  	db.mu.Unlock()
   722  	if syncMaxIdle {
   723  		db.SetMaxIdleConns(n)
   724  	}
   725  }
   726  
   727  // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
   728  //
   729  // Expired connections may be closed lazily before reuse.
   730  //
   731  // If d <= 0, connections are reused forever.
   732  func (db *DB) SetConnMaxLifetime(d time.Duration) {
   733  	if d < 0 {
   734  		d = 0
   735  	}
   736  	db.mu.Lock()
   737  	// wake cleaner up when lifetime is shortened.
   738  	if d > 0 && d < db.maxLifetime && db.cleanerCh != nil {
   739  		select {
   740  		case db.cleanerCh <- struct{}{}:
   741  		default:
   742  		}
   743  	}
   744  	db.maxLifetime = d
   745  	db.startCleanerLocked()
   746  	db.mu.Unlock()
   747  }
   748  
   749  // startCleanerLocked starts connectionCleaner if needed.
   750  func (db *DB) startCleanerLocked() {
   751  	if db.maxLifetime > 0 && db.numOpen > 0 && db.cleanerCh == nil {
   752  		db.cleanerCh = make(chan struct{}, 1)
   753  		go db.connectionCleaner(db.maxLifetime)
   754  	}
   755  }
   756  
   757  func (db *DB) connectionCleaner(d time.Duration) {
   758  	const minInterval = time.Second
   759  
   760  	if d < minInterval {
   761  		d = minInterval
   762  	}
   763  	t := time.NewTimer(d)
   764  
   765  	for {
   766  		select {
   767  		case <-t.C:
   768  		case <-db.cleanerCh: // maxLifetime was changed or db was closed.
   769  		}
   770  
   771  		db.mu.Lock()
   772  		d = db.maxLifetime
   773  		if db.closed || db.numOpen == 0 || d <= 0 {
   774  			db.cleanerCh = nil
   775  			db.mu.Unlock()
   776  			return
   777  		}
   778  
   779  		expiredSince := nowFunc().Add(-d)
   780  		var closing []*driverConn
   781  		for i := 0; i < len(db.freeConn); i++ {
   782  			c := db.freeConn[i]
   783  			if c.createdAt.Before(expiredSince) {
   784  				closing = append(closing, c)
   785  				last := len(db.freeConn) - 1
   786  				db.freeConn[i] = db.freeConn[last]
   787  				db.freeConn[last] = nil
   788  				db.freeConn = db.freeConn[:last]
   789  				i--
   790  			}
   791  		}
   792  		db.mu.Unlock()
   793  
   794  		for _, c := range closing {
   795  			c.Close()
   796  		}
   797  
   798  		if d < minInterval {
   799  			d = minInterval
   800  		}
   801  		t.Reset(d)
   802  	}
   803  }
   804  
   805  // DBStats contains database statistics.
   806  type DBStats struct {
   807  	// OpenConnections is the number of open connections to the database.
   808  	OpenConnections int
   809  }
   810  
   811  // Stats returns database statistics.
   812  func (db *DB) Stats() DBStats {
   813  	db.mu.Lock()
   814  	stats := DBStats{
   815  		OpenConnections: db.numOpen,
   816  	}
   817  	db.mu.Unlock()
   818  	return stats
   819  }
   820  
   821  // Assumes db.mu is locked.
   822  // If there are connRequests and the connection limit hasn't been reached,
   823  // then tell the connectionOpener to open new connections.
   824  func (db *DB) maybeOpenNewConnections() {
   825  	numRequests := len(db.connRequests)
   826  	if db.maxOpen > 0 {
   827  		numCanOpen := db.maxOpen - db.numOpen
   828  		if numRequests > numCanOpen {
   829  			numRequests = numCanOpen
   830  		}
   831  	}
   832  	for numRequests > 0 {
   833  		db.numOpen++ // optimistically
   834  		numRequests--
   835  		if db.closed {
   836  			return
   837  		}
   838  		db.openerCh <- struct{}{}
   839  	}
   840  }
   841  
   842  // Runs in a separate goroutine, opens new connections when requested.
   843  func (db *DB) connectionOpener() {
   844  	for range db.openerCh {
   845  		db.openNewConnection()
   846  	}
   847  }
   848  
   849  // Open one new connection
   850  func (db *DB) openNewConnection() {
   851  	// maybeOpenNewConnctions has already executed db.numOpen++ before it sent
   852  	// on db.openerCh. This function must execute db.numOpen-- if the
   853  	// connection fails or is closed before returning.
   854  	ci, err := db.driver.Open(db.dsn)
   855  	db.mu.Lock()
   856  	defer db.mu.Unlock()
   857  	if db.closed {
   858  		if err == nil {
   859  			ci.Close()
   860  		}
   861  		db.numOpen--
   862  		return
   863  	}
   864  	if err != nil {
   865  		db.numOpen--
   866  		db.putConnDBLocked(nil, err)
   867  		db.maybeOpenNewConnections()
   868  		return
   869  	}
   870  	dc := &driverConn{
   871  		db:        db,
   872  		createdAt: nowFunc(),
   873  		ci:        ci,
   874  	}
   875  	if db.putConnDBLocked(dc, err) {
   876  		db.addDepLocked(dc, dc)
   877  	} else {
   878  		db.numOpen--
   879  		ci.Close()
   880  	}
   881  }
   882  
   883  // connRequest represents one request for a new connection
   884  // When there are no idle connections available, DB.conn will create
   885  // a new connRequest and put it on the db.connRequests list.
   886  type connRequest struct {
   887  	conn *driverConn
   888  	err  error
   889  }
   890  
   891  var errDBClosed = errors.New("sql: database is closed")
   892  
   893  // nextRequestKeyLocked returns the next connection request key.
   894  // It is assumed that nextRequest will not overflow.
   895  func (db *DB) nextRequestKeyLocked() uint64 {
   896  	next := db.nextRequest
   897  	db.nextRequest++
   898  	return next
   899  }
   900  
   901  // conn returns a newly-opened or cached *driverConn.
   902  func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) {
   903  	db.mu.Lock()
   904  	if db.closed {
   905  		db.mu.Unlock()
   906  		return nil, errDBClosed
   907  	}
   908  	// Check if the context is expired.
   909  	select {
   910  	default:
   911  	case <-ctx.Done():
   912  		db.mu.Unlock()
   913  		return nil, ctx.Err()
   914  	}
   915  	lifetime := db.maxLifetime
   916  
   917  	// Prefer a free connection, if possible.
   918  	numFree := len(db.freeConn)
   919  	if strategy == cachedOrNewConn && numFree > 0 {
   920  		conn := db.freeConn[0]
   921  		copy(db.freeConn, db.freeConn[1:])
   922  		db.freeConn = db.freeConn[:numFree-1]
   923  		conn.inUse = true
   924  		db.mu.Unlock()
   925  		if conn.expired(lifetime) {
   926  			conn.Close()
   927  			return nil, driver.ErrBadConn
   928  		}
   929  		return conn, nil
   930  	}
   931  
   932  	// Out of free connections or we were asked not to use one. If we're not
   933  	// allowed to open any more connections, make a request and wait.
   934  	if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
   935  		// Make the connRequest channel. It's buffered so that the
   936  		// connectionOpener doesn't block while waiting for the req to be read.
   937  		req := make(chan connRequest, 1)
   938  		reqKey := db.nextRequestKeyLocked()
   939  		db.connRequests[reqKey] = req
   940  		db.mu.Unlock()
   941  
   942  		// Timeout the connection request with the context.
   943  		select {
   944  		case <-ctx.Done():
   945  			// Remove the connection request and ensure no value has been sent
   946  			// on it after removing.
   947  			db.mu.Lock()
   948  			delete(db.connRequests, reqKey)
   949  			db.mu.Unlock()
   950  			select {
   951  			default:
   952  			case ret, ok := <-req:
   953  				if ok {
   954  					db.putConn(ret.conn, ret.err)
   955  				}
   956  			}
   957  			return nil, ctx.Err()
   958  		case ret, ok := <-req:
   959  			if !ok {
   960  				return nil, errDBClosed
   961  			}
   962  			if ret.err == nil && ret.conn.expired(lifetime) {
   963  				ret.conn.Close()
   964  				return nil, driver.ErrBadConn
   965  			}
   966  			return ret.conn, ret.err
   967  		}
   968  	}
   969  
   970  	db.numOpen++ // optimistically
   971  	db.mu.Unlock()
   972  	ci, err := db.driver.Open(db.dsn)
   973  	if err != nil {
   974  		db.mu.Lock()
   975  		db.numOpen-- // correct for earlier optimism
   976  		db.maybeOpenNewConnections()
   977  		db.mu.Unlock()
   978  		return nil, err
   979  	}
   980  	db.mu.Lock()
   981  	dc := &driverConn{
   982  		db:        db,
   983  		createdAt: nowFunc(),
   984  		ci:        ci,
   985  		inUse:     true,
   986  	}
   987  	db.addDepLocked(dc, dc)
   988  	db.mu.Unlock()
   989  	return dc, nil
   990  }
   991  
   992  // putConnHook is a hook for testing.
   993  var putConnHook func(*DB, *driverConn)
   994  
   995  // noteUnusedDriverStatement notes that ds is no longer used and should
   996  // be closed whenever possible (when c is next not in use), unless c is
   997  // already closed.
   998  func (db *DB) noteUnusedDriverStatement(c *driverConn, ds *driverStmt) {
   999  	db.mu.Lock()
  1000  	defer db.mu.Unlock()
  1001  	if c.inUse {
  1002  		c.onPut = append(c.onPut, func() {
  1003  			ds.Close()
  1004  		})
  1005  	} else {
  1006  		c.Lock()
  1007  		fc := c.finalClosed
  1008  		c.Unlock()
  1009  		if !fc {
  1010  			ds.Close()
  1011  		}
  1012  	}
  1013  }
  1014  
  1015  // debugGetPut determines whether getConn & putConn calls' stack traces
  1016  // are returned for more verbose crashes.
  1017  const debugGetPut = false
  1018  
  1019  // putConn adds a connection to the db's free pool.
  1020  // err is optionally the last error that occurred on this connection.
  1021  func (db *DB) putConn(dc *driverConn, err error) {
  1022  	db.mu.Lock()
  1023  	if !dc.inUse {
  1024  		if debugGetPut {
  1025  			fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
  1026  		}
  1027  		panic("sql: connection returned that was never out")
  1028  	}
  1029  	if debugGetPut {
  1030  		db.lastPut[dc] = stack()
  1031  	}
  1032  	dc.inUse = false
  1033  
  1034  	for _, fn := range dc.onPut {
  1035  		fn()
  1036  	}
  1037  	dc.onPut = nil
  1038  
  1039  	if err == driver.ErrBadConn {
  1040  		// Don't reuse bad connections.
  1041  		// Since the conn is considered bad and is being discarded, treat it
  1042  		// as closed. Don't decrement the open count here, finalClose will
  1043  		// take care of that.
  1044  		db.maybeOpenNewConnections()
  1045  		db.mu.Unlock()
  1046  		dc.Close()
  1047  		return
  1048  	}
  1049  	if putConnHook != nil {
  1050  		putConnHook(db, dc)
  1051  	}
  1052  	added := db.putConnDBLocked(dc, nil)
  1053  	db.mu.Unlock()
  1054  
  1055  	if !added {
  1056  		dc.Close()
  1057  	}
  1058  }
  1059  
  1060  // Satisfy a connRequest or put the driverConn in the idle pool and return true
  1061  // or return false.
  1062  // putConnDBLocked will satisfy a connRequest if there is one, or it will
  1063  // return the *driverConn to the freeConn list if err == nil and the idle
  1064  // connection limit will not be exceeded.
  1065  // If err != nil, the value of dc is ignored.
  1066  // If err == nil, then dc must not equal nil.
  1067  // If a connRequest was fulfilled or the *driverConn was placed in the
  1068  // freeConn list, then true is returned, otherwise false is returned.
  1069  func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
  1070  	if db.closed {
  1071  		return false
  1072  	}
  1073  	if db.maxOpen > 0 && db.numOpen > db.maxOpen {
  1074  		return false
  1075  	}
  1076  	if c := len(db.connRequests); c > 0 {
  1077  		var req chan connRequest
  1078  		var reqKey uint64
  1079  		for reqKey, req = range db.connRequests {
  1080  			break
  1081  		}
  1082  		delete(db.connRequests, reqKey) // Remove from pending requests.
  1083  		if err == nil {
  1084  			dc.inUse = true
  1085  		}
  1086  		req <- connRequest{
  1087  			conn: dc,
  1088  			err:  err,
  1089  		}
  1090  		return true
  1091  	} else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
  1092  		db.freeConn = append(db.freeConn, dc)
  1093  		db.startCleanerLocked()
  1094  		return true
  1095  	}
  1096  	return false
  1097  }
  1098  
  1099  // maxBadConnRetries is the number of maximum retries if the driver returns
  1100  // driver.ErrBadConn to signal a broken connection before forcing a new
  1101  // connection to be opened.
  1102  const maxBadConnRetries = 2
  1103  
  1104  // PrepareContext creates a prepared statement for later queries or executions.
  1105  // Multiple queries or executions may be run concurrently from the
  1106  // returned statement.
  1107  // The caller must call the statement's Close method
  1108  // when the statement is no longer needed.
  1109  //
  1110  // The provided context is used for the preparation of the statement, not for the
  1111  // execution of the statement.
  1112  func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1113  	var stmt *Stmt
  1114  	var err error
  1115  	for i := 0; i < maxBadConnRetries; i++ {
  1116  		stmt, err = db.prepare(ctx, query, cachedOrNewConn)
  1117  		if err != driver.ErrBadConn {
  1118  			break
  1119  		}
  1120  	}
  1121  	if err == driver.ErrBadConn {
  1122  		return db.prepare(ctx, query, alwaysNewConn)
  1123  	}
  1124  	return stmt, err
  1125  }
  1126  
  1127  // Prepare creates a prepared statement for later queries or executions.
  1128  // Multiple queries or executions may be run concurrently from the
  1129  // returned statement.
  1130  // The caller must call the statement's Close method
  1131  // when the statement is no longer needed.
  1132  func (db *DB) Prepare(query string) (*Stmt, error) {
  1133  	return db.PrepareContext(context.Background(), query)
  1134  }
  1135  
  1136  func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*Stmt, error) {
  1137  	// TODO: check if db.driver supports an optional
  1138  	// driver.Preparer interface and call that instead, if so,
  1139  	// otherwise we make a prepared statement that's bound
  1140  	// to a connection, and to execute this prepared statement
  1141  	// we either need to use this connection (if it's free), else
  1142  	// get a new connection + re-prepare + execute on that one.
  1143  	dc, err := db.conn(ctx, strategy)
  1144  	if err != nil {
  1145  		return nil, err
  1146  	}
  1147  	return db.prepareDC(ctx, dc, dc.releaseConn, query)
  1148  }
  1149  
  1150  func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error), query string) (*Stmt, error) {
  1151  	var ds *driverStmt
  1152  	var err error
  1153  	defer func() {
  1154  		release(err)
  1155  	}()
  1156  	withLock(dc, func() {
  1157  		ds, err = dc.prepareLocked(ctx, query)
  1158  	})
  1159  	if err != nil {
  1160  		return nil, err
  1161  	}
  1162  	stmt := &Stmt{
  1163  		db:            db,
  1164  		query:         query,
  1165  		css:           []connStmt{{dc, ds}},
  1166  		lastNumClosed: atomic.LoadUint64(&db.numClosed),
  1167  	}
  1168  	db.addDep(stmt, stmt)
  1169  	return stmt, nil
  1170  }
  1171  
  1172  // ExecContext executes a query without returning any rows.
  1173  // The args are for any placeholder parameters in the query.
  1174  func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
  1175  	var res Result
  1176  	var err error
  1177  	for i := 0; i < maxBadConnRetries; i++ {
  1178  		res, err = db.exec(ctx, query, args, cachedOrNewConn)
  1179  		if err != driver.ErrBadConn {
  1180  			break
  1181  		}
  1182  	}
  1183  	if err == driver.ErrBadConn {
  1184  		return db.exec(ctx, query, args, alwaysNewConn)
  1185  	}
  1186  	return res, err
  1187  }
  1188  
  1189  // Exec executes a query without returning any rows.
  1190  // The args are for any placeholder parameters in the query.
  1191  func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
  1192  	return db.ExecContext(context.Background(), query, args...)
  1193  }
  1194  
  1195  func (db *DB) exec(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (Result, error) {
  1196  	dc, err := db.conn(ctx, strategy)
  1197  	if err != nil {
  1198  		return nil, err
  1199  	}
  1200  	return db.execDC(ctx, dc, dc.releaseConn, query, args)
  1201  }
  1202  
  1203  func (db *DB) execDC(ctx context.Context, dc *driverConn, release func(error), query string, args []interface{}) (res Result, err error) {
  1204  	defer func() {
  1205  		release(err)
  1206  	}()
  1207  	if execer, ok := dc.ci.(driver.Execer); ok {
  1208  		var dargs []driver.NamedValue
  1209  		dargs, err = driverArgs(nil, args)
  1210  		if err != nil {
  1211  			return nil, err
  1212  		}
  1213  		var resi driver.Result
  1214  		withLock(dc, func() {
  1215  			resi, err = ctxDriverExec(ctx, execer, query, dargs)
  1216  		})
  1217  		if err != driver.ErrSkip {
  1218  			if err != nil {
  1219  				return nil, err
  1220  			}
  1221  			return driverResult{dc, resi}, nil
  1222  		}
  1223  	}
  1224  
  1225  	var si driver.Stmt
  1226  	withLock(dc, func() {
  1227  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1228  	})
  1229  	if err != nil {
  1230  		return nil, err
  1231  	}
  1232  	ds := &driverStmt{Locker: dc, si: si}
  1233  	defer ds.Close()
  1234  	return resultFromStatement(ctx, ds, args...)
  1235  }
  1236  
  1237  // QueryContext executes a query that returns rows, typically a SELECT.
  1238  // The args are for any placeholder parameters in the query.
  1239  func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  1240  	var rows *Rows
  1241  	var err error
  1242  	for i := 0; i < maxBadConnRetries; i++ {
  1243  		rows, err = db.query(ctx, query, args, cachedOrNewConn)
  1244  		if err != driver.ErrBadConn {
  1245  			break
  1246  		}
  1247  	}
  1248  	if err == driver.ErrBadConn {
  1249  		return db.query(ctx, query, args, alwaysNewConn)
  1250  	}
  1251  	return rows, err
  1252  }
  1253  
  1254  // Query executes a query that returns rows, typically a SELECT.
  1255  // The args are for any placeholder parameters in the query.
  1256  func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
  1257  	return db.QueryContext(context.Background(), query, args...)
  1258  }
  1259  
  1260  func (db *DB) query(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
  1261  	dc, err := db.conn(ctx, strategy)
  1262  	if err != nil {
  1263  		return nil, err
  1264  	}
  1265  
  1266  	return db.queryDC(ctx, dc, dc.releaseConn, query, args)
  1267  }
  1268  
  1269  // queryDC executes a query on the given connection.
  1270  // The connection gets released by the releaseConn function.
  1271  func (db *DB) queryDC(ctx context.Context, dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
  1272  	if queryer, ok := dc.ci.(driver.Queryer); ok {
  1273  		dargs, err := driverArgs(nil, args)
  1274  		if err != nil {
  1275  			releaseConn(err)
  1276  			return nil, err
  1277  		}
  1278  		var rowsi driver.Rows
  1279  		withLock(dc, func() {
  1280  			rowsi, err = ctxDriverQuery(ctx, queryer, query, dargs)
  1281  		})
  1282  		if err != driver.ErrSkip {
  1283  			if err != nil {
  1284  				releaseConn(err)
  1285  				return nil, err
  1286  			}
  1287  			// Note: ownership of dc passes to the *Rows, to be freed
  1288  			// with releaseConn.
  1289  			rows := &Rows{
  1290  				dc:          dc,
  1291  				releaseConn: releaseConn,
  1292  				rowsi:       rowsi,
  1293  			}
  1294  			rows.initContextClose(ctx)
  1295  			return rows, nil
  1296  		}
  1297  	}
  1298  
  1299  	var si driver.Stmt
  1300  	var err error
  1301  	withLock(dc, func() {
  1302  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1303  	})
  1304  	if err != nil {
  1305  		releaseConn(err)
  1306  		return nil, err
  1307  	}
  1308  
  1309  	ds := &driverStmt{Locker: dc, si: si}
  1310  	rowsi, err := rowsiFromStatement(ctx, ds, args...)
  1311  	if err != nil {
  1312  		ds.Close()
  1313  		releaseConn(err)
  1314  		return nil, err
  1315  	}
  1316  
  1317  	// Note: ownership of ci passes to the *Rows, to be freed
  1318  	// with releaseConn.
  1319  	rows := &Rows{
  1320  		dc:          dc,
  1321  		releaseConn: releaseConn,
  1322  		rowsi:       rowsi,
  1323  		closeStmt:   ds,
  1324  	}
  1325  	rows.initContextClose(ctx)
  1326  	return rows, nil
  1327  }
  1328  
  1329  // QueryRowContext executes a query that is expected to return at most one row.
  1330  // QueryRowContext always returns a non-nil value. Errors are deferred until
  1331  // Row's Scan method is called.
  1332  func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  1333  	rows, err := db.QueryContext(ctx, query, args...)
  1334  	return &Row{rows: rows, err: err}
  1335  }
  1336  
  1337  // QueryRow executes a query that is expected to return at most one row.
  1338  // QueryRow always returns a non-nil value. Errors are deferred until
  1339  // Row's Scan method is called.
  1340  func (db *DB) QueryRow(query string, args ...interface{}) *Row {
  1341  	return db.QueryRowContext(context.Background(), query, args...)
  1342  }
  1343  
  1344  // BeginTx starts a transaction.
  1345  //
  1346  // The provided context is used until the transaction is committed or rolled back.
  1347  // If the context is canceled, the sql package will roll back
  1348  // the transaction. Tx.Commit will return an error if the context provided to
  1349  // BeginTx is canceled.
  1350  //
  1351  // The provided TxOptions is optional and may be nil if defaults should be used.
  1352  // If a non-default isolation level is used that the driver doesn't support,
  1353  // an error will be returned.
  1354  func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
  1355  	var tx *Tx
  1356  	var err error
  1357  	for i := 0; i < maxBadConnRetries; i++ {
  1358  		tx, err = db.begin(ctx, opts, cachedOrNewConn)
  1359  		if err != driver.ErrBadConn {
  1360  			break
  1361  		}
  1362  	}
  1363  	if err == driver.ErrBadConn {
  1364  		return db.begin(ctx, opts, alwaysNewConn)
  1365  	}
  1366  	return tx, err
  1367  }
  1368  
  1369  // Begin starts a transaction. The default isolation level is dependent on
  1370  // the driver.
  1371  func (db *DB) Begin() (*Tx, error) {
  1372  	return db.BeginTx(context.Background(), nil)
  1373  }
  1374  
  1375  func (db *DB) begin(ctx context.Context, opts *TxOptions, strategy connReuseStrategy) (tx *Tx, err error) {
  1376  	dc, err := db.conn(ctx, strategy)
  1377  	if err != nil {
  1378  		return nil, err
  1379  	}
  1380  	return db.beginDC(ctx, dc, dc.releaseConn, opts)
  1381  }
  1382  
  1383  // beginDC starts a transaction. The provided dc must be valid and ready to use.
  1384  func (db *DB) beginDC(ctx context.Context, dc *driverConn, release func(error), opts *TxOptions) (tx *Tx, err error) {
  1385  	var txi driver.Tx
  1386  	withLock(dc, func() {
  1387  		txi, err = ctxDriverBegin(ctx, opts, dc.ci)
  1388  	})
  1389  	if err != nil {
  1390  		release(err)
  1391  		return nil, err
  1392  	}
  1393  
  1394  	// Schedule the transaction to rollback when the context is cancelled.
  1395  	// The cancel function in Tx will be called after done is set to true.
  1396  	ctx, cancel := context.WithCancel(ctx)
  1397  	tx = &Tx{
  1398  		db:          db,
  1399  		dc:          dc,
  1400  		releaseConn: release,
  1401  		txi:         txi,
  1402  		cancel:      cancel,
  1403  		ctx:         ctx,
  1404  	}
  1405  	go tx.awaitDone()
  1406  	return tx, nil
  1407  }
  1408  
  1409  // Driver returns the database's underlying driver.
  1410  func (db *DB) Driver() driver.Driver {
  1411  	return db.driver
  1412  }
  1413  
  1414  // ErrConnDone is returned by any operation that is performed on a connection
  1415  // that has already been committed or rolled back.
  1416  var ErrConnDone = errors.New("database/sql: connection is already closed")
  1417  
  1418  // Conn returns a single connection by either opening a new connection
  1419  // or returning an existing connection from the connection pool. Conn will
  1420  // block until either a connection is returned or ctx is canceled.
  1421  // Queries run on the same Conn will be run in the same database session.
  1422  //
  1423  // Every Conn must be returned to the database pool after use by
  1424  // calling Conn.Close.
  1425  func (db *DB) Conn(ctx context.Context) (*Conn, error) {
  1426  	var dc *driverConn
  1427  	var err error
  1428  	for i := 0; i < maxBadConnRetries; i++ {
  1429  		dc, err = db.conn(ctx, cachedOrNewConn)
  1430  		if err != driver.ErrBadConn {
  1431  			break
  1432  		}
  1433  	}
  1434  	if err == driver.ErrBadConn {
  1435  		dc, err = db.conn(ctx, cachedOrNewConn)
  1436  	}
  1437  	if err != nil {
  1438  		return nil, err
  1439  	}
  1440  
  1441  	conn := &Conn{
  1442  		db: db,
  1443  		dc: dc,
  1444  	}
  1445  	return conn, nil
  1446  }
  1447  
  1448  // Conn represents a single database session rather a pool of database
  1449  // sessions. Prefer running queries from DB unless there is a specific
  1450  // need for a continuous single database session.
  1451  //
  1452  // A Conn must call Close to return the connection to the database pool
  1453  // and may do so concurrently with a running query.
  1454  //
  1455  // After a call to Close, all operations on the
  1456  // connection fail with ErrConnDone.
  1457  type Conn struct {
  1458  	db *DB
  1459  
  1460  	// closemu prevents the connection from closing while there
  1461  	// is an active query. It is held for read during queries
  1462  	// and exclusively during close.
  1463  	closemu sync.RWMutex
  1464  
  1465  	// dc is owned until close, at which point
  1466  	// it's returned to the connection pool.
  1467  	dc *driverConn
  1468  
  1469  	// done transitions from 0 to 1 exactly once, on close.
  1470  	// Once done, all operations fail with ErrConnDone.
  1471  	// Use atomic operations on value when checking value.
  1472  	done int32
  1473  }
  1474  
  1475  func (c *Conn) grabConn() (*driverConn, error) {
  1476  	if atomic.LoadInt32(&c.done) != 0 {
  1477  		return nil, ErrConnDone
  1478  	}
  1479  	return c.dc, nil
  1480  }
  1481  
  1482  // PingContext verifies the connection to the database is still alive.
  1483  func (c *Conn) PingContext(ctx context.Context) error {
  1484  	dc, err := c.grabConn()
  1485  	if err != nil {
  1486  		return err
  1487  	}
  1488  
  1489  	c.closemu.RLock()
  1490  	return c.db.pingDC(ctx, dc, c.closemuRUnlockCondReleaseConn)
  1491  }
  1492  
  1493  // ExecContext executes a query without returning any rows.
  1494  // The args are for any placeholder parameters in the query.
  1495  func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
  1496  	dc, err := c.grabConn()
  1497  	if err != nil {
  1498  		return nil, err
  1499  	}
  1500  
  1501  	c.closemu.RLock()
  1502  	return c.db.execDC(ctx, dc, c.closemuRUnlockCondReleaseConn, query, args)
  1503  }
  1504  
  1505  // QueryContext executes a query that returns rows, typically a SELECT.
  1506  // The args are for any placeholder parameters in the query.
  1507  func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  1508  	dc, err := c.grabConn()
  1509  	if err != nil {
  1510  		return nil, err
  1511  	}
  1512  
  1513  	c.closemu.RLock()
  1514  	return c.db.queryDC(ctx, dc, c.closemuRUnlockCondReleaseConn, query, args)
  1515  }
  1516  
  1517  // QueryRowContext executes a query that is expected to return at most one row.
  1518  // QueryRowContext always returns a non-nil value. Errors are deferred until
  1519  // Row's Scan method is called.
  1520  func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  1521  	rows, err := c.QueryContext(ctx, query, args...)
  1522  	return &Row{rows: rows, err: err}
  1523  }
  1524  
  1525  // PrepareContext creates a prepared statement for later queries or executions.
  1526  // Multiple queries or executions may be run concurrently from the
  1527  // returned statement.
  1528  // The caller must call the statement's Close method
  1529  // when the statement is no longer needed.
  1530  //
  1531  // The provided context is used for the preparation of the statement, not for the
  1532  // execution of the statement.
  1533  func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1534  	dc, err := c.grabConn()
  1535  	if err != nil {
  1536  		return nil, err
  1537  	}
  1538  
  1539  	c.closemu.RLock()
  1540  	return c.db.prepareDC(ctx, dc, c.closemuRUnlockCondReleaseConn, query)
  1541  }
  1542  
  1543  // BeginTx starts a transaction.
  1544  //
  1545  // The provided context is used until the transaction is committed or rolled back.
  1546  // If the context is canceled, the sql package will roll back
  1547  // the transaction. Tx.Commit will return an error if the context provided to
  1548  // BeginTx is canceled.
  1549  //
  1550  // The provided TxOptions is optional and may be nil if defaults should be used.
  1551  // If a non-default isolation level is used that the driver doesn't support,
  1552  // an error will be returned.
  1553  func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
  1554  	dc, err := c.grabConn()
  1555  	if err != nil {
  1556  		return nil, err
  1557  	}
  1558  
  1559  	c.closemu.RLock()
  1560  	return c.db.beginDC(ctx, dc, c.closemuRUnlockCondReleaseConn, opts)
  1561  }
  1562  
  1563  // closemuRUnlockCondReleaseConn read unlocks closemu
  1564  // as the sql operation is done with the dc.
  1565  func (c *Conn) closemuRUnlockCondReleaseConn(err error) {
  1566  	c.closemu.RUnlock()
  1567  	if err == driver.ErrBadConn {
  1568  		c.close(err)
  1569  	}
  1570  }
  1571  
  1572  func (c *Conn) close(err error) error {
  1573  	if !atomic.CompareAndSwapInt32(&c.done, 0, 1) {
  1574  		return ErrConnDone
  1575  	}
  1576  
  1577  	// Lock around releasing the driver connection
  1578  	// to ensure all queries have been stopped before doing so.
  1579  	c.closemu.Lock()
  1580  	defer c.closemu.Unlock()
  1581  
  1582  	c.dc.releaseConn(err)
  1583  	c.dc = nil
  1584  	c.db = nil
  1585  	return err
  1586  }
  1587  
  1588  // Close returns the connection to the connection pool.
  1589  // All operations after a Close will return with ErrConnDone.
  1590  // Close is safe to call concurrently with other operations and will
  1591  // block until all other operations finish. It may be useful to first
  1592  // cancel any used context and then call close directly after.
  1593  func (c *Conn) Close() error {
  1594  	return c.close(nil)
  1595  }
  1596  
  1597  // Tx is an in-progress database transaction.
  1598  //
  1599  // A transaction must end with a call to Commit or Rollback.
  1600  //
  1601  // After a call to Commit or Rollback, all operations on the
  1602  // transaction fail with ErrTxDone.
  1603  //
  1604  // The statements prepared for a transaction by calling
  1605  // the transaction's Prepare or Stmt methods are closed
  1606  // by the call to Commit or Rollback.
  1607  type Tx struct {
  1608  	db *DB
  1609  
  1610  	// closemu prevents the transaction from closing while there
  1611  	// is an active query. It is held for read during queries
  1612  	// and exclusively during close.
  1613  	closemu sync.RWMutex
  1614  
  1615  	// dc is owned exclusively until Commit or Rollback, at which point
  1616  	// it's returned with putConn.
  1617  	dc  *driverConn
  1618  	txi driver.Tx
  1619  
  1620  	// releaseConn is called once the Tx is closed to release
  1621  	// any held driverConn back to the pool.
  1622  	releaseConn func(error)
  1623  
  1624  	// done transitions from 0 to 1 exactly once, on Commit
  1625  	// or Rollback. once done, all operations fail with
  1626  	// ErrTxDone.
  1627  	// Use atomic operations on value when checking value.
  1628  	done int32
  1629  
  1630  	// All Stmts prepared for this transaction. These will be closed after the
  1631  	// transaction has been committed or rolled back.
  1632  	stmts struct {
  1633  		sync.Mutex
  1634  		v []*Stmt
  1635  	}
  1636  
  1637  	// cancel is called after done transitions from 0 to 1.
  1638  	cancel func()
  1639  
  1640  	// ctx lives for the life of the transaction.
  1641  	ctx context.Context
  1642  }
  1643  
  1644  // awaitDone blocks until the context in Tx is canceled and rolls back
  1645  // the transaction if it's not already done.
  1646  func (tx *Tx) awaitDone() {
  1647  	// Wait for either the transaction to be committed or rolled
  1648  	// back, or for the associated context to be closed.
  1649  	<-tx.ctx.Done()
  1650  
  1651  	// Discard and close the connection used to ensure the
  1652  	// transaction is closed and the resources are released.  This
  1653  	// rollback does nothing if the transaction has already been
  1654  	// committed or rolled back.
  1655  	tx.rollback(true)
  1656  }
  1657  
  1658  func (tx *Tx) isDone() bool {
  1659  	return atomic.LoadInt32(&tx.done) != 0
  1660  }
  1661  
  1662  // ErrTxDone is returned by any operation that is performed on a transaction
  1663  // that has already been committed or rolled back.
  1664  var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
  1665  
  1666  // close returns the connection to the pool and
  1667  // must only be called by Tx.rollback or Tx.Commit.
  1668  func (tx *Tx) close(err error) {
  1669  	tx.closemu.Lock()
  1670  	defer tx.closemu.Unlock()
  1671  
  1672  	tx.releaseConn(err)
  1673  	tx.cancel()
  1674  	tx.dc = nil
  1675  	tx.txi = nil
  1676  }
  1677  
  1678  // hookTxGrabConn specifies an optional hook to be called on
  1679  // a successful call to (*Tx).grabConn. For tests.
  1680  var hookTxGrabConn func()
  1681  
  1682  func (tx *Tx) grabConn(ctx context.Context) (*driverConn, error) {
  1683  	select {
  1684  	default:
  1685  	case <-ctx.Done():
  1686  		return nil, ctx.Err()
  1687  	}
  1688  	if tx.isDone() {
  1689  		return nil, ErrTxDone
  1690  	}
  1691  	if hookTxGrabConn != nil { // test hook
  1692  		hookTxGrabConn()
  1693  	}
  1694  	return tx.dc, nil
  1695  }
  1696  
  1697  // closemuRUnlockRelease is used as a func(error) method value in
  1698  // ExecContext and QueryContext. Unlocking in the releaseConn keeps
  1699  // the driver conn from being returned to the connection pool until
  1700  // the Rows has been closed.
  1701  func (tx *Tx) closemuRUnlockRelease(error) {
  1702  	tx.closemu.RUnlock()
  1703  }
  1704  
  1705  // Closes all Stmts prepared for this transaction.
  1706  func (tx *Tx) closePrepared() {
  1707  	tx.stmts.Lock()
  1708  	defer tx.stmts.Unlock()
  1709  	for _, stmt := range tx.stmts.v {
  1710  		stmt.Close()
  1711  	}
  1712  }
  1713  
  1714  // Commit commits the transaction.
  1715  func (tx *Tx) Commit() error {
  1716  	if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  1717  		return ErrTxDone
  1718  	}
  1719  	select {
  1720  	default:
  1721  	case <-tx.ctx.Done():
  1722  		return tx.ctx.Err()
  1723  	}
  1724  	var err error
  1725  	withLock(tx.dc, func() {
  1726  		err = tx.txi.Commit()
  1727  	})
  1728  	if err != driver.ErrBadConn {
  1729  		tx.closePrepared()
  1730  	}
  1731  	tx.close(err)
  1732  	return err
  1733  }
  1734  
  1735  // rollback aborts the transaction and optionally forces the pool to discard
  1736  // the connection.
  1737  func (tx *Tx) rollback(discardConn bool) error {
  1738  	if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  1739  		return ErrTxDone
  1740  	}
  1741  	var err error
  1742  	withLock(tx.dc, func() {
  1743  		err = tx.txi.Rollback()
  1744  	})
  1745  	if err != driver.ErrBadConn {
  1746  		tx.closePrepared()
  1747  	}
  1748  	if discardConn {
  1749  		err = driver.ErrBadConn
  1750  	}
  1751  	tx.close(err)
  1752  	return err
  1753  }
  1754  
  1755  // Rollback aborts the transaction.
  1756  func (tx *Tx) Rollback() error {
  1757  	return tx.rollback(false)
  1758  }
  1759  
  1760  // Prepare creates a prepared statement for use within a transaction.
  1761  //
  1762  // The returned statement operates within the transaction and will be closed
  1763  // when the transaction has been committed or rolled back.
  1764  //
  1765  // To use an existing prepared statement on this transaction, see Tx.Stmt.
  1766  //
  1767  // The provided context will be used for the preparation of the context, not
  1768  // for the execution of the returned statement. The returned statement
  1769  // will run in the transaction context.
  1770  func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1771  	tx.closemu.RLock()
  1772  	defer tx.closemu.RUnlock()
  1773  
  1774  	dc, err := tx.grabConn(ctx)
  1775  	if err != nil {
  1776  		return nil, err
  1777  	}
  1778  
  1779  	var si driver.Stmt
  1780  	withLock(dc, func() {
  1781  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1782  	})
  1783  	if err != nil {
  1784  		return nil, err
  1785  	}
  1786  
  1787  	stmt := &Stmt{
  1788  		db: tx.db,
  1789  		tx: tx,
  1790  		txds: &driverStmt{
  1791  			Locker: dc,
  1792  			si:     si,
  1793  		},
  1794  		query: query,
  1795  	}
  1796  	tx.stmts.Lock()
  1797  	tx.stmts.v = append(tx.stmts.v, stmt)
  1798  	tx.stmts.Unlock()
  1799  	return stmt, nil
  1800  }
  1801  
  1802  // Prepare creates a prepared statement for use within a transaction.
  1803  //
  1804  // The returned statement operates within the transaction and can no longer
  1805  // be used once the transaction has been committed or rolled back.
  1806  //
  1807  // To use an existing prepared statement on this transaction, see Tx.Stmt.
  1808  func (tx *Tx) Prepare(query string) (*Stmt, error) {
  1809  	return tx.PrepareContext(context.Background(), query)
  1810  }
  1811  
  1812  // StmtContext returns a transaction-specific prepared statement from
  1813  // an existing statement.
  1814  //
  1815  // Example:
  1816  //  updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  1817  //  ...
  1818  //  tx, err := db.Begin()
  1819  //  ...
  1820  //  res, err := tx.StmtContext(ctx, updateMoney).Exec(123.45, 98293203)
  1821  //
  1822  // The returned statement operates within the transaction and will be closed
  1823  // when the transaction has been committed or rolled back.
  1824  func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
  1825  	tx.closemu.RLock()
  1826  	defer tx.closemu.RUnlock()
  1827  
  1828  	if tx.db != stmt.db {
  1829  		return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
  1830  	}
  1831  	dc, err := tx.grabConn(ctx)
  1832  	if err != nil {
  1833  		return &Stmt{stickyErr: err}
  1834  	}
  1835  	var si driver.Stmt
  1836  	var parentStmt *Stmt
  1837  	stmt.mu.Lock()
  1838  	if stmt.closed || stmt.tx != nil {
  1839  		// If the statement has been closed or already belongs to a
  1840  		// transaction, we can't reuse it in this connection.
  1841  		// Since tx.StmtContext should never need to be called with a
  1842  		// Stmt already belonging to tx, we ignore this edge case and
  1843  		// re-prepare the statement in this case. No need to add
  1844  		// code-complexity for this.
  1845  		stmt.mu.Unlock()
  1846  		withLock(dc, func() {
  1847  			si, err = ctxDriverPrepare(ctx, dc.ci, stmt.query)
  1848  		})
  1849  		if err != nil {
  1850  			return &Stmt{stickyErr: err}
  1851  		}
  1852  	} else {
  1853  		stmt.removeClosedStmtLocked()
  1854  		// See if the statement has already been prepared on this connection,
  1855  		// and reuse it if possible.
  1856  		for _, v := range stmt.css {
  1857  			if v.dc == dc {
  1858  				si = v.ds.si
  1859  				break
  1860  			}
  1861  		}
  1862  
  1863  		stmt.mu.Unlock()
  1864  
  1865  		if si == nil {
  1866  			cs, err := stmt.prepareOnConnLocked(ctx, dc)
  1867  			if err != nil {
  1868  				return &Stmt{stickyErr: err}
  1869  			}
  1870  			si = cs.si
  1871  		}
  1872  		parentStmt = stmt
  1873  	}
  1874  
  1875  	txs := &Stmt{
  1876  		db: tx.db,
  1877  		tx: tx,
  1878  		txds: &driverStmt{
  1879  			Locker: dc,
  1880  			si:     si,
  1881  		},
  1882  		parentStmt: parentStmt,
  1883  		query:      stmt.query,
  1884  	}
  1885  	if parentStmt != nil {
  1886  		tx.db.addDep(parentStmt, txs)
  1887  	}
  1888  	tx.stmts.Lock()
  1889  	tx.stmts.v = append(tx.stmts.v, txs)
  1890  	tx.stmts.Unlock()
  1891  	return txs
  1892  }
  1893  
  1894  // Stmt returns a transaction-specific prepared statement from
  1895  // an existing statement.
  1896  //
  1897  // Example:
  1898  //  updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  1899  //  ...
  1900  //  tx, err := db.Begin()
  1901  //  ...
  1902  //  res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
  1903  //
  1904  // The returned statement operates within the transaction and will be closed
  1905  // when the transaction has been committed or rolled back.
  1906  func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  1907  	return tx.StmtContext(context.Background(), stmt)
  1908  }
  1909  
  1910  // ExecContext executes a query that doesn't return rows.
  1911  // For example: an INSERT and UPDATE.
  1912  func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
  1913  	tx.closemu.RLock()
  1914  
  1915  	dc, err := tx.grabConn(ctx)
  1916  	if err != nil {
  1917  		tx.closemu.RUnlock()
  1918  		return nil, err
  1919  	}
  1920  	return tx.db.execDC(ctx, dc, tx.closemuRUnlockRelease, query, args)
  1921  }
  1922  
  1923  // Exec executes a query that doesn't return rows.
  1924  // For example: an INSERT and UPDATE.
  1925  func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
  1926  	return tx.ExecContext(context.Background(), query, args...)
  1927  }
  1928  
  1929  // QueryContext executes a query that returns rows, typically a SELECT.
  1930  func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  1931  	tx.closemu.RLock()
  1932  
  1933  	dc, err := tx.grabConn(ctx)
  1934  	if err != nil {
  1935  		tx.closemu.RUnlock()
  1936  		return nil, err
  1937  	}
  1938  
  1939  	return tx.db.queryDC(ctx, dc, tx.closemuRUnlockRelease, query, args)
  1940  }
  1941  
  1942  // Query executes a query that returns rows, typically a SELECT.
  1943  func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
  1944  	return tx.QueryContext(context.Background(), query, args...)
  1945  }
  1946  
  1947  // QueryRowContext executes a query that is expected to return at most one row.
  1948  // QueryRowContext always returns a non-nil value. Errors are deferred until
  1949  // Row's Scan method is called.
  1950  func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  1951  	rows, err := tx.QueryContext(ctx, query, args...)
  1952  	return &Row{rows: rows, err: err}
  1953  }
  1954  
  1955  // QueryRow executes a query that is expected to return at most one row.
  1956  // QueryRow always returns a non-nil value. Errors are deferred until
  1957  // Row's Scan method is called.
  1958  func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
  1959  	return tx.QueryRowContext(context.Background(), query, args...)
  1960  }
  1961  
  1962  // connStmt is a prepared statement on a particular connection.
  1963  type connStmt struct {
  1964  	dc *driverConn
  1965  	ds *driverStmt
  1966  }
  1967  
  1968  // Stmt is a prepared statement.
  1969  // A Stmt is safe for concurrent use by multiple goroutines.
  1970  type Stmt struct {
  1971  	// Immutable:
  1972  	db        *DB    // where we came from
  1973  	query     string // that created the Stmt
  1974  	stickyErr error  // if non-nil, this error is returned for all operations
  1975  
  1976  	closemu sync.RWMutex // held exclusively during close, for read otherwise.
  1977  
  1978  	// If in a transaction, else both nil:
  1979  	tx   *Tx
  1980  	txds *driverStmt
  1981  
  1982  	// parentStmt is set when a transaction-specific statement
  1983  	// is requested from an identical statement prepared on the same
  1984  	// conn. parentStmt is used to track the dependency of this statement
  1985  	// on its originating ("parent") statement so that parentStmt may
  1986  	// be closed by the user without them having to know whether or not
  1987  	// any transactions are still using it.
  1988  	parentStmt *Stmt
  1989  
  1990  	mu     sync.Mutex // protects the rest of the fields
  1991  	closed bool
  1992  
  1993  	// css is a list of underlying driver statement interfaces
  1994  	// that are valid on particular connections. This is only
  1995  	// used if tx == nil and one is found that has idle
  1996  	// connections. If tx != nil, txds is always used.
  1997  	css []connStmt
  1998  
  1999  	// lastNumClosed is copied from db.numClosed when Stmt is created
  2000  	// without tx and closed connections in css are removed.
  2001  	lastNumClosed uint64
  2002  }
  2003  
  2004  // ExecContext executes a prepared statement with the given arguments and
  2005  // returns a Result summarizing the effect of the statement.
  2006  func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error) {
  2007  	s.closemu.RLock()
  2008  	defer s.closemu.RUnlock()
  2009  
  2010  	var res Result
  2011  	for i := 0; i < maxBadConnRetries; i++ {
  2012  		_, releaseConn, ds, err := s.connStmt(ctx)
  2013  		if err != nil {
  2014  			if err == driver.ErrBadConn {
  2015  				continue
  2016  			}
  2017  			return nil, err
  2018  		}
  2019  
  2020  		res, err = resultFromStatement(ctx, ds, args...)
  2021  		releaseConn(err)
  2022  		if err != driver.ErrBadConn {
  2023  			return res, err
  2024  		}
  2025  	}
  2026  	return nil, driver.ErrBadConn
  2027  }
  2028  
  2029  // Exec executes a prepared statement with the given arguments and
  2030  // returns a Result summarizing the effect of the statement.
  2031  func (s *Stmt) Exec(args ...interface{}) (Result, error) {
  2032  	return s.ExecContext(context.Background(), args...)
  2033  }
  2034  
  2035  func driverNumInput(ds *driverStmt) int {
  2036  	ds.Lock()
  2037  	defer ds.Unlock() // in case NumInput panics
  2038  	return ds.si.NumInput()
  2039  }
  2040  
  2041  func resultFromStatement(ctx context.Context, ds *driverStmt, args ...interface{}) (Result, error) {
  2042  	want := driverNumInput(ds)
  2043  
  2044  	// -1 means the driver doesn't know how to count the number of
  2045  	// placeholders, so we won't sanity check input here and instead let the
  2046  	// driver deal with errors.
  2047  	if want != -1 && len(args) != want {
  2048  		return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
  2049  	}
  2050  
  2051  	dargs, err := driverArgs(ds, args)
  2052  	if err != nil {
  2053  		return nil, err
  2054  	}
  2055  
  2056  	ds.Lock()
  2057  	defer ds.Unlock()
  2058  
  2059  	resi, err := ctxDriverStmtExec(ctx, ds.si, dargs)
  2060  	if err != nil {
  2061  		return nil, err
  2062  	}
  2063  	return driverResult{ds.Locker, resi}, nil
  2064  }
  2065  
  2066  // removeClosedStmtLocked removes closed conns in s.css.
  2067  //
  2068  // To avoid lock contention on DB.mu, we do it only when
  2069  // s.db.numClosed - s.lastNum is large enough.
  2070  func (s *Stmt) removeClosedStmtLocked() {
  2071  	t := len(s.css)/2 + 1
  2072  	if t > 10 {
  2073  		t = 10
  2074  	}
  2075  	dbClosed := atomic.LoadUint64(&s.db.numClosed)
  2076  	if dbClosed-s.lastNumClosed < uint64(t) {
  2077  		return
  2078  	}
  2079  
  2080  	s.db.mu.Lock()
  2081  	for i := 0; i < len(s.css); i++ {
  2082  		if s.css[i].dc.dbmuClosed {
  2083  			s.css[i] = s.css[len(s.css)-1]
  2084  			s.css = s.css[:len(s.css)-1]
  2085  			i--
  2086  		}
  2087  	}
  2088  	s.db.mu.Unlock()
  2089  	s.lastNumClosed = dbClosed
  2090  }
  2091  
  2092  // connStmt returns a free driver connection on which to execute the
  2093  // statement, a function to call to release the connection, and a
  2094  // statement bound to that connection.
  2095  func (s *Stmt) connStmt(ctx context.Context) (ci *driverConn, releaseConn func(error), ds *driverStmt, err error) {
  2096  	if err = s.stickyErr; err != nil {
  2097  		return
  2098  	}
  2099  	s.mu.Lock()
  2100  	if s.closed {
  2101  		s.mu.Unlock()
  2102  		err = errors.New("sql: statement is closed")
  2103  		return
  2104  	}
  2105  
  2106  	// In a transaction, we always use the connection that the
  2107  	// transaction was created on.
  2108  	if s.tx != nil {
  2109  		s.mu.Unlock()
  2110  		ci, err = s.tx.grabConn(ctx) // blocks, waiting for the connection.
  2111  		if err != nil {
  2112  			return
  2113  		}
  2114  		releaseConn = func(error) {}
  2115  		return ci, releaseConn, s.txds, nil
  2116  	}
  2117  
  2118  	s.removeClosedStmtLocked()
  2119  	s.mu.Unlock()
  2120  
  2121  	dc, err := s.db.conn(ctx, cachedOrNewConn)
  2122  	if err != nil {
  2123  		return nil, nil, nil, err
  2124  	}
  2125  
  2126  	s.mu.Lock()
  2127  	for _, v := range s.css {
  2128  		if v.dc == dc {
  2129  			s.mu.Unlock()
  2130  			return dc, dc.releaseConn, v.ds, nil
  2131  		}
  2132  	}
  2133  	s.mu.Unlock()
  2134  
  2135  	// No luck; we need to prepare the statement on this connection
  2136  	withLock(dc, func() {
  2137  		ds, err = s.prepareOnConnLocked(ctx, dc)
  2138  	})
  2139  	if err != nil {
  2140  		dc.releaseConn(err)
  2141  		return nil, nil, nil, err
  2142  	}
  2143  
  2144  	return dc, dc.releaseConn, ds, nil
  2145  }
  2146  
  2147  // prepareOnConnLocked prepares the query in Stmt s on dc and adds it to the list of
  2148  // open connStmt on the statement. It assumes the caller is holding the lock on dc.
  2149  func (s *Stmt) prepareOnConnLocked(ctx context.Context, dc *driverConn) (*driverStmt, error) {
  2150  	si, err := dc.prepareLocked(ctx, s.query)
  2151  	if err != nil {
  2152  		return nil, err
  2153  	}
  2154  	cs := connStmt{dc, si}
  2155  	s.mu.Lock()
  2156  	s.css = append(s.css, cs)
  2157  	s.mu.Unlock()
  2158  	return cs.ds, nil
  2159  }
  2160  
  2161  // QueryContext executes a prepared query statement with the given arguments
  2162  // and returns the query results as a *Rows.
  2163  func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) {
  2164  	s.closemu.RLock()
  2165  	defer s.closemu.RUnlock()
  2166  
  2167  	var rowsi driver.Rows
  2168  	for i := 0; i < maxBadConnRetries; i++ {
  2169  		dc, releaseConn, ds, err := s.connStmt(ctx)
  2170  		if err != nil {
  2171  			if err == driver.ErrBadConn {
  2172  				continue
  2173  			}
  2174  			return nil, err
  2175  		}
  2176  
  2177  		rowsi, err = rowsiFromStatement(ctx, ds, args...)
  2178  		if err == nil {
  2179  			// Note: ownership of ci passes to the *Rows, to be freed
  2180  			// with releaseConn.
  2181  			rows := &Rows{
  2182  				dc:    dc,
  2183  				rowsi: rowsi,
  2184  				// releaseConn set below
  2185  			}
  2186  			rows.initContextClose(ctx)
  2187  			s.db.addDep(s, rows)
  2188  			rows.releaseConn = func(err error) {
  2189  				releaseConn(err)
  2190  				s.db.removeDep(s, rows)
  2191  			}
  2192  			return rows, nil
  2193  		}
  2194  
  2195  		releaseConn(err)
  2196  		if err != driver.ErrBadConn {
  2197  			return nil, err
  2198  		}
  2199  	}
  2200  	return nil, driver.ErrBadConn
  2201  }
  2202  
  2203  // Query executes a prepared query statement with the given arguments
  2204  // and returns the query results as a *Rows.
  2205  func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
  2206  	return s.QueryContext(context.Background(), args...)
  2207  }
  2208  
  2209  func rowsiFromStatement(ctx context.Context, ds *driverStmt, args ...interface{}) (driver.Rows, error) {
  2210  	var want int
  2211  	withLock(ds, func() {
  2212  		want = ds.si.NumInput()
  2213  	})
  2214  
  2215  	// -1 means the driver doesn't know how to count the number of
  2216  	// placeholders, so we won't sanity check input here and instead let the
  2217  	// driver deal with errors.
  2218  	if want != -1 && len(args) != want {
  2219  		return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
  2220  	}
  2221  
  2222  	dargs, err := driverArgs(ds, args)
  2223  	if err != nil {
  2224  		return nil, err
  2225  	}
  2226  
  2227  	ds.Lock()
  2228  	defer ds.Unlock()
  2229  
  2230  	rowsi, err := ctxDriverStmtQuery(ctx, ds.si, dargs)
  2231  	if err != nil {
  2232  		return nil, err
  2233  	}
  2234  	return rowsi, nil
  2235  }
  2236  
  2237  // QueryRowContext executes a prepared query statement with the given arguments.
  2238  // If an error occurs during the execution of the statement, that error will
  2239  // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2240  // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2241  // Otherwise, the *Row's Scan scans the first selected row and discards
  2242  // the rest.
  2243  //
  2244  // Example usage:
  2245  //
  2246  //  var name string
  2247  //  err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name)
  2248  func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row {
  2249  	rows, err := s.QueryContext(ctx, args...)
  2250  	if err != nil {
  2251  		return &Row{err: err}
  2252  	}
  2253  	return &Row{rows: rows}
  2254  }
  2255  
  2256  // QueryRow executes a prepared query statement with the given arguments.
  2257  // If an error occurs during the execution of the statement, that error will
  2258  // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2259  // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2260  // Otherwise, the *Row's Scan scans the first selected row and discards
  2261  // the rest.
  2262  //
  2263  // Example usage:
  2264  //
  2265  //  var name string
  2266  //  err := nameByUseridStmt.QueryRow(id).Scan(&name)
  2267  func (s *Stmt) QueryRow(args ...interface{}) *Row {
  2268  	return s.QueryRowContext(context.Background(), args...)
  2269  }
  2270  
  2271  // Close closes the statement.
  2272  func (s *Stmt) Close() error {
  2273  	s.closemu.Lock()
  2274  	defer s.closemu.Unlock()
  2275  
  2276  	if s.stickyErr != nil {
  2277  		return s.stickyErr
  2278  	}
  2279  	s.mu.Lock()
  2280  	if s.closed {
  2281  		s.mu.Unlock()
  2282  		return nil
  2283  	}
  2284  	s.closed = true
  2285  	s.mu.Unlock()
  2286  
  2287  	if s.tx == nil {
  2288  		return s.db.removeDep(s, s)
  2289  	}
  2290  
  2291  	if s.parentStmt != nil {
  2292  		// If parentStmt is set, we must not close s.txds since it's stored
  2293  		// in the css array of the parentStmt.
  2294  		return s.db.removeDep(s.parentStmt, s)
  2295  	}
  2296  	return s.txds.Close()
  2297  }
  2298  
  2299  func (s *Stmt) finalClose() error {
  2300  	s.mu.Lock()
  2301  	defer s.mu.Unlock()
  2302  	if s.css != nil {
  2303  		for _, v := range s.css {
  2304  			s.db.noteUnusedDriverStatement(v.dc, v.ds)
  2305  			v.dc.removeOpenStmt(v.ds)
  2306  		}
  2307  		s.css = nil
  2308  	}
  2309  	return nil
  2310  }
  2311  
  2312  // Rows is the result of a query. Its cursor starts before the first row
  2313  // of the result set. Use Next to advance through the rows:
  2314  //
  2315  //     rows, err := db.Query("SELECT ...")
  2316  //     ...
  2317  //     defer rows.Close()
  2318  //     for rows.Next() {
  2319  //         var id int
  2320  //         var name string
  2321  //         err = rows.Scan(&id, &name)
  2322  //         ...
  2323  //     }
  2324  //     err = rows.Err() // get any error encountered during iteration
  2325  //     ...
  2326  type Rows struct {
  2327  	dc          *driverConn // owned; must call releaseConn when closed to release
  2328  	releaseConn func(error)
  2329  	rowsi       driver.Rows
  2330  	cancel      func()      // called when Rows is closed, may be nil.
  2331  	closeStmt   *driverStmt // if non-nil, statement to Close on close
  2332  
  2333  	// closemu prevents Rows from closing while there
  2334  	// is an active streaming result. It is held for read during non-close operations
  2335  	// and exclusively during close.
  2336  	//
  2337  	// closemu guards lasterr and closed.
  2338  	closemu sync.RWMutex
  2339  	closed  bool
  2340  	lasterr error // non-nil only if closed is true
  2341  
  2342  	// lastcols is only used in Scan, Next, and NextResultSet which are expected
  2343  	// not to be called concurrently.
  2344  	lastcols []driver.Value
  2345  }
  2346  
  2347  func (rs *Rows) initContextClose(ctx context.Context) {
  2348  	ctx, rs.cancel = context.WithCancel(ctx)
  2349  	go rs.awaitDone(ctx)
  2350  }
  2351  
  2352  // awaitDone blocks until the rows are closed or the context canceled.
  2353  func (rs *Rows) awaitDone(ctx context.Context) {
  2354  	<-ctx.Done()
  2355  	rs.close(ctx.Err())
  2356  }
  2357  
  2358  // Next prepares the next result row for reading with the Scan method. It
  2359  // returns true on success, or false if there is no next result row or an error
  2360  // happened while preparing it. Err should be consulted to distinguish between
  2361  // the two cases.
  2362  //
  2363  // Every call to Scan, even the first one, must be preceded by a call to Next.
  2364  func (rs *Rows) Next() bool {
  2365  	var doClose, ok bool
  2366  	withLock(rs.closemu.RLocker(), func() {
  2367  		doClose, ok = rs.nextLocked()
  2368  	})
  2369  	if doClose {
  2370  		rs.Close()
  2371  	}
  2372  	return ok
  2373  }
  2374  
  2375  func (rs *Rows) nextLocked() (doClose, ok bool) {
  2376  	if rs.closed {
  2377  		return false, false
  2378  	}
  2379  	if rs.lastcols == nil {
  2380  		rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
  2381  	}
  2382  	rs.lasterr = rs.rowsi.Next(rs.lastcols)
  2383  	if rs.lasterr != nil {
  2384  		// Close the connection if there is a driver error.
  2385  		if rs.lasterr != io.EOF {
  2386  			return true, false
  2387  		}
  2388  		nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2389  		if !ok {
  2390  			return true, false
  2391  		}
  2392  		// The driver is at the end of the current result set.
  2393  		// Test to see if there is another result set after the current one.
  2394  		// Only close Rows if there is no further result sets to read.
  2395  		if !nextResultSet.HasNextResultSet() {
  2396  			doClose = true
  2397  		}
  2398  		return doClose, false
  2399  	}
  2400  	return false, true
  2401  }
  2402  
  2403  // NextResultSet prepares the next result set for reading. It returns true if
  2404  // there is further result sets, or false if there is no further result set
  2405  // or if there is an error advancing to it. The Err method should be consulted
  2406  // to distinguish between the two cases.
  2407  //
  2408  // After calling NextResultSet, the Next method should always be called before
  2409  // scanning. If there are further result sets they may not have rows in the result
  2410  // set.
  2411  func (rs *Rows) NextResultSet() bool {
  2412  	var doClose bool
  2413  	defer func() {
  2414  		if doClose {
  2415  			rs.Close()
  2416  		}
  2417  	}()
  2418  	rs.closemu.RLock()
  2419  	defer rs.closemu.RUnlock()
  2420  
  2421  	if rs.closed {
  2422  		return false
  2423  	}
  2424  
  2425  	rs.lastcols = nil
  2426  	nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2427  	if !ok {
  2428  		doClose = true
  2429  		return false
  2430  	}
  2431  	rs.lasterr = nextResultSet.NextResultSet()
  2432  	if rs.lasterr != nil {
  2433  		doClose = true
  2434  		return false
  2435  	}
  2436  	return true
  2437  }
  2438  
  2439  // Err returns the error, if any, that was encountered during iteration.
  2440  // Err may be called after an explicit or implicit Close.
  2441  func (rs *Rows) Err() error {
  2442  	rs.closemu.RLock()
  2443  	defer rs.closemu.RUnlock()
  2444  	if rs.lasterr == io.EOF {
  2445  		return nil
  2446  	}
  2447  	return rs.lasterr
  2448  }
  2449  
  2450  // Columns returns the column names.
  2451  // Columns returns an error if the rows are closed, or if the rows
  2452  // are from QueryRow and there was a deferred error.
  2453  func (rs *Rows) Columns() ([]string, error) {
  2454  	rs.closemu.RLock()
  2455  	defer rs.closemu.RUnlock()
  2456  	if rs.closed {
  2457  		return nil, errors.New("sql: Rows are closed")
  2458  	}
  2459  	if rs.rowsi == nil {
  2460  		return nil, errors.New("sql: no Rows available")
  2461  	}
  2462  	return rs.rowsi.Columns(), nil
  2463  }
  2464  
  2465  // ColumnTypes returns column information such as column type, length,
  2466  // and nullable. Some information may not be available from some drivers.
  2467  func (rs *Rows) ColumnTypes() ([]*ColumnType, error) {
  2468  	rs.closemu.RLock()
  2469  	defer rs.closemu.RUnlock()
  2470  	if rs.closed {
  2471  		return nil, errors.New("sql: Rows are closed")
  2472  	}
  2473  	if rs.rowsi == nil {
  2474  		return nil, errors.New("sql: no Rows available")
  2475  	}
  2476  	return rowsColumnInfoSetup(rs.rowsi), nil
  2477  }
  2478  
  2479  // ColumnType contains the name and type of a column.
  2480  type ColumnType struct {
  2481  	name string
  2482  
  2483  	hasNullable       bool
  2484  	hasLength         bool
  2485  	hasPrecisionScale bool
  2486  
  2487  	nullable     bool
  2488  	length       int64
  2489  	databaseType string
  2490  	precision    int64
  2491  	scale        int64
  2492  	scanType     reflect.Type
  2493  }
  2494  
  2495  // Name returns the name or alias of the column.
  2496  func (ci *ColumnType) Name() string {
  2497  	return ci.name
  2498  }
  2499  
  2500  // Length returns the column type length for variable length column types such
  2501  // as text and binary field types. If the type length is unbounded the value will
  2502  // be math.MaxInt64 (any database limits will still apply).
  2503  // If the column type is not variable length, such as an int, or if not supported
  2504  // by the driver ok is false.
  2505  func (ci *ColumnType) Length() (length int64, ok bool) {
  2506  	return ci.length, ci.hasLength
  2507  }
  2508  
  2509  // DecimalSize returns the scale and precision of a decimal type.
  2510  // If not applicable or if not supported ok is false.
  2511  func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool) {
  2512  	return ci.precision, ci.scale, ci.hasPrecisionScale
  2513  }
  2514  
  2515  // ScanType returns a Go type suitable for scanning into using Rows.Scan.
  2516  // If a driver does not support this property ScanType will return
  2517  // the type of an empty interface.
  2518  func (ci *ColumnType) ScanType() reflect.Type {
  2519  	return ci.scanType
  2520  }
  2521  
  2522  // Nullable returns whether the column may be null.
  2523  // If a driver does not support this property ok will be false.
  2524  func (ci *ColumnType) Nullable() (nullable, ok bool) {
  2525  	return ci.nullable, ci.hasNullable
  2526  }
  2527  
  2528  // DatabaseTypeName returns the database system name of the column type. If an empty
  2529  // string is returned the driver type name is not supported.
  2530  // Consult your driver documentation for a list of driver data types. Length specifiers
  2531  // are not included.
  2532  // Common type include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", "BIGINT".
  2533  func (ci *ColumnType) DatabaseTypeName() string {
  2534  	return ci.databaseType
  2535  }
  2536  
  2537  func rowsColumnInfoSetup(rowsi driver.Rows) []*ColumnType {
  2538  	names := rowsi.Columns()
  2539  
  2540  	list := make([]*ColumnType, len(names))
  2541  	for i := range list {
  2542  		ci := &ColumnType{
  2543  			name: names[i],
  2544  		}
  2545  		list[i] = ci
  2546  
  2547  		if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok {
  2548  			ci.scanType = prop.ColumnTypeScanType(i)
  2549  		} else {
  2550  			ci.scanType = reflect.TypeOf(new(interface{})).Elem()
  2551  		}
  2552  		if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok {
  2553  			ci.databaseType = prop.ColumnTypeDatabaseTypeName(i)
  2554  		}
  2555  		if prop, ok := rowsi.(driver.RowsColumnTypeLength); ok {
  2556  			ci.length, ci.hasLength = prop.ColumnTypeLength(i)
  2557  		}
  2558  		if prop, ok := rowsi.(driver.RowsColumnTypeNullable); ok {
  2559  			ci.nullable, ci.hasNullable = prop.ColumnTypeNullable(i)
  2560  		}
  2561  		if prop, ok := rowsi.(driver.RowsColumnTypePrecisionScale); ok {
  2562  			ci.precision, ci.scale, ci.hasPrecisionScale = prop.ColumnTypePrecisionScale(i)
  2563  		}
  2564  	}
  2565  	return list
  2566  }
  2567  
  2568  // Scan copies the columns in the current row into the values pointed
  2569  // at by dest. The number of values in dest must be the same as the
  2570  // number of columns in Rows.
  2571  //
  2572  // Scan converts columns read from the database into the following
  2573  // common Go types and special types provided by the sql package:
  2574  //
  2575  //    *string
  2576  //    *[]byte
  2577  //    *int, *int8, *int16, *int32, *int64
  2578  //    *uint, *uint8, *uint16, *uint32, *uint64
  2579  //    *bool
  2580  //    *float32, *float64
  2581  //    *interface{}
  2582  //    *RawBytes
  2583  //    any type implementing Scanner (see Scanner docs)
  2584  //
  2585  // In the most simple case, if the type of the value from the source
  2586  // column is an integer, bool or string type T and dest is of type *T,
  2587  // Scan simply assigns the value through the pointer.
  2588  //
  2589  // Scan also converts between string and numeric types, as long as no
  2590  // information would be lost. While Scan stringifies all numbers
  2591  // scanned from numeric database columns into *string, scans into
  2592  // numeric types are checked for overflow. For example, a float64 with
  2593  // value 300 or a string with value "300" can scan into a uint16, but
  2594  // not into a uint8, though float64(255) or "255" can scan into a
  2595  // uint8. One exception is that scans of some float64 numbers to
  2596  // strings may lose information when stringifying. In general, scan
  2597  // floating point columns into *float64.
  2598  //
  2599  // If a dest argument has type *[]byte, Scan saves in that argument a
  2600  // copy of the corresponding data. The copy is owned by the caller and
  2601  // can be modified and held indefinitely. The copy can be avoided by
  2602  // using an argument of type *RawBytes instead; see the documentation
  2603  // for RawBytes for restrictions on its use.
  2604  //
  2605  // If an argument has type *interface{}, Scan copies the value
  2606  // provided by the underlying driver without conversion. When scanning
  2607  // from a source value of type []byte to *interface{}, a copy of the
  2608  // slice is made and the caller owns the result.
  2609  //
  2610  // Source values of type time.Time may be scanned into values of type
  2611  // *time.Time, *interface{}, *string, or *[]byte. When converting to
  2612  // the latter two, time.Format3339Nano is used.
  2613  //
  2614  // Source values of type bool may be scanned into types *bool,
  2615  // *interface{}, *string, *[]byte, or *RawBytes.
  2616  //
  2617  // For scanning into *bool, the source may be true, false, 1, 0, or
  2618  // string inputs parseable by strconv.ParseBool.
  2619  func (rs *Rows) Scan(dest ...interface{}) error {
  2620  	rs.closemu.RLock()
  2621  	if rs.closed {
  2622  		rs.closemu.RUnlock()
  2623  		return errors.New("sql: Rows are closed")
  2624  	}
  2625  	rs.closemu.RUnlock()
  2626  
  2627  	if rs.lastcols == nil {
  2628  		return errors.New("sql: Scan called without calling Next")
  2629  	}
  2630  	if len(dest) != len(rs.lastcols) {
  2631  		return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
  2632  	}
  2633  	for i, sv := range rs.lastcols {
  2634  		err := convertAssign(dest[i], sv)
  2635  		if err != nil {
  2636  			return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
  2637  		}
  2638  	}
  2639  	return nil
  2640  }
  2641  
  2642  // rowsCloseHook returns a function so tests may install the
  2643  // hook through a test only mutex.
  2644  var rowsCloseHook = func() func(*Rows, *error) { return nil }
  2645  
  2646  // Close closes the Rows, preventing further enumeration. If Next is called
  2647  // and returns false and there are no further result sets,
  2648  // the Rows are closed automatically and it will suffice to check the
  2649  // result of Err. Close is idempotent and does not affect the result of Err.
  2650  func (rs *Rows) Close() error {
  2651  	return rs.close(nil)
  2652  }
  2653  
  2654  func (rs *Rows) close(err error) error {
  2655  	rs.closemu.Lock()
  2656  	defer rs.closemu.Unlock()
  2657  
  2658  	if rs.closed {
  2659  		return nil
  2660  	}
  2661  	rs.closed = true
  2662  
  2663  	if rs.lasterr == nil {
  2664  		rs.lasterr = err
  2665  	}
  2666  
  2667  	err = rs.rowsi.Close()
  2668  	if fn := rowsCloseHook(); fn != nil {
  2669  		fn(rs, &err)
  2670  	}
  2671  	if rs.cancel != nil {
  2672  		rs.cancel()
  2673  	}
  2674  
  2675  	if rs.closeStmt != nil {
  2676  		rs.closeStmt.Close()
  2677  	}
  2678  	rs.releaseConn(err)
  2679  	return err
  2680  }
  2681  
  2682  // Row is the result of calling QueryRow to select a single row.
  2683  type Row struct {
  2684  	// One of these two will be non-nil:
  2685  	err  error // deferred error for easy chaining
  2686  	rows *Rows
  2687  }
  2688  
  2689  // Scan copies the columns from the matched row into the values
  2690  // pointed at by dest. See the documentation on Rows.Scan for details.
  2691  // If more than one row matches the query,
  2692  // Scan uses the first row and discards the rest. If no row matches
  2693  // the query, Scan returns ErrNoRows.
  2694  func (r *Row) Scan(dest ...interface{}) error {
  2695  	if r.err != nil {
  2696  		return r.err
  2697  	}
  2698  
  2699  	// TODO(bradfitz): for now we need to defensively clone all
  2700  	// []byte that the driver returned (not permitting
  2701  	// *RawBytes in Rows.Scan), since we're about to close
  2702  	// the Rows in our defer, when we return from this function.
  2703  	// the contract with the driver.Next(...) interface is that it
  2704  	// can return slices into read-only temporary memory that's
  2705  	// only valid until the next Scan/Close. But the TODO is that
  2706  	// for a lot of drivers, this copy will be unnecessary. We
  2707  	// should provide an optional interface for drivers to
  2708  	// implement to say, "don't worry, the []bytes that I return
  2709  	// from Next will not be modified again." (for instance, if
  2710  	// they were obtained from the network anyway) But for now we
  2711  	// don't care.
  2712  	defer r.rows.Close()
  2713  	for _, dp := range dest {
  2714  		if _, ok := dp.(*RawBytes); ok {
  2715  			return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  2716  		}
  2717  	}
  2718  
  2719  	if !r.rows.Next() {
  2720  		if err := r.rows.Err(); err != nil {
  2721  			return err
  2722  		}
  2723  		return ErrNoRows
  2724  	}
  2725  	err := r.rows.Scan(dest...)
  2726  	if err != nil {
  2727  		return err
  2728  	}
  2729  	// Make sure the query can be processed to completion with no errors.
  2730  	if err := r.rows.Close(); err != nil {
  2731  		return err
  2732  	}
  2733  
  2734  	return nil
  2735  }
  2736  
  2737  // A Result summarizes an executed SQL command.
  2738  type Result interface {
  2739  	// LastInsertId returns the integer generated by the database
  2740  	// in response to a command. Typically this will be from an
  2741  	// "auto increment" column when inserting a new row. Not all
  2742  	// databases support this feature, and the syntax of such
  2743  	// statements varies.
  2744  	LastInsertId() (int64, error)
  2745  
  2746  	// RowsAffected returns the number of rows affected by an
  2747  	// update, insert, or delete. Not every database or database
  2748  	// driver may support this.
  2749  	RowsAffected() (int64, error)
  2750  }
  2751  
  2752  type driverResult struct {
  2753  	sync.Locker // the *driverConn
  2754  	resi        driver.Result
  2755  }
  2756  
  2757  func (dr driverResult) LastInsertId() (int64, error) {
  2758  	dr.Lock()
  2759  	defer dr.Unlock()
  2760  	return dr.resi.LastInsertId()
  2761  }
  2762  
  2763  func (dr driverResult) RowsAffected() (int64, error) {
  2764  	dr.Lock()
  2765  	defer dr.Unlock()
  2766  	return dr.resi.RowsAffected()
  2767  }
  2768  
  2769  func stack() string {
  2770  	var buf [2 << 10]byte
  2771  	return string(buf[:runtime.Stack(buf[:], false)])
  2772  }
  2773  
  2774  // withLock runs while holding lk.
  2775  func withLock(lk sync.Locker, fn func()) {
  2776  	lk.Lock()
  2777  	defer lk.Unlock() // in case fn panics
  2778  	fn()
  2779  }