2011-10-27 01:57:58 +02:00
|
|
|
// 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.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
|
|
|
// The sql package must be used in conjunction with a database driver.
|
|
|
|
// See http://golang.org/s/sqldrivers for a list of drivers.
|
2013-11-06 20:49:01 +01:00
|
|
|
//
|
|
|
|
// For more usage examples, see the wiki page at
|
|
|
|
// http://golang.org/s/sqlwiki.
|
2011-10-27 01:57:58 +02:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
2013-11-06 20:49:01 +01:00
|
|
|
"container/list"
|
2012-01-25 22:54:22 +01:00
|
|
|
"database/sql/driver"
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2011-10-27 01:57:58 +02:00
|
|
|
"fmt"
|
2011-12-03 03:17:34 +01:00
|
|
|
"io"
|
2013-07-16 08:54:42 +02:00
|
|
|
"runtime"
|
2011-10-27 01:57:58 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var drivers = make(map[string]driver.Driver)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if driver == nil {
|
2012-01-13 06:11:45 +01:00
|
|
|
panic("sql: Register driver is nil")
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
2012-01-13 06:11:45 +01:00
|
|
|
panic("sql: Register called twice for driver " + name)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
drivers[name] = driver
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
// RawBytes is a byte slice that holds a reference to memory owned by
|
|
|
|
// the database itself. After a Scan into a RawBytes, the slice is only
|
|
|
|
// valid until the next call to Next, Scan, or Close.
|
|
|
|
type RawBytes []byte
|
|
|
|
|
|
|
|
// NullString represents a string that may be null.
|
2012-03-02 17:38:43 +01:00
|
|
|
// NullString implements the Scanner interface so
|
2011-10-27 01:57:58 +02:00
|
|
|
// it can be used as a scan destination:
|
|
|
|
//
|
2012-01-25 22:54:22 +01:00
|
|
|
// var s NullString
|
2011-10-27 01:57:58 +02:00
|
|
|
// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
|
|
|
|
// ...
|
|
|
|
// if s.Valid {
|
|
|
|
// // use s.String
|
|
|
|
// } else {
|
|
|
|
// // NULL value
|
|
|
|
// }
|
|
|
|
//
|
2012-01-25 22:54:22 +01:00
|
|
|
type NullString struct {
|
2011-10-27 01:57:58 +02:00
|
|
|
String string
|
|
|
|
Valid bool // Valid is true if String is not NULL
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (ns *NullString) Scan(value interface{}) error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if value == nil {
|
2012-01-25 22:54:22 +01:00
|
|
|
ns.String, ns.Valid = "", false
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
ns.Valid = true
|
|
|
|
return convertAssign(&ns.String, value)
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (ns NullString) Value() (driver.Value, error) {
|
2012-01-25 22:54:22 +01:00
|
|
|
if !ns.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return ns.String, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// NullInt64 represents an int64 that may be null.
|
2012-03-02 17:38:43 +01:00
|
|
|
// NullInt64 implements the Scanner interface so
|
2012-02-01 20:26:59 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (n *NullInt64) Scan(value interface{}) error {
|
2012-02-01 20:26:59 +01:00
|
|
|
if value == nil {
|
|
|
|
n.Int64, n.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n.Valid = true
|
|
|
|
return convertAssign(&n.Int64, value)
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (n NullInt64) Value() (driver.Value, error) {
|
2012-02-01 20:26:59 +01:00
|
|
|
if !n.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return n.Int64, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NullFloat64 represents a float64 that may be null.
|
2012-03-02 17:38:43 +01:00
|
|
|
// NullFloat64 implements the Scanner interface so
|
2012-02-01 20:26:59 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (n *NullFloat64) Scan(value interface{}) error {
|
2012-02-01 20:26:59 +01:00
|
|
|
if value == nil {
|
|
|
|
n.Float64, n.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n.Valid = true
|
|
|
|
return convertAssign(&n.Float64, value)
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (n NullFloat64) Value() (driver.Value, error) {
|
2012-02-01 20:26:59 +01:00
|
|
|
if !n.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return n.Float64, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NullBool represents a bool that may be null.
|
2012-03-02 17:38:43 +01:00
|
|
|
// NullBool implements the Scanner interface so
|
2012-02-01 20:26:59 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (n *NullBool) Scan(value interface{}) error {
|
2012-02-01 20:26:59 +01:00
|
|
|
if value == nil {
|
|
|
|
n.Bool, n.Valid = false, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n.Valid = true
|
|
|
|
return convertAssign(&n.Bool, value)
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (n NullBool) Value() (driver.Value, error) {
|
2012-02-01 20:26:59 +01:00
|
|
|
if !n.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return n.Bool, nil
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Scanner is an interface used by Scan.
|
|
|
|
type Scanner interface {
|
|
|
|
// Scan assigns a value from a database driver.
|
2011-10-27 01:57:58 +02:00
|
|
|
//
|
2012-03-02 17:38:43 +01:00
|
|
|
// The src value will be of one of the following restricted
|
2011-10-27 01:57:58 +02:00
|
|
|
// set of types:
|
|
|
|
//
|
|
|
|
// int64
|
|
|
|
// float64
|
|
|
|
// bool
|
|
|
|
// []byte
|
2012-03-02 17:38:43 +01:00
|
|
|
// string
|
|
|
|
// time.Time
|
2011-10-27 01:57:58 +02:00
|
|
|
// nil - for NULL values
|
|
|
|
//
|
|
|
|
// An error should be returned if the value can not be stored
|
|
|
|
// without loss of information.
|
2012-03-02 17:38:43 +01:00
|
|
|
Scan(src interface{}) error
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNoRows is returned by Scan when QueryRow doesn't return a
|
|
|
|
// row. In such a case, QueryRow returns a placeholder *Row value that
|
|
|
|
// defers this error until a Scan.
|
2012-01-13 06:11:45 +01:00
|
|
|
var ErrNoRows = errors.New("sql: no rows in result set")
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
// DB is a database handle. It's safe for concurrent use by multiple
|
|
|
|
// goroutines.
|
2012-03-30 23:27:11 +02:00
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// 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 only be reliably
|
|
|
|
// observed within a transaction. Once DB.Begin is called, the
|
|
|
|
// returned Tx is bound to a single connection. Once Commit or
|
|
|
|
// 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 SetMaxIdleConns.
|
2011-10-27 01:57:58 +02:00
|
|
|
type DB struct {
|
|
|
|
driver driver.Driver
|
|
|
|
dsn string
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
mu sync.Mutex // protects following fields
|
|
|
|
freeConn *list.List // of *driverConn
|
|
|
|
connRequests *list.List // of connRequest
|
|
|
|
numOpen int
|
|
|
|
pendingOpens int
|
2013-11-27 02:05:38 +01:00
|
|
|
// Used to signal the need for new connections
|
2013-11-06 20:49:01 +01:00
|
|
|
// 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{}
|
2011-12-13 00:40:51 +01:00
|
|
|
closed bool
|
2013-07-16 08:54:42 +02:00
|
|
|
dep map[finalCloser]depSet
|
|
|
|
lastPut map[*driverConn]string // stacktrace of last conn's put; debug only
|
|
|
|
maxIdle int // zero means defaultMaxIdleConns; negative means 0
|
2013-11-06 20:49:01 +01:00
|
|
|
maxOpen int // <= 0 means unlimited
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
sync.Mutex // guards following
|
|
|
|
ci driver.Conn
|
|
|
|
closed bool
|
|
|
|
finalClosed bool // ci.Close has been called
|
|
|
|
openStmt map[driver.Stmt]bool
|
|
|
|
|
|
|
|
// guarded by db.mu
|
|
|
|
inUse bool
|
|
|
|
onPut []func() // code (with db.mu held) run when conn is next returned
|
|
|
|
dbmuClosed bool // same as closed, but guarded by db.mu, for connIfFree
|
2013-11-06 20:49:01 +01:00
|
|
|
// This is the Element returned by db.freeConn.PushFront(conn).
|
|
|
|
// It's used by connIfFree to remove the conn from the freeConn list.
|
|
|
|
listElem *list.Element
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *driverConn) releaseConn(err error) {
|
|
|
|
dc.db.putConn(dc, err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *driverConn) removeOpenStmt(si driver.Stmt) {
|
|
|
|
dc.Lock()
|
|
|
|
defer dc.Unlock()
|
|
|
|
delete(dc.openStmt, si)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *driverConn) prepareLocked(query string) (driver.Stmt, error) {
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
if err == nil {
|
|
|
|
// Track each driverConn's open statements, so we can close them
|
|
|
|
// before closing the conn.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): let drivers opt out of caring about
|
|
|
|
// stmt closes if the conn is about to close anyway? For now
|
|
|
|
// do the safe thing, in case stmts need to be closed.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): after Go 1.1, closing driver.Stmts
|
|
|
|
// should be moved to driverStmt, using unique
|
|
|
|
// *driverStmts everywhere (including from
|
|
|
|
// *Stmt.connStmt, instead of returning a
|
|
|
|
// driver.Stmt), using driverStmt as a pointer
|
|
|
|
// everywhere, and making it a finalCloser.
|
|
|
|
if dc.openStmt == nil {
|
|
|
|
dc.openStmt = make(map[driver.Stmt]bool)
|
|
|
|
}
|
|
|
|
dc.openStmt[si] = true
|
|
|
|
}
|
|
|
|
return si, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the dc.db's Mutex is held.
|
2013-11-06 20:49:01 +01:00
|
|
|
func (dc *driverConn) closeDBLocked() func() error {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
2013-11-06 20:49:01 +01:00
|
|
|
defer dc.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
if dc.closed {
|
2013-11-06 20:49:01 +01:00
|
|
|
return func() error { return errors.New("sql: duplicate driverConn close") }
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
dc.closed = true
|
2013-11-06 20:49:01 +01:00
|
|
|
return dc.db.removeDepLocked(dc, dc)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
dc.Lock()
|
|
|
|
|
|
|
|
for si := range dc.openStmt {
|
|
|
|
si.Close()
|
|
|
|
}
|
|
|
|
dc.openStmt = nil
|
|
|
|
|
|
|
|
err := dc.ci.Close()
|
|
|
|
dc.ci = nil
|
|
|
|
dc.finalClosed = true
|
|
|
|
dc.Unlock()
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
dc.db.mu.Lock()
|
|
|
|
dc.db.numOpen--
|
|
|
|
dc.db.maybeOpenNewConnections()
|
|
|
|
dc.db.mu.Unlock()
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *driverStmt) Close() error {
|
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
return ds.si.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// depSet is a finalCloser's outstanding dependencies
|
|
|
|
type depSet map[interface{}]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 interface{}) {
|
|
|
|
//println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep))
|
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
db.addDepLocked(x, dep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) addDepLocked(x finalCloser, dep interface{}) {
|
|
|
|
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 interface{}) error {
|
|
|
|
db.mu.Lock()
|
|
|
|
fn := db.removeDepLocked(x, dep)
|
|
|
|
db.mu.Unlock()
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error {
|
|
|
|
//println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep))
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
xdep, ok := db.dep[x]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
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.
|
2013-07-16 08:54:42 +02:00
|
|
|
return func() error { return nil }
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// This is the size of the connectionOpener request chan (dn.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
|
|
|
|
// to block until the connectionOpener can satify the backlog of requests.
|
|
|
|
var connectionRequestQueueSize = 1000000
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// 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
|
2013-07-16 08:54:42 +02:00
|
|
|
// helper function that returns a *DB. No database drivers are included
|
|
|
|
// in the Go standard library. See http://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
|
|
|
|
// Ping.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
driveri, ok := drivers[driverName]
|
2011-10-27 01:57:58 +02:00
|
|
|
if !ok {
|
2012-01-13 06:11:45 +01:00
|
|
|
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
db := &DB{
|
2013-11-06 20:49:01 +01:00
|
|
|
driver: driveri,
|
|
|
|
dsn: dataSourceName,
|
|
|
|
openerCh: make(chan struct{}, connectionRequestQueueSize),
|
|
|
|
lastPut: make(map[*driverConn]string),
|
|
|
|
}
|
|
|
|
db.freeConn = list.New()
|
|
|
|
db.connRequests = list.New()
|
|
|
|
go db.connectionOpener()
|
2013-07-16 08:54:42 +02:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping verifies a connection to the database is still alive,
|
|
|
|
// establishing a connection if necessary.
|
|
|
|
func (db *DB) Ping() error {
|
|
|
|
// TODO(bradfitz): give drivers an optional hook to implement
|
|
|
|
// this in a more efficient or more reliable way, if they
|
|
|
|
// have one.
|
|
|
|
dc, err := db.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
db.putConn(dc, nil)
|
|
|
|
return nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Close closes the database, releasing any open resources.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
db.mu.Lock()
|
2013-11-06 20:49:01 +01:00
|
|
|
if db.closed { // Make DB.Close idempotent
|
|
|
|
db.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
close(db.openerCh)
|
2011-12-13 00:40:51 +01:00
|
|
|
var err error
|
2013-11-06 20:49:01 +01:00
|
|
|
fns := make([]func() error, 0, db.freeConn.Len())
|
|
|
|
for db.freeConn.Front() != nil {
|
|
|
|
dc := db.freeConn.Front().Value.(*driverConn)
|
|
|
|
dc.listElem = nil
|
|
|
|
fns = append(fns, dc.closeDBLocked())
|
|
|
|
db.freeConn.Remove(db.freeConn.Front())
|
|
|
|
}
|
|
|
|
db.closed = true
|
|
|
|
for db.connRequests.Front() != nil {
|
|
|
|
req := db.connRequests.Front().Value.(connRequest)
|
|
|
|
db.connRequests.Remove(db.connRequests.Front())
|
|
|
|
close(req)
|
|
|
|
}
|
|
|
|
db.mu.Unlock()
|
|
|
|
for _, fn := range fns {
|
|
|
|
err1 := fn()
|
2011-12-13 00:40:51 +01:00
|
|
|
if err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
const defaultMaxIdleConns = 2
|
|
|
|
|
|
|
|
func (db *DB) maxIdleConnsLocked() int {
|
|
|
|
n := db.maxIdle
|
|
|
|
switch {
|
|
|
|
case n == 0:
|
|
|
|
// TODO(bradfitz): ask driver, if supported, for its default preference
|
|
|
|
return defaultMaxIdleConns
|
|
|
|
case n < 0:
|
|
|
|
return 0
|
|
|
|
default:
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMaxIdleConns sets the maximum number of connections in the idle
|
|
|
|
// connection pool.
|
|
|
|
//
|
2013-11-06 20:49:01 +01:00
|
|
|
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
|
|
|
|
// then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
|
|
|
|
//
|
2013-07-16 08:54:42 +02:00
|
|
|
// If n <= 0, no idle connections are retained.
|
|
|
|
func (db *DB) SetMaxIdleConns(n int) {
|
|
|
|
db.mu.Lock()
|
|
|
|
if n > 0 {
|
|
|
|
db.maxIdle = n
|
|
|
|
} else {
|
|
|
|
// No idle connections.
|
|
|
|
db.maxIdle = -1
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
// Make sure maxIdle doesn't exceed maxOpen
|
|
|
|
if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
|
|
|
|
db.maxIdle = db.maxOpen
|
|
|
|
}
|
|
|
|
var closing []*driverConn
|
|
|
|
for db.freeConn.Len() > db.maxIdleConnsLocked() {
|
|
|
|
dc := db.freeConn.Back().Value.(*driverConn)
|
|
|
|
dc.listElem = nil
|
|
|
|
db.freeConn.Remove(db.freeConn.Back())
|
|
|
|
closing = append(closing, dc)
|
|
|
|
}
|
|
|
|
db.mu.Unlock()
|
|
|
|
for _, c := range closing {
|
|
|
|
c.Close()
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// SetMaxOpenConns sets the maximum number of open connections to the database.
|
|
|
|
//
|
|
|
|
// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
|
|
|
|
// MaxIdleConns, then MaxIdleConns will be reduced to match the new
|
|
|
|
// MaxOpenConns limit
|
|
|
|
//
|
|
|
|
// If n <= 0, then there is no limit on the number of open connections.
|
|
|
|
// The default is 0 (unlimited).
|
|
|
|
func (db *DB) SetMaxOpenConns(n int) {
|
|
|
|
db.mu.Lock()
|
|
|
|
db.maxOpen = n
|
|
|
|
if n < 0 {
|
|
|
|
db.maxOpen = 0
|
|
|
|
}
|
|
|
|
syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
|
|
|
|
db.mu.Unlock()
|
|
|
|
if syncMaxIdle {
|
|
|
|
db.SetMaxIdleConns(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes db.mu is locked.
|
|
|
|
// If there are connRequests and the connection limit hasn't been reached,
|
|
|
|
// then tell the connectionOpener to open new connections.
|
|
|
|
func (db *DB) maybeOpenNewConnections() {
|
|
|
|
numRequests := db.connRequests.Len() - db.pendingOpens
|
|
|
|
if db.maxOpen > 0 {
|
|
|
|
numCanOpen := db.maxOpen - (db.numOpen + db.pendingOpens)
|
|
|
|
if numRequests > numCanOpen {
|
|
|
|
numRequests = numCanOpen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for numRequests > 0 {
|
|
|
|
db.pendingOpens++
|
|
|
|
numRequests--
|
|
|
|
db.openerCh <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs in a seperate goroutine, opens new connections when requested.
|
|
|
|
func (db *DB) connectionOpener() {
|
|
|
|
for _ = range db.openerCh {
|
|
|
|
db.openNewConnection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open one new connection
|
|
|
|
func (db *DB) openNewConnection() {
|
|
|
|
ci, err := db.driver.Open(db.dsn)
|
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
if db.closed {
|
|
|
|
if err == nil {
|
|
|
|
ci.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
db.pendingOpens--
|
|
|
|
if err != nil {
|
|
|
|
db.putConnDBLocked(nil, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dc := &driverConn{
|
|
|
|
db: db,
|
|
|
|
ci: ci,
|
|
|
|
}
|
|
|
|
if db.putConnDBLocked(dc, err) {
|
|
|
|
db.addDepLocked(dc, dc)
|
|
|
|
db.numOpen++
|
|
|
|
} else {
|
|
|
|
ci.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// connRequest represents one request for a new connection
|
|
|
|
// When there are no idle connections available, DB.conn will create
|
|
|
|
// a new connRequest and put it on the db.connRequests list.
|
|
|
|
type connRequest chan<- interface{} // takes either a *driverConn or an error
|
|
|
|
|
|
|
|
var errDBClosed = errors.New("sql: database is closed")
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// conn returns a newly-opened or cached *driverConn
|
|
|
|
func (db *DB) conn() (*driverConn, error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
db.mu.Lock()
|
2011-12-13 00:40:51 +01:00
|
|
|
if db.closed {
|
2012-01-12 02:31:45 +01:00
|
|
|
db.mu.Unlock()
|
2013-11-06 20:49:01 +01:00
|
|
|
return nil, errDBClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// If db.maxOpen > 0 and the number of open connections is over the limit
|
2014-03-03 21:14:52 +01:00
|
|
|
// and there are no free connection, make a request and wait.
|
|
|
|
if db.maxOpen > 0 && db.numOpen >= db.maxOpen && db.freeConn.Len() == 0 {
|
2013-11-06 20:49:01 +01:00
|
|
|
// Make the connRequest channel. It's buffered so that the
|
|
|
|
// connectionOpener doesn't block while waiting for the req to be read.
|
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
req := connRequest(ch)
|
|
|
|
db.connRequests.PushBack(req)
|
|
|
|
db.maybeOpenNewConnections()
|
|
|
|
db.mu.Unlock()
|
|
|
|
ret, ok := <-ch
|
|
|
|
if !ok {
|
|
|
|
return nil, errDBClosed
|
|
|
|
}
|
|
|
|
switch ret.(type) {
|
|
|
|
case *driverConn:
|
|
|
|
return ret.(*driverConn), nil
|
|
|
|
case error:
|
|
|
|
return nil, ret.(error)
|
|
|
|
default:
|
|
|
|
panic("sql: Unexpected type passed through connRequest.ch")
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
if f := db.freeConn.Front(); f != nil {
|
|
|
|
conn := f.Value.(*driverConn)
|
|
|
|
conn.listElem = nil
|
|
|
|
db.freeConn.Remove(f)
|
2013-07-16 08:54:42 +02:00
|
|
|
conn.inUse = true
|
2011-10-27 01:57:58 +02:00
|
|
|
db.mu.Unlock()
|
|
|
|
return conn, nil
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
db.mu.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
ci, err := db.driver.Open(db.dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
db.mu.Lock()
|
|
|
|
db.numOpen++
|
2013-07-16 08:54:42 +02:00
|
|
|
dc := &driverConn{
|
|
|
|
db: db,
|
|
|
|
ci: ci,
|
|
|
|
}
|
|
|
|
db.addDepLocked(dc, dc)
|
|
|
|
dc.inUse = true
|
|
|
|
db.mu.Unlock()
|
|
|
|
return dc, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
var (
|
|
|
|
errConnClosed = errors.New("database/sql: internal sentinel error: conn is closed")
|
|
|
|
errConnBusy = errors.New("database/sql: internal sentinel error: conn is busy")
|
|
|
|
)
|
|
|
|
|
|
|
|
// connIfFree returns (wanted, nil) if wanted is still a valid conn and
|
|
|
|
// isn't in use.
|
|
|
|
//
|
|
|
|
// The error is errConnClosed if the connection if the requested connection
|
|
|
|
// is invalid because it's been closed.
|
|
|
|
//
|
|
|
|
// The error is errConnBusy if the connection is in use.
|
|
|
|
func (db *DB) connIfFree(wanted *driverConn) (*driverConn, error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
if wanted.dbmuClosed {
|
|
|
|
return nil, errConnClosed
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if wanted.inUse {
|
|
|
|
return nil, errConnBusy
|
|
|
|
}
|
|
|
|
if wanted.listElem != nil {
|
|
|
|
db.freeConn.Remove(wanted.listElem)
|
|
|
|
wanted.listElem = nil
|
2013-07-16 08:54:42 +02:00
|
|
|
wanted.inUse = true
|
|
|
|
return wanted, nil
|
|
|
|
}
|
|
|
|
// TODO(bradfitz): shouldn't get here. After Go 1.1, change this to:
|
|
|
|
// panic("connIfFree call requested a non-closed, non-busy, non-free conn")
|
|
|
|
// Which passes all the tests, but I'm too paranoid to include this
|
|
|
|
// late in Go 1.1.
|
|
|
|
// Instead, treat it like a busy connection:
|
|
|
|
return nil, errConnBusy
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
// putConnHook is a hook for testing.
|
2013-07-16 08:54:42 +02:00
|
|
|
var putConnHook func(*DB, *driverConn)
|
|
|
|
|
|
|
|
// noteUnusedDriverStatement notes that si is no longer used and should
|
|
|
|
// be closed whenever possible (when c is next not in use), unless c is
|
|
|
|
// already closed.
|
|
|
|
func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) {
|
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
if c.inUse {
|
|
|
|
c.onPut = append(c.onPut, func() {
|
|
|
|
si.Close()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
if !c.finalClosed {
|
|
|
|
si.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debugGetPut determines whether getConn & putConn calls' stack traces
|
|
|
|
// are returned for more verbose crashes.
|
|
|
|
const debugGetPut = false
|
2012-03-30 23:27:11 +02:00
|
|
|
|
|
|
|
// putConn adds a connection to the db's free pool.
|
2012-12-22 02:15:33 +01:00
|
|
|
// err is optionally the last error that occurred on this connection.
|
2013-07-16 08:54:42 +02:00
|
|
|
func (db *DB) putConn(dc *driverConn, err error) {
|
|
|
|
db.mu.Lock()
|
|
|
|
if !dc.inUse {
|
|
|
|
if debugGetPut {
|
|
|
|
fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
|
|
|
|
}
|
|
|
|
panic("sql: connection returned that was never out")
|
|
|
|
}
|
|
|
|
if debugGetPut {
|
|
|
|
db.lastPut[dc] = stack()
|
|
|
|
}
|
|
|
|
dc.inUse = false
|
|
|
|
|
|
|
|
for _, fn := range dc.onPut {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
dc.onPut = nil
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
// Don't reuse bad connections.
|
2013-11-06 20:49:01 +01:00
|
|
|
// Since the conn is considered bad and is being discarded, treat it
|
|
|
|
// as closed. Don't decrement the open count here, finalClose will
|
|
|
|
// take care of that.
|
|
|
|
db.maybeOpenNewConnections()
|
2013-07-16 08:54:42 +02:00
|
|
|
db.mu.Unlock()
|
2013-11-06 20:49:01 +01:00
|
|
|
dc.Close()
|
2012-03-30 23:27:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if putConnHook != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
putConnHook(db, dc)
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
added := db.putConnDBLocked(dc, nil)
|
2012-03-30 23:27:11 +02:00
|
|
|
db.mu.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if !added {
|
|
|
|
dc.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Satisfy a connRequest or put the driverConn in the idle pool and return true
|
|
|
|
// or return false.
|
|
|
|
// putConnDBLocked will satisfy a connRequest if there is one, or it will
|
|
|
|
// return the *driverConn to the freeConn list if err != nil and the idle
|
|
|
|
// connection limit would not be reached.
|
|
|
|
// If err != nil, the value of dc is ignored.
|
|
|
|
// If err == nil, then dc must not equal nil.
|
|
|
|
// If a connRequest was fullfilled or the *driverConn was placed in the
|
|
|
|
// freeConn list, then true is returned, otherwise false is returned.
|
|
|
|
func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
|
|
|
|
if db.connRequests.Len() > 0 {
|
|
|
|
req := db.connRequests.Front().Value.(connRequest)
|
|
|
|
db.connRequests.Remove(db.connRequests.Front())
|
|
|
|
if err != nil {
|
|
|
|
req <- err
|
|
|
|
} else {
|
|
|
|
dc.inUse = true
|
|
|
|
req <- dc
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else if err == nil && !db.closed && db.maxIdleConnsLocked() > 0 && db.maxIdleConnsLocked() > db.freeConn.Len() {
|
|
|
|
dc.listElem = db.freeConn.PushFront(dc)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Prepare creates a prepared statement for later queries or executions.
|
|
|
|
// Multiple queries or executions may be run concurrently from the
|
|
|
|
// returned statement.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (db *DB) Prepare(query string) (*Stmt, error) {
|
2012-03-30 23:27:11 +02:00
|
|
|
var stmt *Stmt
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stmt, err = db.prepare(query)
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stmt, err
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func (db *DB) prepare(query string) (*Stmt, error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
// TODO: check if db.driver supports an optional
|
|
|
|
// driver.Preparer interface and call that instead, if so,
|
|
|
|
// otherwise we make a prepared statement that's bound
|
|
|
|
// to a connection, and to execute this prepared statement
|
|
|
|
// we either need to use this connection (if it's free), else
|
|
|
|
// get a new connection + re-prepare + execute on that one.
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := db.conn()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.prepareLocked(query)
|
|
|
|
dc.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
db.putConn(dc, err)
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
stmt := &Stmt{
|
2011-10-27 01:57:58 +02:00
|
|
|
db: db,
|
|
|
|
query: query,
|
2013-07-16 08:54:42 +02:00
|
|
|
css: []connStmt{{dc, si}},
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
db.addDep(stmt, stmt)
|
|
|
|
db.putConn(dc, nil)
|
2011-10-27 01:57:58 +02:00
|
|
|
return stmt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes a query without returning any rows.
|
2013-07-16 08:54:42 +02:00
|
|
|
// The args are for any placeholder parameters in the query.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
|
2012-03-30 23:27:11 +02:00
|
|
|
var res Result
|
2012-10-23 06:31:11 +02:00
|
|
|
var err error
|
2012-03-30 23:27:11 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
2012-10-23 06:31:11 +02:00
|
|
|
res, err = db.exec(query, args)
|
2012-03-30 23:27:11 +02:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
break
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
return res, err
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func (db *DB) exec(query string, args []interface{}) (res Result, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := db.conn()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-10-03 07:27:36 +02:00
|
|
|
defer func() {
|
2013-07-16 08:54:42 +02:00
|
|
|
db.putConn(dc, err)
|
2012-10-03 07:27:36 +02:00
|
|
|
}()
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
if execer, ok := dc.ci.(driver.Execer); ok {
|
2012-10-23 06:31:11 +02:00
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
2012-10-23 06:31:11 +02:00
|
|
|
resi, err := execer.Exec(query, dargs)
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Unlock()
|
2011-12-13 00:40:51 +01:00
|
|
|
if err != driver.ErrSkip {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return driverResult{dc, resi}, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
dc.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
defer withLock(dc, func() { si.Close() })
|
|
|
|
return resultFromStatement(driverStmt{dc, si}, args...)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query executes a query that returns rows, typically a SELECT.
|
2013-01-29 21:52:43 +01:00
|
|
|
// The args are for any placeholder parameters in the query.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
var rows *Rows
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
rows, err = db.query(query, args)
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) query(query string, args []interface{}) (*Rows, error) {
|
|
|
|
ci, err := db.conn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
return db.queryConn(ci, ci.releaseConn, query, args)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// queryConn executes a query on the given connection.
|
|
|
|
// The connection gets released by the releaseConn function.
|
|
|
|
func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
|
|
|
|
if queryer, ok := dc.ci.(driver.Queryer); ok {
|
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
if err != nil {
|
|
|
|
releaseConn(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dc.Lock()
|
|
|
|
rowsi, err := queryer.Query(query, dargs)
|
|
|
|
dc.Unlock()
|
|
|
|
if err != driver.ErrSkip {
|
|
|
|
if err != nil {
|
|
|
|
releaseConn(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Note: ownership of dc passes to the *Rows, to be freed
|
|
|
|
// with releaseConn.
|
|
|
|
rows := &Rows{
|
|
|
|
dc: dc,
|
|
|
|
releaseConn: releaseConn,
|
|
|
|
rowsi: rowsi,
|
|
|
|
}
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
dc.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
releaseConn(err)
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
ds := driverStmt{dc, si}
|
|
|
|
rowsi, err := rowsiFromStatement(ds, args...)
|
2012-01-25 21:56:26 +01:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si.Close()
|
|
|
|
dc.Unlock()
|
2013-11-06 20:49:01 +01:00
|
|
|
releaseConn(err)
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
// Note: ownership of ci passes to the *Rows, to be freed
|
|
|
|
// with releaseConn.
|
|
|
|
rows := &Rows{
|
|
|
|
dc: dc,
|
|
|
|
releaseConn: releaseConn,
|
|
|
|
rowsi: rowsi,
|
|
|
|
closeStmt: si,
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
return rows, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRow executes a query that is expected to return at most one row.
|
|
|
|
// QueryRow always return a non-nil value. Errors are deferred until
|
|
|
|
// Row's Scan method is called.
|
|
|
|
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
|
|
|
rows, err := db.Query(query, args...)
|
2011-12-07 02:11:29 +01:00
|
|
|
return &Row{rows: rows, err: err}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// Begin starts a transaction. The isolation level is dependent on
|
2011-10-27 01:57:58 +02:00
|
|
|
// the driver.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (db *DB) Begin() (*Tx, error) {
|
2012-03-30 23:27:11 +02:00
|
|
|
var tx *Tx
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
tx, err = db.begin()
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) begin() (tx *Tx, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := db.conn()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
txi, err := dc.ci.Begin()
|
|
|
|
dc.Unlock()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
db.putConn(dc, err)
|
2012-12-22 02:15:33 +01:00
|
|
|
return nil, err
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
return &Tx{
|
|
|
|
db: db,
|
2013-07-16 08:54:42 +02:00
|
|
|
dc: dc,
|
2011-12-07 02:11:29 +01:00
|
|
|
txi: txi,
|
|
|
|
}, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Driver returns the database's underlying driver.
|
2011-10-27 01:57:58 +02:00
|
|
|
func (db *DB) Driver() driver.Driver {
|
|
|
|
return db.driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tx is an in-progress database transaction.
|
2011-12-07 02:11:29 +01:00
|
|
|
//
|
|
|
|
// A transaction must end with a call to Commit or Rollback.
|
|
|
|
//
|
|
|
|
// After a call to Commit or Rollback, all operations on the
|
2012-03-02 17:38:43 +01:00
|
|
|
// transaction fail with ErrTxDone.
|
2011-10-27 01:57:58 +02:00
|
|
|
type Tx struct {
|
2011-12-07 02:11:29 +01:00
|
|
|
db *DB
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// dc is owned exclusively until Commit or Rollback, at which point
|
2011-12-07 02:11:29 +01:00
|
|
|
// it's returned with putConn.
|
2013-07-16 08:54:42 +02:00
|
|
|
dc *driverConn
|
2011-12-07 02:11:29 +01:00
|
|
|
txi driver.Tx
|
|
|
|
|
|
|
|
// done transitions from false to true exactly once, on Commit
|
|
|
|
// or Rollback. once done, all operations fail with
|
2012-03-02 17:38:43 +01:00
|
|
|
// ErrTxDone.
|
2011-12-07 02:11:29 +01:00
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
|
2011-12-07 02:11:29 +01:00
|
|
|
|
|
|
|
func (tx *Tx) close() {
|
|
|
|
if tx.done {
|
|
|
|
panic("double close") // internal error
|
|
|
|
}
|
|
|
|
tx.done = true
|
2013-07-16 08:54:42 +02:00
|
|
|
tx.db.putConn(tx.dc, nil)
|
|
|
|
tx.dc = nil
|
2011-12-07 02:11:29 +01:00
|
|
|
tx.txi = nil
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func (tx *Tx) grabConn() (*driverConn, error) {
|
2011-12-07 02:11:29 +01:00
|
|
|
if tx.done {
|
2012-03-02 17:38:43 +01:00
|
|
|
return nil, ErrTxDone
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return tx.dc, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit commits the transaction.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (tx *Tx) Commit() error {
|
2011-12-07 02:11:29 +01:00
|
|
|
if tx.done {
|
2012-03-02 17:38:43 +01:00
|
|
|
return ErrTxDone
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
defer tx.close()
|
2013-07-16 08:54:42 +02:00
|
|
|
tx.dc.Lock()
|
|
|
|
defer tx.dc.Unlock()
|
2011-12-07 02:11:29 +01:00
|
|
|
return tx.txi.Commit()
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback aborts the transaction.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (tx *Tx) Rollback() error {
|
2011-12-07 02:11:29 +01:00
|
|
|
if tx.done {
|
2012-03-02 17:38:43 +01:00
|
|
|
return ErrTxDone
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
defer tx.close()
|
2013-07-16 08:54:42 +02:00
|
|
|
tx.dc.Lock()
|
|
|
|
defer tx.dc.Unlock()
|
2011-12-07 02:11:29 +01:00
|
|
|
return tx.txi.Rollback()
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Prepare creates a prepared statement for use within a transaction.
|
2011-12-07 02:11:29 +01:00
|
|
|
//
|
2011-12-13 20:16:27 +01:00
|
|
|
// The returned statement operates within the transaction and can no longer
|
|
|
|
// be used once the transaction has been committed or rolled back.
|
|
|
|
//
|
|
|
|
// To use an existing prepared statement on this transaction, see Tx.Stmt.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
2011-12-13 20:16:27 +01:00
|
|
|
// TODO(bradfitz): We could be more efficient here and either
|
|
|
|
// provide a method to take an existing Stmt (created on
|
|
|
|
// perhaps a different Conn), and re-create it on this Conn if
|
|
|
|
// necessary. Or, better: keep a map in DB of query string to
|
|
|
|
// Stmts, and have Stmt.Execute do the right thing and
|
|
|
|
// re-prepare if the Conn in use doesn't have that prepared
|
|
|
|
// statement. But we'll want to avoid caching the statement
|
|
|
|
// in the case where we only call conn.Prepare implicitly
|
|
|
|
// (such as in db.Exec or tx.Exec), but the caller package
|
|
|
|
// can't be holding a reference to the returned statement.
|
|
|
|
// Perhaps just looking at the reference count (by noting
|
|
|
|
// Stmt.Close) would be enough. We might also want a finalizer
|
|
|
|
// on Stmt to drop the reference count.
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := tx.grabConn()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
dc.Unlock()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt := &Stmt{
|
2013-07-16 08:54:42 +02:00
|
|
|
db: tx.db,
|
|
|
|
tx: tx,
|
|
|
|
txsi: &driverStmt{
|
|
|
|
Locker: dc,
|
|
|
|
si: si,
|
|
|
|
},
|
2011-12-07 02:11:29 +01:00
|
|
|
query: query,
|
|
|
|
}
|
|
|
|
return stmt, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Stmt returns a transaction-specific prepared statement from
|
|
|
|
// an existing statement.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
|
|
|
|
// ...
|
|
|
|
// tx, err := db.Begin()
|
|
|
|
// ...
|
|
|
|
// res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
|
|
|
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
|
|
|
// TODO(bradfitz): optimize this. Currently this re-prepares
|
|
|
|
// each time. This is fine for now to illustrate the API but
|
|
|
|
// we should really cache already-prepared statements
|
|
|
|
// per-Conn. See also the big comment in Tx.Prepare.
|
|
|
|
|
|
|
|
if tx.db != stmt.db {
|
|
|
|
return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := tx.grabConn()
|
2011-12-13 20:16:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return &Stmt{stickyErr: err}
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.ci.Prepare(stmt.query)
|
|
|
|
dc.Unlock()
|
2011-12-13 20:16:27 +01:00
|
|
|
return &Stmt{
|
2013-07-16 08:54:42 +02:00
|
|
|
db: tx.db,
|
|
|
|
tx: tx,
|
|
|
|
txsi: &driverStmt{
|
|
|
|
Locker: dc,
|
|
|
|
si: si,
|
|
|
|
},
|
2011-12-13 20:16:27 +01:00
|
|
|
query: stmt.query,
|
|
|
|
stickyErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// Exec executes a query that doesn't return rows.
|
|
|
|
// For example: an INSERT and UPDATE.
|
2011-12-07 02:11:29 +01:00
|
|
|
func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := tx.grabConn()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
if execer, ok := dc.ci.(driver.Execer); ok {
|
2012-10-23 06:31:11 +02:00
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
2012-10-23 06:31:11 +02:00
|
|
|
resi, err := execer.Exec(query, dargs)
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Unlock()
|
2012-03-02 17:38:43 +01:00
|
|
|
if err == nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
return driverResult{dc, resi}, nil
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
if err != driver.ErrSkip {
|
2011-12-07 02:11:29 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
dc.Unlock()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
defer withLock(dc, func() { si.Close() })
|
2011-12-13 00:40:51 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
return resultFromStatement(driverStmt{dc, si}, args...)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query executes a query that returns rows, typically a SELECT.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := tx.grabConn()
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
releaseConn := func(error) {}
|
|
|
|
return tx.db.queryConn(dc, releaseConn, query, args)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRow executes a query that is expected to return at most one row.
|
|
|
|
// QueryRow always return a non-nil value. Errors are deferred until
|
|
|
|
// Row's Scan method is called.
|
|
|
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
2011-12-07 02:11:29 +01:00
|
|
|
rows, err := tx.Query(query, args...)
|
|
|
|
return &Row{rows: rows, err: err}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// connStmt is a prepared statement on a particular connection.
|
|
|
|
type connStmt struct {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc *driverConn
|
2011-10-27 01:57:58 +02:00
|
|
|
si driver.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.
|
|
|
|
type Stmt struct {
|
|
|
|
// Immutable:
|
2011-12-13 20:16:27 +01:00
|
|
|
db *DB // where we came from
|
|
|
|
query string // that created the Stmt
|
|
|
|
stickyErr error // if non-nil, this error is returned for all operations
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
closemu sync.RWMutex // held exclusively during close, for read otherwise.
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// If in a transaction, else both nil:
|
|
|
|
tx *Tx
|
2013-07-16 08:54:42 +02:00
|
|
|
txsi *driverStmt
|
2011-12-07 02:11:29 +01:00
|
|
|
|
|
|
|
mu sync.Mutex // protects the rest of the fields
|
2011-10-27 01:57:58 +02:00
|
|
|
closed bool
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// css is a list of underlying driver statement interfaces
|
|
|
|
// that are valid on particular connections. This is only
|
|
|
|
// used if tx == nil and one is found that has idle
|
|
|
|
// connections. If tx != nil, txsi is always used.
|
|
|
|
css []connStmt
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes a prepared statement with the given arguments and
|
|
|
|
// returns a Result summarizing the effect of the statement.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (s *Stmt) Exec(args ...interface{}) (Result, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
s.closemu.RLock()
|
|
|
|
defer s.closemu.RUnlock()
|
|
|
|
dc, releaseConn, si, err := s.connStmt()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
defer releaseConn(nil)
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
return resultFromStatement(driverStmt{dc, si}, args...)
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) {
|
|
|
|
ds.Lock()
|
|
|
|
want := ds.si.NumInput()
|
|
|
|
ds.Unlock()
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// -1 means the driver doesn't know how to count the number of
|
|
|
|
// placeholders, so we won't sanity check input here and instead let the
|
|
|
|
// driver deal with errors.
|
2013-07-16 08:54:42 +02:00
|
|
|
if want != -1 && len(args) != want {
|
2012-01-13 06:11:45 +01:00
|
|
|
return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
dargs, err := driverArgs(&ds, args)
|
2012-10-23 06:31:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
ds.Lock()
|
|
|
|
resi, err := ds.si.Exec(dargs)
|
|
|
|
ds.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return driverResult{ds.Locker, resi}, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// connStmt returns a free driver connection on which to execute the
|
|
|
|
// statement, a function to call to release the connection, and a
|
|
|
|
// statement bound to that connection.
|
2013-07-16 08:54:42 +02:00
|
|
|
func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) {
|
2012-01-25 21:56:26 +01:00
|
|
|
if err = s.stickyErr; err != nil {
|
|
|
|
return
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
if s.closed {
|
2011-12-07 02:11:29 +01:00
|
|
|
s.mu.Unlock()
|
2012-01-13 06:11:45 +01:00
|
|
|
err = errors.New("sql: statement is closed")
|
2011-12-07 02:11:29 +01:00
|
|
|
return
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2011-12-07 02:11:29 +01:00
|
|
|
|
|
|
|
// In a transaction, we always use the connection that the
|
|
|
|
// transaction was created on.
|
|
|
|
if s.tx != nil {
|
|
|
|
s.mu.Unlock()
|
|
|
|
ci, err = s.tx.grabConn() // blocks, waiting for the connection.
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
releaseConn = func(error) {}
|
|
|
|
return ci, releaseConn, s.txsi.si, nil
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
var cs connStmt
|
|
|
|
match := false
|
2013-07-16 08:54:42 +02:00
|
|
|
for i := 0; i < len(s.css); i++ {
|
|
|
|
v := s.css[i]
|
|
|
|
_, err := s.db.connIfFree(v.dc)
|
|
|
|
if err == nil {
|
|
|
|
match = true
|
2011-10-27 01:57:58 +02:00
|
|
|
cs = v
|
|
|
|
break
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if err == errConnClosed {
|
|
|
|
// Lazily remove dead conn from our freelist.
|
|
|
|
s.css[i] = s.css[len(s.css)-1]
|
|
|
|
s.css = s.css[:len(s.css)-1]
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
// Make a new conn if all are busy.
|
|
|
|
// TODO(bradfitz): or wait for one? make configurable later?
|
|
|
|
if !match {
|
2012-03-30 23:27:11 +02:00
|
|
|
for i := 0; ; i++ {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc, err := s.db.conn()
|
2012-03-30 23:27:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
dc.Lock()
|
|
|
|
si, err := dc.prepareLocked(s.query)
|
|
|
|
dc.Unlock()
|
2012-03-30 23:27:11 +02:00
|
|
|
if err == driver.ErrBadConn && i < 10 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
2013-07-16 08:54:42 +02:00
|
|
|
cs = connStmt{dc, si}
|
2012-03-30 23:27:11 +02:00
|
|
|
s.css = append(s.css, cs)
|
|
|
|
s.mu.Unlock()
|
|
|
|
break
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
conn := cs.dc
|
2013-11-06 20:49:01 +01:00
|
|
|
return conn, conn.releaseConn, cs.si, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query executes a prepared query statement with the given arguments
|
|
|
|
// and returns the query results as a *Rows.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
s.closemu.RLock()
|
|
|
|
defer s.closemu.RUnlock()
|
|
|
|
|
|
|
|
dc, releaseConn, si, err := s.connStmt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := driverStmt{dc, si}
|
|
|
|
rowsi, err := rowsiFromStatement(ds, args...)
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
releaseConn(err)
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Note: ownership of ci passes to the *Rows, to be freed
|
|
|
|
// with releaseConn.
|
|
|
|
rows := &Rows{
|
|
|
|
dc: dc,
|
|
|
|
rowsi: rowsi,
|
|
|
|
// releaseConn set below
|
|
|
|
}
|
|
|
|
s.db.addDep(s, rows)
|
|
|
|
rows.releaseConn = func(err error) {
|
|
|
|
releaseConn(err)
|
|
|
|
s.db.removeDep(s, rows)
|
|
|
|
}
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) {
|
|
|
|
ds.Lock()
|
|
|
|
want := ds.si.NumInput()
|
|
|
|
ds.Unlock()
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// -1 means the driver doesn't know how to count the number of
|
|
|
|
// placeholders, so we won't sanity check input here and instead let the
|
|
|
|
// driver deal with errors.
|
2013-07-16 08:54:42 +02:00
|
|
|
if want != -1 && len(args) != want {
|
|
|
|
return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
dargs, err := driverArgs(&ds, args)
|
2011-12-13 00:40:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
ds.Lock()
|
|
|
|
rowsi, err := ds.si.Query(dargs)
|
|
|
|
ds.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return rowsi, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRow executes a prepared query statement with the given arguments.
|
|
|
|
// If an error occurs during the execution of the statement, that error will
|
|
|
|
// be returned by a call to Scan on the returned *Row, which is always non-nil.
|
|
|
|
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
|
|
|
|
// Otherwise, the *Row's Scan scans the first selected row and discards
|
|
|
|
// the rest.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
//
|
|
|
|
// var name string
|
2012-03-02 17:38:43 +01:00
|
|
|
// err := nameByUseridStmt.QueryRow(id).Scan(&name)
|
2011-10-27 01:57:58 +02:00
|
|
|
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
|
|
|
rows, err := s.Query(args...)
|
|
|
|
if err != nil {
|
|
|
|
return &Row{err: err}
|
|
|
|
}
|
|
|
|
return &Row{rows: rows}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the statement.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (s *Stmt) Close() error {
|
2013-07-16 08:54:42 +02:00
|
|
|
s.closemu.Lock()
|
|
|
|
defer s.closemu.Unlock()
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
if s.stickyErr != nil {
|
|
|
|
return s.stickyErr
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
if s.closed {
|
2013-11-06 20:49:01 +01:00
|
|
|
s.mu.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.closed = true
|
2011-12-07 02:11:29 +01:00
|
|
|
|
|
|
|
if s.tx != nil {
|
|
|
|
s.txsi.Close()
|
2013-11-06 20:49:01 +01:00
|
|
|
s.mu.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
s.mu.Unlock()
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
return s.db.removeDep(s, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stmt) finalClose() error {
|
2013-11-06 20:49:01 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.css != nil {
|
|
|
|
for _, v := range s.css {
|
|
|
|
s.db.noteUnusedDriverStatement(v.dc, v.si)
|
|
|
|
v.dc.removeOpenStmt(v.si)
|
|
|
|
}
|
|
|
|
s.css = nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rows is the result of a query. Its cursor starts before the first row
|
|
|
|
// of the result set. Use Next to advance through the rows:
|
|
|
|
//
|
|
|
|
// rows, err := db.Query("SELECT ...")
|
|
|
|
// ...
|
|
|
|
// for rows.Next() {
|
|
|
|
// var id int
|
|
|
|
// var name string
|
|
|
|
// err = rows.Scan(&id, &name)
|
|
|
|
// ...
|
|
|
|
// }
|
2011-12-07 02:11:29 +01:00
|
|
|
// err = rows.Err() // get any error encountered during iteration
|
2011-10-27 01:57:58 +02:00
|
|
|
// ...
|
|
|
|
type Rows struct {
|
2013-07-16 08:54:42 +02:00
|
|
|
dc *driverConn // owned; must call releaseConn when closed to release
|
2012-03-30 23:27:11 +02:00
|
|
|
releaseConn func(error)
|
2011-12-07 02:11:29 +01:00
|
|
|
rowsi driver.Rows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
closed bool
|
2012-03-02 21:01:37 +01:00
|
|
|
lastcols []driver.Value
|
2013-11-06 20:49:01 +01:00
|
|
|
lasterr error // non-nil only if closed is true
|
2013-07-16 08:54:42 +02:00
|
|
|
closeStmt driver.Stmt // if non-nil, statement to Close on close
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next prepares the next result row for reading with the Scan method.
|
|
|
|
// It returns true on success, false if there is no next result row.
|
|
|
|
// Every call to Scan, even the first one, must be preceded by a call
|
|
|
|
// to Next.
|
|
|
|
func (rs *Rows) Next() bool {
|
|
|
|
if rs.closed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if rs.lastcols == nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
rs.lasterr = rs.rowsi.Next(rs.lastcols)
|
2013-11-06 20:49:01 +01:00
|
|
|
if rs.lasterr != nil {
|
2012-01-25 21:56:26 +01:00
|
|
|
rs.Close()
|
2013-11-06 20:49:01 +01:00
|
|
|
return false
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return true
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// Err returns the error, if any, that was encountered during iteration.
|
2013-11-06 20:49:01 +01:00
|
|
|
// Err may be called after an explicit or implicit Close.
|
2011-12-07 02:11:29 +01:00
|
|
|
func (rs *Rows) Err() error {
|
2011-12-03 03:17:34 +01:00
|
|
|
if rs.lasterr == io.EOF {
|
2011-10-27 01:57:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rs.lasterr
|
|
|
|
}
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// Columns returns the column names.
|
|
|
|
// Columns returns an error if the rows are closed, or if the rows
|
|
|
|
// are from QueryRow and there was a deferred error.
|
|
|
|
func (rs *Rows) Columns() ([]string, error) {
|
|
|
|
if rs.closed {
|
|
|
|
return nil, errors.New("sql: Rows are closed")
|
|
|
|
}
|
|
|
|
if rs.rowsi == nil {
|
|
|
|
return nil, errors.New("sql: no Rows available")
|
|
|
|
}
|
|
|
|
return rs.rowsi.Columns(), nil
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// Scan copies the columns in the current row into the values pointed
|
2012-01-25 22:54:22 +01:00
|
|
|
// at by dest.
|
|
|
|
//
|
|
|
|
// If an argument has type *[]byte, Scan saves in that argument a copy
|
|
|
|
// of the corresponding data. The copy is owned by the caller and can
|
|
|
|
// be modified and held indefinitely. The copy can be avoided by using
|
|
|
|
// an argument of type *RawBytes instead; see the documentation for
|
|
|
|
// RawBytes for restrictions on its use.
|
2012-02-09 09:19:58 +01:00
|
|
|
//
|
|
|
|
// If an argument has type *interface{}, Scan copies the value
|
|
|
|
// provided by the underlying driver without conversion. If the value
|
|
|
|
// is of type []byte, a copy is made and the caller owns the result.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (rs *Rows) Scan(dest ...interface{}) error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if rs.closed {
|
2013-11-06 20:49:01 +01:00
|
|
|
return errors.New("sql: Rows are closed")
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
if rs.lastcols == nil {
|
2012-01-13 06:11:45 +01:00
|
|
|
return errors.New("sql: Scan called without calling Next")
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
if len(dest) != len(rs.lastcols) {
|
2012-01-13 06:11:45 +01:00
|
|
|
return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
for i, sv := range rs.lastcols {
|
|
|
|
err := convertAssign(dest[i], sv)
|
|
|
|
if err != nil {
|
2012-01-13 06:11:45 +01:00
|
|
|
return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
var rowsCloseHook func(*Rows, *error)
|
|
|
|
|
|
|
|
// Close closes the Rows, preventing further enumeration. If Next returns
|
|
|
|
// false, the Rows are closed automatically and it will suffice to check the
|
|
|
|
// result of Err. Close is idempotent and does not affect the result of Err.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (rs *Rows) Close() error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if rs.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rs.closed = true
|
|
|
|
err := rs.rowsi.Close()
|
2013-11-06 20:49:01 +01:00
|
|
|
if fn := rowsCloseHook; fn != nil {
|
|
|
|
fn(rs, &err)
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
if rs.closeStmt != nil {
|
|
|
|
rs.closeStmt.Close()
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
rs.releaseConn(err)
|
2011-10-27 01:57:58 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Row is the result of calling QueryRow to select a single row.
|
|
|
|
type Row struct {
|
|
|
|
// One of these two will be non-nil:
|
2011-12-03 03:17:34 +01:00
|
|
|
err error // deferred error for easy chaining
|
2011-10-27 01:57:58 +02:00
|
|
|
rows *Rows
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan copies the columns from the matched row into the values
|
|
|
|
// pointed at by dest. If more than one row matches the query,
|
|
|
|
// Scan uses the first row and discards the rest. If no row matches
|
|
|
|
// the query, Scan returns ErrNoRows.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *Row) Scan(dest ...interface{}) error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
|
|
|
|
// TODO(bradfitz): for now we need to defensively clone all
|
2012-03-30 23:27:11 +02:00
|
|
|
// []byte that the driver returned (not permitting
|
2012-02-01 20:26:59 +01:00
|
|
|
// *RawBytes in Rows.Scan), since we're about to close
|
2012-01-25 21:56:26 +01:00
|
|
|
// the Rows in our defer, when we return from this function.
|
|
|
|
// the contract with the driver.Next(...) interface is that it
|
|
|
|
// can return slices into read-only temporary memory that's
|
|
|
|
// only valid until the next Scan/Close. But the TODO is that
|
|
|
|
// for a lot of drivers, this copy will be unnecessary. We
|
|
|
|
// should provide an optional interface for drivers to
|
|
|
|
// implement to say, "don't worry, the []bytes that I return
|
|
|
|
// from Next will not be modified again." (for instance, if
|
|
|
|
// they were obtained from the network anyway) But for now we
|
|
|
|
// don't care.
|
2013-11-06 20:49:01 +01:00
|
|
|
defer r.rows.Close()
|
2012-01-25 21:56:26 +01:00
|
|
|
for _, dp := range dest {
|
2012-01-25 22:54:22 +01:00
|
|
|
if _, ok := dp.(*RawBytes); ok {
|
|
|
|
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
|
|
|
|
if !r.rows.Next() {
|
|
|
|
return ErrNoRows
|
|
|
|
}
|
|
|
|
err := r.rows.Scan(dest...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Result summarizes an executed SQL command.
|
|
|
|
type Result interface {
|
2013-11-27 02:05:38 +01:00
|
|
|
// LastInsertId returns the integer generated by the database
|
|
|
|
// in response to a command. Typically this will be from an
|
|
|
|
// "auto increment" column when inserting a new row. Not all
|
|
|
|
// databases support this feature, and the syntax of such
|
|
|
|
// statements varies.
|
2011-12-03 03:17:34 +01:00
|
|
|
LastInsertId() (int64, error)
|
2013-11-27 02:05:38 +01:00
|
|
|
|
|
|
|
// RowsAffected returns the number of rows affected by an
|
|
|
|
// update, insert, or delete. Not every database or database
|
|
|
|
// driver may support this.
|
2011-12-03 03:17:34 +01:00
|
|
|
RowsAffected() (int64, error)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
type driverResult struct {
|
|
|
|
sync.Locker // the *driverConn
|
|
|
|
resi driver.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr driverResult) LastInsertId() (int64, error) {
|
|
|
|
dr.Lock()
|
|
|
|
defer dr.Unlock()
|
|
|
|
return dr.resi.LastInsertId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr driverResult) RowsAffected() (int64, error) {
|
|
|
|
dr.Lock()
|
|
|
|
defer dr.Unlock()
|
|
|
|
return dr.resi.RowsAffected()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stack() string {
|
|
|
|
var buf [2 << 10]byte
|
|
|
|
return string(buf[:runtime.Stack(buf[:], false)])
|
|
|
|
}
|
|
|
|
|
|
|
|
// withLock runs while holding lk.
|
|
|
|
func withLock(lk sync.Locker, fn func()) {
|
|
|
|
lk.Lock()
|
|
|
|
fn()
|
|
|
|
lk.Unlock()
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|