rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/database/sql/sql.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package sql provides a generic interface around SQL (or SQL-like)
     6  // databases.
     7  //
     8  // The sql package must be used in conjunction with a database driver.
     9  // See http://golang.org/s/sqldrivers for a list of drivers.
    10  //
    11  // For more usage examples, see the wiki page at
    12  // http://golang.org/s/sqlwiki.
    13  package sql
    14  
    15  import (
    16  	"database/sql/driver"
    17  	"errors"
    18  	"fmt"
    19  	"io"
    20  	"runtime"
    21  	"sort"
    22  	"sync"
    23  	"sync/atomic"
    24  )
    25  
    26  var drivers = make(map[string]driver.Driver)
    27  
    28  // Register makes a database driver available by the provided name.
    29  // If Register is called twice with the same name or if driver is nil,
    30  // it panics.
    31  func Register(name string, driver driver.Driver) {
    32  	if driver == nil {
    33  		panic("sql: Register driver is nil")
    34  	}
    35  	if _, dup := drivers[name]; dup {
    36  		panic("sql: Register called twice for driver " + name)
    37  	}
    38  	drivers[name] = driver
    39  }
    40  
    41  func unregisterAllDrivers() {
    42  	// For tests.
    43  	drivers = make(map[string]driver.Driver)
    44  }
    45  
    46  // Drivers returns a sorted list of the names of the registered drivers.
    47  func Drivers() []string {
    48  	var list []string
    49  	for name := range drivers {
    50  		list = append(list, name)
    51  	}
    52  	sort.Strings(list)
    53  	return list
    54  }
    55  
    56  // RawBytes is a byte slice that holds a reference to memory owned by
    57  // the database itself. After a Scan into a RawBytes, the slice is only
    58  // valid until the next call to Next, Scan, or Close.
    59  type RawBytes []byte
    60  
    61  // NullString represents a string that may be null.
    62  // NullString implements the Scanner interface so
    63  // it can be used as a scan destination:
    64  //
    65  //  var s NullString
    66  //  err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
    67  //  ...
    68  //  if s.Valid {
    69  //     // use s.String
    70  //  } else {
    71  //     // NULL value
    72  //  }
    73  //
    74  type NullString struct {
    75  	String string
    76  	Valid  bool // Valid is true if String is not NULL
    77  }
    78  
    79  // Scan implements the Scanner interface.
    80  func (ns *NullString) Scan(value interface{}) error {
    81  	if value == nil {
    82  		ns.String, ns.Valid = "", false
    83  		return nil
    84  	}
    85  	ns.Valid = true
    86  	return convertAssign(&ns.String, value)
    87  }
    88  
    89  // Value implements the driver Valuer interface.
    90  func (ns NullString) Value() (driver.Value, error) {
    91  	if !ns.Valid {
    92  		return nil, nil
    93  	}
    94  	return ns.String, nil
    95  }
    96  
    97  // NullInt64 represents an int64 that may be null.
    98  // NullInt64 implements the Scanner interface so
    99  // it can be used as a scan destination, similar to NullString.
   100  type NullInt64 struct {
   101  	Int64 int64
   102  	Valid bool // Valid is true if Int64 is not NULL
   103  }
   104  
   105  // Scan implements the Scanner interface.
   106  func (n *NullInt64) Scan(value interface{}) error {
   107  	if value == nil {
   108  		n.Int64, n.Valid = 0, false
   109  		return nil
   110  	}
   111  	n.Valid = true
   112  	return convertAssign(&n.Int64, value)
   113  }
   114  
   115  // Value implements the driver Valuer interface.
   116  func (n NullInt64) Value() (driver.Value, error) {
   117  	if !n.Valid {
   118  		return nil, nil
   119  	}
   120  	return n.Int64, nil
   121  }
   122  
   123  // NullFloat64 represents a float64 that may be null.
   124  // NullFloat64 implements the Scanner interface so
   125  // it can be used as a scan destination, similar to NullString.
   126  type NullFloat64 struct {
   127  	Float64 float64
   128  	Valid   bool // Valid is true if Float64 is not NULL
   129  }
   130  
   131  // Scan implements the Scanner interface.
   132  func (n *NullFloat64) Scan(value interface{}) error {
   133  	if value == nil {
   134  		n.Float64, n.Valid = 0, false
   135  		return nil
   136  	}
   137  	n.Valid = true
   138  	return convertAssign(&n.Float64, value)
   139  }
   140  
   141  // Value implements the driver Valuer interface.
   142  func (n NullFloat64) Value() (driver.Value, error) {
   143  	if !n.Valid {
   144  		return nil, nil
   145  	}
   146  	return n.Float64, nil
   147  }
   148  
   149  // NullBool represents a bool that may be null.
   150  // NullBool implements the Scanner interface so
   151  // it can be used as a scan destination, similar to NullString.
   152  type NullBool struct {
   153  	Bool  bool
   154  	Valid bool // Valid is true if Bool is not NULL
   155  }
   156  
   157  // Scan implements the Scanner interface.
   158  func (n *NullBool) Scan(value interface{}) error {
   159  	if value == nil {
   160  		n.Bool, n.Valid = false, false
   161  		return nil
   162  	}
   163  	n.Valid = true
   164  	return convertAssign(&n.Bool, value)
   165  }
   166  
   167  // Value implements the driver Valuer interface.
   168  func (n NullBool) Value() (driver.Value, error) {
   169  	if !n.Valid {
   170  		return nil, nil
   171  	}
   172  	return n.Bool, nil
   173  }
   174  
   175  // Scanner is an interface used by Scan.
   176  type Scanner interface {
   177  	// Scan assigns a value from a database driver.
   178  	//
   179  	// The src value will be of one of the following restricted
   180  	// set of types:
   181  	//
   182  	//    int64
   183  	//    float64
   184  	//    bool
   185  	//    []byte
   186  	//    string
   187  	//    time.Time
   188  	//    nil - for NULL values
   189  	//
   190  	// An error should be returned if the value can not be stored
   191  	// without loss of information.
   192  	Scan(src interface{}) error
   193  }
   194  
   195  // ErrNoRows is returned by Scan when QueryRow doesn't return a
   196  // row. In such a case, QueryRow returns a placeholder *Row value that
   197  // defers this error until a Scan.
   198  var ErrNoRows = errors.New("sql: no rows in result set")
   199  
   200  // DB is a database handle representing a pool of zero or more
   201  // underlying connections. It's safe for concurrent use by multiple
   202  // goroutines.
   203  //
   204  // The sql package creates and frees connections automatically; it
   205  // also maintains a free pool of idle connections. If the database has
   206  // a concept of per-connection state, such state can only be reliably
   207  // observed within a transaction. Once DB.Begin is called, the
   208  // returned Tx is bound to a single connection. Once Commit or
   209  // Rollback is called on the transaction, that transaction's
   210  // connection is returned to DB's idle connection pool. The pool size
   211  // can be controlled with SetMaxIdleConns.
   212  type DB struct {
   213  	driver driver.Driver
   214  	dsn    string
   215  	// numClosed is an atomic counter which represents a total number of
   216  	// closed connections. Stmt.openStmt checks it before cleaning closed
   217  	// connections in Stmt.css.
   218  	numClosed uint64
   219  
   220  	mu           sync.Mutex // protects following fields
   221  	freeConn     []*driverConn
   222  	connRequests []chan connRequest
   223  	numOpen      int
   224  	pendingOpens int
   225  	// Used to signal the need for new connections
   226  	// a goroutine running connectionOpener() reads on this chan and
   227  	// maybeOpenNewConnections sends on the chan (one send per needed connection)
   228  	// It is closed during db.Close(). The close tells the connectionOpener
   229  	// goroutine to exit.
   230  	openerCh chan struct{}
   231  	closed   bool
   232  	dep      map[finalCloser]depSet
   233  	lastPut  map[*driverConn]string // stacktrace of last conn's put; debug only
   234  	maxIdle  int                    // zero means defaultMaxIdleConns; negative means 0
   235  	maxOpen  int                    // <= 0 means unlimited
   236  }
   237  
   238  // connReuseStrategy determines how (*DB).conn returns database connections.
   239  type connReuseStrategy uint8
   240  
   241  const (
   242  	// alwaysNewConn forces a new connection to the database.
   243  	alwaysNewConn connReuseStrategy = iota
   244  	// cachedOrNewConn returns a cached connection, if available, else waits
   245  	// for one to become available (if MaxOpenConns has been reached) or
   246  	// creates a new database connection.
   247  	cachedOrNewConn
   248  )
   249  
   250  // driverConn wraps a driver.Conn with a mutex, to
   251  // be held during all calls into the Conn. (including any calls onto
   252  // interfaces returned via that Conn, such as calls on Tx, Stmt,
   253  // Result, Rows)
   254  type driverConn struct {
   255  	db *DB
   256  
   257  	sync.Mutex  // guards following
   258  	ci          driver.Conn
   259  	closed      bool
   260  	finalClosed bool // ci.Close has been called
   261  	openStmt    map[driver.Stmt]bool
   262  
   263  	// guarded by db.mu
   264  	inUse      bool
   265  	onPut      []func() // code (with db.mu held) run when conn is next returned
   266  	dbmuClosed bool     // same as closed, but guarded by db.mu, for removeClosedStmtLocked
   267  }
   268  
   269  func (dc *driverConn) releaseConn(err error) {
   270  	dc.db.putConn(dc, err)
   271  }
   272  
   273  func (dc *driverConn) removeOpenStmt(si driver.Stmt) {
   274  	dc.Lock()
   275  	defer dc.Unlock()
   276  	delete(dc.openStmt, si)
   277  }
   278  
   279  func (dc *driverConn) prepareLocked(query string) (driver.Stmt, error) {
   280  	si, err := dc.ci.Prepare(query)
   281  	if err == nil {
   282  		// Track each driverConn's open statements, so we can close them
   283  		// before closing the conn.
   284  		//
   285  		// TODO(bradfitz): let drivers opt out of caring about
   286  		// stmt closes if the conn is about to close anyway? For now
   287  		// do the safe thing, in case stmts need to be closed.
   288  		//
   289  		// TODO(bradfitz): after Go 1.2, closing driver.Stmts
   290  		// should be moved to driverStmt, using unique
   291  		// *driverStmts everywhere (including from
   292  		// *Stmt.connStmt, instead of returning a
   293  		// driver.Stmt), using driverStmt as a pointer
   294  		// everywhere, and making it a finalCloser.
   295  		if dc.openStmt == nil {
   296  			dc.openStmt = make(map[driver.Stmt]bool)
   297  		}
   298  		dc.openStmt[si] = true
   299  	}
   300  	return si, err
   301  }
   302  
   303  // the dc.db's Mutex is held.
   304  func (dc *driverConn) closeDBLocked() func() error {
   305  	dc.Lock()
   306  	defer dc.Unlock()
   307  	if dc.closed {
   308  		return func() error { return errors.New("sql: duplicate driverConn close") }
   309  	}
   310  	dc.closed = true
   311  	return dc.db.removeDepLocked(dc, dc)
   312  }
   313  
   314  func (dc *driverConn) Close() error {
   315  	dc.Lock()
   316  	if dc.closed {
   317  		dc.Unlock()
   318  		return errors.New("sql: duplicate driverConn close")
   319  	}
   320  	dc.closed = true
   321  	dc.Unlock() // not defer; removeDep finalClose calls may need to lock
   322  
   323  	// And now updates that require holding dc.mu.Lock.
   324  	dc.db.mu.Lock()
   325  	dc.dbmuClosed = true
   326  	fn := dc.db.removeDepLocked(dc, dc)
   327  	dc.db.mu.Unlock()
   328  	return fn()
   329  }
   330  
   331  func (dc *driverConn) finalClose() error {
   332  	dc.Lock()
   333  
   334  	for si := range dc.openStmt {
   335  		si.Close()
   336  	}
   337  	dc.openStmt = nil
   338  
   339  	err := dc.ci.Close()
   340  	dc.ci = nil
   341  	dc.finalClosed = true
   342  	dc.Unlock()
   343  
   344  	dc.db.mu.Lock()
   345  	dc.db.numOpen--
   346  	dc.db.maybeOpenNewConnections()
   347  	dc.db.mu.Unlock()
   348  
   349  	atomic.AddUint64(&dc.db.numClosed, 1)
   350  	return err
   351  }
   352  
   353  // driverStmt associates a driver.Stmt with the
   354  // *driverConn from which it came, so the driverConn's lock can be
   355  // held during calls.
   356  type driverStmt struct {
   357  	sync.Locker // the *driverConn
   358  	si          driver.Stmt
   359  }
   360  
   361  func (ds *driverStmt) Close() error {
   362  	ds.Lock()
   363  	defer ds.Unlock()
   364  	return ds.si.Close()
   365  }
   366  
   367  // depSet is a finalCloser's outstanding dependencies
   368  type depSet map[interface{}]bool // set of true bools
   369  
   370  // The finalCloser interface is used by (*DB).addDep and related
   371  // dependency reference counting.
   372  type finalCloser interface {
   373  	// finalClose is called when the reference count of an object
   374  	// goes to zero. (*DB).mu is not held while calling it.
   375  	finalClose() error
   376  }
   377  
   378  // addDep notes that x now depends on dep, and x's finalClose won't be
   379  // called until all of x's dependencies are removed with removeDep.
   380  func (db *DB) addDep(x finalCloser, dep interface{}) {
   381  	//println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep))
   382  	db.mu.Lock()
   383  	defer db.mu.Unlock()
   384  	db.addDepLocked(x, dep)
   385  }
   386  
   387  func (db *DB) addDepLocked(x finalCloser, dep interface{}) {
   388  	if db.dep == nil {
   389  		db.dep = make(map[finalCloser]depSet)
   390  	}
   391  	xdep := db.dep[x]
   392  	if xdep == nil {
   393  		xdep = make(depSet)
   394  		db.dep[x] = xdep
   395  	}
   396  	xdep[dep] = true
   397  }
   398  
   399  // removeDep notes that x no longer depends on dep.
   400  // If x still has dependencies, nil is returned.
   401  // If x no longer has any dependencies, its finalClose method will be
   402  // called and its error value will be returned.
   403  func (db *DB) removeDep(x finalCloser, dep interface{}) error {
   404  	db.mu.Lock()
   405  	fn := db.removeDepLocked(x, dep)
   406  	db.mu.Unlock()
   407  	return fn()
   408  }
   409  
   410  func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error {
   411  	//println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep))
   412  
   413  	xdep, ok := db.dep[x]
   414  	if !ok {
   415  		panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
   416  	}
   417  
   418  	l0 := len(xdep)
   419  	delete(xdep, dep)
   420  
   421  	switch len(xdep) {
   422  	case l0:
   423  		// Nothing removed. Shouldn't happen.
   424  		panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
   425  	case 0:
   426  		// No more dependencies.
   427  		delete(db.dep, x)
   428  		return x.finalClose
   429  	default:
   430  		// Dependencies remain.
   431  		return func() error { return nil }
   432  	}
   433  }
   434  
   435  // This is the size of the connectionOpener request chan (dn.openerCh).
   436  // This value should be larger than the maximum typical value
   437  // used for db.maxOpen. If maxOpen is significantly larger than
   438  // connectionRequestQueueSize then it is possible for ALL calls into the *DB
   439  // to block until the connectionOpener can satisfy the backlog of requests.
   440  var connectionRequestQueueSize = 1000000
   441  
   442  // Open opens a database specified by its database driver name and a
   443  // driver-specific data source name, usually consisting of at least a
   444  // database name and connection information.
   445  //
   446  // Most users will open a database via a driver-specific connection
   447  // helper function that returns a *DB. No database drivers are included
   448  // in the Go standard library. See http://golang.org/s/sqldrivers for
   449  // a list of third-party drivers.
   450  //
   451  // Open may just validate its arguments without creating a connection
   452  // to the database. To verify that the data source name is valid, call
   453  // Ping.
   454  //
   455  // The returned DB is safe for concurrent use by multiple goroutines
   456  // and maintains its own pool of idle connections. Thus, the Open
   457  // function should be called just once. It is rarely necessary to
   458  // close a DB.
   459  func Open(driverName, dataSourceName string) (*DB, error) {
   460  	driveri, ok := drivers[driverName]
   461  	if !ok {
   462  		return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
   463  	}
   464  	db := &DB{
   465  		driver:   driveri,
   466  		dsn:      dataSourceName,
   467  		openerCh: make(chan struct{}, connectionRequestQueueSize),
   468  		lastPut:  make(map[*driverConn]string),
   469  	}
   470  	go db.connectionOpener()
   471  	return db, nil
   472  }
   473  
   474  // Ping verifies a connection to the database is still alive,
   475  // establishing a connection if necessary.
   476  func (db *DB) Ping() error {
   477  	// TODO(bradfitz): give drivers an optional hook to implement
   478  	// this in a more efficient or more reliable way, if they
   479  	// have one.
   480  	dc, err := db.conn(cachedOrNewConn)
   481  	if err != nil {
   482  		return err
   483  	}
   484  	db.putConn(dc, nil)
   485  	return nil
   486  }
   487  
   488  // Close closes the database, releasing any open resources.
   489  //
   490  // It is rare to Close a DB, as the DB handle is meant to be
   491  // long-lived and shared between many goroutines.
   492  func (db *DB) Close() error {
   493  	db.mu.Lock()
   494  	if db.closed { // Make DB.Close idempotent
   495  		db.mu.Unlock()
   496  		return nil
   497  	}
   498  	close(db.openerCh)
   499  	var err error
   500  	fns := make([]func() error, 0, len(db.freeConn))
   501  	for _, dc := range db.freeConn {
   502  		fns = append(fns, dc.closeDBLocked())
   503  	}
   504  	db.freeConn = nil
   505  	db.closed = true
   506  	for _, req := range db.connRequests {
   507  		close(req)
   508  	}
   509  	db.mu.Unlock()
   510  	for _, fn := range fns {
   511  		err1 := fn()
   512  		if err1 != nil {
   513  			err = err1
   514  		}
   515  	}
   516  	return err
   517  }
   518  
   519  const defaultMaxIdleConns = 2
   520  
   521  func (db *DB) maxIdleConnsLocked() int {
   522  	n := db.maxIdle
   523  	switch {
   524  	case n == 0:
   525  		// TODO(bradfitz): ask driver, if supported, for its default preference
   526  		return defaultMaxIdleConns
   527  	case n < 0:
   528  		return 0
   529  	default:
   530  		return n
   531  	}
   532  }
   533  
   534  // SetMaxIdleConns sets the maximum number of connections in the idle
   535  // connection pool.
   536  //
   537  // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
   538  // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
   539  //
   540  // If n <= 0, no idle connections are retained.
   541  func (db *DB) SetMaxIdleConns(n int) {
   542  	db.mu.Lock()
   543  	if n > 0 {
   544  		db.maxIdle = n
   545  	} else {
   546  		// No idle connections.
   547  		db.maxIdle = -1
   548  	}
   549  	// Make sure maxIdle doesn't exceed maxOpen
   550  	if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
   551  		db.maxIdle = db.maxOpen
   552  	}
   553  	var closing []*driverConn
   554  	idleCount := len(db.freeConn)
   555  	maxIdle := db.maxIdleConnsLocked()
   556  	if idleCount > maxIdle {
   557  		closing = db.freeConn[maxIdle:]
   558  		db.freeConn = db.freeConn[:maxIdle]
   559  	}
   560  	db.mu.Unlock()
   561  	for _, c := range closing {
   562  		c.Close()
   563  	}
   564  }
   565  
   566  // SetMaxOpenConns sets the maximum number of open connections to the database.
   567  //
   568  // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
   569  // MaxIdleConns, then MaxIdleConns will be reduced to match the new
   570  // MaxOpenConns limit
   571  //
   572  // If n <= 0, then there is no limit on the number of open connections.
   573  // The default is 0 (unlimited).
   574  func (db *DB) SetMaxOpenConns(n int) {
   575  	db.mu.Lock()
   576  	db.maxOpen = n
   577  	if n < 0 {
   578  		db.maxOpen = 0
   579  	}
   580  	syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
   581  	db.mu.Unlock()
   582  	if syncMaxIdle {
   583  		db.SetMaxIdleConns(n)
   584  	}
   585  }
   586  
   587  // DBStats contains database statistics.
   588  type DBStats struct {
   589  	// OpenConnections is the number of open connections to the database.
   590  	OpenConnections int
   591  }
   592  
   593  // Stats returns database statistics.
   594  func (db *DB) Stats() DBStats {
   595  	db.mu.Lock()
   596  	stats := DBStats{
   597  		OpenConnections: db.numOpen,
   598  	}
   599  	db.mu.Unlock()
   600  	return stats
   601  }
   602  
   603  // Assumes db.mu is locked.
   604  // If there are connRequests and the connection limit hasn't been reached,
   605  // then tell the connectionOpener to open new connections.
   606  func (db *DB) maybeOpenNewConnections() {
   607  	numRequests := len(db.connRequests) - db.pendingOpens
   608  	if db.maxOpen > 0 {
   609  		numCanOpen := db.maxOpen - (db.numOpen + db.pendingOpens)
   610  		if numRequests > numCanOpen {
   611  			numRequests = numCanOpen
   612  		}
   613  	}
   614  	for numRequests > 0 {
   615  		db.pendingOpens++
   616  		numRequests--
   617  		db.openerCh <- struct{}{}
   618  	}
   619  }
   620  
   621  // Runs in a separate goroutine, opens new connections when requested.
   622  func (db *DB) connectionOpener() {
   623  	for range db.openerCh {
   624  		db.openNewConnection()
   625  	}
   626  }
   627  
   628  // Open one new connection
   629  func (db *DB) openNewConnection() {
   630  	ci, err := db.driver.Open(db.dsn)
   631  	db.mu.Lock()
   632  	defer db.mu.Unlock()
   633  	if db.closed {
   634  		if err == nil {
   635  			ci.Close()
   636  		}
   637  		return
   638  	}
   639  	db.pendingOpens--
   640  	if err != nil {
   641  		db.putConnDBLocked(nil, err)
   642  		return
   643  	}
   644  	dc := &driverConn{
   645  		db: db,
   646  		ci: ci,
   647  	}
   648  	if db.putConnDBLocked(dc, err) {
   649  		db.addDepLocked(dc, dc)
   650  		db.numOpen++
   651  	} else {
   652  		ci.Close()
   653  	}
   654  }
   655  
   656  // connRequest represents one request for a new connection
   657  // When there are no idle connections available, DB.conn will create
   658  // a new connRequest and put it on the db.connRequests list.
   659  type connRequest struct {
   660  	conn *driverConn
   661  	err  error
   662  }
   663  
   664  var errDBClosed = errors.New("sql: database is closed")
   665  
   666  // conn returns a newly-opened or cached *driverConn.
   667  func (db *DB) conn(strategy connReuseStrategy) (*driverConn, error) {
   668  	db.mu.Lock()
   669  	if db.closed {
   670  		db.mu.Unlock()
   671  		return nil, errDBClosed
   672  	}
   673  
   674  	// Prefer a free connection, if possible.
   675  	numFree := len(db.freeConn)
   676  	if strategy == cachedOrNewConn && numFree > 0 {
   677  		conn := db.freeConn[0]
   678  		copy(db.freeConn, db.freeConn[1:])
   679  		db.freeConn = db.freeConn[:numFree-1]
   680  		conn.inUse = true
   681  		db.mu.Unlock()
   682  		return conn, nil
   683  	}
   684  
   685  	// Out of free connections or we were asked not to use one.  If we're not
   686  	// allowed to open any more connections, make a request and wait.
   687  	if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
   688  		// Make the connRequest channel. It's buffered so that the
   689  		// connectionOpener doesn't block while waiting for the req to be read.
   690  		req := make(chan connRequest, 1)
   691  		db.connRequests = append(db.connRequests, req)
   692  		db.mu.Unlock()
   693  		ret := <-req
   694  		return ret.conn, ret.err
   695  	}
   696  
   697  	db.numOpen++ // optimistically
   698  	db.mu.Unlock()
   699  	ci, err := db.driver.Open(db.dsn)
   700  	if err != nil {
   701  		db.mu.Lock()
   702  		db.numOpen-- // correct for earlier optimism
   703  		db.mu.Unlock()
   704  		return nil, err
   705  	}
   706  	db.mu.Lock()
   707  	dc := &driverConn{
   708  		db: db,
   709  		ci: ci,
   710  	}
   711  	db.addDepLocked(dc, dc)
   712  	dc.inUse = true
   713  	db.mu.Unlock()
   714  	return dc, nil
   715  }
   716  
   717  var (
   718  	errConnClosed = errors.New("database/sql: internal sentinel error: conn is closed")
   719  	errConnBusy   = errors.New("database/sql: internal sentinel error: conn is busy")
   720  )
   721  
   722  // putConnHook is a hook for testing.
   723  var putConnHook func(*DB, *driverConn)
   724  
   725  // noteUnusedDriverStatement notes that si is no longer used and should
   726  // be closed whenever possible (when c is next not in use), unless c is
   727  // already closed.
   728  func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) {
   729  	db.mu.Lock()
   730  	defer db.mu.Unlock()
   731  	if c.inUse {
   732  		c.onPut = append(c.onPut, func() {
   733  			si.Close()
   734  		})
   735  	} else {
   736  		c.Lock()
   737  		defer c.Unlock()
   738  		if !c.finalClosed {
   739  			si.Close()
   740  		}
   741  	}
   742  }
   743  
   744  // debugGetPut determines whether getConn & putConn calls' stack traces
   745  // are returned for more verbose crashes.
   746  const debugGetPut = false
   747  
   748  // putConn adds a connection to the db's free pool.
   749  // err is optionally the last error that occurred on this connection.
   750  func (db *DB) putConn(dc *driverConn, err error) {
   751  	db.mu.Lock()
   752  	if !dc.inUse {
   753  		if debugGetPut {
   754  			fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
   755  		}
   756  		panic("sql: connection returned that was never out")
   757  	}
   758  	if debugGetPut {
   759  		db.lastPut[dc] = stack()
   760  	}
   761  	dc.inUse = false
   762  
   763  	for _, fn := range dc.onPut {
   764  		fn()
   765  	}
   766  	dc.onPut = nil
   767  
   768  	if err == driver.ErrBadConn {
   769  		// Don't reuse bad connections.
   770  		// Since the conn is considered bad and is being discarded, treat it
   771  		// as closed. Don't decrement the open count here, finalClose will
   772  		// take care of that.
   773  		db.maybeOpenNewConnections()
   774  		db.mu.Unlock()
   775  		dc.Close()
   776  		return
   777  	}
   778  	if putConnHook != nil {
   779  		putConnHook(db, dc)
   780  	}
   781  	added := db.putConnDBLocked(dc, nil)
   782  	db.mu.Unlock()
   783  
   784  	if !added {
   785  		dc.Close()
   786  	}
   787  }
   788  
   789  // Satisfy a connRequest or put the driverConn in the idle pool and return true
   790  // or return false.
   791  // putConnDBLocked will satisfy a connRequest if there is one, or it will
   792  // return the *driverConn to the freeConn list if err == nil and the idle
   793  // connection limit will not be exceeded.
   794  // If err != nil, the value of dc is ignored.
   795  // If err == nil, then dc must not equal nil.
   796  // If a connRequest was fulfilled or the *driverConn was placed in the
   797  // freeConn list, then true is returned, otherwise false is returned.
   798  func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
   799  	if db.maxOpen > 0 && db.numOpen > db.maxOpen {
   800  		return false
   801  	}
   802  	if c := len(db.connRequests); c > 0 {
   803  		req := db.connRequests[0]
   804  		// This copy is O(n) but in practice faster than a linked list.
   805  		// TODO: consider compacting it down less often and
   806  		// moving the base instead?
   807  		copy(db.connRequests, db.connRequests[1:])
   808  		db.connRequests = db.connRequests[:c-1]
   809  		if err == nil {
   810  			dc.inUse = true
   811  		}
   812  		req <- connRequest{
   813  			conn: dc,
   814  			err:  err,
   815  		}
   816  		return true
   817  	} else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
   818  		db.freeConn = append(db.freeConn, dc)
   819  		return true
   820  	}
   821  	return false
   822  }
   823  
   824  // maxBadConnRetries is the number of maximum retries if the driver returns
   825  // driver.ErrBadConn to signal a broken connection before forcing a new
   826  // connection to be opened.
   827  const maxBadConnRetries = 2
   828  
   829  // Prepare creates a prepared statement for later queries or executions.
   830  // Multiple queries or executions may be run concurrently from the
   831  // returned statement.
   832  func (db *DB) Prepare(query string) (*Stmt, error) {
   833  	var stmt *Stmt
   834  	var err error
   835  	for i := 0; i < maxBadConnRetries; i++ {
   836  		stmt, err = db.prepare(query, cachedOrNewConn)
   837  		if err != driver.ErrBadConn {
   838  			break
   839  		}
   840  	}
   841  	if err == driver.ErrBadConn {
   842  		return db.prepare(query, alwaysNewConn)
   843  	}
   844  	return stmt, err
   845  }
   846  
   847  func (db *DB) prepare(query string, strategy connReuseStrategy) (*Stmt, error) {
   848  	// TODO: check if db.driver supports an optional
   849  	// driver.Preparer interface and call that instead, if so,
   850  	// otherwise we make a prepared statement that's bound
   851  	// to a connection, and to execute this prepared statement
   852  	// we either need to use this connection (if it's free), else
   853  	// get a new connection + re-prepare + execute on that one.
   854  	dc, err := db.conn(strategy)
   855  	if err != nil {
   856  		return nil, err
   857  	}
   858  	dc.Lock()
   859  	si, err := dc.prepareLocked(query)
   860  	dc.Unlock()
   861  	if err != nil {
   862  		db.putConn(dc, err)
   863  		return nil, err
   864  	}
   865  	stmt := &Stmt{
   866  		db:            db,
   867  		query:         query,
   868  		css:           []connStmt{{dc, si}},
   869  		lastNumClosed: atomic.LoadUint64(&db.numClosed),
   870  	}
   871  	db.addDep(stmt, stmt)
   872  	db.putConn(dc, nil)
   873  	return stmt, nil
   874  }
   875  
   876  // Exec executes a query without returning any rows.
   877  // The args are for any placeholder parameters in the query.
   878  func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
   879  	var res Result
   880  	var err error
   881  	for i := 0; i < maxBadConnRetries; i++ {
   882  		res, err = db.exec(query, args, cachedOrNewConn)
   883  		if err != driver.ErrBadConn {
   884  			break
   885  		}
   886  	}
   887  	if err == driver.ErrBadConn {
   888  		return db.exec(query, args, alwaysNewConn)
   889  	}
   890  	return res, err
   891  }
   892  
   893  func (db *DB) exec(query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) {
   894  	dc, err := db.conn(strategy)
   895  	if err != nil {
   896  		return nil, err
   897  	}
   898  	defer func() {
   899  		db.putConn(dc, err)
   900  	}()
   901  
   902  	if execer, ok := dc.ci.(driver.Execer); ok {
   903  		dargs, err := driverArgs(nil, args)
   904  		if err != nil {
   905  			return nil, err
   906  		}
   907  		dc.Lock()
   908  		resi, err := execer.Exec(query, dargs)
   909  		dc.Unlock()
   910  		if err != driver.ErrSkip {
   911  			if err != nil {
   912  				return nil, err
   913  			}
   914  			return driverResult{dc, resi}, nil
   915  		}
   916  	}
   917  
   918  	dc.Lock()
   919  	si, err := dc.ci.Prepare(query)
   920  	dc.Unlock()
   921  	if err != nil {
   922  		return nil, err
   923  	}
   924  	defer withLock(dc, func() { si.Close() })
   925  	return resultFromStatement(driverStmt{dc, si}, args...)
   926  }
   927  
   928  // Query executes a query that returns rows, typically a SELECT.
   929  // The args are for any placeholder parameters in the query.
   930  func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
   931  	var rows *Rows
   932  	var err error
   933  	for i := 0; i < maxBadConnRetries; i++ {
   934  		rows, err = db.query(query, args, cachedOrNewConn)
   935  		if err != driver.ErrBadConn {
   936  			break
   937  		}
   938  	}
   939  	if err == driver.ErrBadConn {
   940  		return db.query(query, args, alwaysNewConn)
   941  	}
   942  	return rows, err
   943  }
   944  
   945  func (db *DB) query(query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
   946  	ci, err := db.conn(strategy)
   947  	if err != nil {
   948  		return nil, err
   949  	}
   950  
   951  	return db.queryConn(ci, ci.releaseConn, query, args)
   952  }
   953  
   954  // queryConn executes a query on the given connection.
   955  // The connection gets released by the releaseConn function.
   956  func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
   957  	if queryer, ok := dc.ci.(driver.Queryer); ok {
   958  		dargs, err := driverArgs(nil, args)
   959  		if err != nil {
   960  			releaseConn(err)
   961  			return nil, err
   962  		}
   963  		dc.Lock()
   964  		rowsi, err := queryer.Query(query, dargs)
   965  		dc.Unlock()
   966  		if err != driver.ErrSkip {
   967  			if err != nil {
   968  				releaseConn(err)
   969  				return nil, err
   970  			}
   971  			// Note: ownership of dc passes to the *Rows, to be freed
   972  			// with releaseConn.
   973  			rows := &Rows{
   974  				dc:          dc,
   975  				releaseConn: releaseConn,
   976  				rowsi:       rowsi,
   977  			}
   978  			return rows, nil
   979  		}
   980  	}
   981  
   982  	dc.Lock()
   983  	si, err := dc.ci.Prepare(query)
   984  	dc.Unlock()
   985  	if err != nil {
   986  		releaseConn(err)
   987  		return nil, err
   988  	}
   989  
   990  	ds := driverStmt{dc, si}
   991  	rowsi, err := rowsiFromStatement(ds, args...)
   992  	if err != nil {
   993  		dc.Lock()
   994  		si.Close()
   995  		dc.Unlock()
   996  		releaseConn(err)
   997  		return nil, err
   998  	}
   999  
  1000  	// Note: ownership of ci passes to the *Rows, to be freed
  1001  	// with releaseConn.
  1002  	rows := &Rows{
  1003  		dc:          dc,
  1004  		releaseConn: releaseConn,
  1005  		rowsi:       rowsi,
  1006  		closeStmt:   si,
  1007  	}
  1008  	return rows, nil
  1009  }
  1010  
  1011  // QueryRow executes a query that is expected to return at most one row.
  1012  // QueryRow always return a non-nil value. Errors are deferred until
  1013  // Row's Scan method is called.
  1014  func (db *DB) QueryRow(query string, args ...interface{}) *Row {
  1015  	rows, err := db.Query(query, args...)
  1016  	return &Row{rows: rows, err: err}
  1017  }
  1018  
  1019  // Begin starts a transaction. The isolation level is dependent on
  1020  // the driver.
  1021  func (db *DB) Begin() (*Tx, error) {
  1022  	var tx *Tx
  1023  	var err error
  1024  	for i := 0; i < maxBadConnRetries; i++ {
  1025  		tx, err = db.begin(cachedOrNewConn)
  1026  		if err != driver.ErrBadConn {
  1027  			break
  1028  		}
  1029  	}
  1030  	if err == driver.ErrBadConn {
  1031  		return db.begin(alwaysNewConn)
  1032  	}
  1033  	return tx, err
  1034  }
  1035  
  1036  func (db *DB) begin(strategy connReuseStrategy) (tx *Tx, err error) {
  1037  	dc, err := db.conn(strategy)
  1038  	if err != nil {
  1039  		return nil, err
  1040  	}
  1041  	dc.Lock()
  1042  	txi, err := dc.ci.Begin()
  1043  	dc.Unlock()
  1044  	if err != nil {
  1045  		db.putConn(dc, err)
  1046  		return nil, err
  1047  	}
  1048  	return &Tx{
  1049  		db:  db,
  1050  		dc:  dc,
  1051  		txi: txi,
  1052  	}, nil
  1053  }
  1054  
  1055  // Driver returns the database's underlying driver.
  1056  func (db *DB) Driver() driver.Driver {
  1057  	return db.driver
  1058  }
  1059  
  1060  // Tx is an in-progress database transaction.
  1061  //
  1062  // A transaction must end with a call to Commit or Rollback.
  1063  //
  1064  // After a call to Commit or Rollback, all operations on the
  1065  // transaction fail with ErrTxDone.
  1066  type Tx struct {
  1067  	db *DB
  1068  
  1069  	// dc is owned exclusively until Commit or Rollback, at which point
  1070  	// it's returned with putConn.
  1071  	dc  *driverConn
  1072  	txi driver.Tx
  1073  
  1074  	// done transitions from false to true exactly once, on Commit
  1075  	// or Rollback. once done, all operations fail with
  1076  	// ErrTxDone.
  1077  	done bool
  1078  
  1079  	// All Stmts prepared for this transaction.  These will be closed after the
  1080  	// transaction has been committed or rolled back.
  1081  	stmts struct {
  1082  		sync.Mutex
  1083  		v []*Stmt
  1084  	}
  1085  }
  1086  
  1087  var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
  1088  
  1089  func (tx *Tx) close() {
  1090  	if tx.done {
  1091  		panic("double close") // internal error
  1092  	}
  1093  	tx.done = true
  1094  	tx.db.putConn(tx.dc, nil)
  1095  	tx.dc = nil
  1096  	tx.txi = nil
  1097  }
  1098  
  1099  func (tx *Tx) grabConn() (*driverConn, error) {
  1100  	if tx.done {
  1101  		return nil, ErrTxDone
  1102  	}
  1103  	return tx.dc, nil
  1104  }
  1105  
  1106  // Closes all Stmts prepared for this transaction.
  1107  func (tx *Tx) closePrepared() {
  1108  	tx.stmts.Lock()
  1109  	for _, stmt := range tx.stmts.v {
  1110  		stmt.Close()
  1111  	}
  1112  	tx.stmts.Unlock()
  1113  }
  1114  
  1115  // Commit commits the transaction.
  1116  func (tx *Tx) Commit() error {
  1117  	if tx.done {
  1118  		return ErrTxDone
  1119  	}
  1120  	defer tx.close()
  1121  	tx.dc.Lock()
  1122  	err := tx.txi.Commit()
  1123  	tx.dc.Unlock()
  1124  	if err != driver.ErrBadConn {
  1125  		tx.closePrepared()
  1126  	}
  1127  	return err
  1128  }
  1129  
  1130  // Rollback aborts the transaction.
  1131  func (tx *Tx) Rollback() error {
  1132  	if tx.done {
  1133  		return ErrTxDone
  1134  	}
  1135  	defer tx.close()
  1136  	tx.dc.Lock()
  1137  	err := tx.txi.Rollback()
  1138  	tx.dc.Unlock()
  1139  	if err != driver.ErrBadConn {
  1140  		tx.closePrepared()
  1141  	}
  1142  	return err
  1143  }
  1144  
  1145  // Prepare creates a prepared statement for use within a transaction.
  1146  //
  1147  // The returned statement operates within the transaction and can no longer
  1148  // be used once the transaction has been committed or rolled back.
  1149  //
  1150  // To use an existing prepared statement on this transaction, see Tx.Stmt.
  1151  func (tx *Tx) Prepare(query string) (*Stmt, error) {
  1152  	// TODO(bradfitz): We could be more efficient here and either
  1153  	// provide a method to take an existing Stmt (created on
  1154  	// perhaps a different Conn), and re-create it on this Conn if
  1155  	// necessary. Or, better: keep a map in DB of query string to
  1156  	// Stmts, and have Stmt.Execute do the right thing and
  1157  	// re-prepare if the Conn in use doesn't have that prepared
  1158  	// statement.  But we'll want to avoid caching the statement
  1159  	// in the case where we only call conn.Prepare implicitly
  1160  	// (such as in db.Exec or tx.Exec), but the caller package
  1161  	// can't be holding a reference to the returned statement.
  1162  	// Perhaps just looking at the reference count (by noting
  1163  	// Stmt.Close) would be enough. We might also want a finalizer
  1164  	// on Stmt to drop the reference count.
  1165  	dc, err := tx.grabConn()
  1166  	if err != nil {
  1167  		return nil, err
  1168  	}
  1169  
  1170  	dc.Lock()
  1171  	si, err := dc.ci.Prepare(query)
  1172  	dc.Unlock()
  1173  	if err != nil {
  1174  		return nil, err
  1175  	}
  1176  
  1177  	stmt := &Stmt{
  1178  		db: tx.db,
  1179  		tx: tx,
  1180  		txsi: &driverStmt{
  1181  			Locker: dc,
  1182  			si:     si,
  1183  		},
  1184  		query: query,
  1185  	}
  1186  	tx.stmts.Lock()
  1187  	tx.stmts.v = append(tx.stmts.v, stmt)
  1188  	tx.stmts.Unlock()
  1189  	return stmt, nil
  1190  }
  1191  
  1192  // Stmt returns a transaction-specific prepared statement from
  1193  // an existing statement.
  1194  //
  1195  // Example:
  1196  //  updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  1197  //  ...
  1198  //  tx, err := db.Begin()
  1199  //  ...
  1200  //  res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
  1201  func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  1202  	// TODO(bradfitz): optimize this. Currently this re-prepares
  1203  	// each time.  This is fine for now to illustrate the API but
  1204  	// we should really cache already-prepared statements
  1205  	// per-Conn. See also the big comment in Tx.Prepare.
  1206  
  1207  	if tx.db != stmt.db {
  1208  		return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
  1209  	}
  1210  	dc, err := tx.grabConn()
  1211  	if err != nil {
  1212  		return &Stmt{stickyErr: err}
  1213  	}
  1214  	dc.Lock()
  1215  	si, err := dc.ci.Prepare(stmt.query)
  1216  	dc.Unlock()
  1217  	txs := &Stmt{
  1218  		db: tx.db,
  1219  		tx: tx,
  1220  		txsi: &driverStmt{
  1221  			Locker: dc,
  1222  			si:     si,
  1223  		},
  1224  		query:     stmt.query,
  1225  		stickyErr: err,
  1226  	}
  1227  	tx.stmts.Lock()
  1228  	tx.stmts.v = append(tx.stmts.v, txs)
  1229  	tx.stmts.Unlock()
  1230  	return txs
  1231  }
  1232  
  1233  // Exec executes a query that doesn't return rows.
  1234  // For example: an INSERT and UPDATE.
  1235  func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
  1236  	dc, err := tx.grabConn()
  1237  	if err != nil {
  1238  		return nil, err
  1239  	}
  1240  
  1241  	if execer, ok := dc.ci.(driver.Execer); ok {
  1242  		dargs, err := driverArgs(nil, args)
  1243  		if err != nil {
  1244  			return nil, err
  1245  		}
  1246  		dc.Lock()
  1247  		resi, err := execer.Exec(query, dargs)
  1248  		dc.Unlock()
  1249  		if err == nil {
  1250  			return driverResult{dc, resi}, nil
  1251  		}
  1252  		if err != driver.ErrSkip {
  1253  			return nil, err
  1254  		}
  1255  	}
  1256  
  1257  	dc.Lock()
  1258  	si, err := dc.ci.Prepare(query)
  1259  	dc.Unlock()
  1260  	if err != nil {
  1261  		return nil, err
  1262  	}
  1263  	defer withLock(dc, func() { si.Close() })
  1264  
  1265  	return resultFromStatement(driverStmt{dc, si}, args...)
  1266  }
  1267  
  1268  // Query executes a query that returns rows, typically a SELECT.
  1269  func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
  1270  	dc, err := tx.grabConn()
  1271  	if err != nil {
  1272  		return nil, err
  1273  	}
  1274  	releaseConn := func(error) {}
  1275  	return tx.db.queryConn(dc, releaseConn, query, args)
  1276  }
  1277  
  1278  // QueryRow executes a query that is expected to return at most one row.
  1279  // QueryRow always return a non-nil value. Errors are deferred until
  1280  // Row's Scan method is called.
  1281  func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
  1282  	rows, err := tx.Query(query, args...)
  1283  	return &Row{rows: rows, err: err}
  1284  }
  1285  
  1286  // connStmt is a prepared statement on a particular connection.
  1287  type connStmt struct {
  1288  	dc *driverConn
  1289  	si driver.Stmt
  1290  }
  1291  
  1292  // Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.
  1293  type Stmt struct {
  1294  	// Immutable:
  1295  	db        *DB    // where we came from
  1296  	query     string // that created the Stmt
  1297  	stickyErr error  // if non-nil, this error is returned for all operations
  1298  
  1299  	closemu sync.RWMutex // held exclusively during close, for read otherwise.
  1300  
  1301  	// If in a transaction, else both nil:
  1302  	tx   *Tx
  1303  	txsi *driverStmt
  1304  
  1305  	mu     sync.Mutex // protects the rest of the fields
  1306  	closed bool
  1307  
  1308  	// css is a list of underlying driver statement interfaces
  1309  	// that are valid on particular connections.  This is only
  1310  	// used if tx == nil and one is found that has idle
  1311  	// connections.  If tx != nil, txsi is always used.
  1312  	css []connStmt
  1313  
  1314  	// lastNumClosed is copied from db.numClosed when Stmt is created
  1315  	// without tx and closed connections in css are removed.
  1316  	lastNumClosed uint64
  1317  }
  1318  
  1319  // Exec executes a prepared statement with the given arguments and
  1320  // returns a Result summarizing the effect of the statement.
  1321  func (s *Stmt) Exec(args ...interface{}) (Result, error) {
  1322  	s.closemu.RLock()
  1323  	defer s.closemu.RUnlock()
  1324  
  1325  	var res Result
  1326  	for i := 0; i < maxBadConnRetries; i++ {
  1327  		dc, releaseConn, si, err := s.connStmt()
  1328  		if err != nil {
  1329  			if err == driver.ErrBadConn {
  1330  				continue
  1331  			}
  1332  			return nil, err
  1333  		}
  1334  
  1335  		res, err = resultFromStatement(driverStmt{dc, si}, args...)
  1336  		releaseConn(err)
  1337  		if err != driver.ErrBadConn {
  1338  			return res, err
  1339  		}
  1340  	}
  1341  	return nil, driver.ErrBadConn
  1342  }
  1343  
  1344  func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) {
  1345  	ds.Lock()
  1346  	want := ds.si.NumInput()
  1347  	ds.Unlock()
  1348  
  1349  	// -1 means the driver doesn't know how to count the number of
  1350  	// placeholders, so we won't sanity check input here and instead let the
  1351  	// driver deal with errors.
  1352  	if want != -1 && len(args) != want {
  1353  		return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
  1354  	}
  1355  
  1356  	dargs, err := driverArgs(&ds, args)
  1357  	if err != nil {
  1358  		return nil, err
  1359  	}
  1360  
  1361  	ds.Lock()
  1362  	resi, err := ds.si.Exec(dargs)
  1363  	ds.Unlock()
  1364  	if err != nil {
  1365  		return nil, err
  1366  	}
  1367  	return driverResult{ds.Locker, resi}, nil
  1368  }
  1369  
  1370  // removeClosedStmtLocked removes closed conns in s.css.
  1371  //
  1372  // To avoid lock contention on DB.mu, we do it only when
  1373  // s.db.numClosed - s.lastNum is large enough.
  1374  func (s *Stmt) removeClosedStmtLocked() {
  1375  	t := len(s.css)/2 + 1
  1376  	if t > 10 {
  1377  		t = 10
  1378  	}
  1379  	dbClosed := atomic.LoadUint64(&s.db.numClosed)
  1380  	if dbClosed-s.lastNumClosed < uint64(t) {
  1381  		return
  1382  	}
  1383  
  1384  	s.db.mu.Lock()
  1385  	for i := 0; i < len(s.css); i++ {
  1386  		if s.css[i].dc.dbmuClosed {
  1387  			s.css[i] = s.css[len(s.css)-1]
  1388  			s.css = s.css[:len(s.css)-1]
  1389  			i--
  1390  		}
  1391  	}
  1392  	s.db.mu.Unlock()
  1393  	s.lastNumClosed = dbClosed
  1394  }
  1395  
  1396  // connStmt returns a free driver connection on which to execute the
  1397  // statement, a function to call to release the connection, and a
  1398  // statement bound to that connection.
  1399  func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) {
  1400  	if err = s.stickyErr; err != nil {
  1401  		return
  1402  	}
  1403  	s.mu.Lock()
  1404  	if s.closed {
  1405  		s.mu.Unlock()
  1406  		err = errors.New("sql: statement is closed")
  1407  		return
  1408  	}
  1409  
  1410  	// In a transaction, we always use the connection that the
  1411  	// transaction was created on.
  1412  	if s.tx != nil {
  1413  		s.mu.Unlock()
  1414  		ci, err = s.tx.grabConn() // blocks, waiting for the connection.
  1415  		if err != nil {
  1416  			return
  1417  		}
  1418  		releaseConn = func(error) {}
  1419  		return ci, releaseConn, s.txsi.si, nil
  1420  	}
  1421  
  1422  	s.removeClosedStmtLocked()
  1423  	s.mu.Unlock()
  1424  
  1425  	// TODO(bradfitz): or always wait for one? make configurable later?
  1426  	dc, err := s.db.conn(cachedOrNewConn)
  1427  	if err != nil {
  1428  		return nil, nil, nil, err
  1429  	}
  1430  
  1431  	s.mu.Lock()
  1432  	for _, v := range s.css {
  1433  		if v.dc == dc {
  1434  			s.mu.Unlock()
  1435  			return dc, dc.releaseConn, v.si, nil
  1436  		}
  1437  	}
  1438  	s.mu.Unlock()
  1439  
  1440  	// No luck; we need to prepare the statement on this connection
  1441  	dc.Lock()
  1442  	si, err = dc.prepareLocked(s.query)
  1443  	dc.Unlock()
  1444  	if err != nil {
  1445  		s.db.putConn(dc, err)
  1446  		return nil, nil, nil, err
  1447  	}
  1448  	s.mu.Lock()
  1449  	cs := connStmt{dc, si}
  1450  	s.css = append(s.css, cs)
  1451  	s.mu.Unlock()
  1452  
  1453  	return dc, dc.releaseConn, si, nil
  1454  }
  1455  
  1456  // Query executes a prepared query statement with the given arguments
  1457  // and returns the query results as a *Rows.
  1458  func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
  1459  	s.closemu.RLock()
  1460  	defer s.closemu.RUnlock()
  1461  
  1462  	var rowsi driver.Rows
  1463  	for i := 0; i < maxBadConnRetries; i++ {
  1464  		dc, releaseConn, si, err := s.connStmt()
  1465  		if err != nil {
  1466  			if err == driver.ErrBadConn {
  1467  				continue
  1468  			}
  1469  			return nil, err
  1470  		}
  1471  
  1472  		rowsi, err = rowsiFromStatement(driverStmt{dc, si}, args...)
  1473  		if err == nil {
  1474  			// Note: ownership of ci passes to the *Rows, to be freed
  1475  			// with releaseConn.
  1476  			rows := &Rows{
  1477  				dc:    dc,
  1478  				rowsi: rowsi,
  1479  				// releaseConn set below
  1480  			}
  1481  			s.db.addDep(s, rows)
  1482  			rows.releaseConn = func(err error) {
  1483  				releaseConn(err)
  1484  				s.db.removeDep(s, rows)
  1485  			}
  1486  			return rows, nil
  1487  		}
  1488  
  1489  		releaseConn(err)
  1490  		if err != driver.ErrBadConn {
  1491  			return nil, err
  1492  		}
  1493  	}
  1494  	return nil, driver.ErrBadConn
  1495  }
  1496  
  1497  func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) {
  1498  	ds.Lock()
  1499  	want := ds.si.NumInput()
  1500  	ds.Unlock()
  1501  
  1502  	// -1 means the driver doesn't know how to count the number of
  1503  	// placeholders, so we won't sanity check input here and instead let the
  1504  	// driver deal with errors.
  1505  	if want != -1 && len(args) != want {
  1506  		return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
  1507  	}
  1508  
  1509  	dargs, err := driverArgs(&ds, args)
  1510  	if err != nil {
  1511  		return nil, err
  1512  	}
  1513  
  1514  	ds.Lock()
  1515  	rowsi, err := ds.si.Query(dargs)
  1516  	ds.Unlock()
  1517  	if err != nil {
  1518  		return nil, err
  1519  	}
  1520  	return rowsi, nil
  1521  }
  1522  
  1523  // QueryRow executes a prepared query statement with the given arguments.
  1524  // If an error occurs during the execution of the statement, that error will
  1525  // be returned by a call to Scan on the returned *Row, which is always non-nil.
  1526  // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  1527  // Otherwise, the *Row's Scan scans the first selected row and discards
  1528  // the rest.
  1529  //
  1530  // Example usage:
  1531  //
  1532  //  var name string
  1533  //  err := nameByUseridStmt.QueryRow(id).Scan(&name)
  1534  func (s *Stmt) QueryRow(args ...interface{}) *Row {
  1535  	rows, err := s.Query(args...)
  1536  	if err != nil {
  1537  		return &Row{err: err}
  1538  	}
  1539  	return &Row{rows: rows}
  1540  }
  1541  
  1542  // Close closes the statement.
  1543  func (s *Stmt) Close() error {
  1544  	s.closemu.Lock()
  1545  	defer s.closemu.Unlock()
  1546  
  1547  	if s.stickyErr != nil {
  1548  		return s.stickyErr
  1549  	}
  1550  	s.mu.Lock()
  1551  	if s.closed {
  1552  		s.mu.Unlock()
  1553  		return nil
  1554  	}
  1555  	s.closed = true
  1556  
  1557  	if s.tx != nil {
  1558  		s.txsi.Close()
  1559  		s.mu.Unlock()
  1560  		return nil
  1561  	}
  1562  	s.mu.Unlock()
  1563  
  1564  	return s.db.removeDep(s, s)
  1565  }
  1566  
  1567  func (s *Stmt) finalClose() error {
  1568  	s.mu.Lock()
  1569  	defer s.mu.Unlock()
  1570  	if s.css != nil {
  1571  		for _, v := range s.css {
  1572  			s.db.noteUnusedDriverStatement(v.dc, v.si)
  1573  			v.dc.removeOpenStmt(v.si)
  1574  		}
  1575  		s.css = nil
  1576  	}
  1577  	return nil
  1578  }
  1579  
  1580  // Rows is the result of a query. Its cursor starts before the first row
  1581  // of the result set. Use Next to advance through the rows:
  1582  //
  1583  //     rows, err := db.Query("SELECT ...")
  1584  //     ...
  1585  //     defer rows.Close()
  1586  //     for rows.Next() {
  1587  //         var id int
  1588  //         var name string
  1589  //         err = rows.Scan(&id, &name)
  1590  //         ...
  1591  //     }
  1592  //     err = rows.Err() // get any error encountered during iteration
  1593  //     ...
  1594  type Rows struct {
  1595  	dc          *driverConn // owned; must call releaseConn when closed to release
  1596  	releaseConn func(error)
  1597  	rowsi       driver.Rows
  1598  
  1599  	closed    bool
  1600  	lastcols  []driver.Value
  1601  	lasterr   error       // non-nil only if closed is true
  1602  	closeStmt driver.Stmt // if non-nil, statement to Close on close
  1603  }
  1604  
  1605  // Next prepares the next result row for reading with the Scan method.  It
  1606  // returns true on success, or false if there is no next result row or an error
  1607  // happened while preparing it.  Err should be consulted to distinguish between
  1608  // the two cases.
  1609  //
  1610  // Every call to Scan, even the first one, must be preceded by a call to Next.
  1611  func (rs *Rows) Next() bool {
  1612  	if rs.closed {
  1613  		return false
  1614  	}
  1615  	if rs.lastcols == nil {
  1616  		rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
  1617  	}
  1618  	rs.lasterr = rs.rowsi.Next(rs.lastcols)
  1619  	if rs.lasterr != nil {
  1620  		rs.Close()
  1621  		return false
  1622  	}
  1623  	return true
  1624  }
  1625  
  1626  // Err returns the error, if any, that was encountered during iteration.
  1627  // Err may be called after an explicit or implicit Close.
  1628  func (rs *Rows) Err() error {
  1629  	if rs.lasterr == io.EOF {
  1630  		return nil
  1631  	}
  1632  	return rs.lasterr
  1633  }
  1634  
  1635  // Columns returns the column names.
  1636  // Columns returns an error if the rows are closed, or if the rows
  1637  // are from QueryRow and there was a deferred error.
  1638  func (rs *Rows) Columns() ([]string, error) {
  1639  	if rs.closed {
  1640  		return nil, errors.New("sql: Rows are closed")
  1641  	}
  1642  	if rs.rowsi == nil {
  1643  		return nil, errors.New("sql: no Rows available")
  1644  	}
  1645  	return rs.rowsi.Columns(), nil
  1646  }
  1647  
  1648  // Scan copies the columns in the current row into the values pointed
  1649  // at by dest.
  1650  //
  1651  // If an argument has type *[]byte, Scan saves in that argument a copy
  1652  // of the corresponding data. The copy is owned by the caller and can
  1653  // be modified and held indefinitely. The copy can be avoided by using
  1654  // an argument of type *RawBytes instead; see the documentation for
  1655  // RawBytes for restrictions on its use.
  1656  //
  1657  // If an argument has type *interface{}, Scan copies the value
  1658  // provided by the underlying driver without conversion. If the value
  1659  // is of type []byte, a copy is made and the caller owns the result.
  1660  func (rs *Rows) Scan(dest ...interface{}) error {
  1661  	if rs.closed {
  1662  		return errors.New("sql: Rows are closed")
  1663  	}
  1664  	if rs.lastcols == nil {
  1665  		return errors.New("sql: Scan called without calling Next")
  1666  	}
  1667  	if len(dest) != len(rs.lastcols) {
  1668  		return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
  1669  	}
  1670  	for i, sv := range rs.lastcols {
  1671  		err := convertAssign(dest[i], sv)
  1672  		if err != nil {
  1673  			return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
  1674  		}
  1675  	}
  1676  	return nil
  1677  }
  1678  
  1679  var rowsCloseHook func(*Rows, *error)
  1680  
  1681  // Close closes the Rows, preventing further enumeration. If Next returns
  1682  // false, the Rows are closed automatically and it will suffice to check the
  1683  // result of Err. Close is idempotent and does not affect the result of Err.
  1684  func (rs *Rows) Close() error {
  1685  	if rs.closed {
  1686  		return nil
  1687  	}
  1688  	rs.closed = true
  1689  	err := rs.rowsi.Close()
  1690  	if fn := rowsCloseHook; fn != nil {
  1691  		fn(rs, &err)
  1692  	}
  1693  	if rs.closeStmt != nil {
  1694  		rs.closeStmt.Close()
  1695  	}
  1696  	rs.releaseConn(err)
  1697  	return err
  1698  }
  1699  
  1700  // Row is the result of calling QueryRow to select a single row.
  1701  type Row struct {
  1702  	// One of these two will be non-nil:
  1703  	err  error // deferred error for easy chaining
  1704  	rows *Rows
  1705  }
  1706  
  1707  // Scan copies the columns from the matched row into the values
  1708  // pointed at by dest.  If more than one row matches the query,
  1709  // Scan uses the first row and discards the rest.  If no row matches
  1710  // the query, Scan returns ErrNoRows.
  1711  func (r *Row) Scan(dest ...interface{}) error {
  1712  	if r.err != nil {
  1713  		return r.err
  1714  	}
  1715  
  1716  	// TODO(bradfitz): for now we need to defensively clone all
  1717  	// []byte that the driver returned (not permitting
  1718  	// *RawBytes in Rows.Scan), since we're about to close
  1719  	// the Rows in our defer, when we return from this function.
  1720  	// the contract with the driver.Next(...) interface is that it
  1721  	// can return slices into read-only temporary memory that's
  1722  	// only valid until the next Scan/Close.  But the TODO is that
  1723  	// for a lot of drivers, this copy will be unnecessary.  We
  1724  	// should provide an optional interface for drivers to
  1725  	// implement to say, "don't worry, the []bytes that I return
  1726  	// from Next will not be modified again." (for instance, if
  1727  	// they were obtained from the network anyway) But for now we
  1728  	// don't care.
  1729  	defer r.rows.Close()
  1730  	for _, dp := range dest {
  1731  		if _, ok := dp.(*RawBytes); ok {
  1732  			return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  1733  		}
  1734  	}
  1735  
  1736  	if !r.rows.Next() {
  1737  		if err := r.rows.Err(); err != nil {
  1738  			return err
  1739  		}
  1740  		return ErrNoRows
  1741  	}
  1742  	err := r.rows.Scan(dest...)
  1743  	if err != nil {
  1744  		return err
  1745  	}
  1746  	// Make sure the query can be processed to completion with no errors.
  1747  	if err := r.rows.Close(); err != nil {
  1748  		return err
  1749  	}
  1750  
  1751  	return nil
  1752  }
  1753  
  1754  // A Result summarizes an executed SQL command.
  1755  type Result interface {
  1756  	// LastInsertId returns the integer generated by the database
  1757  	// in response to a command. Typically this will be from an
  1758  	// "auto increment" column when inserting a new row. Not all
  1759  	// databases support this feature, and the syntax of such
  1760  	// statements varies.
  1761  	LastInsertId() (int64, error)
  1762  
  1763  	// RowsAffected returns the number of rows affected by an
  1764  	// update, insert, or delete. Not every database or database
  1765  	// driver may support this.
  1766  	RowsAffected() (int64, error)
  1767  }
  1768  
  1769  type driverResult struct {
  1770  	sync.Locker // the *driverConn
  1771  	resi        driver.Result
  1772  }
  1773  
  1774  func (dr driverResult) LastInsertId() (int64, error) {
  1775  	dr.Lock()
  1776  	defer dr.Unlock()
  1777  	return dr.resi.LastInsertId()
  1778  }
  1779  
  1780  func (dr driverResult) RowsAffected() (int64, error) {
  1781  	dr.Lock()
  1782  	defer dr.Unlock()
  1783  	return dr.resi.RowsAffected()
  1784  }
  1785  
  1786  func stack() string {
  1787  	var buf [2 << 10]byte
  1788  	return string(buf[:runtime.Stack(buf[:], false)])
  1789  }
  1790  
  1791  // withLock runs while holding lk.
  1792  func withLock(lk sync.Locker, fn func()) {
  1793  	lk.Lock()
  1794  	fn()
  1795  	lk.Unlock()
  1796  }