Skip to content
Snippets Groups Projects
sql.go 104 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2011 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    // Package sql provides a generic interface around SQL (or SQL-like)
    // databases.
    
    //
    // The sql package must be used in conjunction with a database driver.
    
    // See https://golang.org/s/sqldrivers for a list of drivers.
    
    // Drivers that do not support context cancellation will not return until
    
    // after the query is completed.
    //
    // For usage examples, see the wiki page at
    
    // https://golang.org/s/sqlwiki.
    
    	"database/sql/driver"
    
    var driversMu sync.RWMutex
    
    // drivers should be an internal detail,
    // but widely used packages access it using linkname.
    // (It is extra wrong that they linkname drivers but not driversMu.)
    // Notable members of the hall of shame include:
    //   - github.com/instana/go-sensor
    //
    // Do not remove or change the type signature.
    // See go.dev/issue/67401.
    //
    //go:linkname drivers
    var drivers = make(map[string]driver.Driver)
    
    // nowFunc returns the current time; it's overridden in tests.
    var nowFunc = time.Now
    
    
    // Register makes a database driver available by the provided name.
    // If Register is called twice with the same name or if driver is nil,
    // it panics.
    func Register(name string, driver driver.Driver) {
    
    	driversMu.Lock()
    	defer driversMu.Unlock()
    
    	if driver == nil {
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    		panic("sql: Register driver is nil")
    
    	}
    	if _, dup := drivers[name]; dup {
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    		panic("sql: Register called twice for driver " + name)
    
    	}
    	drivers[name] = driver
    }
    
    
    func unregisterAllDrivers() {
    
    	driversMu.Lock()
    	defer driversMu.Unlock()
    
    	// For tests.
    	drivers = make(map[string]driver.Driver)
    }
    
    
    // Drivers returns a sorted list of the names of the registered drivers.
    func Drivers() []string {
    
    	driversMu.RLock()
    	defer driversMu.RUnlock()
    
    	return slices.Sorted(maps.Keys(drivers))
    
    // A NamedArg is a named argument. NamedArg values may be used as
    
    // arguments to [DB.Query] or [DB.Exec] and bind to the corresponding named
    
    // parameter in the SQL statement.
    //
    // For a more concise way to create NamedArg values, see
    
    // the [Named] function.
    
    	_NamedFieldsRequired struct{}
    
    	// Name is the name of the parameter placeholder.
    	//
    	// If empty, the ordinal position in the argument list will be
    	// used.
    
    	//
    	// Name must omit any symbol prefix.
    
    	// Value is the value of the parameter.
    	// It may be assigned the same value types as the query
    	// arguments.
    
    // Named provides a more concise way to create [NamedArg] values.
    
    Russ Cox's avatar
    Russ Cox committed
    //	db.ExecContext(ctx, `
    //	    delete from Invoice
    //	    where
    //	        TimeCreated < @end
    //	        and TimeCreated >= @start;`,
    //	    sql.Named("start", startTime),
    //	    sql.Named("end", endTime),
    //	)
    
    func Named(name string, value any) NamedArg {
    
    	// This method exists because the go1compat promise
    	// doesn't guarantee that structs don't grow more fields,
    	// so unkeyed struct literals are a vet error. Thus, we don't
    
    	// want to allow sql.NamedArg{name, value}.
    	return NamedArg{Name: name, Value: value}
    
    // IsolationLevel is the transaction isolation level used in [TxOptions].
    
    // Various isolation levels that drivers may support in [DB.BeginTx].
    
    // If a driver does not support a given isolation level an error may be returned.
    
    //
    // See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
    
    const (
    	LevelDefault IsolationLevel = iota
    
    	LevelReadUncommitted
    	LevelReadCommitted
    	LevelWriteCommitted
    
    	LevelRepeatableRead
    	LevelSnapshot
    	LevelSerializable
    	LevelLinearizable
    )
    
    
    // String returns the name of the transaction isolation level.
    
    func (i IsolationLevel) String() string {
    	switch i {
    	case LevelDefault:
    		return "Default"
    	case LevelReadUncommitted:
    		return "Read Uncommitted"
    	case LevelReadCommitted:
    		return "Read Committed"
    	case LevelWriteCommitted:
    		return "Write Committed"
    	case LevelRepeatableRead:
    		return "Repeatable Read"
    	case LevelSnapshot:
    		return "Snapshot"
    	case LevelSerializable:
    		return "Serializable"
    	case LevelLinearizable:
    		return "Linearizable"
    	default:
    		return "IsolationLevel(" + strconv.Itoa(int(i)) + ")"
    	}
    }
    
    var _ fmt.Stringer = LevelDefault
    
    
    // TxOptions holds the transaction options to be used in [DB.BeginTx].
    
    type TxOptions struct {
    	// Isolation is the transaction isolation level.
    	// If zero, the driver or database's default level is used.
    	Isolation IsolationLevel
    	ReadOnly  bool
    
    // RawBytes is a byte slice that holds a reference to memory owned by
    
    // the database itself. After a [Rows.Scan] into a RawBytes, the slice is only
    // valid until the next call to [Rows.Next], [Rows.Scan], or [Rows.Close].
    
    // NullString represents a string that may be null.
    
    // NullString implements the [Scanner] interface so
    
    // it can be used as a scan destination:
    //
    
    Russ Cox's avatar
    Russ Cox committed
    //	var s NullString
    //	err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
    //	...
    //	if s.Valid {
    //	   // use s.String
    //	} else {
    //	   // NULL value
    //	}
    
    	String string
    	Valid  bool // Valid is true if String is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (ns *NullString) Scan(value any) error {
    
    	if value == nil {
    
    	ns.Valid = true
    	return convertAssign(&ns.String, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (ns NullString) Value() (driver.Value, error) {
    
    	if !ns.Valid {
    		return nil, nil
    	}
    	return ns.String, nil
    
    // NullInt64 represents an int64 that may be null.
    
    // NullInt64 implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullInt64 struct {
    	Int64 int64
    	Valid bool // Valid is true if Int64 is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullInt64) Scan(value any) error {
    
    	if value == nil {
    		n.Int64, n.Valid = 0, false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.Int64, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullInt64) Value() (driver.Value, error) {
    
    	if !n.Valid {
    		return nil, nil
    	}
    	return n.Int64, nil
    }
    
    
    // NullInt32 represents an int32 that may be null.
    
    // NullInt32 implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullInt32 struct {
    	Int32 int32
    	Valid bool // Valid is true if Int32 is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullInt32) Scan(value any) error {
    
    	if value == nil {
    		n.Int32, n.Valid = 0, false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.Int32, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullInt32) Value() (driver.Value, error) {
    	if !n.Valid {
    		return nil, nil
    	}
    	return int64(n.Int32), nil
    }
    
    
    // NullInt16 represents an int16 that may be null.
    
    // NullInt16 implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullInt16 struct {
    	Int16 int16
    	Valid bool // Valid is true if Int16 is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullInt16) Scan(value any) error {
    
    	if value == nil {
    		n.Int16, n.Valid = 0, false
    		return nil
    	}
    	err := convertAssign(&n.Int16, value)
    	n.Valid = err == nil
    	return err
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullInt16) Value() (driver.Value, error) {
    	if !n.Valid {
    		return nil, nil
    	}
    	return int64(n.Int16), nil
    }
    
    // NullByte represents a byte that may be null.
    
    // NullByte implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullByte struct {
    	Byte  byte
    	Valid bool // Valid is true if Byte is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullByte) Scan(value any) error {
    
    	if value == nil {
    		n.Byte, n.Valid = 0, false
    		return nil
    	}
    	err := convertAssign(&n.Byte, value)
    	n.Valid = err == nil
    	return err
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullByte) Value() (driver.Value, error) {
    	if !n.Valid {
    		return nil, nil
    	}
    	return int64(n.Byte), nil
    }
    
    
    // NullFloat64 represents a float64 that may be null.
    
    // NullFloat64 implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullFloat64 struct {
    	Float64 float64
    	Valid   bool // Valid is true if Float64 is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullFloat64) Scan(value any) error {
    
    	if value == nil {
    		n.Float64, n.Valid = 0, false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.Float64, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullFloat64) Value() (driver.Value, error) {
    
    	if !n.Valid {
    		return nil, nil
    	}
    	return n.Float64, nil
    }
    
    // NullBool represents a bool that may be null.
    
    // NullBool implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullBool struct {
    	Bool  bool
    	Valid bool // Valid is true if Bool is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullBool) Scan(value any) error {
    
    	if value == nil {
    		n.Bool, n.Valid = false, false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.Bool, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullBool) Value() (driver.Value, error) {
    
    	if !n.Valid {
    		return nil, nil
    	}
    	return n.Bool, nil
    }
    
    
    // NullTime represents a [time.Time] that may be null.
    // NullTime implements the [Scanner] interface so
    // it can be used as a scan destination, similar to [NullString].
    
    type NullTime struct {
    	Time  time.Time
    	Valid bool // Valid is true if Time is not NULL
    }
    
    
    // Scan implements the [Scanner] interface.
    
    func (n *NullTime) Scan(value any) error {
    
    	if value == nil {
    		n.Time, n.Valid = time.Time{}, false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.Time, value)
    }
    
    
    // Value implements the [driver.Valuer] interface.
    
    func (n NullTime) Value() (driver.Value, error) {
    	if !n.Valid {
    		return nil, nil
    	}
    	return n.Time, nil
    }
    
    
    // Null represents a value that may be null.
    
    // Null implements the [Scanner] interface so
    
    // it can be used as a scan destination:
    //
    //	var s Null[string]
    //	err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
    //	...
    //	if s.Valid {
    //	   // use s.V
    //	} else {
    //	   // NULL value
    //	}
    
    //
    // T should be one of the types accepted by [driver.Value].
    
    type Null[T any] struct {
    	V     T
    	Valid bool
    }
    
    func (n *Null[T]) Scan(value any) error {
    	if value == nil {
    		n.V, n.Valid = *new(T), false
    		return nil
    	}
    	n.Valid = true
    	return convertAssign(&n.V, value)
    }
    
    func (n Null[T]) Value() (driver.Value, error) {
    	if !n.Valid {
    		return nil, nil
    	}
    
    	v := any(n.V)
    	// See issue 69728.
    	if valuer, ok := v.(driver.Valuer); ok {
    		val, err := callValuerValue(valuer)
    		if err != nil {
    			return val, err
    		}
    		v = val
    	}
    	// See issue 69837.
    	return driver.DefaultParameterConverter.ConvertValue(v)
    
    // Scanner is an interface used by [Rows.Scan].
    
    type Scanner interface {
    	// Scan assigns a value from a database driver.
    
    	// The src value will be of one of the following types:
    
    	//
    	//    int64
    	//    float64
    	//    bool
    	//    []byte
    
    	//    nil - for NULL values
    	//
    
    	// An error should be returned if the value cannot be stored
    
    	// without loss of information.
    
    	//
    	// Reference types such as []byte are only valid until the next call to Scan
    	// and should not be retained. Their underlying memory is owned by the driver.
    	// If retention is necessary, copy their values before the next call to Scan.
    
    	Scan(src any) error
    
    // Out may be used to retrieve OUTPUT value parameters from stored procedures.
    //
    // Not all drivers and databases support OUTPUT value parameters.
    //
    // Example usage:
    //
    
    Russ Cox's avatar
    Russ Cox committed
    //	var outArg string
    //	_, err := db.ExecContext(ctx, "ProcName", sql.Named("Arg1", sql.Out{Dest: &outArg}))
    
    	_NamedFieldsRequired struct{}
    
    
    	// Dest is a pointer to the value that will be set to the result of the
    	// stored procedure's OUTPUT parameter.
    
    
    	// In is whether the parameter is an INOUT parameter. If so, the input value to the stored
    	// procedure is the dereferenced value of Dest's pointer, which is then replaced with
    	// the output value.
    	In bool
    }
    
    
    // ErrNoRows is returned by [Row.Scan] when [DB.QueryRow] doesn't return a
    // row. In such a case, QueryRow returns a placeholder [*Row] value that
    
    // defers this error until a Scan.
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    var ErrNoRows = errors.New("sql: no rows in result set")
    
    // DB is a database handle representing a pool of zero or more
    // underlying connections. It's safe for concurrent use by multiple
    
    // The sql package creates and frees connections automatically; it
    // also maintains a free pool of idle connections. If the database has
    
    // a concept of per-connection state, such state can be reliably observed
    
    // within a transaction ([Tx]) or connection ([Conn]). Once [DB.Begin] is called, the
    
    // returned [Tx] is bound to a single connection. Once [Tx.Commit] or
    // [Tx.Rollback] is called on the transaction, that transaction's
    
    // connection is returned to [DB]'s idle connection pool. The pool size
    
    // can be controlled with [DB.SetMaxIdleConns].
    
    type DB struct {
    
    	// Total time waited for new connections.
    	waitDuration atomic.Int64
    
    	// numClosed is an atomic counter which represents a total number of
    	// closed connections. Stmt.openStmt checks it before cleaning closed
    	// connections in Stmt.css.
    
    	mu           sync.Mutex    // protects following fields
    	freeConn     []*driverConn // free connections ordered by returnedAt oldest to newest
    
    	connRequests connRequestSet
    	numOpen      int // number of opened and pending open connections
    
    	// Used to signal the need for new connections
    
    	// a goroutine running connectionOpener() reads on this chan and
    	// maybeOpenNewConnections sends on the chan (one send per needed connection)
    	// It is closed during db.Close(). The close tells the connectionOpener
    	// goroutine to exit.
    
    	openerCh          chan struct{}
    	closed            bool
    	dep               map[finalCloser]depSet
    	lastPut           map[*driverConn]string // stacktrace of last conn's put; debug only
    
    	maxIdleCount      int                    // zero means defaultMaxIdleConns; negative means 0
    
    	maxOpen           int                    // <= 0 means unlimited
    	maxLifetime       time.Duration          // maximum amount of time a connection may be reused
    
    	maxIdleTime       time.Duration          // maximum amount of time a connection may be idle before being closed
    
    	cleanerCh         chan struct{}
    	waitCount         int64 // Total number of connections waited for.
    
    	maxIdleClosed     int64 // Total number of connections closed due to idle count.
    	maxIdleTimeClosed int64 // Total number of connections closed due to idle time.
    
    	maxLifetimeClosed int64 // Total number of connections closed due to max connection lifetime limit.
    
    	stop func() // stop cancels the connection opener.
    
    // connReuseStrategy determines how (*DB).conn returns database connections.
    type connReuseStrategy uint8
    
    const (
    	// alwaysNewConn forces a new connection to the database.
    	alwaysNewConn connReuseStrategy = iota
    	// cachedOrNewConn returns a cached connection, if available, else waits
    	// for one to become available (if MaxOpenConns has been reached) or
    	// creates a new database connection.
    	cachedOrNewConn
    )
    
    
    // driverConn wraps a driver.Conn with a mutex, to
    // be held during all calls into the Conn. (including any calls onto
    // interfaces returned via that Conn, such as calls on Tx, Stmt,
    // Result, Rows)
    type driverConn struct {
    
    	db        *DB
    	createdAt time.Time
    
    	sync.Mutex  // guards following
    	ci          driver.Conn
    
    	needReset   bool // The connection session should be reset before use if true.
    
    	closed      bool
    	finalClosed bool // ci.Close has been called
    
    	openStmt    map[*driverStmt]bool
    
    	dbmuClosed bool      // same as closed, but guarded by db.mu, for removeClosedStmtLocked
    
    	returnedAt time.Time // Time the connection was created or returned.
    	onPut      []func()  // code (with db.mu held) run when conn is next returned
    
    func (dc *driverConn) releaseConn(err error) {
    
    func (dc *driverConn) removeOpenStmt(ds *driverStmt) {
    
    func (dc *driverConn) expired(timeout time.Duration) bool {
    	if timeout <= 0 {
    		return false
    	}
    	return dc.createdAt.Add(timeout).Before(nowFunc())
    }
    
    
    // resetSession checks if the driver connection needs the
    // session to be reset and if required, resets it.
    func (dc *driverConn) resetSession(ctx context.Context) error {
    	dc.Lock()
    	defer dc.Unlock()
    
    	if !dc.needReset {
    		return nil
    	}
    	if cr, ok := dc.ci.(driver.SessionResetter); ok {
    		return cr.ResetSession(ctx)
    	}
    	return nil
    }
    
    // validateConnection checks if the connection is valid and can
    // still be used. It also marks the session for reset if required.
    func (dc *driverConn) validateConnection(needsReset bool) bool {
    	dc.Lock()
    	defer dc.Unlock()
    
    	if needsReset {
    		dc.needReset = true
    	}
    
    	if cv, ok := dc.ci.(driver.Validator); ok {
    		return cv.IsValid()
    
    // prepareLocked prepares the query on dc. When cg == nil the dc must keep track of
    // the prepared statements in a pool.
    func (dc *driverConn) prepareLocked(ctx context.Context, cg stmtConnGrabber, query string) (*driverStmt, error) {
    
    	si, err := ctxDriverPrepare(ctx, dc.ci, query)
    
    	if err != nil {
    		return nil, err
    
    	ds := &driverStmt{Locker: dc, si: si}
    
    	// No need to manage open statements if there is a single connection grabber.
    	if cg != nil {
    		return ds, nil
    	}
    
    
    	// Track each driverConn's open statements, so we can close them
    	// before closing the conn.
    	//
    	// Wrap all driver.Stmt is *driverStmt to ensure they are only closed once.
    	if dc.openStmt == nil {
    		dc.openStmt = make(map[*driverStmt]bool)
    	}
    	dc.openStmt[ds] = true
    	return ds, nil
    
    func (dc *driverConn) closeDBLocked() func() error {
    
    	defer dc.Unlock()
    
    		return func() error { return errors.New("sql: duplicate driverConn close") }
    
    	return dc.db.removeDepLocked(dc, dc)
    
    }
    
    func (dc *driverConn) Close() error {
    	dc.Lock()
    	if dc.closed {
    		dc.Unlock()
    		return errors.New("sql: duplicate driverConn close")
    	}
    	dc.closed = true
    	dc.Unlock() // not defer; removeDep finalClose calls may need to lock
    
    
    	// And now updates that require holding dc.mu.Lock.
    	dc.db.mu.Lock()
    	dc.dbmuClosed = true
    	fn := dc.db.removeDepLocked(dc, dc)
    	dc.db.mu.Unlock()
    	return fn()
    
    }
    
    func (dc *driverConn) finalClose() error {
    
    
    	// Each *driverStmt has a lock to the dc. Copy the list out of the dc
    	// before calling close on each stmt.
    	var openStmt []*driverStmt
    
    		openStmt = make([]*driverStmt, 0, len(dc.openStmt))
    		for ds := range dc.openStmt {
    			openStmt = append(openStmt, ds)
    
    		dc.openStmt = nil
    	})
    	for _, ds := range openStmt {
    		ds.Close()
    	}
    	withLock(dc, func() {
    		dc.finalClosed = true
    		err = dc.ci.Close()
    		dc.ci = nil
    
    
    	dc.db.mu.Lock()
    	dc.db.numOpen--
    	dc.db.maybeOpenNewConnections()
    	dc.db.mu.Unlock()
    
    
    }
    
    // driverStmt associates a driver.Stmt with the
    // *driverConn from which it came, so the driverConn's lock can be
    // held during calls.
    type driverStmt struct {
    	sync.Locker // the *driverConn
    	si          driver.Stmt
    
    	closed      bool
    	closeErr    error // return value of previous Close call
    
    // Close ensures driver.Stmt is only closed once and always returns the same
    
    func (ds *driverStmt) Close() error {
    	ds.Lock()
    	defer ds.Unlock()
    
    	if ds.closed {
    		return ds.closeErr
    	}
    	ds.closed = true
    	ds.closeErr = ds.si.Close()
    	return ds.closeErr
    
    }
    
    // depSet is a finalCloser's outstanding dependencies
    
    type depSet map[any]bool // set of true bools
    
    // The finalCloser interface is used by (*DB).addDep and related
    // dependency reference counting.
    
    type finalCloser interface {
    	// finalClose is called when the reference count of an object
    	// goes to zero. (*DB).mu is not held while calling it.
    	finalClose() error
    }
    
    // addDep notes that x now depends on dep, and x's finalClose won't be
    // called until all of x's dependencies are removed with removeDep.
    
    func (db *DB) addDep(x finalCloser, dep any) {
    
    	db.mu.Lock()
    	defer db.mu.Unlock()
    
    func (db *DB) addDepLocked(x finalCloser, dep any) {
    
    	if db.dep == nil {
    		db.dep = make(map[finalCloser]depSet)
    	}
    	xdep := db.dep[x]
    	if xdep == nil {
    		xdep = make(depSet)
    		db.dep[x] = xdep
    	}
    	xdep[dep] = true
    }
    
    // removeDep notes that x no longer depends on dep.
    // If x still has dependencies, nil is returned.
    // If x no longer has any dependencies, its finalClose method will be
    // called and its error value will be returned.
    
    func (db *DB) removeDep(x finalCloser, dep any) error {
    
    	db.mu.Lock()
    	fn := db.removeDepLocked(x, dep)
    	db.mu.Unlock()
    	return fn()
    }
    
    
    func (db *DB) removeDepLocked(x finalCloser, dep any) func() error {
    
    	xdep, ok := db.dep[x]
    	if !ok {
    		panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
    
    	l0 := len(xdep)
    	delete(xdep, dep)
    
    	switch len(xdep) {
    	case l0:
    		// Nothing removed. Shouldn't happen.
    		panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
    	case 0:
    		// No more dependencies.
    		delete(db.dep, x)
    		return x.finalClose
    	default:
    		// Dependencies remain.
    
    Andrew Gerrand's avatar
    Andrew Gerrand committed
    // This is the size of the connectionOpener request chan (DB.openerCh).
    
    // This value should be larger than the maximum typical value
    
    // used for DB.maxOpen. If maxOpen is significantly larger than
    
    // connectionRequestQueueSize then it is possible for ALL calls into the *DB
    
    Robert Hencke's avatar
    Robert Hencke committed
    // to block until the connectionOpener can satisfy the backlog of requests.
    
    var connectionRequestQueueSize = 1000000
    
    
    type dsnConnector struct {
    	dsn    string
    	driver driver.Driver
    }
    
    func (t dsnConnector) Connect(_ context.Context) (driver.Conn, error) {
    	return t.driver.Open(t.dsn)
    }
    
    func (t dsnConnector) Driver() driver.Driver {
    	return t.driver
    }
    
    
    // OpenDB opens a database using a [driver.Connector], allowing drivers to
    
    // bypass a string based data source name.
    //
    // Most users will open a database via a driver-specific connection
    
    // helper function that returns a [*DB]. No database drivers are included
    
    // in the Go standard library. See https://golang.org/s/sqldrivers for
    // a list of third-party drivers.
    //
    // OpenDB may just validate its arguments without creating a connection
    // to the database. To verify that the data source name is valid, call
    
    // [DB.Ping].
    
    // The returned [DB] is safe for concurrent use by multiple goroutines
    
    // and maintains its own pool of idle connections. Thus, the OpenDB
    // function should be called just once. It is rarely necessary to
    
    // close a [DB].
    
    func OpenDB(c driver.Connector) *DB {
    
    	ctx, cancel := context.WithCancel(context.Background())
    
    		connector: c,
    		openerCh:  make(chan struct{}, connectionRequestQueueSize),
    		lastPut:   make(map[*driverConn]string),
    		stop:      cancel,
    
    // Open opens a database specified by its database driver name and a
    // driver-specific data source name, usually consisting of at least a
    // database name and connection information.
    //
    // Most users will open a database via a driver-specific connection
    
    // helper function that returns a [*DB]. No database drivers are included
    
    // in the Go standard library. See https://golang.org/s/sqldrivers for
    
    // a list of third-party drivers.
    
    //
    // Open may just validate its arguments without creating a connection
    // to the database. To verify that the data source name is valid, call
    
    // [DB.Ping].
    
    // The returned [DB] is safe for concurrent use by multiple goroutines
    
    // and maintains its own pool of idle connections. Thus, the Open
    // function should be called just once. It is rarely necessary to
    
    // close a [DB].
    
    func Open(driverName, dataSourceName string) (*DB, error) {
    
    	driveri, ok := drivers[driverName]
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    		return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
    
    	if driverCtx, ok := driveri.(driver.DriverContext); ok {
    		connector, err := driverCtx.OpenConnector(dataSourceName)
    		if err != nil {
    			return nil, err
    		}
    		return OpenDB(connector), nil
    	}
    
    
    	return OpenDB(dsnConnector{dsn: dataSourceName, driver: driveri}), nil
    
    func (db *DB) pingDC(ctx context.Context, dc *driverConn, release func(error)) error {
    	var err error
    	if pinger, ok := dc.ci.(driver.Pinger); ok {
    		withLock(dc, func() {
    			err = pinger.Ping(ctx)
    		})
    	}
    	release(err)
    	return err
    }
    
    
    // PingContext verifies a connection to the database is still alive,
    
    // establishing a connection if necessary.
    
    func (db *DB) PingContext(ctx context.Context) error {
    
    	var dc *driverConn
    	var err error
    
    
    	err = db.retry(func(strategy connReuseStrategy) error {
    		dc, err = db.conn(ctx, strategy)
    		return err
    	})
    
    
    	return db.pingDC(ctx, dc, dc.releaseConn)
    
    // Ping verifies a connection to the database is still alive,
    // establishing a connection if necessary.
    
    // Ping uses [context.Background] internally; to specify the context, use
    
    // [DB.PingContext].
    
    func (db *DB) Ping() error {
    	return db.PingContext(context.Background())
    }
    
    
    // Close closes the database and prevents new queries from starting.
    // Close then waits for all queries that have started processing on the server
    // to finish.
    
    // It is rare to Close a [DB], as the [DB] handle is meant to be
    
    // long-lived and shared between many goroutines.
    
    func (db *DB) Close() error {
    	db.mu.Lock()
    
    	if db.closed { // Make DB.Close idempotent
    		db.mu.Unlock()
    		return nil
    	}
    
    	if db.cleanerCh != nil {
    		close(db.cleanerCh)
    	}
    
    	fns := make([]func() error, 0, len(db.freeConn))
    	for _, dc := range db.freeConn {
    
    		fns = append(fns, dc.closeDBLocked())
    	}
    
    	db.closed = true
    
    	db.connRequests.CloseAndRemoveAll()
    
    	db.mu.Unlock()
    	for _, fn := range fns {
    		err1 := fn()
    
    	if c, ok := db.connector.(io.Closer); ok {
    		err1 := c.Close()
    		if err1 != nil {
    			err = err1
    		}
    	}
    
    const defaultMaxIdleConns = 2
    
    func (db *DB) maxIdleConnsLocked() int {
    
    	n := db.maxIdleCount
    
    	switch {
    	case n == 0:
    		// TODO(bradfitz): ask driver, if supported, for its default preference
    		return defaultMaxIdleConns
    	case n < 0:
    		return 0
    	default:
    		return n
    	}
    }
    
    
    func (db *DB) shortestIdleTimeLocked() time.Duration {
    
    	if db.maxIdleTime <= 0 {
    		return db.maxLifetime
    	}
    	if db.maxLifetime <= 0 {
    		return db.maxIdleTime
    	}
    
    	return min(db.maxIdleTime, db.maxLifetime)
    
    // SetMaxIdleConns sets the maximum number of connections in the idle
    // connection pool.
    //
    
    // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
    // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
    
    // If n <= 0, no idle connections are retained.
    
    //
    // The default max idle connections is currently 2. This may change in
    // a future release.
    
    func (db *DB) SetMaxIdleConns(n int) {
    	db.mu.Lock()
    	if n > 0 {
    
    		db.maxIdleCount = n
    
    	} else {
    		// No idle connections.