Skip to content
Snippets Groups Projects
sql.go 34.5 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 http://golang.org/s/sqldrivers for a list of drivers.
    
    	"database/sql/driver"
    
    	"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 {
    
    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
    }
    
    
    // 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.
    
    // NullString implements the Scanner interface so
    
    // it can be used as a scan destination:
    //
    
    //  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 interface{}) 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 interface{}) 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
    }
    
    // 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 interface{}) 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 interface{}) 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
    }
    
    
    // Scanner is an interface used by Scan.
    type Scanner interface {
    	// Scan assigns a value from a database driver.
    
    	// The src value will be of one of the following restricted
    
    	// set of types:
    	//
    	//    int64
    	//    float64
    	//    bool
    	//    []byte
    
    	//    nil - for NULL values
    	//
    	// An error should be returned if the value can not be stored
    	// without loss of information.
    
    	Scan(src interface{}) error
    
    }
    
    // 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.
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    var ErrNoRows = errors.New("sql: no rows in result set")
    
    
    // DB is a database handle. It's safe for concurrent use by multiple
    // goroutines.
    
    // 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.
    
    type DB struct {
    	driver driver.Driver
    	dsn    string
    
    
    	mu        sync.Mutex           // protects following fields
    
    	outConn   map[*driverConn]bool // whether the conn is in use
    	freeConn  []*driverConn
    
    	closed    bool
    	dep       map[finalCloser]depSet
    
    	onConnPut map[*driverConn][]func() // code (with mu held) run when conn is next returned
    	lastPut   map[*driverConn]string   // stacktrace of last conn's put; debug only
    
    	maxIdle   int                      // zero means defaultMaxIdleConns; negative means 0
    
    }
    
    // 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
    }
    
    // the dc.db's Mutex is held.
    func (dc *driverConn) closeDBLocked() 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
    	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
    	return dc.db.removeDep(dc, dc)
    }
    
    func (dc *driverConn) finalClose() error {
    	dc.Lock()
    	err := dc.ci.Close()
    	dc.ci = nil
    	dc.Unlock()
    	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 (*DB).get
    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))
    	done := false
    
    	xdep := db.dep[x]
    	if xdep != nil {
    		delete(xdep, dep)
    		if len(xdep) == 0 {
    			delete(db.dep, x)
    			done = true
    		}
    	}
    
    	if !done {
    
    		return func() error { return nil }
    	}
    	return func() error {
    		//println(fmt.Sprintf("calling final close on %T %v (%#v)", x, x, x))
    		return x.finalClose()
    
    }
    
    // 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 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.
    
    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)
    
    	db := &DB{
    		driver:    driveri,
    		dsn:       dataSourceName,
    
    		outConn:   make(map[*driverConn]bool),
    		lastPut:   make(map[*driverConn]string),
    		onConnPut: make(map[*driverConn][]func()),
    
    // 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.
    
    // Close closes the database, releasing any open resources.
    func (db *DB) Close() error {
    	db.mu.Lock()
    	defer db.mu.Unlock()
    	var err error
    
    	for _, dc := range db.freeConn {
    
    		if err1 != nil {
    			err = err1
    		}
    	}
    	db.freeConn = nil
    	db.closed = true
    	return err
    }
    
    
    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.
    //
    // If n <= 0, no idle connections are retained.
    func (db *DB) SetMaxIdleConns(n int) {
    	db.mu.Lock()
    	defer db.mu.Unlock()
    	if n > 0 {
    		db.maxIdle = n
    	} else {
    		// No idle connections.
    		db.maxIdle = -1
    	}
    	for len(db.freeConn) > 0 && len(db.freeConn) > n {
    		nfree := len(db.freeConn)
    		dc := db.freeConn[nfree-1]
    		db.freeConn[nfree-1] = nil
    		db.freeConn = db.freeConn[:nfree-1]
    
    // conn returns a newly-opened or cached *driverConn
    func (db *DB) conn() (*driverConn, error) {
    
    		return nil, errors.New("sql: database is closed")
    	}
    
    	if n := len(db.freeConn); n > 0 {
    		conn := db.freeConn[n-1]
    		db.freeConn = db.freeConn[:n-1]
    
    		db.outConn[conn] = true
    
    		db.mu.Unlock()
    		return conn, nil
    	}
    	db.mu.Unlock()
    
    
    	ci, err := db.driver.Open(db.dsn)
    	if err != nil {
    		return nil, err
    
    	db.outConn[dc] = true
    	db.mu.Unlock()
    	return dc, nil
    
    // connIfFree returns (wanted, true) if wanted is still a valid conn and
    // isn't in use.
    //
    // If wanted is valid but in use, connIfFree returns (wanted, false).
    // If wanted is invalid, connIfFre returns (nil, false).
    
    func (db *DB) connIfFree(wanted *driverConn) (conn *driverConn, ok bool) {
    
    	db.mu.Lock()
    	defer db.mu.Unlock()
    
    	if db.outConn[wanted] {
    		return conn, false
    	}
    
    	for i, conn := range db.freeConn {
    		if conn != wanted {
    			continue
    
    		db.freeConn[i] = db.freeConn[len(db.freeConn)-1]
    		db.freeConn = db.freeConn[:len(db.freeConn)-1]
    
    		db.outConn[wanted] = true
    
    		return wanted, true
    
    // putConnHook is a hook for testing.
    
    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 db.outConn[c] {
    		db.onConnPut[c] = append(db.onConnPut[c], func() {
    			si.Close()
    		})
    	} else {
    		si.Close()
    	}
    }
    
    // debugGetPut determines whether getConn & putConn calls' stack traces
    // are returned for more verbose crashes.
    const debugGetPut = false
    
    
    // putConn adds a connection to the db's free pool.
    
    Shenghou Ma's avatar
    Shenghou Ma committed
    // err is optionally the last error that occurred on this connection.
    
    func (db *DB) putConn(dc *driverConn, err error) {
    
    			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 {
    
    	if fns, ok := db.onConnPut[dc]; ok {
    
    		for _, fn := range fns {
    			fn()
    		}
    
    	if err == driver.ErrBadConn {
    		// Don't reuse bad connections.
    
    	if n := len(db.freeConn); !db.closed && n < db.maxIdleConnsLocked() {
    
    		db.freeConn = append(db.freeConn, dc)
    
    		db.mu.Unlock()
    
    	// TODO: check to see if we need this Conn for any prepared
    	// statements which are still active?
    	db.mu.Unlock()
    
    // Prepare creates a prepared statement for later queries or executions.
    // Multiple queries or executions may be run concurrently from the
    // returned statement.
    
    func (db *DB) Prepare(query string) (*Stmt, error) {
    
    	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
    }
    
    
    func (db *DB) prepare(query string) (*Stmt, error) {
    
    	// 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.
    
    	if err != nil {
    		return nil, err
    	}
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(query)
    	dc.Unlock()
    
    	if err != nil {
    
    		db:    db,
    		query: query,
    
    	db.addDep(stmt, stmt)
    
    	return stmt, nil
    }
    
    // Exec executes a query without returning any rows.
    
    // The args are for any placeholder parameters in the query.
    
    func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
    
    	var res Result
    
    	for i := 0; i < 10; i++ {
    
    		res, err = db.exec(query, args)
    
    		if err != driver.ErrBadConn {
    			break
    		}
    
    func (db *DB) exec(query string, args []interface{}) (res Result, err error) {
    
    	if err != nil {
    		return nil, err
    	}
    
    	if execer, ok := dc.ci.(driver.Execer); ok {
    
    		dargs, err := driverArgs(nil, args)
    		if err != nil {
    			return nil, err
    		}
    
    		resi, err := execer.Exec(query, dargs)
    
    		if err != driver.ErrSkip {
    			if err != nil {
    				return nil, err
    			}
    
    			return driverResult{dc, resi}, nil
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(query)
    	dc.Unlock()
    
    	if err != nil {
    		return nil, err
    	}
    
    	defer withLock(dc, func() { si.Close() })
    
    	return resultFromStatement(driverStmt{dc, si}, args...)
    
    }
    
    // Query executes a query that returns rows, typically a SELECT.
    
    // The args are for any placeholder parameters in the query.
    
    func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
    
    	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
    	}
    
    
    	releaseConn := func(err error) { db.putConn(ci, err) }
    
    	return db.queryConn(ci, releaseConn, query, args)
    }
    
    // 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
    		}
    
    		rowsi, err := queryer.Query(query, dargs)
    
    		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{
    				db:          db,
    
    				releaseConn: releaseConn,
    				rowsi:       rowsi,
    			}
    			return rows, nil
    		}
    	}
    
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(query)
    	dc.Unlock()
    
    	if err != nil {
    
    		return nil, err
    	}
    
    	ds := driverStmt{dc, si}
    	rowsi, err := rowsiFromStatement(ds, args...)
    
    	if err != nil {
    		releaseConn(err)
    
    		return nil, err
    	}
    
    	// Note: ownership of ci passes to the *Rows, to be freed
    	// with releaseConn.
    	rows := &Rows{
    		db:          db,
    
    		releaseConn: releaseConn,
    		rowsi:       rowsi,
    
    	return rows, nil
    
    }
    
    // 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...)
    
    	return &Row{rows: rows, err: err}
    
    // Begin starts a transaction. The isolation level is dependent on
    
    func (db *DB) Begin() (*Tx, error) {
    
    	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) {
    
    	if err != nil {
    		return nil, err
    	}
    
    	dc.Lock()
    	txi, err := dc.ci.Begin()
    	dc.Unlock()
    
    // Driver returns the database's underlying driver.
    
    func (db *DB) Driver() driver.Driver {
    	return db.driver
    }
    
    // Tx is an in-progress database transaction.
    
    //
    // A transaction must end with a call to Commit or Rollback.
    //
    // After a call to Commit or Rollback, all operations on the
    
    // transaction fail with ErrTxDone.
    
    type Tx struct {
    
    	// dc is owned exclusively until Commit or Rollback, at which point
    
    	// it's returned with putConn.
    
    	txi driver.Tx
    
    	// done transitions from false to true exactly once, on Commit
    	// or Rollback. once done, all operations fail with
    
    var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
    
    
    func (tx *Tx) close() {
    	if tx.done {
    		panic("double close") // internal error
    	}
    	tx.done = true
    
    	tx.db.putConn(tx.dc, nil)
    	tx.dc = nil
    
    func (tx *Tx) grabConn() (*driverConn, error) {
    
    // Commit commits the transaction.
    
    func (tx *Tx) Commit() error {
    
    	tx.dc.Lock()
    	defer tx.dc.Unlock()
    
    }
    
    // Rollback aborts the transaction.
    
    func (tx *Tx) Rollback() error {
    
    	tx.dc.Lock()
    	defer tx.dc.Unlock()
    
    	return tx.txi.Rollback()
    
    // Prepare creates a prepared statement for use within a transaction.
    
    // 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.
    
    func (tx *Tx) Prepare(query string) (*Stmt, error) {
    
    	// 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.
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(query)
    	dc.Unlock()
    
    	if err != nil {
    		return nil, err
    	}
    
    	stmt := &Stmt{
    
    		db: tx.db,
    		tx: tx,
    		txsi: &driverStmt{
    			Locker: dc,
    			si:     si,
    		},
    
    		query: query,
    	}
    	return stmt, nil
    
    // 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")}
    	}
    
    	if err != nil {
    		return &Stmt{stickyErr: err}
    	}
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(stmt.query)
    	dc.Unlock()
    
    		db: tx.db,
    		tx: tx,
    		txsi: &driverStmt{
    			Locker: dc,
    			si:     si,
    		},
    
    // Exec executes a query that doesn't return rows.
    // For example: an INSERT and UPDATE.
    
    func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
    
    	if execer, ok := dc.ci.(driver.Execer); ok {
    
    		dargs, err := driverArgs(nil, args)
    		if err != nil {
    			return nil, err
    		}
    
    		resi, err := execer.Exec(query, dargs)
    
    		if err == nil {
    
    			return driverResult{dc, resi}, nil
    
    		}
    		if err != driver.ErrSkip {
    
    	dc.Lock()
    	si, err := dc.ci.Prepare(query)
    	dc.Unlock()
    
    	if err != nil {
    		return nil, err
    	}
    
    	defer withLock(dc, func() { si.Close() })
    
    	return resultFromStatement(driverStmt{dc, si}, args...)
    
    }
    
    // Query executes a query that returns rows, typically a SELECT.
    
    func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
    
    	dc, err := tx.grabConn()
    
    	if err != nil {
    		return nil, err
    
    	releaseConn := func(error) {}
    	return tx.db.queryConn(dc, releaseConn, query, args)
    
    }
    
    // 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 {
    
    	rows, err := tx.Query(query, args...)
    	return &Row{rows: rows, err: err}
    
    }
    
    // connStmt is a prepared statement on a particular connection.
    type connStmt struct {
    
    	si driver.Stmt
    }
    
    // Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.
    type Stmt struct {
    	// Immutable:
    
    	db        *DB    // where we came from
    	query     string // that created the Stmt
    	stickyErr error  // if non-nil, this error is returned for all operations
    
    	closemu sync.RWMutex // held exclusively during close, for read otherwise.
    
    
    	// If in a transaction, else both nil:
    	tx   *Tx
    
    
    	mu     sync.Mutex // protects the rest of the fields
    
    	// 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
    
    }
    
    // Exec executes a prepared statement with the given arguments and
    // returns a Result summarizing the effect of the statement.
    
    func (s *Stmt) Exec(args ...interface{}) (Result, error) {
    
    	s.closemu.RLock()
    	defer s.closemu.RUnlock()
    
    	dc, releaseConn, si, err := s.connStmt()
    
    	if err != nil {
    		return nil, err
    	}
    
    	return resultFromStatement(driverStmt{dc, si}, args...)
    
    func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) {