github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/database/sql/sql.go (about)

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