Skip to content
Snippets Groups Projects
sql.go 87.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • func (rs *Rows) close(err error) error {
    	rs.closemu.Lock()
    	defer rs.closemu.Unlock()
    
    	if rs.closed {
    
    	rs.closed = true
    
    	if rs.lasterr == nil {
    		rs.lasterr = err
    	}
    
    	withLock(rs.dc, func() {
    		err = rs.rowsi.Close()
    	})
    
    	if fn := rowsCloseHook(); fn != nil {
    
    	if rs.closeStmt != nil {
    		rs.closeStmt.Close()
    	}
    
    	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:
    
    	err  error // deferred error for easy chaining
    
    	rows *Rows
    }
    
    // Scan copies the columns from the matched row into the values
    
    // pointed at by dest. See the documentation on Rows.Scan for details.
    // 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.
    
    func (r *Row) Scan(dest ...interface{}) error {
    
    	if r.err != nil {
    		return r.err
    	}
    
    
    	// TODO(bradfitz): for now we need to defensively clone all
    
    	// []byte that the driver returned (not permitting
    
    	// *RawBytes in Rows.Scan), since we're about to close
    
    	// 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.
    
    		if _, ok := dp.(*RawBytes); ok {
    			return errors.New("sql: RawBytes isn't allowed on Row.Scan")
    		}
    
    		if err := r.rows.Err(); err != nil {
    			return err
    		}
    
    		return ErrNoRows
    	}
    	err := r.rows.Scan(dest...)
    	if err != nil {
    		return err
    	}
    
    	// Make sure the query can be processed to completion with no errors.
    
    	return r.rows.Close()
    
    }
    
    // A Result summarizes an executed SQL command.
    type Result interface {
    
    	// 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.
    
    	LastInsertId() (int64, error)
    
    
    	// RowsAffected returns the number of rows affected by an
    	// update, insert, or delete. Not every database or database
    	// driver may support this.
    
    	RowsAffected() (int64, error)
    
    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()
    
    	return string(buf[:runtime.Stack(buf[:], false)])
    }
    
    
    // withLock runs while holding lk.
    func withLock(lk sync.Locker, fn func()) {
    	lk.Lock()
    
    	defer lk.Unlock() // in case fn panics