github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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  // PingContext verifies a connection to the database is still alive,
   587  // establishing a connection if necessary.
   588  func (db *DB) PingContext(ctx context.Context) error {
   589  	var dc *driverConn
   590  	var err error
   591  
   592  	for i := 0; i < maxBadConnRetries; i++ {
   593  		dc, err = db.conn(ctx, cachedOrNewConn)
   594  		if err != driver.ErrBadConn {
   595  			break
   596  		}
   597  	}
   598  	if err == driver.ErrBadConn {
   599  		dc, err = db.conn(ctx, alwaysNewConn)
   600  	}
   601  	if err != nil {
   602  		return err
   603  	}
   604  
   605  	if pinger, ok := dc.ci.(driver.Pinger); ok {
   606  		err = pinger.Ping(ctx)
   607  	}
   608  	dc.releaseConn(err)
   609  	return err
   610  }
   611  
   612  // Ping verifies a connection to the database is still alive,
   613  // establishing a connection if necessary.
   614  func (db *DB) Ping() error {
   615  	return db.PingContext(context.Background())
   616  }
   617  
   618  // Close closes the database, releasing any open resources.
   619  //
   620  // It is rare to Close a DB, as the DB handle is meant to be
   621  // long-lived and shared between many goroutines.
   622  func (db *DB) Close() error {
   623  	db.mu.Lock()
   624  	if db.closed { // Make DB.Close idempotent
   625  		db.mu.Unlock()
   626  		return nil
   627  	}
   628  	close(db.openerCh)
   629  	if db.cleanerCh != nil {
   630  		close(db.cleanerCh)
   631  	}
   632  	var err error
   633  	fns := make([]func() error, 0, len(db.freeConn))
   634  	for _, dc := range db.freeConn {
   635  		fns = append(fns, dc.closeDBLocked())
   636  	}
   637  	db.freeConn = nil
   638  	db.closed = true
   639  	for _, req := range db.connRequests {
   640  		close(req)
   641  	}
   642  	db.mu.Unlock()
   643  	for _, fn := range fns {
   644  		err1 := fn()
   645  		if err1 != nil {
   646  			err = err1
   647  		}
   648  	}
   649  	return err
   650  }
   651  
   652  const defaultMaxIdleConns = 2
   653  
   654  func (db *DB) maxIdleConnsLocked() int {
   655  	n := db.maxIdle
   656  	switch {
   657  	case n == 0:
   658  		// TODO(bradfitz): ask driver, if supported, for its default preference
   659  		return defaultMaxIdleConns
   660  	case n < 0:
   661  		return 0
   662  	default:
   663  		return n
   664  	}
   665  }
   666  
   667  // SetMaxIdleConns sets the maximum number of connections in the idle
   668  // connection pool.
   669  //
   670  // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
   671  // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
   672  //
   673  // If n <= 0, no idle connections are retained.
   674  func (db *DB) SetMaxIdleConns(n int) {
   675  	db.mu.Lock()
   676  	if n > 0 {
   677  		db.maxIdle = n
   678  	} else {
   679  		// No idle connections.
   680  		db.maxIdle = -1
   681  	}
   682  	// Make sure maxIdle doesn't exceed maxOpen
   683  	if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
   684  		db.maxIdle = db.maxOpen
   685  	}
   686  	var closing []*driverConn
   687  	idleCount := len(db.freeConn)
   688  	maxIdle := db.maxIdleConnsLocked()
   689  	if idleCount > maxIdle {
   690  		closing = db.freeConn[maxIdle:]
   691  		db.freeConn = db.freeConn[:maxIdle]
   692  	}
   693  	db.mu.Unlock()
   694  	for _, c := range closing {
   695  		c.Close()
   696  	}
   697  }
   698  
   699  // SetMaxOpenConns sets the maximum number of open connections to the database.
   700  //
   701  // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
   702  // MaxIdleConns, then MaxIdleConns will be reduced to match the new
   703  // MaxOpenConns limit
   704  //
   705  // If n <= 0, then there is no limit on the number of open connections.
   706  // The default is 0 (unlimited).
   707  func (db *DB) SetMaxOpenConns(n int) {
   708  	db.mu.Lock()
   709  	db.maxOpen = n
   710  	if n < 0 {
   711  		db.maxOpen = 0
   712  	}
   713  	syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
   714  	db.mu.Unlock()
   715  	if syncMaxIdle {
   716  		db.SetMaxIdleConns(n)
   717  	}
   718  }
   719  
   720  // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
   721  //
   722  // Expired connections may be closed lazily before reuse.
   723  //
   724  // If d <= 0, connections are reused forever.
   725  func (db *DB) SetConnMaxLifetime(d time.Duration) {
   726  	if d < 0 {
   727  		d = 0
   728  	}
   729  	db.mu.Lock()
   730  	// wake cleaner up when lifetime is shortened.
   731  	if d > 0 && d < db.maxLifetime && db.cleanerCh != nil {
   732  		select {
   733  		case db.cleanerCh <- struct{}{}:
   734  		default:
   735  		}
   736  	}
   737  	db.maxLifetime = d
   738  	db.startCleanerLocked()
   739  	db.mu.Unlock()
   740  }
   741  
   742  // startCleanerLocked starts connectionCleaner if needed.
   743  func (db *DB) startCleanerLocked() {
   744  	if db.maxLifetime > 0 && db.numOpen > 0 && db.cleanerCh == nil {
   745  		db.cleanerCh = make(chan struct{}, 1)
   746  		go db.connectionCleaner(db.maxLifetime)
   747  	}
   748  }
   749  
   750  func (db *DB) connectionCleaner(d time.Duration) {
   751  	const minInterval = time.Second
   752  
   753  	if d < minInterval {
   754  		d = minInterval
   755  	}
   756  	t := time.NewTimer(d)
   757  
   758  	for {
   759  		select {
   760  		case <-t.C:
   761  		case <-db.cleanerCh: // maxLifetime was changed or db was closed.
   762  		}
   763  
   764  		db.mu.Lock()
   765  		d = db.maxLifetime
   766  		if db.closed || db.numOpen == 0 || d <= 0 {
   767  			db.cleanerCh = nil
   768  			db.mu.Unlock()
   769  			return
   770  		}
   771  
   772  		expiredSince := nowFunc().Add(-d)
   773  		var closing []*driverConn
   774  		for i := 0; i < len(db.freeConn); i++ {
   775  			c := db.freeConn[i]
   776  			if c.createdAt.Before(expiredSince) {
   777  				closing = append(closing, c)
   778  				last := len(db.freeConn) - 1
   779  				db.freeConn[i] = db.freeConn[last]
   780  				db.freeConn[last] = nil
   781  				db.freeConn = db.freeConn[:last]
   782  				i--
   783  			}
   784  		}
   785  		db.mu.Unlock()
   786  
   787  		for _, c := range closing {
   788  			c.Close()
   789  		}
   790  
   791  		if d < minInterval {
   792  			d = minInterval
   793  		}
   794  		t.Reset(d)
   795  	}
   796  }
   797  
   798  // DBStats contains database statistics.
   799  type DBStats struct {
   800  	// OpenConnections is the number of open connections to the database.
   801  	OpenConnections int
   802  }
   803  
   804  // Stats returns database statistics.
   805  func (db *DB) Stats() DBStats {
   806  	db.mu.Lock()
   807  	stats := DBStats{
   808  		OpenConnections: db.numOpen,
   809  	}
   810  	db.mu.Unlock()
   811  	return stats
   812  }
   813  
   814  // Assumes db.mu is locked.
   815  // If there are connRequests and the connection limit hasn't been reached,
   816  // then tell the connectionOpener to open new connections.
   817  func (db *DB) maybeOpenNewConnections() {
   818  	numRequests := len(db.connRequests)
   819  	if db.maxOpen > 0 {
   820  		numCanOpen := db.maxOpen - db.numOpen
   821  		if numRequests > numCanOpen {
   822  			numRequests = numCanOpen
   823  		}
   824  	}
   825  	for numRequests > 0 {
   826  		db.numOpen++ // optimistically
   827  		numRequests--
   828  		if db.closed {
   829  			return
   830  		}
   831  		db.openerCh <- struct{}{}
   832  	}
   833  }
   834  
   835  // Runs in a separate goroutine, opens new connections when requested.
   836  func (db *DB) connectionOpener() {
   837  	for range db.openerCh {
   838  		db.openNewConnection()
   839  	}
   840  }
   841  
   842  // Open one new connection
   843  func (db *DB) openNewConnection() {
   844  	// maybeOpenNewConnctions has already executed db.numOpen++ before it sent
   845  	// on db.openerCh. This function must execute db.numOpen-- if the
   846  	// connection fails or is closed before returning.
   847  	ci, err := db.driver.Open(db.dsn)
   848  	db.mu.Lock()
   849  	defer db.mu.Unlock()
   850  	if db.closed {
   851  		if err == nil {
   852  			ci.Close()
   853  		}
   854  		db.numOpen--
   855  		return
   856  	}
   857  	if err != nil {
   858  		db.numOpen--
   859  		db.putConnDBLocked(nil, err)
   860  		db.maybeOpenNewConnections()
   861  		return
   862  	}
   863  	dc := &driverConn{
   864  		db:        db,
   865  		createdAt: nowFunc(),
   866  		ci:        ci,
   867  	}
   868  	if db.putConnDBLocked(dc, err) {
   869  		db.addDepLocked(dc, dc)
   870  	} else {
   871  		db.numOpen--
   872  		ci.Close()
   873  	}
   874  }
   875  
   876  // connRequest represents one request for a new connection
   877  // When there are no idle connections available, DB.conn will create
   878  // a new connRequest and put it on the db.connRequests list.
   879  type connRequest struct {
   880  	conn *driverConn
   881  	err  error
   882  }
   883  
   884  var errDBClosed = errors.New("sql: database is closed")
   885  
   886  // nextRequestKeyLocked returns the next connection request key.
   887  // It is assumed that nextRequest will not overflow.
   888  func (db *DB) nextRequestKeyLocked() uint64 {
   889  	next := db.nextRequest
   890  	db.nextRequest++
   891  	return next
   892  }
   893  
   894  // conn returns a newly-opened or cached *driverConn.
   895  func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) {
   896  	db.mu.Lock()
   897  	if db.closed {
   898  		db.mu.Unlock()
   899  		return nil, errDBClosed
   900  	}
   901  	// Check if the context is expired.
   902  	select {
   903  	default:
   904  	case <-ctx.Done():
   905  		db.mu.Unlock()
   906  		return nil, ctx.Err()
   907  	}
   908  	lifetime := db.maxLifetime
   909  
   910  	// Prefer a free connection, if possible.
   911  	numFree := len(db.freeConn)
   912  	if strategy == cachedOrNewConn && numFree > 0 {
   913  		conn := db.freeConn[0]
   914  		copy(db.freeConn, db.freeConn[1:])
   915  		db.freeConn = db.freeConn[:numFree-1]
   916  		conn.inUse = true
   917  		db.mu.Unlock()
   918  		if conn.expired(lifetime) {
   919  			conn.Close()
   920  			return nil, driver.ErrBadConn
   921  		}
   922  		return conn, nil
   923  	}
   924  
   925  	// Out of free connections or we were asked not to use one. If we're not
   926  	// allowed to open any more connections, make a request and wait.
   927  	if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
   928  		// Make the connRequest channel. It's buffered so that the
   929  		// connectionOpener doesn't block while waiting for the req to be read.
   930  		req := make(chan connRequest, 1)
   931  		reqKey := db.nextRequestKeyLocked()
   932  		db.connRequests[reqKey] = req
   933  		db.mu.Unlock()
   934  
   935  		// Timeout the connection request with the context.
   936  		select {
   937  		case <-ctx.Done():
   938  			// Remove the connection request and ensure no value has been sent
   939  			// on it after removing.
   940  			db.mu.Lock()
   941  			delete(db.connRequests, reqKey)
   942  			db.mu.Unlock()
   943  			select {
   944  			default:
   945  			case ret, ok := <-req:
   946  				if ok {
   947  					db.putConn(ret.conn, ret.err)
   948  				}
   949  			}
   950  			return nil, ctx.Err()
   951  		case ret, ok := <-req:
   952  			if !ok {
   953  				return nil, errDBClosed
   954  			}
   955  			if ret.err == nil && ret.conn.expired(lifetime) {
   956  				ret.conn.Close()
   957  				return nil, driver.ErrBadConn
   958  			}
   959  			return ret.conn, ret.err
   960  		}
   961  	}
   962  
   963  	db.numOpen++ // optimistically
   964  	db.mu.Unlock()
   965  	ci, err := db.driver.Open(db.dsn)
   966  	if err != nil {
   967  		db.mu.Lock()
   968  		db.numOpen-- // correct for earlier optimism
   969  		db.maybeOpenNewConnections()
   970  		db.mu.Unlock()
   971  		return nil, err
   972  	}
   973  	db.mu.Lock()
   974  	dc := &driverConn{
   975  		db:        db,
   976  		createdAt: nowFunc(),
   977  		ci:        ci,
   978  		inUse:     true,
   979  	}
   980  	db.addDepLocked(dc, dc)
   981  	db.mu.Unlock()
   982  	return dc, nil
   983  }
   984  
   985  // putConnHook is a hook for testing.
   986  var putConnHook func(*DB, *driverConn)
   987  
   988  // noteUnusedDriverStatement notes that ds is no longer used and should
   989  // be closed whenever possible (when c is next not in use), unless c is
   990  // already closed.
   991  func (db *DB) noteUnusedDriverStatement(c *driverConn, ds *driverStmt) {
   992  	db.mu.Lock()
   993  	defer db.mu.Unlock()
   994  	if c.inUse {
   995  		c.onPut = append(c.onPut, func() {
   996  			ds.Close()
   997  		})
   998  	} else {
   999  		c.Lock()
  1000  		fc := c.finalClosed
  1001  		c.Unlock()
  1002  		if !fc {
  1003  			ds.Close()
  1004  		}
  1005  	}
  1006  }
  1007  
  1008  // debugGetPut determines whether getConn & putConn calls' stack traces
  1009  // are returned for more verbose crashes.
  1010  const debugGetPut = false
  1011  
  1012  // putConn adds a connection to the db's free pool.
  1013  // err is optionally the last error that occurred on this connection.
  1014  func (db *DB) putConn(dc *driverConn, err error) {
  1015  	db.mu.Lock()
  1016  	if !dc.inUse {
  1017  		if debugGetPut {
  1018  			fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
  1019  		}
  1020  		panic("sql: connection returned that was never out")
  1021  	}
  1022  	if debugGetPut {
  1023  		db.lastPut[dc] = stack()
  1024  	}
  1025  	dc.inUse = false
  1026  
  1027  	for _, fn := range dc.onPut {
  1028  		fn()
  1029  	}
  1030  	dc.onPut = nil
  1031  
  1032  	if err == driver.ErrBadConn {
  1033  		// Don't reuse bad connections.
  1034  		// Since the conn is considered bad and is being discarded, treat it
  1035  		// as closed. Don't decrement the open count here, finalClose will
  1036  		// take care of that.
  1037  		db.maybeOpenNewConnections()
  1038  		db.mu.Unlock()
  1039  		dc.Close()
  1040  		return
  1041  	}
  1042  	if putConnHook != nil {
  1043  		putConnHook(db, dc)
  1044  	}
  1045  	added := db.putConnDBLocked(dc, nil)
  1046  	db.mu.Unlock()
  1047  
  1048  	if !added {
  1049  		dc.Close()
  1050  	}
  1051  }
  1052  
  1053  // Satisfy a connRequest or put the driverConn in the idle pool and return true
  1054  // or return false.
  1055  // putConnDBLocked will satisfy a connRequest if there is one, or it will
  1056  // return the *driverConn to the freeConn list if err == nil and the idle
  1057  // connection limit will not be exceeded.
  1058  // If err != nil, the value of dc is ignored.
  1059  // If err == nil, then dc must not equal nil.
  1060  // If a connRequest was fulfilled or the *driverConn was placed in the
  1061  // freeConn list, then true is returned, otherwise false is returned.
  1062  func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
  1063  	if db.closed {
  1064  		return false
  1065  	}
  1066  	if db.maxOpen > 0 && db.numOpen > db.maxOpen {
  1067  		return false
  1068  	}
  1069  	if c := len(db.connRequests); c > 0 {
  1070  		var req chan connRequest
  1071  		var reqKey uint64
  1072  		for reqKey, req = range db.connRequests {
  1073  			break
  1074  		}
  1075  		delete(db.connRequests, reqKey) // Remove from pending requests.
  1076  		if err == nil {
  1077  			dc.inUse = true
  1078  		}
  1079  		req <- connRequest{
  1080  			conn: dc,
  1081  			err:  err,
  1082  		}
  1083  		return true
  1084  	} else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
  1085  		db.freeConn = append(db.freeConn, dc)
  1086  		db.startCleanerLocked()
  1087  		return true
  1088  	}
  1089  	return false
  1090  }
  1091  
  1092  // maxBadConnRetries is the number of maximum retries if the driver returns
  1093  // driver.ErrBadConn to signal a broken connection before forcing a new
  1094  // connection to be opened.
  1095  const maxBadConnRetries = 2
  1096  
  1097  // PrepareContext creates a prepared statement for later queries or executions.
  1098  // Multiple queries or executions may be run concurrently from the
  1099  // returned statement.
  1100  // The caller must call the statement's Close method
  1101  // when the statement is no longer needed.
  1102  //
  1103  // The provided context is used for the preparation of the statement, not for the
  1104  // execution of the statement.
  1105  func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1106  	var stmt *Stmt
  1107  	var err error
  1108  	for i := 0; i < maxBadConnRetries; i++ {
  1109  		stmt, err = db.prepare(ctx, query, cachedOrNewConn)
  1110  		if err != driver.ErrBadConn {
  1111  			break
  1112  		}
  1113  	}
  1114  	if err == driver.ErrBadConn {
  1115  		return db.prepare(ctx, query, alwaysNewConn)
  1116  	}
  1117  	return stmt, err
  1118  }
  1119  
  1120  // Prepare creates a prepared statement for later queries or executions.
  1121  // Multiple queries or executions may be run concurrently from the
  1122  // returned statement.
  1123  // The caller must call the statement's Close method
  1124  // when the statement is no longer needed.
  1125  func (db *DB) Prepare(query string) (*Stmt, error) {
  1126  	return db.PrepareContext(context.Background(), query)
  1127  }
  1128  
  1129  func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*Stmt, error) {
  1130  	// TODO: check if db.driver supports an optional
  1131  	// driver.Preparer interface and call that instead, if so,
  1132  	// otherwise we make a prepared statement that's bound
  1133  	// to a connection, and to execute this prepared statement
  1134  	// we either need to use this connection (if it's free), else
  1135  	// get a new connection + re-prepare + execute on that one.
  1136  	dc, err := db.conn(ctx, strategy)
  1137  	if err != nil {
  1138  		return nil, err
  1139  	}
  1140  	return db.prepareDC(ctx, dc, dc.releaseConn, query)
  1141  }
  1142  
  1143  func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error), query string) (*Stmt, error) {
  1144  	var ds *driverStmt
  1145  	var err error
  1146  	defer func() {
  1147  		release(err)
  1148  	}()
  1149  	withLock(dc, func() {
  1150  		ds, err = dc.prepareLocked(ctx, query)
  1151  	})
  1152  	if err != nil {
  1153  		return nil, err
  1154  	}
  1155  	stmt := &Stmt{
  1156  		db:            db,
  1157  		query:         query,
  1158  		css:           []connStmt{{dc, ds}},
  1159  		lastNumClosed: atomic.LoadUint64(&db.numClosed),
  1160  	}
  1161  	db.addDep(stmt, stmt)
  1162  	return stmt, nil
  1163  }
  1164  
  1165  // ExecContext executes a query without returning any rows.
  1166  // The args are for any placeholder parameters in the query.
  1167  func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
  1168  	var res Result
  1169  	var err error
  1170  	for i := 0; i < maxBadConnRetries; i++ {
  1171  		res, err = db.exec(ctx, query, args, cachedOrNewConn)
  1172  		if err != driver.ErrBadConn {
  1173  			break
  1174  		}
  1175  	}
  1176  	if err == driver.ErrBadConn {
  1177  		return db.exec(ctx, query, args, alwaysNewConn)
  1178  	}
  1179  	return res, err
  1180  }
  1181  
  1182  // Exec executes a query without returning any rows.
  1183  // The args are for any placeholder parameters in the query.
  1184  func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
  1185  	return db.ExecContext(context.Background(), query, args...)
  1186  }
  1187  
  1188  func (db *DB) exec(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (Result, error) {
  1189  	dc, err := db.conn(ctx, strategy)
  1190  	if err != nil {
  1191  		return nil, err
  1192  	}
  1193  	return db.execDC(ctx, dc, dc.releaseConn, query, args)
  1194  }
  1195  
  1196  func (db *DB) execDC(ctx context.Context, dc *driverConn, release func(error), query string, args []interface{}) (res Result, err error) {
  1197  	defer func() {
  1198  		release(err)
  1199  	}()
  1200  	if execer, ok := dc.ci.(driver.Execer); ok {
  1201  		var dargs []driver.NamedValue
  1202  		dargs, err = driverArgs(nil, args)
  1203  		if err != nil {
  1204  			return nil, err
  1205  		}
  1206  		var resi driver.Result
  1207  		withLock(dc, func() {
  1208  			resi, err = ctxDriverExec(ctx, execer, query, dargs)
  1209  		})
  1210  		if err != driver.ErrSkip {
  1211  			if err != nil {
  1212  				return nil, err
  1213  			}
  1214  			return driverResult{dc, resi}, nil
  1215  		}
  1216  	}
  1217  
  1218  	var si driver.Stmt
  1219  	withLock(dc, func() {
  1220  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1221  	})
  1222  	if err != nil {
  1223  		return nil, err
  1224  	}
  1225  	ds := &driverStmt{Locker: dc, si: si}
  1226  	defer ds.Close()
  1227  	return resultFromStatement(ctx, ds, args...)
  1228  }
  1229  
  1230  // QueryContext executes a query that returns rows, typically a SELECT.
  1231  // The args are for any placeholder parameters in the query.
  1232  func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  1233  	var rows *Rows
  1234  	var err error
  1235  	for i := 0; i < maxBadConnRetries; i++ {
  1236  		rows, err = db.query(ctx, query, args, cachedOrNewConn)
  1237  		if err != driver.ErrBadConn {
  1238  			break
  1239  		}
  1240  	}
  1241  	if err == driver.ErrBadConn {
  1242  		return db.query(ctx, query, args, alwaysNewConn)
  1243  	}
  1244  	return rows, err
  1245  }
  1246  
  1247  // Query executes a query that returns rows, typically a SELECT.
  1248  // The args are for any placeholder parameters in the query.
  1249  func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
  1250  	return db.QueryContext(context.Background(), query, args...)
  1251  }
  1252  
  1253  func (db *DB) query(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
  1254  	dc, err := db.conn(ctx, strategy)
  1255  	if err != nil {
  1256  		return nil, err
  1257  	}
  1258  
  1259  	return db.queryDC(ctx, dc, dc.releaseConn, query, args)
  1260  }
  1261  
  1262  // queryDC executes a query on the given connection.
  1263  // The connection gets released by the releaseConn function.
  1264  func (db *DB) queryDC(ctx context.Context, dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
  1265  	if queryer, ok := dc.ci.(driver.Queryer); ok {
  1266  		dargs, err := driverArgs(nil, args)
  1267  		if err != nil {
  1268  			releaseConn(err)
  1269  			return nil, err
  1270  		}
  1271  		var rowsi driver.Rows
  1272  		withLock(dc, func() {
  1273  			rowsi, err = ctxDriverQuery(ctx, queryer, query, dargs)
  1274  		})
  1275  		if err != driver.ErrSkip {
  1276  			if err != nil {
  1277  				releaseConn(err)
  1278  				return nil, err
  1279  			}
  1280  			// Note: ownership of dc passes to the *Rows, to be freed
  1281  			// with releaseConn.
  1282  			rows := &Rows{
  1283  				dc:          dc,
  1284  				releaseConn: releaseConn,
  1285  				rowsi:       rowsi,
  1286  			}
  1287  			rows.initContextClose(ctx)
  1288  			return rows, nil
  1289  		}
  1290  	}
  1291  
  1292  	var si driver.Stmt
  1293  	var err error
  1294  	withLock(dc, func() {
  1295  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1296  	})
  1297  	if err != nil {
  1298  		releaseConn(err)
  1299  		return nil, err
  1300  	}
  1301  
  1302  	ds := &driverStmt{Locker: dc, si: si}
  1303  	rowsi, err := rowsiFromStatement(ctx, ds, args...)
  1304  	if err != nil {
  1305  		ds.Close()
  1306  		releaseConn(err)
  1307  		return nil, err
  1308  	}
  1309  
  1310  	// Note: ownership of ci passes to the *Rows, to be freed
  1311  	// with releaseConn.
  1312  	rows := &Rows{
  1313  		dc:          dc,
  1314  		releaseConn: releaseConn,
  1315  		rowsi:       rowsi,
  1316  		closeStmt:   ds,
  1317  	}
  1318  	rows.initContextClose(ctx)
  1319  	return rows, nil
  1320  }
  1321  
  1322  // QueryRowContext executes a query that is expected to return at most one row.
  1323  // QueryRowContext always returns a non-nil value. Errors are deferred until
  1324  // Row's Scan method is called.
  1325  func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  1326  	rows, err := db.QueryContext(ctx, query, args...)
  1327  	return &Row{rows: rows, err: err}
  1328  }
  1329  
  1330  // QueryRow executes a query that is expected to return at most one row.
  1331  // QueryRow always returns a non-nil value. Errors are deferred until
  1332  // Row's Scan method is called.
  1333  func (db *DB) QueryRow(query string, args ...interface{}) *Row {
  1334  	return db.QueryRowContext(context.Background(), query, args...)
  1335  }
  1336  
  1337  // BeginTx starts a transaction.
  1338  //
  1339  // The provided context is used until the transaction is committed or rolled back.
  1340  // If the context is canceled, the sql package will roll back
  1341  // the transaction. Tx.Commit will return an error if the context provided to
  1342  // BeginTx is canceled.
  1343  //
  1344  // The provided TxOptions is optional and may be nil if defaults should be used.
  1345  // If a non-default isolation level is used that the driver doesn't support,
  1346  // an error will be returned.
  1347  func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
  1348  	var tx *Tx
  1349  	var err error
  1350  	for i := 0; i < maxBadConnRetries; i++ {
  1351  		tx, err = db.begin(ctx, opts, cachedOrNewConn)
  1352  		if err != driver.ErrBadConn {
  1353  			break
  1354  		}
  1355  	}
  1356  	if err == driver.ErrBadConn {
  1357  		return db.begin(ctx, opts, alwaysNewConn)
  1358  	}
  1359  	return tx, err
  1360  }
  1361  
  1362  // Begin starts a transaction. The default isolation level is dependent on
  1363  // the driver.
  1364  func (db *DB) Begin() (*Tx, error) {
  1365  	return db.BeginTx(context.Background(), nil)
  1366  }
  1367  
  1368  func (db *DB) begin(ctx context.Context, opts *TxOptions, strategy connReuseStrategy) (tx *Tx, err error) {
  1369  	dc, err := db.conn(ctx, strategy)
  1370  	if err != nil {
  1371  		return nil, err
  1372  	}
  1373  	return db.beginDC(ctx, dc, dc.releaseConn, opts)
  1374  }
  1375  
  1376  // beginDC starts a transaction. The provided dc must be valid and ready to use.
  1377  func (db *DB) beginDC(ctx context.Context, dc *driverConn, release func(error), opts *TxOptions) (tx *Tx, err error) {
  1378  	var txi driver.Tx
  1379  	withLock(dc, func() {
  1380  		txi, err = ctxDriverBegin(ctx, opts, dc.ci)
  1381  	})
  1382  	if err != nil {
  1383  		release(err)
  1384  		return nil, err
  1385  	}
  1386  
  1387  	// Schedule the transaction to rollback when the context is cancelled.
  1388  	// The cancel function in Tx will be called after done is set to true.
  1389  	ctx, cancel := context.WithCancel(ctx)
  1390  	tx = &Tx{
  1391  		db:          db,
  1392  		dc:          dc,
  1393  		releaseConn: release,
  1394  		txi:         txi,
  1395  		cancel:      cancel,
  1396  		ctx:         ctx,
  1397  	}
  1398  	go tx.awaitDone()
  1399  	return tx, nil
  1400  }
  1401  
  1402  // Driver returns the database's underlying driver.
  1403  func (db *DB) Driver() driver.Driver {
  1404  	return db.driver
  1405  }
  1406  
  1407  // Tx is an in-progress database transaction.
  1408  //
  1409  // A transaction must end with a call to Commit or Rollback.
  1410  //
  1411  // After a call to Commit or Rollback, all operations on the
  1412  // transaction fail with ErrTxDone.
  1413  //
  1414  // The statements prepared for a transaction by calling
  1415  // the transaction's Prepare or Stmt methods are closed
  1416  // by the call to Commit or Rollback.
  1417  type Tx struct {
  1418  	db *DB
  1419  
  1420  	// closemu prevents the transaction from closing while there
  1421  	// is an active query. It is held for read during queries
  1422  	// and exclusively during close.
  1423  	closemu sync.RWMutex
  1424  
  1425  	// dc is owned exclusively until Commit or Rollback, at which point
  1426  	// it's returned with putConn.
  1427  	dc  *driverConn
  1428  	txi driver.Tx
  1429  
  1430  	// releaseConn is called once the Tx is closed to release
  1431  	// any held driverConn back to the pool.
  1432  	releaseConn func(error)
  1433  
  1434  	// done transitions from 0 to 1 exactly once, on Commit
  1435  	// or Rollback. once done, all operations fail with
  1436  	// ErrTxDone.
  1437  	// Use atomic operations on value when checking value.
  1438  	done int32
  1439  
  1440  	// All Stmts prepared for this transaction. These will be closed after the
  1441  	// transaction has been committed or rolled back.
  1442  	stmts struct {
  1443  		sync.Mutex
  1444  		v []*Stmt
  1445  	}
  1446  
  1447  	// cancel is called after done transitions from 0 to 1.
  1448  	cancel func()
  1449  
  1450  	// ctx lives for the life of the transaction.
  1451  	ctx context.Context
  1452  }
  1453  
  1454  // awaitDone blocks until the context in Tx is canceled and rolls back
  1455  // the transaction if it's not already done.
  1456  func (tx *Tx) awaitDone() {
  1457  	// Wait for either the transaction to be committed or rolled
  1458  	// back, or for the associated context to be closed.
  1459  	<-tx.ctx.Done()
  1460  
  1461  	// Discard and close the connection used to ensure the
  1462  	// transaction is closed and the resources are released.  This
  1463  	// rollback does nothing if the transaction has already been
  1464  	// committed or rolled back.
  1465  	tx.rollback(true)
  1466  }
  1467  
  1468  func (tx *Tx) isDone() bool {
  1469  	return atomic.LoadInt32(&tx.done) != 0
  1470  }
  1471  
  1472  // ErrTxDone is returned by any operation that is performed on a transaction
  1473  // that has already been committed or rolled back.
  1474  var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
  1475  
  1476  // close returns the connection to the pool and
  1477  // must only be called by Tx.rollback or Tx.Commit.
  1478  func (tx *Tx) close(err error) {
  1479  	tx.closemu.Lock()
  1480  	defer tx.closemu.Unlock()
  1481  
  1482  	tx.releaseConn(err)
  1483  	tx.cancel()
  1484  	tx.dc = nil
  1485  	tx.txi = nil
  1486  }
  1487  
  1488  // hookTxGrabConn specifies an optional hook to be called on
  1489  // a successful call to (*Tx).grabConn. For tests.
  1490  var hookTxGrabConn func()
  1491  
  1492  func (tx *Tx) grabConn(ctx context.Context) (*driverConn, error) {
  1493  	select {
  1494  	default:
  1495  	case <-ctx.Done():
  1496  		return nil, ctx.Err()
  1497  	}
  1498  	if tx.isDone() {
  1499  		return nil, ErrTxDone
  1500  	}
  1501  	if hookTxGrabConn != nil { // test hook
  1502  		hookTxGrabConn()
  1503  	}
  1504  	return tx.dc, nil
  1505  }
  1506  
  1507  // closemuRUnlockRelease is used as a func(error) method value in
  1508  // ExecContext and QueryContext. Unlocking in the releaseConn keeps
  1509  // the driver conn from being returned to the connection pool until
  1510  // the Rows has been closed.
  1511  func (tx *Tx) closemuRUnlockRelease(error) {
  1512  	tx.closemu.RUnlock()
  1513  }
  1514  
  1515  // Closes all Stmts prepared for this transaction.
  1516  func (tx *Tx) closePrepared() {
  1517  	tx.stmts.Lock()
  1518  	defer tx.stmts.Unlock()
  1519  	for _, stmt := range tx.stmts.v {
  1520  		stmt.Close()
  1521  	}
  1522  }
  1523  
  1524  // Commit commits the transaction.
  1525  func (tx *Tx) Commit() error {
  1526  	if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  1527  		return ErrTxDone
  1528  	}
  1529  	select {
  1530  	default:
  1531  	case <-tx.ctx.Done():
  1532  		return tx.ctx.Err()
  1533  	}
  1534  	var err error
  1535  	withLock(tx.dc, func() {
  1536  		err = tx.txi.Commit()
  1537  	})
  1538  	if err != driver.ErrBadConn {
  1539  		tx.closePrepared()
  1540  	}
  1541  	tx.close(err)
  1542  	return err
  1543  }
  1544  
  1545  // rollback aborts the transaction and optionally forces the pool to discard
  1546  // the connection.
  1547  func (tx *Tx) rollback(discardConn bool) error {
  1548  	if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  1549  		return ErrTxDone
  1550  	}
  1551  	var err error
  1552  	withLock(tx.dc, func() {
  1553  		err = tx.txi.Rollback()
  1554  	})
  1555  	if err != driver.ErrBadConn {
  1556  		tx.closePrepared()
  1557  	}
  1558  	if discardConn {
  1559  		err = driver.ErrBadConn
  1560  	}
  1561  	tx.close(err)
  1562  	return err
  1563  }
  1564  
  1565  // Rollback aborts the transaction.
  1566  func (tx *Tx) Rollback() error {
  1567  	return tx.rollback(false)
  1568  }
  1569  
  1570  // Prepare creates a prepared statement for use within a transaction.
  1571  //
  1572  // The returned statement operates within the transaction and will be closed
  1573  // when the transaction has been committed or rolled back.
  1574  //
  1575  // To use an existing prepared statement on this transaction, see Tx.Stmt.
  1576  //
  1577  // The provided context will be used for the preparation of the context, not
  1578  // for the execution of the returned statement. The returned statement
  1579  // will run in the transaction context.
  1580  func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1581  	tx.closemu.RLock()
  1582  	defer tx.closemu.RUnlock()
  1583  
  1584  	dc, err := tx.grabConn(ctx)
  1585  	if err != nil {
  1586  		return nil, err
  1587  	}
  1588  
  1589  	var si driver.Stmt
  1590  	withLock(dc, func() {
  1591  		si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1592  	})
  1593  	if err != nil {
  1594  		return nil, err
  1595  	}
  1596  
  1597  	stmt := &Stmt{
  1598  		db: tx.db,
  1599  		tx: tx,
  1600  		txds: &driverStmt{
  1601  			Locker: dc,
  1602  			si:     si,
  1603  		},
  1604  		query: query,
  1605  	}
  1606  	tx.stmts.Lock()
  1607  	tx.stmts.v = append(tx.stmts.v, stmt)
  1608  	tx.stmts.Unlock()
  1609  	return stmt, nil
  1610  }
  1611  
  1612  // Prepare creates a prepared statement for use within a transaction.
  1613  //
  1614  // The returned statement operates within the transaction and can no longer
  1615  // be used once the transaction has been committed or rolled back.
  1616  //
  1617  // To use an existing prepared statement on this transaction, see Tx.Stmt.
  1618  func (tx *Tx) Prepare(query string) (*Stmt, error) {
  1619  	return tx.PrepareContext(context.Background(), query)
  1620  }
  1621  
  1622  // StmtContext returns a transaction-specific prepared statement from
  1623  // an existing statement.
  1624  //
  1625  // Example:
  1626  //  updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  1627  //  ...
  1628  //  tx, err := db.Begin()
  1629  //  ...
  1630  //  res, err := tx.StmtContext(ctx, updateMoney).Exec(123.45, 98293203)
  1631  //
  1632  // The returned statement operates within the transaction and will be closed
  1633  // when the transaction has been committed or rolled back.
  1634  func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
  1635  	tx.closemu.RLock()
  1636  	defer tx.closemu.RUnlock()
  1637  
  1638  	if tx.db != stmt.db {
  1639  		return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
  1640  	}
  1641  	dc, err := tx.grabConn(ctx)
  1642  	if err != nil {
  1643  		return &Stmt{stickyErr: err}
  1644  	}
  1645  	var si driver.Stmt
  1646  	var parentStmt *Stmt
  1647  	stmt.mu.Lock()
  1648  	if stmt.closed || stmt.tx != nil {
  1649  		// If the statement has been closed or already belongs to a
  1650  		// transaction, we can't reuse it in this connection.
  1651  		// Since tx.StmtContext should never need to be called with a
  1652  		// Stmt already belonging to tx, we ignore this edge case and
  1653  		// re-prepare the statement in this case. No need to add
  1654  		// code-complexity for this.
  1655  		stmt.mu.Unlock()
  1656  		withLock(dc, func() {
  1657  			si, err = ctxDriverPrepare(ctx, dc.ci, stmt.query)
  1658  		})
  1659  		if err != nil {
  1660  			return &Stmt{stickyErr: err}
  1661  		}
  1662  	} else {
  1663  		stmt.removeClosedStmtLocked()
  1664  		// See if the statement has already been prepared on this connection,
  1665  		// and reuse it if possible.
  1666  		for _, v := range stmt.css {
  1667  			if v.dc == dc {
  1668  				si = v.ds.si
  1669  				break
  1670  			}
  1671  		}
  1672  
  1673  		stmt.mu.Unlock()
  1674  
  1675  		if si == nil {
  1676  			cs, err := stmt.prepareOnConnLocked(ctx, dc)
  1677  			if err != nil {
  1678  				return &Stmt{stickyErr: err}
  1679  			}
  1680  			si = cs.si
  1681  		}
  1682  		parentStmt = stmt
  1683  	}
  1684  
  1685  	txs := &Stmt{
  1686  		db: tx.db,
  1687  		tx: tx,
  1688  		txds: &driverStmt{
  1689  			Locker: dc,
  1690  			si:     si,
  1691  		},
  1692  		parentStmt: parentStmt,
  1693  		query:      stmt.query,
  1694  	}
  1695  	if parentStmt != nil {
  1696  		tx.db.addDep(parentStmt, txs)
  1697  	}
  1698  	tx.stmts.Lock()
  1699  	tx.stmts.v = append(tx.stmts.v, txs)
  1700  	tx.stmts.Unlock()
  1701  	return txs
  1702  }
  1703  
  1704  // Stmt returns a transaction-specific prepared statement from
  1705  // an existing statement.
  1706  //
  1707  // Example:
  1708  //  updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  1709  //  ...
  1710  //  tx, err := db.Begin()
  1711  //  ...
  1712  //  res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
  1713  //
  1714  // The returned statement operates within the transaction and will be closed
  1715  // when the transaction has been committed or rolled back.
  1716  func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  1717  	return tx.StmtContext(context.Background(), stmt)
  1718  }
  1719  
  1720  // ExecContext executes a query that doesn't return rows.
  1721  // For example: an INSERT and UPDATE.
  1722  func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
  1723  	tx.closemu.RLock()
  1724  
  1725  	dc, err := tx.grabConn(ctx)
  1726  	if err != nil {
  1727  		tx.closemu.RUnlock()
  1728  		return nil, err
  1729  	}
  1730  	return tx.db.execDC(ctx, dc, tx.closemuRUnlockRelease, query, args)
  1731  }
  1732  
  1733  // Exec executes a query that doesn't return rows.
  1734  // For example: an INSERT and UPDATE.
  1735  func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
  1736  	return tx.ExecContext(context.Background(), query, args...)
  1737  }
  1738  
  1739  // QueryContext executes a query that returns rows, typically a SELECT.
  1740  func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  1741  	tx.closemu.RLock()
  1742  
  1743  	dc, err := tx.grabConn(ctx)
  1744  	if err != nil {
  1745  		tx.closemu.RUnlock()
  1746  		return nil, err
  1747  	}
  1748  
  1749  	return tx.db.queryDC(ctx, dc, tx.closemuRUnlockRelease, query, args)
  1750  }
  1751  
  1752  // Query executes a query that returns rows, typically a SELECT.
  1753  func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
  1754  	return tx.QueryContext(context.Background(), query, args...)
  1755  }
  1756  
  1757  // QueryRowContext executes a query that is expected to return at most one row.
  1758  // QueryRowContext always returns a non-nil value. Errors are deferred until
  1759  // Row's Scan method is called.
  1760  func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  1761  	rows, err := tx.QueryContext(ctx, query, args...)
  1762  	return &Row{rows: rows, err: err}
  1763  }
  1764  
  1765  // QueryRow executes a query that is expected to return at most one row.
  1766  // QueryRow always returns a non-nil value. Errors are deferred until
  1767  // Row's Scan method is called.
  1768  func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
  1769  	return tx.QueryRowContext(context.Background(), query, args...)
  1770  }
  1771  
  1772  // connStmt is a prepared statement on a particular connection.
  1773  type connStmt struct {
  1774  	dc *driverConn
  1775  	ds *driverStmt
  1776  }
  1777  
  1778  // Stmt is a prepared statement.
  1779  // A Stmt is safe for concurrent use by multiple goroutines.
  1780  type Stmt struct {
  1781  	// Immutable:
  1782  	db        *DB    // where we came from
  1783  	query     string // that created the Stmt
  1784  	stickyErr error  // if non-nil, this error is returned for all operations
  1785  
  1786  	closemu sync.RWMutex // held exclusively during close, for read otherwise.
  1787  
  1788  	// If in a transaction, else both nil:
  1789  	tx   *Tx
  1790  	txds *driverStmt
  1791  
  1792  	// parentStmt is set when a transaction-specific statement
  1793  	// is requested from an identical statement prepared on the same
  1794  	// conn. parentStmt is used to track the dependency of this statement
  1795  	// on its originating ("parent") statement so that parentStmt may
  1796  	// be closed by the user without them having to know whether or not
  1797  	// any transactions are still using it.
  1798  	parentStmt *Stmt
  1799  
  1800  	mu     sync.Mutex // protects the rest of the fields
  1801  	closed bool
  1802  
  1803  	// css is a list of underlying driver statement interfaces
  1804  	// that are valid on particular connections. This is only
  1805  	// used if tx == nil and one is found that has idle
  1806  	// connections. If tx != nil, txds is always used.
  1807  	css []connStmt
  1808  
  1809  	// lastNumClosed is copied from db.numClosed when Stmt is created
  1810  	// without tx and closed connections in css are removed.
  1811  	lastNumClosed uint64
  1812  }
  1813  
  1814  // ExecContext executes a prepared statement with the given arguments and
  1815  // returns a Result summarizing the effect of the statement.
  1816  func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error) {
  1817  	s.closemu.RLock()
  1818  	defer s.closemu.RUnlock()
  1819  
  1820  	var res Result
  1821  	for i := 0; i < maxBadConnRetries; i++ {
  1822  		_, releaseConn, ds, err := s.connStmt(ctx)
  1823  		if err != nil {
  1824  			if err == driver.ErrBadConn {
  1825  				continue
  1826  			}
  1827  			return nil, err
  1828  		}
  1829  
  1830  		res, err = resultFromStatement(ctx, ds, args...)
  1831  		releaseConn(err)
  1832  		if err != driver.ErrBadConn {
  1833  			return res, err
  1834  		}
  1835  	}
  1836  	return nil, driver.ErrBadConn
  1837  }
  1838  
  1839  // Exec executes a prepared statement with the given arguments and
  1840  // returns a Result summarizing the effect of the statement.
  1841  func (s *Stmt) Exec(args ...interface{}) (Result, error) {
  1842  	return s.ExecContext(context.Background(), args...)
  1843  }
  1844  
  1845  func driverNumInput(ds *driverStmt) int {
  1846  	ds.Lock()
  1847  	defer ds.Unlock() // in case NumInput panics
  1848  	return ds.si.NumInput()
  1849  }
  1850  
  1851  func resultFromStatement(ctx context.Context, ds *driverStmt, args ...interface{}) (Result, error) {
  1852  	want := driverNumInput(ds)
  1853  
  1854  	// -1 means the driver doesn't know how to count the number of
  1855  	// placeholders, so we won't sanity check input here and instead let the
  1856  	// driver deal with errors.
  1857  	if want != -1 && len(args) != want {
  1858  		return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
  1859  	}
  1860  
  1861  	dargs, err := driverArgs(ds, args)
  1862  	if err != nil {
  1863  		return nil, err
  1864  	}
  1865  
  1866  	ds.Lock()
  1867  	defer ds.Unlock()
  1868  
  1869  	resi, err := ctxDriverStmtExec(ctx, ds.si, dargs)
  1870  	if err != nil {
  1871  		return nil, err
  1872  	}
  1873  	return driverResult{ds.Locker, resi}, nil
  1874  }
  1875  
  1876  // removeClosedStmtLocked removes closed conns in s.css.
  1877  //
  1878  // To avoid lock contention on DB.mu, we do it only when
  1879  // s.db.numClosed - s.lastNum is large enough.
  1880  func (s *Stmt) removeClosedStmtLocked() {
  1881  	t := len(s.css)/2 + 1
  1882  	if t > 10 {
  1883  		t = 10
  1884  	}
  1885  	dbClosed := atomic.LoadUint64(&s.db.numClosed)
  1886  	if dbClosed-s.lastNumClosed < uint64(t) {
  1887  		return
  1888  	}
  1889  
  1890  	s.db.mu.Lock()
  1891  	for i := 0; i < len(s.css); i++ {
  1892  		if s.css[i].dc.dbmuClosed {
  1893  			s.css[i] = s.css[len(s.css)-1]
  1894  			s.css = s.css[:len(s.css)-1]
  1895  			i--
  1896  		}
  1897  	}
  1898  	s.db.mu.Unlock()
  1899  	s.lastNumClosed = dbClosed
  1900  }
  1901  
  1902  // connStmt returns a free driver connection on which to execute the
  1903  // statement, a function to call to release the connection, and a
  1904  // statement bound to that connection.
  1905  func (s *Stmt) connStmt(ctx context.Context) (ci *driverConn, releaseConn func(error), ds *driverStmt, err error) {
  1906  	if err = s.stickyErr; err != nil {
  1907  		return
  1908  	}
  1909  	s.mu.Lock()
  1910  	if s.closed {
  1911  		s.mu.Unlock()
  1912  		err = errors.New("sql: statement is closed")
  1913  		return
  1914  	}
  1915  
  1916  	// In a transaction, we always use the connection that the
  1917  	// transaction was created on.
  1918  	if s.tx != nil {
  1919  		s.mu.Unlock()
  1920  		ci, err = s.tx.grabConn(ctx) // blocks, waiting for the connection.
  1921  		if err != nil {
  1922  			return
  1923  		}
  1924  		releaseConn = func(error) {}
  1925  		return ci, releaseConn, s.txds, nil
  1926  	}
  1927  
  1928  	s.removeClosedStmtLocked()
  1929  	s.mu.Unlock()
  1930  
  1931  	dc, err := s.db.conn(ctx, cachedOrNewConn)
  1932  	if err != nil {
  1933  		return nil, nil, nil, err
  1934  	}
  1935  
  1936  	s.mu.Lock()
  1937  	for _, v := range s.css {
  1938  		if v.dc == dc {
  1939  			s.mu.Unlock()
  1940  			return dc, dc.releaseConn, v.ds, nil
  1941  		}
  1942  	}
  1943  	s.mu.Unlock()
  1944  
  1945  	// No luck; we need to prepare the statement on this connection
  1946  	withLock(dc, func() {
  1947  		ds, err = s.prepareOnConnLocked(ctx, dc)
  1948  	})
  1949  	if err != nil {
  1950  		dc.releaseConn(err)
  1951  		return nil, nil, nil, err
  1952  	}
  1953  
  1954  	return dc, dc.releaseConn, ds, nil
  1955  }
  1956  
  1957  // prepareOnConnLocked prepares the query in Stmt s on dc and adds it to the list of
  1958  // open connStmt on the statement. It assumes the caller is holding the lock on dc.
  1959  func (s *Stmt) prepareOnConnLocked(ctx context.Context, dc *driverConn) (*driverStmt, error) {
  1960  	si, err := dc.prepareLocked(ctx, s.query)
  1961  	if err != nil {
  1962  		return nil, err
  1963  	}
  1964  	cs := connStmt{dc, si}
  1965  	s.mu.Lock()
  1966  	s.css = append(s.css, cs)
  1967  	s.mu.Unlock()
  1968  	return cs.ds, nil
  1969  }
  1970  
  1971  // QueryContext executes a prepared query statement with the given arguments
  1972  // and returns the query results as a *Rows.
  1973  func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) {
  1974  	s.closemu.RLock()
  1975  	defer s.closemu.RUnlock()
  1976  
  1977  	var rowsi driver.Rows
  1978  	for i := 0; i < maxBadConnRetries; i++ {
  1979  		dc, releaseConn, ds, err := s.connStmt(ctx)
  1980  		if err != nil {
  1981  			if err == driver.ErrBadConn {
  1982  				continue
  1983  			}
  1984  			return nil, err
  1985  		}
  1986  
  1987  		rowsi, err = rowsiFromStatement(ctx, ds, args...)
  1988  		if err == nil {
  1989  			// Note: ownership of ci passes to the *Rows, to be freed
  1990  			// with releaseConn.
  1991  			rows := &Rows{
  1992  				dc:    dc,
  1993  				rowsi: rowsi,
  1994  				// releaseConn set below
  1995  			}
  1996  			rows.initContextClose(ctx)
  1997  			s.db.addDep(s, rows)
  1998  			rows.releaseConn = func(err error) {
  1999  				releaseConn(err)
  2000  				s.db.removeDep(s, rows)
  2001  			}
  2002  			return rows, nil
  2003  		}
  2004  
  2005  		releaseConn(err)
  2006  		if err != driver.ErrBadConn {
  2007  			return nil, err
  2008  		}
  2009  	}
  2010  	return nil, driver.ErrBadConn
  2011  }
  2012  
  2013  // Query executes a prepared query statement with the given arguments
  2014  // and returns the query results as a *Rows.
  2015  func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
  2016  	return s.QueryContext(context.Background(), args...)
  2017  }
  2018  
  2019  func rowsiFromStatement(ctx context.Context, ds *driverStmt, args ...interface{}) (driver.Rows, error) {
  2020  	var want int
  2021  	withLock(ds, func() {
  2022  		want = ds.si.NumInput()
  2023  	})
  2024  
  2025  	// -1 means the driver doesn't know how to count the number of
  2026  	// placeholders, so we won't sanity check input here and instead let the
  2027  	// driver deal with errors.
  2028  	if want != -1 && len(args) != want {
  2029  		return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
  2030  	}
  2031  
  2032  	dargs, err := driverArgs(ds, args)
  2033  	if err != nil {
  2034  		return nil, err
  2035  	}
  2036  
  2037  	ds.Lock()
  2038  	defer ds.Unlock()
  2039  
  2040  	rowsi, err := ctxDriverStmtQuery(ctx, ds.si, dargs)
  2041  	if err != nil {
  2042  		return nil, err
  2043  	}
  2044  	return rowsi, nil
  2045  }
  2046  
  2047  // QueryRowContext executes a prepared query statement with the given arguments.
  2048  // If an error occurs during the execution of the statement, that error will
  2049  // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2050  // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2051  // Otherwise, the *Row's Scan scans the first selected row and discards
  2052  // the rest.
  2053  //
  2054  // Example usage:
  2055  //
  2056  //  var name string
  2057  //  err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name)
  2058  func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row {
  2059  	rows, err := s.QueryContext(ctx, args...)
  2060  	if err != nil {
  2061  		return &Row{err: err}
  2062  	}
  2063  	return &Row{rows: rows}
  2064  }
  2065  
  2066  // QueryRow executes a prepared query statement with the given arguments.
  2067  // If an error occurs during the execution of the statement, that error will
  2068  // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2069  // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2070  // Otherwise, the *Row's Scan scans the first selected row and discards
  2071  // the rest.
  2072  //
  2073  // Example usage:
  2074  //
  2075  //  var name string
  2076  //  err := nameByUseridStmt.QueryRow(id).Scan(&name)
  2077  func (s *Stmt) QueryRow(args ...interface{}) *Row {
  2078  	return s.QueryRowContext(context.Background(), args...)
  2079  }
  2080  
  2081  // Close closes the statement.
  2082  func (s *Stmt) Close() error {
  2083  	s.closemu.Lock()
  2084  	defer s.closemu.Unlock()
  2085  
  2086  	if s.stickyErr != nil {
  2087  		return s.stickyErr
  2088  	}
  2089  	s.mu.Lock()
  2090  	if s.closed {
  2091  		s.mu.Unlock()
  2092  		return nil
  2093  	}
  2094  	s.closed = true
  2095  	s.mu.Unlock()
  2096  
  2097  	if s.tx == nil {
  2098  		return s.db.removeDep(s, s)
  2099  	}
  2100  
  2101  	if s.parentStmt != nil {
  2102  		// If parentStmt is set, we must not close s.txds since it's stored
  2103  		// in the css array of the parentStmt.
  2104  		return s.db.removeDep(s.parentStmt, s)
  2105  	}
  2106  	return s.txds.Close()
  2107  }
  2108  
  2109  func (s *Stmt) finalClose() error {
  2110  	s.mu.Lock()
  2111  	defer s.mu.Unlock()
  2112  	if s.css != nil {
  2113  		for _, v := range s.css {
  2114  			s.db.noteUnusedDriverStatement(v.dc, v.ds)
  2115  			v.dc.removeOpenStmt(v.ds)
  2116  		}
  2117  		s.css = nil
  2118  	}
  2119  	return nil
  2120  }
  2121  
  2122  // Rows is the result of a query. Its cursor starts before the first row
  2123  // of the result set. Use Next to advance through the rows:
  2124  //
  2125  //     rows, err := db.Query("SELECT ...")
  2126  //     ...
  2127  //     defer rows.Close()
  2128  //     for rows.Next() {
  2129  //         var id int
  2130  //         var name string
  2131  //         err = rows.Scan(&id, &name)
  2132  //         ...
  2133  //     }
  2134  //     err = rows.Err() // get any error encountered during iteration
  2135  //     ...
  2136  type Rows struct {
  2137  	dc          *driverConn // owned; must call releaseConn when closed to release
  2138  	releaseConn func(error)
  2139  	rowsi       driver.Rows
  2140  	cancel      func()      // called when Rows is closed, may be nil.
  2141  	closeStmt   *driverStmt // if non-nil, statement to Close on close
  2142  
  2143  	// closemu prevents Rows from closing while there
  2144  	// is an active streaming result. It is held for read during non-close operations
  2145  	// and exclusively during close.
  2146  	//
  2147  	// closemu guards lasterr and closed.
  2148  	closemu sync.RWMutex
  2149  	closed  bool
  2150  	lasterr error // non-nil only if closed is true
  2151  
  2152  	// lastcols is only used in Scan, Next, and NextResultSet which are expected
  2153  	// not to be called concurrently.
  2154  	lastcols []driver.Value
  2155  }
  2156  
  2157  func (rs *Rows) initContextClose(ctx context.Context) {
  2158  	ctx, rs.cancel = context.WithCancel(ctx)
  2159  	go rs.awaitDone(ctx)
  2160  }
  2161  
  2162  // awaitDone blocks until the rows are closed or the context canceled.
  2163  func (rs *Rows) awaitDone(ctx context.Context) {
  2164  	<-ctx.Done()
  2165  	rs.close(ctx.Err())
  2166  }
  2167  
  2168  // Next prepares the next result row for reading with the Scan method. It
  2169  // returns true on success, or false if there is no next result row or an error
  2170  // happened while preparing it. Err should be consulted to distinguish between
  2171  // the two cases.
  2172  //
  2173  // Every call to Scan, even the first one, must be preceded by a call to Next.
  2174  func (rs *Rows) Next() bool {
  2175  	var doClose, ok bool
  2176  	withLock(rs.closemu.RLocker(), func() {
  2177  		doClose, ok = rs.nextLocked()
  2178  	})
  2179  	if doClose {
  2180  		rs.Close()
  2181  	}
  2182  	return ok
  2183  }
  2184  
  2185  func (rs *Rows) nextLocked() (doClose, ok bool) {
  2186  	if rs.closed {
  2187  		return false, false
  2188  	}
  2189  	if rs.lastcols == nil {
  2190  		rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
  2191  	}
  2192  	rs.lasterr = rs.rowsi.Next(rs.lastcols)
  2193  	if rs.lasterr != nil {
  2194  		// Close the connection if there is a driver error.
  2195  		if rs.lasterr != io.EOF {
  2196  			return true, false
  2197  		}
  2198  		nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2199  		if !ok {
  2200  			return true, false
  2201  		}
  2202  		// The driver is at the end of the current result set.
  2203  		// Test to see if there is another result set after the current one.
  2204  		// Only close Rows if there is no further result sets to read.
  2205  		if !nextResultSet.HasNextResultSet() {
  2206  			doClose = true
  2207  		}
  2208  		return doClose, false
  2209  	}
  2210  	return false, true
  2211  }
  2212  
  2213  // NextResultSet prepares the next result set for reading. It returns true if
  2214  // there is further result sets, or false if there is no further result set
  2215  // or if there is an error advancing to it. The Err method should be consulted
  2216  // to distinguish between the two cases.
  2217  //
  2218  // After calling NextResultSet, the Next method should always be called before
  2219  // scanning. If there are further result sets they may not have rows in the result
  2220  // set.
  2221  func (rs *Rows) NextResultSet() bool {
  2222  	var doClose bool
  2223  	defer func() {
  2224  		if doClose {
  2225  			rs.Close()
  2226  		}
  2227  	}()
  2228  	rs.closemu.RLock()
  2229  	defer rs.closemu.RUnlock()
  2230  
  2231  	if rs.closed {
  2232  		return false
  2233  	}
  2234  
  2235  	rs.lastcols = nil
  2236  	nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2237  	if !ok {
  2238  		doClose = true
  2239  		return false
  2240  	}
  2241  	rs.lasterr = nextResultSet.NextResultSet()
  2242  	if rs.lasterr != nil {
  2243  		doClose = true
  2244  		return false
  2245  	}
  2246  	return true
  2247  }
  2248  
  2249  // Err returns the error, if any, that was encountered during iteration.
  2250  // Err may be called after an explicit or implicit Close.
  2251  func (rs *Rows) Err() error {
  2252  	rs.closemu.RLock()
  2253  	defer rs.closemu.RUnlock()
  2254  	if rs.lasterr == io.EOF {
  2255  		return nil
  2256  	}
  2257  	return rs.lasterr
  2258  }
  2259  
  2260  // Columns returns the column names.
  2261  // Columns returns an error if the rows are closed, or if the rows
  2262  // are from QueryRow and there was a deferred error.
  2263  func (rs *Rows) Columns() ([]string, error) {
  2264  	rs.closemu.RLock()
  2265  	defer rs.closemu.RUnlock()
  2266  	if rs.closed {
  2267  		return nil, errors.New("sql: Rows are closed")
  2268  	}
  2269  	if rs.rowsi == nil {
  2270  		return nil, errors.New("sql: no Rows available")
  2271  	}
  2272  	return rs.rowsi.Columns(), nil
  2273  }
  2274  
  2275  // ColumnTypes returns column information such as column type, length,
  2276  // and nullable. Some information may not be available from some drivers.
  2277  func (rs *Rows) ColumnTypes() ([]*ColumnType, error) {
  2278  	rs.closemu.RLock()
  2279  	defer rs.closemu.RUnlock()
  2280  	if rs.closed {
  2281  		return nil, errors.New("sql: Rows are closed")
  2282  	}
  2283  	if rs.rowsi == nil {
  2284  		return nil, errors.New("sql: no Rows available")
  2285  	}
  2286  	return rowsColumnInfoSetup(rs.rowsi), nil
  2287  }
  2288  
  2289  // ColumnType contains the name and type of a column.
  2290  type ColumnType struct {
  2291  	name string
  2292  
  2293  	hasNullable       bool
  2294  	hasLength         bool
  2295  	hasPrecisionScale bool
  2296  
  2297  	nullable     bool
  2298  	length       int64
  2299  	databaseType string
  2300  	precision    int64
  2301  	scale        int64
  2302  	scanType     reflect.Type
  2303  }
  2304  
  2305  // Name returns the name or alias of the column.
  2306  func (ci *ColumnType) Name() string {
  2307  	return ci.name
  2308  }
  2309  
  2310  // Length returns the column type length for variable length column types such
  2311  // as text and binary field types. If the type length is unbounded the value will
  2312  // be math.MaxInt64 (any database limits will still apply).
  2313  // If the column type is not variable length, such as an int, or if not supported
  2314  // by the driver ok is false.
  2315  func (ci *ColumnType) Length() (length int64, ok bool) {
  2316  	return ci.length, ci.hasLength
  2317  }
  2318  
  2319  // DecimalSize returns the scale and precision of a decimal type.
  2320  // If not applicable or if not supported ok is false.
  2321  func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool) {
  2322  	return ci.precision, ci.scale, ci.hasPrecisionScale
  2323  }
  2324  
  2325  // ScanType returns a Go type suitable for scanning into using Rows.Scan.
  2326  // If a driver does not support this property ScanType will return
  2327  // the type of an empty interface.
  2328  func (ci *ColumnType) ScanType() reflect.Type {
  2329  	return ci.scanType
  2330  }
  2331  
  2332  // Nullable returns whether the column may be null.
  2333  // If a driver does not support this property ok will be false.
  2334  func (ci *ColumnType) Nullable() (nullable, ok bool) {
  2335  	return ci.nullable, ci.hasNullable
  2336  }
  2337  
  2338  // DatabaseTypeName returns the database system name of the column type. If an empty
  2339  // string is returned the driver type name is not supported.
  2340  // Consult your driver documentation for a list of driver data types. Length specifiers
  2341  // are not included.
  2342  // Common type include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", "BIGINT".
  2343  func (ci *ColumnType) DatabaseTypeName() string {
  2344  	return ci.databaseType
  2345  }
  2346  
  2347  func rowsColumnInfoSetup(rowsi driver.Rows) []*ColumnType {
  2348  	names := rowsi.Columns()
  2349  
  2350  	list := make([]*ColumnType, len(names))
  2351  	for i := range list {
  2352  		ci := &ColumnType{
  2353  			name: names[i],
  2354  		}
  2355  		list[i] = ci
  2356  
  2357  		if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok {
  2358  			ci.scanType = prop.ColumnTypeScanType(i)
  2359  		} else {
  2360  			ci.scanType = reflect.TypeOf(new(interface{})).Elem()
  2361  		}
  2362  		if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok {
  2363  			ci.databaseType = prop.ColumnTypeDatabaseTypeName(i)
  2364  		}
  2365  		if prop, ok := rowsi.(driver.RowsColumnTypeLength); ok {
  2366  			ci.length, ci.hasLength = prop.ColumnTypeLength(i)
  2367  		}
  2368  		if prop, ok := rowsi.(driver.RowsColumnTypeNullable); ok {
  2369  			ci.nullable, ci.hasNullable = prop.ColumnTypeNullable(i)
  2370  		}
  2371  		if prop, ok := rowsi.(driver.RowsColumnTypePrecisionScale); ok {
  2372  			ci.precision, ci.scale, ci.hasPrecisionScale = prop.ColumnTypePrecisionScale(i)
  2373  		}
  2374  	}
  2375  	return list
  2376  }
  2377  
  2378  // Scan copies the columns in the current row into the values pointed
  2379  // at by dest. The number of values in dest must be the same as the
  2380  // number of columns in Rows.
  2381  //
  2382  // Scan converts columns read from the database into the following
  2383  // common Go types and special types provided by the sql package:
  2384  //
  2385  //    *string
  2386  //    *[]byte
  2387  //    *int, *int8, *int16, *int32, *int64
  2388  //    *uint, *uint8, *uint16, *uint32, *uint64
  2389  //    *bool
  2390  //    *float32, *float64
  2391  //    *interface{}
  2392  //    *RawBytes
  2393  //    any type implementing Scanner (see Scanner docs)
  2394  //
  2395  // In the most simple case, if the type of the value from the source
  2396  // column is an integer, bool or string type T and dest is of type *T,
  2397  // Scan simply assigns the value through the pointer.
  2398  //
  2399  // Scan also converts between string and numeric types, as long as no
  2400  // information would be lost. While Scan stringifies all numbers
  2401  // scanned from numeric database columns into *string, scans into
  2402  // numeric types are checked for overflow. For example, a float64 with
  2403  // value 300 or a string with value "300" can scan into a uint16, but
  2404  // not into a uint8, though float64(255) or "255" can scan into a
  2405  // uint8. One exception is that scans of some float64 numbers to
  2406  // strings may lose information when stringifying. In general, scan
  2407  // floating point columns into *float64.
  2408  //
  2409  // If a dest argument has type *[]byte, Scan saves in that argument a
  2410  // copy of the corresponding data. The copy is owned by the caller and
  2411  // can be modified and held indefinitely. The copy can be avoided by
  2412  // using an argument of type *RawBytes instead; see the documentation
  2413  // for RawBytes for restrictions on its use.
  2414  //
  2415  // If an argument has type *interface{}, Scan copies the value
  2416  // provided by the underlying driver without conversion. When scanning
  2417  // from a source value of type []byte to *interface{}, a copy of the
  2418  // slice is made and the caller owns the result.
  2419  //
  2420  // Source values of type time.Time may be scanned into values of type
  2421  // *time.Time, *interface{}, *string, or *[]byte. When converting to
  2422  // the latter two, time.Format3339Nano is used.
  2423  //
  2424  // Source values of type bool may be scanned into types *bool,
  2425  // *interface{}, *string, *[]byte, or *RawBytes.
  2426  //
  2427  // For scanning into *bool, the source may be true, false, 1, 0, or
  2428  // string inputs parseable by strconv.ParseBool.
  2429  func (rs *Rows) Scan(dest ...interface{}) error {
  2430  	rs.closemu.RLock()
  2431  	if rs.closed {
  2432  		rs.closemu.RUnlock()
  2433  		return errors.New("sql: Rows are closed")
  2434  	}
  2435  	rs.closemu.RUnlock()
  2436  
  2437  	if rs.lastcols == nil {
  2438  		return errors.New("sql: Scan called without calling Next")
  2439  	}
  2440  	if len(dest) != len(rs.lastcols) {
  2441  		return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
  2442  	}
  2443  	for i, sv := range rs.lastcols {
  2444  		err := convertAssign(dest[i], sv)
  2445  		if err != nil {
  2446  			return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
  2447  		}
  2448  	}
  2449  	return nil
  2450  }
  2451  
  2452  // rowsCloseHook returns a function so tests may install the
  2453  // hook through a test only mutex.
  2454  var rowsCloseHook = func() func(*Rows, *error) { return nil }
  2455  
  2456  // Close closes the Rows, preventing further enumeration. If Next is called
  2457  // and returns false and there are no further result sets,
  2458  // the Rows are closed automatically and it will suffice to check the
  2459  // result of Err. Close is idempotent and does not affect the result of Err.
  2460  func (rs *Rows) Close() error {
  2461  	return rs.close(nil)
  2462  }
  2463  
  2464  func (rs *Rows) close(err error) error {
  2465  	rs.closemu.Lock()
  2466  	defer rs.closemu.Unlock()
  2467  
  2468  	if rs.closed {
  2469  		return nil
  2470  	}
  2471  	rs.closed = true
  2472  
  2473  	if rs.lasterr == nil {
  2474  		rs.lasterr = err
  2475  	}
  2476  
  2477  	err = rs.rowsi.Close()
  2478  	if fn := rowsCloseHook(); fn != nil {
  2479  		fn(rs, &err)
  2480  	}
  2481  	if rs.cancel != nil {
  2482  		rs.cancel()
  2483  	}
  2484  
  2485  	if rs.closeStmt != nil {
  2486  		rs.closeStmt.Close()
  2487  	}
  2488  	rs.releaseConn(err)
  2489  	return err
  2490  }
  2491  
  2492  // Row is the result of calling QueryRow to select a single row.
  2493  type Row struct {
  2494  	// One of these two will be non-nil:
  2495  	err  error // deferred error for easy chaining
  2496  	rows *Rows
  2497  }
  2498  
  2499  // Scan copies the columns from the matched row into the values
  2500  // pointed at by dest. See the documentation on Rows.Scan for details.
  2501  // If more than one row matches the query,
  2502  // Scan uses the first row and discards the rest. If no row matches
  2503  // the query, Scan returns ErrNoRows.
  2504  func (r *Row) Scan(dest ...interface{}) error {
  2505  	if r.err != nil {
  2506  		return r.err
  2507  	}
  2508  
  2509  	// TODO(bradfitz): for now we need to defensively clone all
  2510  	// []byte that the driver returned (not permitting
  2511  	// *RawBytes in Rows.Scan), since we're about to close
  2512  	// the Rows in our defer, when we return from this function.
  2513  	// the contract with the driver.Next(...) interface is that it
  2514  	// can return slices into read-only temporary memory that's
  2515  	// only valid until the next Scan/Close. But the TODO is that
  2516  	// for a lot of drivers, this copy will be unnecessary. We
  2517  	// should provide an optional interface for drivers to
  2518  	// implement to say, "don't worry, the []bytes that I return
  2519  	// from Next will not be modified again." (for instance, if
  2520  	// they were obtained from the network anyway) But for now we
  2521  	// don't care.
  2522  	defer r.rows.Close()
  2523  	for _, dp := range dest {
  2524  		if _, ok := dp.(*RawBytes); ok {
  2525  			return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  2526  		}
  2527  	}
  2528  
  2529  	if !r.rows.Next() {
  2530  		if err := r.rows.Err(); err != nil {
  2531  			return err
  2532  		}
  2533  		return ErrNoRows
  2534  	}
  2535  	err := r.rows.Scan(dest...)
  2536  	if err != nil {
  2537  		return err
  2538  	}
  2539  	// Make sure the query can be processed to completion with no errors.
  2540  	if err := r.rows.Close(); err != nil {
  2541  		return err
  2542  	}
  2543  
  2544  	return nil
  2545  }
  2546  
  2547  // A Result summarizes an executed SQL command.
  2548  type Result interface {
  2549  	// LastInsertId returns the integer generated by the database
  2550  	// in response to a command. Typically this will be from an
  2551  	// "auto increment" column when inserting a new row. Not all
  2552  	// databases support this feature, and the syntax of such
  2553  	// statements varies.
  2554  	LastInsertId() (int64, error)
  2555  
  2556  	// RowsAffected returns the number of rows affected by an
  2557  	// update, insert, or delete. Not every database or database
  2558  	// driver may support this.
  2559  	RowsAffected() (int64, error)
  2560  }
  2561  
  2562  type driverResult struct {
  2563  	sync.Locker // the *driverConn
  2564  	resi        driver.Result
  2565  }
  2566  
  2567  func (dr driverResult) LastInsertId() (int64, error) {
  2568  	dr.Lock()
  2569  	defer dr.Unlock()
  2570  	return dr.resi.LastInsertId()
  2571  }
  2572  
  2573  func (dr driverResult) RowsAffected() (int64, error) {
  2574  	dr.Lock()
  2575  	defer dr.Unlock()
  2576  	return dr.resi.RowsAffected()
  2577  }
  2578  
  2579  func stack() string {
  2580  	var buf [2 << 10]byte
  2581  	return string(buf[:runtime.Stack(buf[:], false)])
  2582  }
  2583  
  2584  // withLock runs while holding lk.
  2585  func withLock(lk sync.Locker, fn func()) {
  2586  	lk.Lock()
  2587  	defer lk.Unlock() // in case fn panics
  2588  	fn()
  2589  }