1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package sql provides a generic interface around SQL (or SQL-like)
18 var drivers = make(map[string]driver.Driver)
20 // Register makes a database driver available by the provided name.
21 // If Register is called twice with the same name or if driver is nil,
23 func Register(name string, driver driver.Driver) {
25 panic("sql: Register driver is nil")
27 if _, dup := drivers[name]; dup {
28 panic("sql: Register called twice for driver " + name)
30 drivers[name] = driver
33 // NullableString represents a string that may be null.
34 // NullableString implements the ScannerInto interface so
35 // it can be used as a scan destination:
37 // var s NullableString
38 // err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
46 // TODO(bradfitz): add other types.
47 type NullableString struct {
49 Valid bool // Valid is true if String is not NULL
52 // ScanInto implements the ScannerInto interface.
53 func (ms *NullableString) ScanInto(value interface{}) error {
55 ms.String, ms.Valid = "", false
59 return convertAssign(&ms.String, value)
62 // ScannerInto is an interface used by Scan.
63 type ScannerInto interface {
64 // ScanInto assigns a value from a database driver.
66 // The value will be of one of the following restricted
73 // nil - for NULL values
75 // An error should be returned if the value can not be stored
76 // without loss of information.
77 ScanInto(value interface{}) error
80 // ErrNoRows is returned by Scan when QueryRow doesn't return a
81 // row. In such a case, QueryRow returns a placeholder *Row value that
82 // defers this error until a Scan.
83 var ErrNoRows = errors.New("sql: no rows in result set")
85 // DB is a database handle. It's safe for concurrent use by multiple
91 mu sync.Mutex // protects freeConn and closed
92 freeConn []driver.Conn
96 // Open opens a database specified by its database driver name and a
97 // driver-specific data source name, usually consisting of at least a
98 // database name and connection information.
100 // Most users will open a database via a driver-specific connection
101 // helper function that returns a *DB.
102 func Open(driverName, dataSourceName string) (*DB, error) {
103 driver, ok := drivers[driverName]
105 return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
107 return &DB{driver: driver, dsn: dataSourceName}, nil
110 // Close closes the database, releasing any open resources.
111 func (db *DB) Close() error {
115 for _, c := range db.freeConn {
126 func (db *DB) maxIdleConns() int {
127 const defaultMaxIdleConns = 2
128 // TODO(bradfitz): ask driver, if supported, for its default preference
129 // TODO(bradfitz): let users override?
130 return defaultMaxIdleConns
133 // conn returns a newly-opened or cached driver.Conn
134 func (db *DB) conn() (driver.Conn, error) {
138 return nil, errors.New("sql: database is closed")
140 if n := len(db.freeConn); n > 0 {
141 conn := db.freeConn[n-1]
142 db.freeConn = db.freeConn[:n-1]
147 return db.driver.Open(db.dsn)
150 func (db *DB) connIfFree(wanted driver.Conn) (conn driver.Conn, ok bool) {
153 for n, conn := range db.freeConn {
155 db.freeConn[n] = db.freeConn[len(db.freeConn)-1]
156 db.freeConn = db.freeConn[:len(db.freeConn)-1]
163 func (db *DB) putConn(c driver.Conn) {
166 if n := len(db.freeConn); !db.closed && n < db.maxIdleConns() {
167 db.freeConn = append(db.freeConn, c)
170 db.closeConn(c) // TODO(bradfitz): release lock before calling this?
173 func (db *DB) closeConn(c driver.Conn) {
174 // TODO: check to see if we need this Conn for any prepared statements
179 // Prepare creates a prepared statement for later execution.
180 func (db *DB) Prepare(query string) (*Stmt, error) {
181 // TODO: check if db.driver supports an optional
182 // driver.Preparer interface and call that instead, if so,
183 // otherwise we make a prepared statement that's bound
184 // to a connection, and to execute this prepared statement
185 // we either need to use this connection (if it's free), else
186 // get a new connection + re-prepare + execute on that one.
192 si, err := ci.Prepare(query)
199 css: []connStmt{{ci, si}},
204 // Exec executes a query without returning any rows.
205 func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
206 sargs, err := subsetTypeArgs(args)
217 if execer, ok := ci.(driver.Execer); ok {
218 resi, err := execer.Exec(query, sargs)
219 if err != driver.ErrSkip {
223 return result{resi}, nil
227 sti, err := ci.Prepare(query)
233 resi, err := sti.Exec(sargs)
237 return result{resi}, nil
240 // Query executes a query that returns rows, typically a SELECT.
241 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
242 stmt, err := db.Prepare(query)
246 rows, err := stmt.Query(args...)
251 rows.closeStmt = stmt
255 // QueryRow executes a query that is expected to return at most one row.
256 // QueryRow always return a non-nil value. Errors are deferred until
257 // Row's Scan method is called.
258 func (db *DB) QueryRow(query string, args ...interface{}) *Row {
259 rows, err := db.Query(query, args...)
260 return &Row{rows: rows, err: err}
263 // Begin starts a transaction. The isolation level is dependent on
265 func (db *DB) Begin() (*Tx, error) {
270 txi, err := ci.Begin()
273 return nil, fmt.Errorf("sql: failed to Begin transaction: %v", err)
282 // DriverDatabase returns the database's underlying driver.
283 func (db *DB) Driver() driver.Driver {
287 // Tx is an in-progress database transaction.
289 // A transaction must end with a call to Commit or Rollback.
291 // After a call to Commit or Rollback, all operations on the
292 // transaction fail with ErrTransactionFinished.
296 // ci is owned exclusively until Commit or Rollback, at which point
297 // it's returned with putConn.
301 // cimu is held while somebody is using ci (between grabConn
305 // done transitions from false to true exactly once, on Commit
306 // or Rollback. once done, all operations fail with
307 // ErrTransactionFinished.
311 var ErrTransactionFinished = errors.New("sql: Transaction has already been committed or rolled back")
313 func (tx *Tx) close() {
315 panic("double close") // internal error
323 func (tx *Tx) grabConn() (driver.Conn, error) {
325 return nil, ErrTransactionFinished
331 func (tx *Tx) releaseConn() {
335 // Commit commits the transaction.
336 func (tx *Tx) Commit() error {
338 return ErrTransactionFinished
341 return tx.txi.Commit()
344 // Rollback aborts the transaction.
345 func (tx *Tx) Rollback() error {
347 return ErrTransactionFinished
350 return tx.txi.Rollback()
353 // Prepare creates a prepared statement for use within a transaction.
355 // The returned statement operates within the transaction and can no longer
356 // be used once the transaction has been committed or rolled back.
358 // To use an existing prepared statement on this transaction, see Tx.Stmt.
359 func (tx *Tx) Prepare(query string) (*Stmt, error) {
360 // TODO(bradfitz): We could be more efficient here and either
361 // provide a method to take an existing Stmt (created on
362 // perhaps a different Conn), and re-create it on this Conn if
363 // necessary. Or, better: keep a map in DB of query string to
364 // Stmts, and have Stmt.Execute do the right thing and
365 // re-prepare if the Conn in use doesn't have that prepared
366 // statement. But we'll want to avoid caching the statement
367 // in the case where we only call conn.Prepare implicitly
368 // (such as in db.Exec or tx.Exec), but the caller package
369 // can't be holding a reference to the returned statement.
370 // Perhaps just looking at the reference count (by noting
371 // Stmt.Close) would be enough. We might also want a finalizer
372 // on Stmt to drop the reference count.
373 ci, err := tx.grabConn()
377 defer tx.releaseConn()
379 si, err := ci.Prepare(query)
393 // Stmt returns a transaction-specific prepared statement from
394 // an existing statement.
397 // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
399 // tx, err := db.Begin()
401 // res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
402 func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
403 // TODO(bradfitz): optimize this. Currently this re-prepares
404 // each time. This is fine for now to illustrate the API but
405 // we should really cache already-prepared statements
406 // per-Conn. See also the big comment in Tx.Prepare.
408 if tx.db != stmt.db {
409 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
411 ci, err := tx.grabConn()
413 return &Stmt{stickyErr: err}
415 defer tx.releaseConn()
416 si, err := ci.Prepare(stmt.query)
426 // Exec executes a query that doesn't return rows.
427 // For example: an INSERT and UPDATE.
428 func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
429 ci, err := tx.grabConn()
433 defer tx.releaseConn()
435 if execer, ok := ci.(driver.Execer); ok {
436 resi, err := execer.Exec(query, args)
440 return result{resi}, nil
443 sti, err := ci.Prepare(query)
449 sargs, err := subsetTypeArgs(args)
454 resi, err := sti.Exec(sargs)
458 return result{resi}, nil
461 // Query executes a query that returns rows, typically a SELECT.
462 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
464 return nil, ErrTransactionFinished
466 stmt, err := tx.Prepare(query)
471 return stmt.Query(args...)
474 // QueryRow executes a query that is expected to return at most one row.
475 // QueryRow always return a non-nil value. Errors are deferred until
476 // Row's Scan method is called.
477 func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
478 rows, err := tx.Query(query, args...)
479 return &Row{rows: rows, err: err}
482 // connStmt is a prepared statement on a particular connection.
483 type connStmt struct {
488 // Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.
491 db *DB // where we came from
492 query string // that created the Stmt
493 stickyErr error // if non-nil, this error is returned for all operations
495 // If in a transaction, else both nil:
499 mu sync.Mutex // protects the rest of the fields
502 // css is a list of underlying driver statement interfaces
503 // that are valid on particular connections. This is only
504 // used if tx == nil and one is found that has idle
505 // connections. If tx != nil, txsi is always used.
509 // Exec executes a prepared statement with the given arguments and
510 // returns a Result summarizing the effect of the statement.
511 func (s *Stmt) Exec(args ...interface{}) (Result, error) {
512 _, releaseConn, si, err := s.connStmt()
518 // -1 means the driver doesn't know how to count the number of
519 // placeholders, so we won't sanity check input here and instead let the
520 // driver deal with errors.
521 if want := si.NumInput(); want != -1 && len(args) != want {
522 return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
525 // Convert args to subset types.
526 if cc, ok := si.(driver.ColumnConverter); ok {
527 for n, arg := range args {
528 args[n], err = cc.ColumnConverter(n).ConvertValue(arg)
530 return nil, fmt.Errorf("sql: converting Exec argument #%d's type: %v", n, err)
532 if !driver.IsParameterSubsetType(args[n]) {
533 return nil, fmt.Errorf("sql: driver ColumnConverter error converted %T to unsupported type %T",
538 for n, arg := range args {
539 args[n], err = driver.DefaultParameterConverter.ConvertValue(arg)
541 return nil, fmt.Errorf("sql: converting Exec argument #%d's type: %v", n, err)
546 resi, err := si.Exec(args)
550 return result{resi}, nil
553 // connStmt returns a free driver connection on which to execute the
554 // statement, a function to call to release the connection, and a
555 // statement bound to that connection.
556 func (s *Stmt) connStmt() (ci driver.Conn, releaseConn func(), si driver.Stmt, err error) {
557 if err = s.stickyErr; err != nil {
563 err = errors.New("sql: statement is closed")
567 // In a transaction, we always use the connection that the
568 // transaction was created on.
571 ci, err = s.tx.grabConn() // blocks, waiting for the connection.
575 releaseConn = func() { s.tx.releaseConn() }
576 return ci, releaseConn, s.txsi, nil
581 for _, v := range s.css {
582 // TODO(bradfitz): lazily clean up entries in this
583 // list with dead conns while enumerating
584 if _, match = s.db.connIfFree(cs.ci); match {
591 // Make a new conn if all are busy.
592 // TODO(bradfitz): or wait for one? make configurable later?
594 ci, err := s.db.conn()
596 return nil, nil, nil, err
598 si, err := ci.Prepare(s.query)
600 return nil, nil, nil, err
603 cs = connStmt{ci, si}
604 s.css = append(s.css, cs)
609 releaseConn = func() { s.db.putConn(conn) }
610 return conn, releaseConn, cs.si, nil
613 // Query executes a prepared query statement with the given arguments
614 // and returns the query results as a *Rows.
615 func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
616 ci, releaseConn, si, err := s.connStmt()
621 // -1 means the driver doesn't know how to count the number of
622 // placeholders, so we won't sanity check input here and instead let the
623 // driver deal with errors.
624 if want := si.NumInput(); want != -1 && len(args) != want {
625 return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", si.NumInput(), len(args))
627 sargs, err := subsetTypeArgs(args)
631 rowsi, err := si.Query(sargs)
636 // Note: ownership of ci passes to the *Rows, to be freed
641 releaseConn: releaseConn,
647 // QueryRow executes a prepared query statement with the given arguments.
648 // If an error occurs during the execution of the statement, that error will
649 // be returned by a call to Scan on the returned *Row, which is always non-nil.
650 // If the query selects no rows, the *Row's Scan will return ErrNoRows.
651 // Otherwise, the *Row's Scan scans the first selected row and discards
657 // err := nameByUseridStmt.QueryRow(id).Scan(&s)
658 func (s *Stmt) QueryRow(args ...interface{}) *Row {
659 rows, err := s.Query(args...)
661 return &Row{err: err}
663 return &Row{rows: rows}
666 // Close closes the statement.
667 func (s *Stmt) Close() error {
668 if s.stickyErr != nil {
681 for _, v := range s.css {
682 if ci, match := s.db.connIfFree(v.ci); match {
686 // TODO(bradfitz): care that we can't close
687 // this statement because the statement's
688 // connection is in use?
695 // Rows is the result of a query. Its cursor starts before the first row
696 // of the result set. Use Next to advance through the rows:
698 // rows, err := db.Query("SELECT ...")
703 // err = rows.Scan(&id, &name)
706 // err = rows.Err() // get any error encountered during iteration
710 ci driver.Conn // owned; must call putconn when closed to release
715 lastcols []interface{}
717 closeStmt *Stmt // if non-nil, statement to Close on close
720 // Next prepares the next result row for reading with the Scan method.
721 // It returns true on success, false if there is no next result row.
722 // Every call to Scan, even the first one, must be preceded by a call
724 func (rs *Rows) Next() bool {
728 if rs.lasterr != nil {
731 if rs.lastcols == nil {
732 rs.lastcols = make([]interface{}, len(rs.rowsi.Columns()))
734 rs.lasterr = rs.rowsi.Next(rs.lastcols)
735 if rs.lasterr == io.EOF {
738 return rs.lasterr == nil
741 // Err returns the error, if any, that was encountered during iteration.
742 func (rs *Rows) Err() error {
743 if rs.lasterr == io.EOF {
749 // Columns returns the column names.
750 // Columns returns an error if the rows are closed, or if the rows
751 // are from QueryRow and there was a deferred error.
752 func (rs *Rows) Columns() ([]string, error) {
754 return nil, errors.New("sql: Rows are closed")
757 return nil, errors.New("sql: no Rows available")
759 return rs.rowsi.Columns(), nil
762 // Scan copies the columns in the current row into the values pointed
763 // at by dest. If dest contains pointers to []byte, the slices should
764 // not be modified and should only be considered valid until the next
765 // call to Next or Scan.
766 func (rs *Rows) Scan(dest ...interface{}) error {
768 return errors.New("sql: Rows closed")
770 if rs.lasterr != nil {
773 if rs.lastcols == nil {
774 return errors.New("sql: Scan called without calling Next")
776 if len(dest) != len(rs.lastcols) {
777 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
779 for i, sv := range rs.lastcols {
780 err := convertAssign(dest[i], sv)
782 return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
788 // Close closes the Rows, preventing further enumeration. If the
789 // end is encountered, the Rows are closed automatically. Close
791 func (rs *Rows) Close() error {
796 err := rs.rowsi.Close()
798 if rs.closeStmt != nil {
804 // Row is the result of calling QueryRow to select a single row.
806 // One of these two will be non-nil:
807 err error // deferred error for easy chaining
811 // Scan copies the columns from the matched row into the values
812 // pointed at by dest. If more than one row matches the query,
813 // Scan uses the first row and discards the rest. If no row matches
814 // the query, Scan returns ErrNoRows.
815 func (r *Row) Scan(dest ...interface{}) error {
823 err := r.rows.Scan(dest...)
828 // TODO(bradfitz): for now we need to defensively clone all
829 // []byte that the driver returned, since we're about to close
830 // the Rows in our defer, when we return from this function.
831 // the contract with the driver.Next(...) interface is that it
832 // can return slices into read-only temporary memory that's
833 // only valid until the next Scan/Close. But the TODO is that
834 // for a lot of drivers, this copy will be unnecessary. We
835 // should provide an optional interface for drivers to
836 // implement to say, "don't worry, the []bytes that I return
837 // from Next will not be modified again." (for instance, if
838 // they were obtained from the network anyway) But for now we
840 for _, dp := range dest {
841 b, ok := dp.(*[]byte)
845 clone := make([]byte, len(*b))
852 // A Result summarizes an executed SQL command.
853 type Result interface {
854 LastInsertId() (int64, error)
855 RowsAffected() (int64, error)