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