Skip to content
Snippets Groups Projects
sql.go 60 KiB
Newer Older
  • Learn to ignore specific revisions
  • func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
    
    	var stmt *Stmt
    	var err error
    
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		stmt, err = db.prepare(ctx, query, cachedOrNewConn)
    
    		if err != driver.ErrBadConn {
    			break
    		}
    	}
    
    		return db.prepare(ctx, query, alwaysNewConn)
    
    // Prepare creates a prepared statement for later queries or executions.
    // Multiple queries or executions may be run concurrently from the
    // returned statement.
    // The caller must call the statement's Close method
    // when the statement is no longer needed.
    func (db *DB) Prepare(query string) (*Stmt, error) {
    	return db.PrepareContext(context.Background(), query)
    }
    
    func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*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.
    
    	dc, err := db.conn(ctx, strategy)
    
    	if err != nil {
    		return nil, err
    	}
    
    	var si driver.Stmt
    	withLock(dc, func() {
    
    		si, err = dc.prepareLocked(ctx, query)
    
    		db:            db,
    		query:         query,
    		css:           []connStmt{{dc, si}},
    		lastNumClosed: atomic.LoadUint64(&db.numClosed),
    
    	db.addDep(stmt, stmt)
    
    // ExecContext executes a query without returning any rows.
    
    // The args are for any placeholder parameters in the query.
    
    func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
    
    	var res Result
    
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		res, err = db.exec(ctx, query, args, cachedOrNewConn)
    
    		if err != driver.ErrBadConn {
    			break
    		}
    
    		return db.exec(ctx, query, args, alwaysNewConn)
    
    // 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) {
    	return db.ExecContext(context.Background(), query, args...)
    }
    
    func (db *DB) exec(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) {
    	dc, err := db.conn(ctx, strategy)
    
    	if err != nil {
    		return nil, err
    	}
    
    	if execer, ok := dc.ci.(driver.Execer); ok {
    
    		dargs, err = driverArgs(nil, args)
    
    		var resi driver.Result
    		withLock(dc, func() {
    
    			resi, err = ctxDriverExec(ctx, execer, query, dargs)
    
    		if err != driver.ErrSkip {
    			if err != nil {
    				return nil, err
    			}
    
    			return driverResult{dc, resi}, nil
    
    	var si driver.Stmt
    	withLock(dc, func() {
    
    		si, err = ctxDriverPrepare(ctx, dc.ci, query)
    
    	if err != nil {
    		return nil, err
    	}
    
    	defer withLock(dc, func() { si.Close() })
    
    	return resultFromStatement(ctx, driverStmt{dc, si}, args...)
    
    // QueryContext executes a query that returns rows, typically a SELECT.
    
    // The args are for any placeholder parameters in the query.
    
    func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
    
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		rows, err = db.query(ctx, query, args, cachedOrNewConn)
    
    		return db.query(ctx, query, args, alwaysNewConn)
    
    // 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) {
    	return db.QueryContext(context.Background(), query, args...)
    }
    
    func (db *DB) query(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
    	ci, err := db.conn(ctx, strategy)
    
    	if err != nil {
    		return nil, err
    	}
    
    	return db.queryConn(ctx, ci, ci.releaseConn, query, args)
    
    }
    
    // queryConn executes a query on the given connection.
    // The connection gets released by the releaseConn function.
    
    func (db *DB) queryConn(ctx context.Context, 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
    		}
    
    		var rowsi driver.Rows
    		withLock(dc, func() {
    
    			rowsi, err = ctxDriverQuery(ctx, queryer, 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{
    
    				releaseConn: releaseConn,
    				rowsi:       rowsi,
    			}
    
    			rows.initContextClose(ctx)
    
    	var si driver.Stmt
    	var err error
    	withLock(dc, func() {
    
    		si, err = ctxDriverPrepare(ctx, dc.ci, query)
    
    	if err != nil {
    
    		return nil, err
    	}
    
    	rowsi, err := rowsiFromStatement(ctx, ds, args...)
    
    		return nil, err
    	}
    
    	// Note: ownership of ci passes to the *Rows, to be freed
    	// with releaseConn.
    	rows := &Rows{
    
    		releaseConn: releaseConn,
    		rowsi:       rowsi,
    
    	rows.initContextClose(ctx)
    
    	return rows, nil
    
    // QueryRowContext executes a query that is expected to return at most one row.
    // QueryRowContext always returns a non-nil value. Errors are deferred until
    // Row's Scan method is called.
    func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
    	rows, err := db.QueryContext(ctx, query, args...)
    	return &Row{rows: rows, err: err}
    }
    
    
    // QueryRow executes a query that is expected to return at most one row.
    
    Evan Shaw's avatar
    Evan Shaw committed
    // QueryRow always returns a non-nil value. Errors are deferred until
    
    // Row's Scan method is called.
    func (db *DB) QueryRow(query string, args ...interface{}) *Row {
    
    	return db.QueryRowContext(context.Background(), query, args...)
    
    // BeginContext starts a transaction. If a non-default isolation level is used
    // that the driver doesn't support an error will be returned. Different drivers
    // may have slightly different meanings for the same isolation level.
    func (db *DB) BeginContext(ctx context.Context) (*Tx, error) {
    
    	var tx *Tx
    	var err error
    
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		tx, err = db.begin(ctx, cachedOrNewConn)
    
    		if err != driver.ErrBadConn {
    			break
    		}
    	}
    
    		return db.begin(ctx, alwaysNewConn)
    
    // Begin starts a transaction. The default isolation level is dependent on
    // the driver.
    func (db *DB) Begin() (*Tx, error) {
    	return db.BeginContext(context.Background())
    }
    
    func (db *DB) begin(ctx context.Context, strategy connReuseStrategy) (tx *Tx, err error) {
    	dc, err := db.conn(ctx, strategy)
    
    	if err != nil {
    		return nil, err
    	}
    
    	var txi driver.Tx
    	withLock(dc, func() {
    
    		txi, err = ctxDriverBegin(ctx, dc.ci)
    
    
    	// Schedule the transaction to rollback when the context is cancelled.
    	// The cancel function in Tx will be called after done is set to true.
    	ctx, cancel := context.WithCancel(ctx)
    	tx = &Tx{
    		db:     db,
    		dc:     dc,
    		txi:    txi,
    		cancel: cancel,
    	}
    	go func() {
    		select {
    		case <-ctx.Done():
    			if !tx.done {
    				tx.Rollback()
    			}
    		}
    	}()
    	return tx, nil
    
    // 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.
    
    //
    // The statements prepared for a transaction by calling
    // the transaction's Prepare or Stmt methods are closed
    // by the call to Commit or Rollback.
    
    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
    
    	// All Stmts prepared for this transaction. These will be closed after the
    
    	// transaction has been committed or rolled back.
    	stmts struct {
    		sync.Mutex
    		v []*Stmt
    	}
    
    
    	// cancel is called after done transitions from false to true.
    	cancel func()
    
    // ErrTxDone is returned by any operation that is performed on a transaction
    // that has already been committed or rolled back.
    
    var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
    
    func (tx *Tx) close(err error) {
    
    	if tx.done {
    		panic("double close") // internal error
    	}
    	tx.done = true
    
    func (tx *Tx) grabConn(ctx context.Context) (*driverConn, error) {
    
    // Closes all Stmts prepared for this transaction.
    func (tx *Tx) closePrepared() {
    	tx.stmts.Lock()
    
    // Commit commits the transaction.
    
    func (tx *Tx) Commit() error {
    
    	var err error
    	withLock(tx.dc, func() {
    		err = tx.txi.Commit()
    	})
    
    	if err != driver.ErrBadConn {
    		tx.closePrepared()
    	}
    
    }
    
    // Rollback aborts the transaction.
    
    func (tx *Tx) Rollback() error {
    
    	var err error
    	withLock(tx.dc, func() {
    		err = tx.txi.Rollback()
    	})
    
    	if err != driver.ErrBadConn {
    		tx.closePrepared()
    	}
    
    // 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.
    
    //
    // The provided context will be used for the preparation of the context, not
    
    // for the execution of the returned statement. The returned statement
    // will run in the transaction context.
    func (tx *Tx) PrepareContext(ctx context.Context, 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, err := tx.grabConn(ctx)
    
    	var si driver.Stmt
    	withLock(dc, func() {
    		si, err = dc.ci.Prepare(query)
    	})
    
    	if err != nil {
    		return nil, err
    	}
    
    	stmt := &Stmt{
    
    		db: tx.db,
    		tx: tx,
    		txsi: &driverStmt{
    			Locker: dc,
    			si:     si,
    		},
    
    	tx.stmts.Lock()
    	tx.stmts.v = append(tx.stmts.v, stmt)
    	tx.stmts.Unlock()
    
    // 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) {
    	return tx.PrepareContext(context.Background(), query)
    }
    
    // StmtContext 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.StmtContext(ctx, updateMoney).Exec(123.45, 98293203)
    
    //
    // The returned statement operates within the transaction and can no longer
    // be used once the transaction has been committed or rolled back.
    
    func (tx *Tx) StmtContext(ctx context.Context, 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")}
    	}
    
    	dc, err := tx.grabConn(ctx)
    
    	if err != nil {
    		return &Stmt{stickyErr: err}
    	}
    
    	var si driver.Stmt
    	withLock(dc, func() {
    		si, err = dc.ci.Prepare(stmt.query)
    	})
    
    		db: tx.db,
    		tx: tx,
    		txsi: &driverStmt{
    			Locker: dc,
    			si:     si,
    		},
    
    	tx.stmts.Lock()
    	tx.stmts.v = append(tx.stmts.v, txs)
    	tx.stmts.Unlock()
    	return txs
    
    // 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)
    //
    // The returned statement operates within the transaction and can no longer
    // be used once the transaction has been committed or rolled back.
    func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
    	return tx.StmtContext(context.Background(), stmt)
    }
    
    // ExecContext executes a query that doesn't return rows.
    
    // For example: an INSERT and UPDATE.
    
    func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
    	dc, err := tx.grabConn(ctx)
    
    	if execer, ok := dc.ci.(driver.Execer); ok {
    
    		dargs, err := driverArgs(nil, args)
    		if err != nil {
    			return nil, err
    		}
    
    		var resi driver.Result
    		withLock(dc, func() {
    
    			resi, err = ctxDriverExec(ctx, execer, query, dargs)
    
    		if err == nil {
    
    			return driverResult{dc, resi}, nil
    
    		}
    		if err != driver.ErrSkip {
    
    	var si driver.Stmt
    	withLock(dc, func() {
    
    		si, err = ctxDriverPrepare(ctx, dc.ci, query)
    
    	if err != nil {
    		return nil, err
    	}
    
    	defer withLock(dc, func() { si.Close() })
    
    	return resultFromStatement(ctx, driverStmt{dc, si}, args...)
    
    // 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) {
    	return tx.ExecContext(context.Background(), query, args...)
    }
    
    // QueryContext executes a query that returns rows, typically a SELECT.
    func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
    	dc, err := tx.grabConn(ctx)
    
    	if err != nil {
    		return nil, err
    
    	releaseConn := func(error) {}
    
    	return tx.db.queryConn(ctx, dc, releaseConn, query, args)
    }
    
    // Query executes a query that returns rows, typically a SELECT.
    func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
    	return tx.QueryContext(context.Background(), query, args...)
    }
    
    // QueryRowContext executes a query that is expected to return at most one row.
    // QueryRowContext always returns a non-nil value. Errors are deferred until
    // Row's Scan method is called.
    func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
    	rows, err := tx.QueryContext(ctx, query, args...)
    	return &Row{rows: rows, err: err}
    
    }
    
    // QueryRow executes a query that is expected to return at most one row.
    
    Evan Shaw's avatar
    Evan Shaw committed
    // QueryRow always returns a non-nil value. Errors are deferred until
    
    // Row's Scan method is called.
    func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
    
    	return tx.QueryRowContext(context.Background(), query, args...)
    
    }
    
    // connStmt is a prepared statement on a particular connection.
    type connStmt struct {
    
    // Stmt is a prepared statement.
    // A 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.
    
    
    	// lastNumClosed is copied from db.numClosed when Stmt is created
    	// without tx and closed connections in css are removed.
    	lastNumClosed uint64
    
    // ExecContext executes a prepared statement with the given arguments and
    
    // returns a Result summarizing the effect of the statement.
    
    func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error) {
    
    	s.closemu.RLock()
    	defer s.closemu.RUnlock()
    
    	var res Result
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		dc, releaseConn, si, err := s.connStmt(ctx)
    
    		if err != nil {
    			if err == driver.ErrBadConn {
    				continue
    			}
    			return nil, err
    		}
    
    
    		res, err = resultFromStatement(ctx, driverStmt{dc, si}, args...)
    
    		releaseConn(err)
    		if err != driver.ErrBadConn {
    			return res, err
    		}
    	}
    	return nil, driver.ErrBadConn
    
    // 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) {
    	return s.ExecContext(context.Background(), args...)
    }
    
    
    func driverNumInput(ds driverStmt) int {
    
    	defer ds.Unlock() // in case NumInput panics
    	return ds.si.NumInput()
    }
    
    
    func resultFromStatement(ctx context.Context, ds driverStmt, args ...interface{}) (Result, error) {
    
    	// -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.
    
    	if want != -1 && len(args) != want {
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    		return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
    
    	dargs, err := driverArgs(&ds, args)
    
    
    	resi, err := ctxDriverStmtExec(ctx, ds.si, dargs)
    
    	if err != nil {
    		return nil, err
    	}
    
    	return driverResult{ds.Locker, resi}, nil
    
    // removeClosedStmtLocked removes closed conns in s.css.
    //
    // To avoid lock contention on DB.mu, we do it only when
    // s.db.numClosed - s.lastNum is large enough.
    func (s *Stmt) removeClosedStmtLocked() {
    	t := len(s.css)/2 + 1
    	if t > 10 {
    		t = 10
    	}
    	dbClosed := atomic.LoadUint64(&s.db.numClosed)
    	if dbClosed-s.lastNumClosed < uint64(t) {
    		return
    	}
    
    	s.db.mu.Lock()
    	for i := 0; i < len(s.css); i++ {
    		if s.css[i].dc.dbmuClosed {
    			s.css[i] = s.css[len(s.css)-1]
    			s.css = s.css[:len(s.css)-1]
    			i--
    		}
    	}
    	s.db.mu.Unlock()
    	s.lastNumClosed = dbClosed
    }
    
    
    // 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.
    
    func (s *Stmt) connStmt(ctx context.Context) (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) {
    
    	if err = s.stickyErr; err != nil {
    		return
    
    	s.mu.Lock()
    	if s.closed {
    
    Brad Fitzpatrick's avatar
    Brad Fitzpatrick committed
    		err = errors.New("sql: statement is closed")
    
    
    	// 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(ctx) // blocks, waiting for the connection.
    
    		releaseConn = func(error) {}
    
    		return ci, releaseConn, s.txsi.si, nil
    
    	s.removeClosedStmtLocked()
    
    	dc, err := s.db.conn(ctx, cachedOrNewConn)
    
    	if err != nil {
    		return nil, nil, nil, err
    	}
    
    	s.mu.Lock()
    	for _, v := range s.css {
    		if v.dc == dc {
    			s.mu.Unlock()
    			return dc, dc.releaseConn, v.si, nil
    
    	s.mu.Unlock()
    
    	// No luck; we need to prepare the statement on this connection
    
    		si, err = dc.prepareLocked(ctx, s.query)
    
    	if err != nil {
    		s.db.putConn(dc, err)
    		return nil, nil, nil, err
    	}
    	s.mu.Lock()
    	cs := connStmt{dc, si}
    	s.css = append(s.css, cs)
    	s.mu.Unlock()
    
    // QueryContext executes a prepared query statement with the given arguments
    
    // and returns the query results as a *Rows.
    
    func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) {
    
    	s.closemu.RLock()
    	defer s.closemu.RUnlock()
    
    
    	var rowsi driver.Rows
    	for i := 0; i < maxBadConnRetries; i++ {
    
    		dc, releaseConn, si, err := s.connStmt(ctx)
    
    		if err != nil {
    			if err == driver.ErrBadConn {
    				continue
    			}
    			return nil, err
    		}
    
    		rowsi, err = rowsiFromStatement(ctx, driverStmt{dc, si}, args...)
    
    		if err == nil {
    			// Note: ownership of ci passes to the *Rows, to be freed
    			// with releaseConn.
    			rows := &Rows{
    				dc:    dc,
    				rowsi: rowsi,
    				// releaseConn set below
    			}
    
    			rows.initContextClose(ctx)
    
    			s.db.addDep(s, rows)
    			rows.releaseConn = func(err error) {
    				releaseConn(err)
    				s.db.removeDep(s, rows)
    			}
    			return rows, nil
    		}
    
    		if err != driver.ErrBadConn {
    			return nil, err
    		}
    
    	return nil, driver.ErrBadConn
    
    // Query executes a prepared query statement with the given arguments
    // and returns the query results as a *Rows.
    func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
    	return s.QueryContext(context.Background(), args...)
    }
    
    func rowsiFromStatement(ctx context.Context, ds driverStmt, args ...interface{}) (driver.Rows, error) {
    
    	var want int
    	withLock(ds, func() {
    		want = ds.si.NumInput()
    	})
    
    	// -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.
    
    	if want != -1 && len(args) != want {
    		return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
    
    	dargs, err := driverArgs(&ds, args)
    
    
    	rowsi, err := ctxDriverStmtQuery(ctx, ds.si, dargs)
    
    	if err != nil {
    		return nil, err
    	}
    
    // QueryRowContext 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
    
    //  err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name)
    func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row {
    	rows, err := s.QueryContext(ctx, args...)
    
    	if err != nil {
    		return &Row{err: err}
    	}
    	return &Row{rows: rows}
    }
    
    
    // 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
    //  err := nameByUseridStmt.QueryRow(id).Scan(&name)
    func (s *Stmt) QueryRow(args ...interface{}) *Row {
    	return s.QueryRowContext(context.Background(), args...)
    }
    
    
    // Close closes the statement.
    
    func (s *Stmt) Close() error {
    
    	s.closemu.Lock()
    	defer s.closemu.Unlock()
    
    
    	if s.stickyErr != nil {
    		return s.stickyErr
    	}
    
    	s.mu.Lock()
    	if s.closed {
    
    		s.mu.Unlock()
    
    		return nil
    	}
    	s.closed = true
    
    		defer s.mu.Unlock()
    		return s.txsi.Close()
    
    	s.mu.Unlock()
    
    
    	return s.db.removeDep(s, s)
    }
    
    func (s *Stmt) finalClose() error {
    
    	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
    
    	}
    	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)
    //         ...
    //     }
    
    //     err = rows.Err() // get any error encountered during iteration
    
    //     ...
    type Rows struct {
    
    	dc          *driverConn // owned; must call releaseConn when closed to release
    
    	// closed value is 1 when the Rows is closed.
    	// Use atomic operations on value when checking value.
    	closed    int32
    	ctxClose  chan struct{} // closed when Rows is closed, may be null.
    
    	lastcols  []driver.Value
    
    	lasterr   error       // non-nil only if closed is true
    
    	closeStmt driver.Stmt // if non-nil, statement to Close on close
    
    func (rs *Rows) initContextClose(ctx context.Context) {
    	if ctx.Done() == context.Background().Done() {
    		return
    	}
    
    	rs.ctxClose = make(chan struct{})
    	go func() {
    		select {
    		case <-ctx.Done():
    			rs.Close()
    		case <-rs.ctxClose:
    		}
    	}()
    }
    
    
    // Next prepares the next result row for reading with the Scan method. It
    
    // returns true on success, or false if there is no next result row or an error
    
    // happened while preparing it. Err should be consulted to distinguish between
    
    // the two cases.
    //
    // Every call to Scan, even the first one, must be preceded by a call to Next.
    
    func (rs *Rows) Next() bool {
    
    		return false
    	}
    	if rs.lastcols == nil {
    
    		rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
    
    	}
    	rs.lasterr = rs.rowsi.Next(rs.lastcols)
    
    	if rs.lasterr != nil {
    		// Close the connection if there is a driver error.
    		if rs.lasterr != io.EOF {
    			rs.Close()
    			return false
    		}
    		nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
    		if !ok {
    			rs.Close()
    			return false
    		}
    		// The driver is at the end of the current result set.
    		// Test to see if there is another result set after the current one.
    		// Only close Rows if there is no futher result sets to read.
    		if !nextResultSet.HasNextResultSet() {
    			rs.Close()
    		}
    		return false
    	}
    	return true
    }
    
    // NextResultSet prepares the next result set for reading. It returns true if
    // there is further result sets, or false if there is no further result set
    // or if there is an error advancing to it. The Err method should be consulted
    // to distinguish between the two cases.
    //
    // After calling NextResultSet, the Next method should always be called before
    // scanning. If there are further result sets they may not have rows in the result
    // set.
    func (rs *Rows) NextResultSet() bool {
    	if rs.isClosed() {
    		return false
    	}
    	rs.lastcols = nil