blob: aaa4ea28be4527d956e58c08a9d103c2ac7ac42a [file] [log] [blame]
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001// 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.
4
5// Package sql provides a generic interface around SQL (or SQL-like)
6// databases.
Brad Fitzpatrickeee3e632013-03-25 17:38:51 -07007//
8// The sql package must be used in conjunction with a database driver.
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -06009// See https://golang.org/s/sqldrivers for a list of drivers.
Matthew Cottingham17a03d82013-10-24 10:13:23 -070010//
11// For more usage examples, see the wiki page at
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -060012// https://golang.org/s/sqlwiki.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070013package sql
14
15import (
Brad Fitzpatrick7fc4c072012-01-19 16:04:26 -080016 "database/sql/driver"
Russ Coxc2049d22011-11-01 22:04:37 -040017 "errors"
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070018 "fmt"
Russ Coxc2049d22011-11-01 22:04:37 -040019 "io"
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -080020 "runtime"
Russ Cox5318a1b2014-10-15 13:10:14 -040021 "sort"
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070022 "sync"
INADA Naoki1b61a972015-01-23 20:02:37 +090023 "sync/atomic"
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070024)
25
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -070026var (
27 driversMu sync.Mutex
28 drivers = make(map[string]driver.Driver)
29)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070030
31// Register makes a database driver available by the provided name.
32// If Register is called twice with the same name or if driver is nil,
33// it panics.
34func Register(name string, driver driver.Driver) {
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -070035 driversMu.Lock()
36 defer driversMu.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070037 if driver == nil {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -080038 panic("sql: Register driver is nil")
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070039 }
40 if _, dup := drivers[name]; dup {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -080041 panic("sql: Register called twice for driver " + name)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070042 }
43 drivers[name] = driver
44}
45
Brad Fitzpatrick9dc1cce2014-10-31 09:49:42 -070046func unregisterAllDrivers() {
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -070047 driversMu.Lock()
48 defer driversMu.Unlock()
Brad Fitzpatrick9dc1cce2014-10-31 09:49:42 -070049 // For tests.
50 drivers = make(map[string]driver.Driver)
51}
52
Russ Cox5318a1b2014-10-15 13:10:14 -040053// Drivers returns a sorted list of the names of the registered drivers.
54func Drivers() []string {
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -070055 driversMu.Lock()
56 defer driversMu.Unlock()
Russ Cox5318a1b2014-10-15 13:10:14 -040057 var list []string
58 for name := range drivers {
59 list = append(list, name)
60 }
61 sort.Strings(list)
62 return list
63}
64
Brad Fitzpatrickebc80132012-01-17 10:44:35 -080065// RawBytes is a byte slice that holds a reference to memory owned by
66// the database itself. After a Scan into a RawBytes, the slice is only
67// valid until the next call to Next, Scan, or Close.
68type RawBytes []byte
69
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -080070// NullString represents a string that may be null.
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +110071// NullString implements the Scanner interface so
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070072// it can be used as a scan destination:
73//
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -080074// var s NullString
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070075// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
76// ...
77// if s.Valid {
78// // use s.String
79// } else {
80// // NULL value
81// }
82//
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -080083type NullString struct {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070084 String string
85 Valid bool // Valid is true if String is not NULL
86}
87
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +110088// Scan implements the Scanner interface.
89func (ns *NullString) Scan(value interface{}) error {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070090 if value == nil {
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -080091 ns.String, ns.Valid = "", false
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070092 return nil
93 }
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -080094 ns.Valid = true
95 return convertAssign(&ns.String, value)
96}
97
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +110098// Value implements the driver Valuer interface.
99func (ns NullString) Value() (driver.Value, error) {
Brad Fitzpatrickbc0139b2012-01-19 09:27:45 -0800100 if !ns.Valid {
101 return nil, nil
102 }
103 return ns.String, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700104}
105
James P. Cooperc21b3432012-01-25 17:47:32 -0800106// NullInt64 represents an int64 that may be null.
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100107// NullInt64 implements the Scanner interface so
James P. Cooperc21b3432012-01-25 17:47:32 -0800108// it can be used as a scan destination, similar to NullString.
109type NullInt64 struct {
110 Int64 int64
111 Valid bool // Valid is true if Int64 is not NULL
112}
113
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100114// Scan implements the Scanner interface.
115func (n *NullInt64) Scan(value interface{}) error {
James P. Cooperc21b3432012-01-25 17:47:32 -0800116 if value == nil {
117 n.Int64, n.Valid = 0, false
118 return nil
119 }
120 n.Valid = true
121 return convertAssign(&n.Int64, value)
122}
123
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +1100124// Value implements the driver Valuer interface.
125func (n NullInt64) Value() (driver.Value, error) {
James P. Cooperc21b3432012-01-25 17:47:32 -0800126 if !n.Valid {
127 return nil, nil
128 }
129 return n.Int64, nil
130}
131
132// NullFloat64 represents a float64 that may be null.
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100133// NullFloat64 implements the Scanner interface so
James P. Cooperc21b3432012-01-25 17:47:32 -0800134// it can be used as a scan destination, similar to NullString.
135type NullFloat64 struct {
136 Float64 float64
137 Valid bool // Valid is true if Float64 is not NULL
138}
139
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100140// Scan implements the Scanner interface.
141func (n *NullFloat64) Scan(value interface{}) error {
James P. Cooperc21b3432012-01-25 17:47:32 -0800142 if value == nil {
143 n.Float64, n.Valid = 0, false
144 return nil
145 }
146 n.Valid = true
147 return convertAssign(&n.Float64, value)
148}
149
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +1100150// Value implements the driver Valuer interface.
151func (n NullFloat64) Value() (driver.Value, error) {
James P. Cooperc21b3432012-01-25 17:47:32 -0800152 if !n.Valid {
153 return nil, nil
154 }
155 return n.Float64, nil
156}
157
158// NullBool represents a bool that may be null.
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100159// NullBool implements the Scanner interface so
James P. Cooperc21b3432012-01-25 17:47:32 -0800160// it can be used as a scan destination, similar to NullString.
161type NullBool struct {
162 Bool bool
163 Valid bool // Valid is true if Bool is not NULL
164}
165
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100166// Scan implements the Scanner interface.
167func (n *NullBool) Scan(value interface{}) error {
James P. Cooperc21b3432012-01-25 17:47:32 -0800168 if value == nil {
169 n.Bool, n.Valid = false, false
170 return nil
171 }
172 n.Valid = true
173 return convertAssign(&n.Bool, value)
174}
175
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +1100176// Value implements the driver Valuer interface.
177func (n NullBool) Value() (driver.Value, error) {
James P. Cooperc21b3432012-01-25 17:47:32 -0800178 if !n.Valid {
179 return nil, nil
180 }
181 return n.Bool, nil
182}
183
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100184// Scanner is an interface used by Scan.
185type Scanner interface {
186 // Scan assigns a value from a database driver.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700187 //
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100188 // The src value will be of one of the following restricted
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700189 // set of types:
190 //
191 // int64
192 // float64
193 // bool
194 // []byte
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100195 // string
196 // time.Time
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700197 // nil - for NULL values
198 //
199 // An error should be returned if the value can not be stored
200 // without loss of information.
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +1100201 Scan(src interface{}) error
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700202}
203
204// ErrNoRows is returned by Scan when QueryRow doesn't return a
205// row. In such a case, QueryRow returns a placeholder *Row value that
206// defers this error until a Scan.
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -0800207var ErrNoRows = errors.New("sql: no rows in result set")
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700208
Brad Fitzpatrick7b103c52014-05-19 09:54:47 -0700209// DB is a database handle representing a pool of zero or more
210// underlying connections. It's safe for concurrent use by multiple
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700211// goroutines.
Brad Fitzpatrick502e29f2012-03-06 17:44:47 -0800212//
Brad Fitzpatrick646e5412013-03-18 15:54:22 -0700213// The sql package creates and frees connections automatically; it
214// also maintains a free pool of idle connections. If the database has
215// a concept of per-connection state, such state can only be reliably
216// observed within a transaction. Once DB.Begin is called, the
217// returned Tx is bound to a single connection. Once Commit or
218// Rollback is called on the transaction, that transaction's
219// connection is returned to DB's idle connection pool. The pool size
220// can be controlled with SetMaxIdleConns.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700221type DB struct {
222 driver driver.Driver
223 dsn string
INADA Naoki1b61a972015-01-23 20:02:37 +0900224 // numClosed is an atomic counter which represents a total number of
225 // closed connections. Stmt.openStmt checks it before cleaning closed
226 // connections in Stmt.css.
227 numClosed uint64
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700228
Tad Glines41c5d8d2013-08-30 09:27:33 -0700229 mu sync.Mutex // protects following fields
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700230 freeConn []*driverConn
Brad Fitzpatrick558bd8e2014-08-28 11:07:29 -0700231 connRequests []chan connRequest
Tad Glines41c5d8d2013-08-30 09:27:33 -0700232 numOpen int
233 pendingOpens int
Julien Schmidte6c4fa582013-10-29 16:03:13 -0700234 // Used to signal the need for new connections
Tad Glines41c5d8d2013-08-30 09:27:33 -0700235 // a goroutine running connectionOpener() reads on this chan and
236 // maybeOpenNewConnections sends on the chan (one send per needed connection)
237 // It is closed during db.Close(). The close tells the connectionOpener
238 // goroutine to exit.
239 openerCh chan struct{}
James Tucker4f1ef562013-04-03 11:13:40 -0700240 closed bool
241 dep map[finalCloser]depSet
242 lastPut map[*driverConn]string // stacktrace of last conn's put; debug only
243 maxIdle int // zero means defaultMaxIdleConns; negative means 0
Tad Glines41c5d8d2013-08-30 09:27:33 -0700244 maxOpen int // <= 0 means unlimited
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700245}
246
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100247// connReuseStrategy determines how (*DB).conn returns database connections.
248type connReuseStrategy uint8
249
250const (
251 // alwaysNewConn forces a new connection to the database.
252 alwaysNewConn connReuseStrategy = iota
253 // cachedOrNewConn returns a cached connection, if available, else waits
254 // for one to become available (if MaxOpenConns has been reached) or
255 // creates a new database connection.
256 cachedOrNewConn
257)
258
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700259// driverConn wraps a driver.Conn with a mutex, to
260// be held during all calls into the Conn. (including any calls onto
261// interfaces returned via that Conn, such as calls on Tx, Stmt,
262// Result, Rows)
263type driverConn struct {
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700264 db *DB
265
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700266 sync.Mutex // guards following
267 ci driver.Conn
268 closed bool
269 finalClosed bool // ci.Close has been called
270 openStmt map[driver.Stmt]bool
James Tucker4f1ef562013-04-03 11:13:40 -0700271
272 // guarded by db.mu
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700273 inUse bool
274 onPut []func() // code (with db.mu held) run when conn is next returned
INADA Naoki1b61a972015-01-23 20:02:37 +0900275 dbmuClosed bool // same as closed, but guarded by db.mu, for removeClosedStmtLocked
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700276}
277
Brad Fitzpatrick0bbf0ec2013-05-14 16:35:31 -0700278func (dc *driverConn) releaseConn(err error) {
279 dc.db.putConn(dc, err)
280}
281
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700282func (dc *driverConn) removeOpenStmt(si driver.Stmt) {
283 dc.Lock()
284 defer dc.Unlock()
285 delete(dc.openStmt, si)
286}
287
288func (dc *driverConn) prepareLocked(query string) (driver.Stmt, error) {
289 si, err := dc.ci.Prepare(query)
290 if err == nil {
291 // Track each driverConn's open statements, so we can close them
292 // before closing the conn.
293 //
294 // TODO(bradfitz): let drivers opt out of caring about
295 // stmt closes if the conn is about to close anyway? For now
296 // do the safe thing, in case stmts need to be closed.
297 //
Julien Schmidt762a9d92013-12-17 11:57:30 -0800298 // TODO(bradfitz): after Go 1.2, closing driver.Stmts
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700299 // should be moved to driverStmt, using unique
300 // *driverStmts everywhere (including from
301 // *Stmt.connStmt, instead of returning a
302 // driver.Stmt), using driverStmt as a pointer
303 // everywhere, and making it a finalCloser.
304 if dc.openStmt == nil {
305 dc.openStmt = make(map[driver.Stmt]bool)
306 }
307 dc.openStmt[si] = true
308 }
309 return si, err
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700310}
311
312// the dc.db's Mutex is held.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700313func (dc *driverConn) closeDBLocked() func() error {
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700314 dc.Lock()
Tad Glines41c5d8d2013-08-30 09:27:33 -0700315 defer dc.Unlock()
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700316 if dc.closed {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700317 return func() error { return errors.New("sql: duplicate driverConn close") }
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700318 }
319 dc.closed = true
Tad Glines41c5d8d2013-08-30 09:27:33 -0700320 return dc.db.removeDepLocked(dc, dc)
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700321}
322
323func (dc *driverConn) Close() error {
324 dc.Lock()
325 if dc.closed {
326 dc.Unlock()
327 return errors.New("sql: duplicate driverConn close")
328 }
329 dc.closed = true
330 dc.Unlock() // not defer; removeDep finalClose calls may need to lock
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700331
332 // And now updates that require holding dc.mu.Lock.
333 dc.db.mu.Lock()
334 dc.dbmuClosed = true
335 fn := dc.db.removeDepLocked(dc, dc)
336 dc.db.mu.Unlock()
337 return fn()
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700338}
339
340func (dc *driverConn) finalClose() error {
341 dc.Lock()
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700342
343 for si := range dc.openStmt {
344 si.Close()
345 }
346 dc.openStmt = nil
347
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700348 err := dc.ci.Close()
349 dc.ci = nil
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700350 dc.finalClosed = true
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700351 dc.Unlock()
Tad Glines41c5d8d2013-08-30 09:27:33 -0700352
353 dc.db.mu.Lock()
354 dc.db.numOpen--
355 dc.db.maybeOpenNewConnections()
356 dc.db.mu.Unlock()
357
INADA Naoki1b61a972015-01-23 20:02:37 +0900358 atomic.AddUint64(&dc.db.numClosed, 1)
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700359 return err
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700360}
361
362// driverStmt associates a driver.Stmt with the
363// *driverConn from which it came, so the driverConn's lock can be
364// held during calls.
365type driverStmt struct {
366 sync.Locker // the *driverConn
367 si driver.Stmt
368}
369
370func (ds *driverStmt) Close() error {
371 ds.Lock()
372 defer ds.Unlock()
373 return ds.si.Close()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800374}
375
376// depSet is a finalCloser's outstanding dependencies
377type depSet map[interface{}]bool // set of true bools
378
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700379// The finalCloser interface is used by (*DB).addDep and related
380// dependency reference counting.
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800381type finalCloser interface {
382 // finalClose is called when the reference count of an object
383 // goes to zero. (*DB).mu is not held while calling it.
384 finalClose() error
385}
386
387// addDep notes that x now depends on dep, and x's finalClose won't be
388// called until all of x's dependencies are removed with removeDep.
389func (db *DB) addDep(x finalCloser, dep interface{}) {
390 //println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep))
391 db.mu.Lock()
392 defer db.mu.Unlock()
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700393 db.addDepLocked(x, dep)
394}
395
396func (db *DB) addDepLocked(x finalCloser, dep interface{}) {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800397 if db.dep == nil {
398 db.dep = make(map[finalCloser]depSet)
399 }
400 xdep := db.dep[x]
401 if xdep == nil {
402 xdep = make(depSet)
403 db.dep[x] = xdep
404 }
405 xdep[dep] = true
406}
407
408// removeDep notes that x no longer depends on dep.
409// If x still has dependencies, nil is returned.
410// If x no longer has any dependencies, its finalClose method will be
411// called and its error value will be returned.
412func (db *DB) removeDep(x finalCloser, dep interface{}) error {
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700413 db.mu.Lock()
414 fn := db.removeDepLocked(x, dep)
415 db.mu.Unlock()
416 return fn()
417}
418
419func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800420 //println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep))
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800421
Brad Fitzpatrick0e101962013-05-21 14:58:08 -0700422 xdep, ok := db.dep[x]
423 if !ok {
424 panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800425 }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800426
Brad Fitzpatrick0e101962013-05-21 14:58:08 -0700427 l0 := len(xdep)
428 delete(xdep, dep)
429
430 switch len(xdep) {
431 case l0:
432 // Nothing removed. Shouldn't happen.
433 panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
434 case 0:
435 // No more dependencies.
436 delete(db.dep, x)
437 return x.finalClose
438 default:
439 // Dependencies remain.
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700440 return func() error { return nil }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800441 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700442}
443
Tad Glines41c5d8d2013-08-30 09:27:33 -0700444// This is the size of the connectionOpener request chan (dn.openerCh).
445// This value should be larger than the maximum typical value
446// used for db.maxOpen. If maxOpen is significantly larger than
447// connectionRequestQueueSize then it is possible for ALL calls into the *DB
Robert Henckef999e142014-04-29 12:44:40 -0400448// to block until the connectionOpener can satisfy the backlog of requests.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700449var connectionRequestQueueSize = 1000000
450
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700451// Open opens a database specified by its database driver name and a
452// driver-specific data source name, usually consisting of at least a
453// database name and connection information.
454//
455// Most users will open a database via a driver-specific connection
Brad Fitzpatrickeee3e632013-03-25 17:38:51 -0700456// helper function that returns a *DB. No database drivers are included
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600457// in the Go standard library. See https://golang.org/s/sqldrivers for
Brad Fitzpatrickeee3e632013-03-25 17:38:51 -0700458// a list of third-party drivers.
Brad Fitzpatricka4a86512013-03-14 14:06:46 -0700459//
460// Open may just validate its arguments without creating a connection
461// to the database. To verify that the data source name is valid, call
462// Ping.
Brad Fitzpatrick7b103c52014-05-19 09:54:47 -0700463//
464// The returned DB is safe for concurrent use by multiple goroutines
465// and maintains its own pool of idle connections. Thus, the Open
466// function should be called just once. It is rarely necessary to
467// close a DB.
Russ Coxc2049d22011-11-01 22:04:37 -0400468func Open(driverName, dataSourceName string) (*DB, error) {
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -0700469 driversMu.Lock()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800470 driveri, ok := drivers[driverName]
Brad Fitzpatrickfc2eee82015-06-29 17:56:20 -0700471 driversMu.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700472 if !ok {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -0800473 return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700474 }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800475 db := &DB{
Tad Glines41c5d8d2013-08-30 09:27:33 -0700476 driver: driveri,
477 dsn: dataSourceName,
478 openerCh: make(chan struct{}, connectionRequestQueueSize),
479 lastPut: make(map[*driverConn]string),
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800480 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700481 go db.connectionOpener()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800482 return db, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700483}
484
Brad Fitzpatricka4a86512013-03-14 14:06:46 -0700485// Ping verifies a connection to the database is still alive,
486// establishing a connection if necessary.
487func (db *DB) Ping() error {
488 // TODO(bradfitz): give drivers an optional hook to implement
489 // this in a more efficient or more reliable way, if they
490 // have one.
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100491 dc, err := db.conn(cachedOrNewConn)
Brad Fitzpatricka4a86512013-03-14 14:06:46 -0700492 if err != nil {
493 return err
494 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700495 db.putConn(dc, nil)
Brad Fitzpatricka4a86512013-03-14 14:06:46 -0700496 return nil
497}
498
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800499// Close closes the database, releasing any open resources.
Brad Fitzpatrick7b103c52014-05-19 09:54:47 -0700500//
501// It is rare to Close a DB, as the DB handle is meant to be
502// long-lived and shared between many goroutines.
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800503func (db *DB) Close() error {
504 db.mu.Lock()
Tad Glines41c5d8d2013-08-30 09:27:33 -0700505 if db.closed { // Make DB.Close idempotent
506 db.mu.Unlock()
507 return nil
508 }
509 close(db.openerCh)
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800510 var err error
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700511 fns := make([]func() error, 0, len(db.freeConn))
512 for _, dc := range db.freeConn {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700513 fns = append(fns, dc.closeDBLocked())
Tad Glines41c5d8d2013-08-30 09:27:33 -0700514 }
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700515 db.freeConn = nil
Tad Glines41c5d8d2013-08-30 09:27:33 -0700516 db.closed = true
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700517 for _, req := range db.connRequests {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700518 close(req)
519 }
520 db.mu.Unlock()
521 for _, fn := range fns {
522 err1 := fn()
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800523 if err1 != nil {
524 err = err1
525 }
526 }
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800527 return err
528}
529
Brad Fitzpatrick3a2fe622013-03-18 15:33:04 -0700530const defaultMaxIdleConns = 2
531
532func (db *DB) maxIdleConnsLocked() int {
533 n := db.maxIdle
534 switch {
535 case n == 0:
536 // TODO(bradfitz): ask driver, if supported, for its default preference
537 return defaultMaxIdleConns
538 case n < 0:
539 return 0
540 default:
541 return n
542 }
543}
544
545// SetMaxIdleConns sets the maximum number of connections in the idle
546// connection pool.
547//
Tad Glines41c5d8d2013-08-30 09:27:33 -0700548// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
549// then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
550//
Brad Fitzpatrick3a2fe622013-03-18 15:33:04 -0700551// If n <= 0, no idle connections are retained.
552func (db *DB) SetMaxIdleConns(n int) {
553 db.mu.Lock()
Brad Fitzpatrick3a2fe622013-03-18 15:33:04 -0700554 if n > 0 {
555 db.maxIdle = n
556 } else {
557 // No idle connections.
558 db.maxIdle = -1
559 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700560 // Make sure maxIdle doesn't exceed maxOpen
561 if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
562 db.maxIdle = db.maxOpen
563 }
Alberto GarcĂ­a Hierro478f4b62013-10-16 09:17:25 -0700564 var closing []*driverConn
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700565 idleCount := len(db.freeConn)
566 maxIdle := db.maxIdleConnsLocked()
567 if idleCount > maxIdle {
568 closing = db.freeConn[maxIdle:]
569 db.freeConn = db.freeConn[:maxIdle]
Alberto GarcĂ­a Hierro478f4b62013-10-16 09:17:25 -0700570 }
571 db.mu.Unlock()
572 for _, c := range closing {
573 c.Close()
Brad Fitzpatrick3a2fe622013-03-18 15:33:04 -0700574 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700575}
576
Tad Glines41c5d8d2013-08-30 09:27:33 -0700577// SetMaxOpenConns sets the maximum number of open connections to the database.
578//
579// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
580// MaxIdleConns, then MaxIdleConns will be reduced to match the new
581// MaxOpenConns limit
582//
583// If n <= 0, then there is no limit on the number of open connections.
584// The default is 0 (unlimited).
585func (db *DB) SetMaxOpenConns(n int) {
586 db.mu.Lock()
587 db.maxOpen = n
588 if n < 0 {
589 db.maxOpen = 0
590 }
591 syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
592 db.mu.Unlock()
593 if syncMaxIdle {
594 db.SetMaxIdleConns(n)
595 }
596}
597
Andrei Korzhevskii297c1d22015-03-23 18:23:53 +0300598// DBStats contains database statistics.
599type DBStats struct {
600 // OpenConnections is the number of open connections to the database.
601 OpenConnections int
602}
603
604// Stats returns database statistics.
605func (db *DB) Stats() DBStats {
606 db.mu.Lock()
607 stats := DBStats{
608 OpenConnections: db.numOpen,
609 }
610 db.mu.Unlock()
611 return stats
612}
613
Tad Glines41c5d8d2013-08-30 09:27:33 -0700614// Assumes db.mu is locked.
615// If there are connRequests and the connection limit hasn't been reached,
616// then tell the connectionOpener to open new connections.
617func (db *DB) maybeOpenNewConnections() {
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700618 numRequests := len(db.connRequests) - db.pendingOpens
Tad Glines41c5d8d2013-08-30 09:27:33 -0700619 if db.maxOpen > 0 {
620 numCanOpen := db.maxOpen - (db.numOpen + db.pendingOpens)
621 if numRequests > numCanOpen {
622 numRequests = numCanOpen
623 }
624 }
625 for numRequests > 0 {
626 db.pendingOpens++
627 numRequests--
628 db.openerCh <- struct{}{}
629 }
630}
631
Martin Olsson54990342013-12-27 08:59:02 -0800632// Runs in a separate goroutine, opens new connections when requested.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700633func (db *DB) connectionOpener() {
Robert Griesemer8a23c002014-07-16 16:29:51 -0700634 for range db.openerCh {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700635 db.openNewConnection()
636 }
637}
638
639// Open one new connection
640func (db *DB) openNewConnection() {
641 ci, err := db.driver.Open(db.dsn)
642 db.mu.Lock()
643 defer db.mu.Unlock()
644 if db.closed {
645 if err == nil {
646 ci.Close()
647 }
648 return
649 }
650 db.pendingOpens--
651 if err != nil {
652 db.putConnDBLocked(nil, err)
653 return
654 }
655 dc := &driverConn{
656 db: db,
657 ci: ci,
658 }
Alberto GarcĂ­a Hierro37db8802013-10-16 09:22:57 -0700659 if db.putConnDBLocked(dc, err) {
660 db.addDepLocked(dc, dc)
661 db.numOpen++
662 } else {
663 ci.Close()
664 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700665}
666
667// connRequest represents one request for a new connection
668// When there are no idle connections available, DB.conn will create
669// a new connRequest and put it on the db.connRequests list.
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700670type connRequest struct {
671 conn *driverConn
672 err error
673}
Tad Glines41c5d8d2013-08-30 09:27:33 -0700674
675var errDBClosed = errors.New("sql: database is closed")
676
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100677// conn returns a newly-opened or cached *driverConn.
678func (db *DB) conn(strategy connReuseStrategy) (*driverConn, error) {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700679 db.mu.Lock()
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800680 if db.closed {
Brad Fitzpatrick06a9bc62011-12-12 13:56:56 -0800681 db.mu.Unlock()
Tad Glines41c5d8d2013-08-30 09:27:33 -0700682 return nil, errDBClosed
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800683 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700684
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100685 // Prefer a free connection, if possible.
686 numFree := len(db.freeConn)
687 if strategy == cachedOrNewConn && numFree > 0 {
688 conn := db.freeConn[0]
689 copy(db.freeConn, db.freeConn[1:])
690 db.freeConn = db.freeConn[:numFree-1]
691 conn.inUse = true
692 db.mu.Unlock()
693 return conn, nil
694 }
695
696 // Out of free connections or we were asked not to use one. If we're not
697 // allowed to open any more connections, make a request and wait.
698 if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700699 // Make the connRequest channel. It's buffered so that the
700 // connectionOpener doesn't block while waiting for the req to be read.
Brad Fitzpatrick558bd8e2014-08-28 11:07:29 -0700701 req := make(chan connRequest, 1)
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700702 db.connRequests = append(db.connRequests, req)
Tad Glines41c5d8d2013-08-30 09:27:33 -0700703 db.mu.Unlock()
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700704 ret := <-req
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700705 return ret.conn, ret.err
Tad Glines41c5d8d2013-08-30 09:27:33 -0700706 }
707
Brad Fitzpatrickce6b75d2014-05-07 11:54:29 -0700708 db.numOpen++ // optimistically
Tad Glines41c5d8d2013-08-30 09:27:33 -0700709 db.mu.Unlock()
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700710 ci, err := db.driver.Open(db.dsn)
711 if err != nil {
Brad Fitzpatrickce6b75d2014-05-07 11:54:29 -0700712 db.mu.Lock()
713 db.numOpen-- // correct for earlier optimism
714 db.mu.Unlock()
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700715 return nil, err
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800716 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700717 db.mu.Lock()
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700718 dc := &driverConn{
719 db: db,
720 ci: ci,
721 }
Brad Fitzpatrick209f6b12013-03-25 16:50:27 -0700722 db.addDepLocked(dc, dc)
James Tucker4f1ef562013-04-03 11:13:40 -0700723 dc.inUse = true
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700724 db.mu.Unlock()
725 return dc, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700726}
727
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700728var (
729 errConnClosed = errors.New("database/sql: internal sentinel error: conn is closed")
730 errConnBusy = errors.New("database/sql: internal sentinel error: conn is busy")
731)
732
Brad Fitzpatrick3297fc62012-03-10 10:00:02 -0800733// putConnHook is a hook for testing.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700734var putConnHook func(*DB, *driverConn)
Brad Fitzpatrick3297fc62012-03-10 10:00:02 -0800735
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800736// noteUnusedDriverStatement notes that si is no longer used and should
737// be closed whenever possible (when c is next not in use), unless c is
738// already closed.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700739func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800740 db.mu.Lock()
741 defer db.mu.Unlock()
James Tucker4f1ef562013-04-03 11:13:40 -0700742 if c.inUse {
743 c.onPut = append(c.onPut, func() {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800744 si.Close()
745 })
746 } else {
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700747 c.Lock()
748 defer c.Unlock()
749 if !c.finalClosed {
750 si.Close()
751 }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800752 }
753}
754
755// debugGetPut determines whether getConn & putConn calls' stack traces
756// are returned for more verbose crashes.
757const debugGetPut = false
758
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800759// putConn adds a connection to the db's free pool.
Shenghou Mad1ef9b52012-12-19 03:04:09 +0800760// err is optionally the last error that occurred on this connection.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700761func (db *DB) putConn(dc *driverConn, err error) {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800762 db.mu.Lock()
James Tucker4f1ef562013-04-03 11:13:40 -0700763 if !dc.inUse {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800764 if debugGetPut {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700765 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800766 }
767 panic("sql: connection returned that was never out")
768 }
769 if debugGetPut {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700770 db.lastPut[dc] = stack()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800771 }
James Tucker4f1ef562013-04-03 11:13:40 -0700772 dc.inUse = false
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800773
James Tucker4f1ef562013-04-03 11:13:40 -0700774 for _, fn := range dc.onPut {
775 fn()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800776 }
James Tucker4f1ef562013-04-03 11:13:40 -0700777 dc.onPut = nil
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800778
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800779 if err == driver.ErrBadConn {
780 // Don't reuse bad connections.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700781 // Since the conn is considered bad and is being discarded, treat it
Alberto GarcĂ­a Hierro478f4b62013-10-16 09:17:25 -0700782 // as closed. Don't decrement the open count here, finalClose will
783 // take care of that.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700784 db.maybeOpenNewConnections()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800785 db.mu.Unlock()
Matt Joiner13c78962013-08-14 09:27:30 -0700786 dc.Close()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700787 return
788 }
Brad Fitzpatrick3297fc62012-03-10 10:00:02 -0800789 if putConnHook != nil {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700790 putConnHook(db, dc)
Brad Fitzpatrick3297fc62012-03-10 10:00:02 -0800791 }
Tad Glines41c5d8d2013-08-30 09:27:33 -0700792 added := db.putConnDBLocked(dc, nil)
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800793 db.mu.Unlock()
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700794
Tad Glines41c5d8d2013-08-30 09:27:33 -0700795 if !added {
796 dc.Close()
797 }
798}
799
800// Satisfy a connRequest or put the driverConn in the idle pool and return true
801// or return false.
802// putConnDBLocked will satisfy a connRequest if there is one, or it will
Marko Tiikkajaab05a852013-12-17 14:53:31 -0800803// return the *driverConn to the freeConn list if err == nil and the idle
804// connection limit will not be exceeded.
Tad Glines41c5d8d2013-08-30 09:27:33 -0700805// If err != nil, the value of dc is ignored.
806// If err == nil, then dc must not equal nil.
Robert Henckef999e142014-04-29 12:44:40 -0400807// If a connRequest was fulfilled or the *driverConn was placed in the
Tad Glines41c5d8d2013-08-30 09:27:33 -0700808// freeConn list, then true is returned, otherwise false is returned.
809func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
Jiong Ducce127a2014-12-30 16:12:50 +0800810 if db.maxOpen > 0 && db.numOpen > db.maxOpen {
811 return false
812 }
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700813 if c := len(db.connRequests); c > 0 {
814 req := db.connRequests[0]
Brad Fitzpatrick558bd8e2014-08-28 11:07:29 -0700815 // This copy is O(n) but in practice faster than a linked list.
816 // TODO: consider compacting it down less often and
817 // moving the base instead?
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700818 copy(db.connRequests, db.connRequests[1:])
819 db.connRequests = db.connRequests[:c-1]
820 if err == nil {
Tad Glines41c5d8d2013-08-30 09:27:33 -0700821 dc.inUse = true
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700822 }
Brad Fitzpatrick558bd8e2014-08-28 11:07:29 -0700823 req <- connRequest{
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700824 conn: dc,
825 err: err,
Tad Glines41c5d8d2013-08-30 09:27:33 -0700826 }
827 return true
Alberto GarcĂ­a Hierro6fb6f4e2014-08-28 08:49:56 -0700828 } else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
829 db.freeConn = append(db.freeConn, dc)
Tad Glines41c5d8d2013-08-30 09:27:33 -0700830 return true
831 }
832 return false
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700833}
834
Julien Schmidt762a9d92013-12-17 11:57:30 -0800835// maxBadConnRetries is the number of maximum retries if the driver returns
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100836// driver.ErrBadConn to signal a broken connection before forcing a new
837// connection to be opened.
838const maxBadConnRetries = 2
Julien Schmidt762a9d92013-12-17 11:57:30 -0800839
Brad Fitzpatrickc53fab92013-02-20 22:15:36 -0800840// Prepare creates a prepared statement for later queries or executions.
841// Multiple queries or executions may be run concurrently from the
842// returned statement.
Russ Cox3c9f60c2015-07-14 16:28:28 -0400843// The caller must call the statement's Close method
844// when the statement is no longer needed.
Russ Coxc2049d22011-11-01 22:04:37 -0400845func (db *DB) Prepare(query string) (*Stmt, error) {
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800846 var stmt *Stmt
847 var err error
Julien Schmidt762a9d92013-12-17 11:57:30 -0800848 for i := 0; i < maxBadConnRetries; i++ {
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100849 stmt, err = db.prepare(query, cachedOrNewConn)
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800850 if err != driver.ErrBadConn {
851 break
852 }
853 }
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100854 if err == driver.ErrBadConn {
855 return db.prepare(query, alwaysNewConn)
856 }
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800857 return stmt, err
858}
859
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100860func (db *DB) prepare(query string, strategy connReuseStrategy) (*Stmt, error) {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700861 // TODO: check if db.driver supports an optional
862 // driver.Preparer interface and call that instead, if so,
863 // otherwise we make a prepared statement that's bound
864 // to a connection, and to execute this prepared statement
865 // we either need to use this connection (if it's free), else
866 // get a new connection + re-prepare + execute on that one.
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100867 dc, err := db.conn(strategy)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700868 if err != nil {
869 return nil, err
870 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700871 dc.Lock()
Brad Fitzpatrick277047f2013-04-25 14:45:56 -0700872 si, err := dc.prepareLocked(query)
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700873 dc.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700874 if err != nil {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700875 db.putConn(dc, err)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700876 return nil, err
877 }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800878 stmt := &Stmt{
INADA Naoki1b61a972015-01-23 20:02:37 +0900879 db: db,
880 query: query,
881 css: []connStmt{{dc, si}},
882 lastNumClosed: atomic.LoadUint64(&db.numClosed),
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700883 }
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -0800884 db.addDep(stmt, stmt)
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700885 db.putConn(dc, nil)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700886 return stmt, nil
887}
888
889// Exec executes a query without returning any rows.
Julien Schmidt2968e232013-02-13 15:25:39 -0800890// The args are for any placeholder parameters in the query.
Russ Coxc2049d22011-11-01 22:04:37 -0400891func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800892 var res Result
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -0700893 var err error
Julien Schmidt762a9d92013-12-17 11:57:30 -0800894 for i := 0; i < maxBadConnRetries; i++ {
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100895 res, err = db.exec(query, args, cachedOrNewConn)
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800896 if err != driver.ErrBadConn {
897 break
898 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700899 }
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100900 if err == driver.ErrBadConn {
901 return db.exec(query, args, alwaysNewConn)
902 }
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -0800903 return res, err
904}
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700905
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100906func (db *DB) exec(query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) {
907 dc, err := db.conn(strategy)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700908 if err != nil {
909 return nil, err
910 }
Julien Schmidt37b40da2012-08-23 19:29:47 -0700911 defer func() {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700912 db.putConn(dc, err)
Julien Schmidt37b40da2012-08-23 19:29:47 -0700913 }()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700914
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700915 if execer, ok := dc.ci.(driver.Execer); ok {
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -0700916 dargs, err := driverArgs(nil, args)
917 if err != nil {
918 return nil, err
919 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700920 dc.Lock()
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -0700921 resi, err := execer.Exec(query, dargs)
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700922 dc.Unlock()
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -0800923 if err != driver.ErrSkip {
924 if err != nil {
925 return nil, err
926 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700927 return driverResult{dc, resi}, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700928 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700929 }
930
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700931 dc.Lock()
932 si, err := dc.ci.Prepare(query)
933 dc.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700934 if err != nil {
935 return nil, err
936 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700937 defer withLock(dc, func() { si.Close() })
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700938 return resultFromStatement(driverStmt{dc, si}, args...)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700939}
940
941// Query executes a query that returns rows, typically a SELECT.
Brad Fitzpatrick20130f12013-01-11 14:46:49 -0800942// The args are for any placeholder parameters in the query.
Russ Coxc2049d22011-11-01 22:04:37 -0400943func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
Julien Schmidt2968e232013-02-13 15:25:39 -0800944 var rows *Rows
945 var err error
Julien Schmidt762a9d92013-12-17 11:57:30 -0800946 for i := 0; i < maxBadConnRetries; i++ {
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100947 rows, err = db.query(query, args, cachedOrNewConn)
Julien Schmidt2968e232013-02-13 15:25:39 -0800948 if err != driver.ErrBadConn {
949 break
950 }
951 }
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100952 if err == driver.ErrBadConn {
953 return db.query(query, args, alwaysNewConn)
954 }
Julien Schmidt2968e232013-02-13 15:25:39 -0800955 return rows, err
956}
957
Marko Tiikkajac468f9462015-03-27 19:45:12 +0100958func (db *DB) query(query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
959 ci, err := db.conn(strategy)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700960 if err != nil {
961 return nil, err
962 }
Julien Schmidt2968e232013-02-13 15:25:39 -0800963
Brad Fitzpatrick0bbf0ec2013-05-14 16:35:31 -0700964 return db.queryConn(ci, ci.releaseConn, query, args)
Julien Schmidt2968e232013-02-13 15:25:39 -0800965}
966
967// queryConn executes a query on the given connection.
968// The connection gets released by the releaseConn function.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700969func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
970 if queryer, ok := dc.ci.(driver.Queryer); ok {
Julien Schmidt2968e232013-02-13 15:25:39 -0800971 dargs, err := driverArgs(nil, args)
972 if err != nil {
973 releaseConn(err)
974 return nil, err
975 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700976 dc.Lock()
Julien Schmidt2968e232013-02-13 15:25:39 -0800977 rowsi, err := queryer.Query(query, dargs)
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700978 dc.Unlock()
Julien Schmidt2968e232013-02-13 15:25:39 -0800979 if err != driver.ErrSkip {
980 if err != nil {
981 releaseConn(err)
982 return nil, err
983 }
Brad Fitzpatricka7a803c2013-03-18 11:39:00 -0700984 // Note: ownership of dc passes to the *Rows, to be freed
Julien Schmidt2968e232013-02-13 15:25:39 -0800985 // with releaseConn.
986 rows := &Rows{
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700987 dc: dc,
Julien Schmidt2968e232013-02-13 15:25:39 -0800988 releaseConn: releaseConn,
989 rowsi: rowsi,
990 }
991 return rows, nil
992 }
993 }
994
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -0700995 dc.Lock()
996 si, err := dc.ci.Prepare(query)
997 dc.Unlock()
Brad Fitzpatrick1c441e22012-01-13 15:25:07 -0800998 if err != nil {
Julien Schmidt2968e232013-02-13 15:25:39 -0800999 releaseConn(err)
Brad Fitzpatrick1c441e22012-01-13 15:25:07 -08001000 return nil, err
1001 }
Julien Schmidt2968e232013-02-13 15:25:39 -08001002
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001003 ds := driverStmt{dc, si}
1004 rowsi, err := rowsiFromStatement(ds, args...)
Julien Schmidt2968e232013-02-13 15:25:39 -08001005 if err != nil {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001006 dc.Lock()
1007 si.Close()
1008 dc.Unlock()
Alex Brainmana2930652013-07-23 14:09:53 +10001009 releaseConn(err)
Julien Schmidt2968e232013-02-13 15:25:39 -08001010 return nil, err
1011 }
1012
1013 // Note: ownership of ci passes to the *Rows, to be freed
1014 // with releaseConn.
1015 rows := &Rows{
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001016 dc: dc,
Julien Schmidt2968e232013-02-13 15:25:39 -08001017 releaseConn: releaseConn,
1018 rowsi: rowsi,
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001019 closeStmt: si,
Julien Schmidt2968e232013-02-13 15:25:39 -08001020 }
Brad Fitzpatrick1c441e22012-01-13 15:25:07 -08001021 return rows, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001022}
1023
1024// QueryRow executes a query that is expected to return at most one row.
1025// QueryRow always return a non-nil value. Errors are deferred until
1026// Row's Scan method is called.
1027func (db *DB) QueryRow(query string, args ...interface{}) *Row {
1028 rows, err := db.Query(query, args...)
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001029 return &Row{rows: rows, err: err}
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001030}
1031
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001032// Begin starts a transaction. The isolation level is dependent on
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001033// the driver.
Russ Coxc2049d22011-11-01 22:04:37 -04001034func (db *DB) Begin() (*Tx, error) {
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -08001035 var tx *Tx
1036 var err error
Julien Schmidt762a9d92013-12-17 11:57:30 -08001037 for i := 0; i < maxBadConnRetries; i++ {
Marko Tiikkajac468f9462015-03-27 19:45:12 +01001038 tx, err = db.begin(cachedOrNewConn)
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -08001039 if err != driver.ErrBadConn {
1040 break
1041 }
1042 }
Marko Tiikkajac468f9462015-03-27 19:45:12 +01001043 if err == driver.ErrBadConn {
1044 return db.begin(alwaysNewConn)
1045 }
Brad Fitzpatrick9fb68a92012-03-08 10:09:52 -08001046 return tx, err
1047}
1048
Marko Tiikkajac468f9462015-03-27 19:45:12 +01001049func (db *DB) begin(strategy connReuseStrategy) (tx *Tx, err error) {
1050 dc, err := db.conn(strategy)
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001051 if err != nil {
1052 return nil, err
1053 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001054 dc.Lock()
1055 txi, err := dc.ci.Begin()
1056 dc.Unlock()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001057 if err != nil {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001058 db.putConn(dc, err)
James David Chalfant309eae12012-12-12 22:04:55 -08001059 return nil, err
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001060 }
1061 return &Tx{
1062 db: db,
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001063 dc: dc,
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001064 txi: txi,
1065 }, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001066}
1067
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001068// Driver returns the database's underlying driver.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001069func (db *DB) Driver() driver.Driver {
1070 return db.driver
1071}
1072
1073// Tx is an in-progress database transaction.
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001074//
1075// A transaction must end with a call to Commit or Rollback.
1076//
1077// After a call to Commit or Rollback, all operations on the
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001078// transaction fail with ErrTxDone.
Russ Cox3c9f60c2015-07-14 16:28:28 -04001079//
1080// The statements prepared for a transaction by calling
1081// the transaction's Prepare or Stmt methods are closed
1082// by the call to Commit or Rollback.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001083type Tx struct {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001084 db *DB
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001085
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001086 // dc is owned exclusively until Commit or Rollback, at which point
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001087 // it's returned with putConn.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001088 dc *driverConn
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001089 txi driver.Tx
1090
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001091 // done transitions from false to true exactly once, on Commit
1092 // or Rollback. once done, all operations fail with
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001093 // ErrTxDone.
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001094 done bool
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001095
1096 // All Stmts prepared for this transaction. These will be closed after the
1097 // transaction has been committed or rolled back.
1098 stmts struct {
1099 sync.Mutex
1100 v []*Stmt
1101 }
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001102}
1103
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001104var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001105
1106func (tx *Tx) close() {
1107 if tx.done {
1108 panic("double close") // internal error
1109 }
1110 tx.done = true
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001111 tx.db.putConn(tx.dc, nil)
1112 tx.dc = nil
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001113 tx.txi = nil
1114}
1115
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001116func (tx *Tx) grabConn() (*driverConn, error) {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001117 if tx.done {
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001118 return nil, ErrTxDone
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001119 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001120 return tx.dc, nil
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001121}
1122
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001123// Closes all Stmts prepared for this transaction.
1124func (tx *Tx) closePrepared() {
1125 tx.stmts.Lock()
1126 for _, stmt := range tx.stmts.v {
1127 stmt.Close()
1128 }
1129 tx.stmts.Unlock()
1130}
1131
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001132// Commit commits the transaction.
Russ Coxc2049d22011-11-01 22:04:37 -04001133func (tx *Tx) Commit() error {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001134 if tx.done {
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001135 return ErrTxDone
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001136 }
1137 defer tx.close()
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001138 tx.dc.Lock()
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001139 err := tx.txi.Commit()
1140 tx.dc.Unlock()
1141 if err != driver.ErrBadConn {
1142 tx.closePrepared()
1143 }
1144 return err
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001145}
1146
1147// Rollback aborts the transaction.
Russ Coxc2049d22011-11-01 22:04:37 -04001148func (tx *Tx) Rollback() error {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001149 if tx.done {
Brad Fitzpatrick00651a22012-02-10 09:12:32 +11001150 return ErrTxDone
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001151 }
1152 defer tx.close()
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001153 tx.dc.Lock()
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001154 err := tx.txi.Rollback()
1155 tx.dc.Unlock()
1156 if err != driver.ErrBadConn {
1157 tx.closePrepared()
1158 }
1159 return err
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001160}
1161
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001162// Prepare creates a prepared statement for use within a transaction.
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001163//
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001164// The returned statement operates within the transaction and can no longer
1165// be used once the transaction has been committed or rolled back.
1166//
1167// To use an existing prepared statement on this transaction, see Tx.Stmt.
Russ Coxc2049d22011-11-01 22:04:37 -04001168func (tx *Tx) Prepare(query string) (*Stmt, error) {
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001169 // TODO(bradfitz): We could be more efficient here and either
1170 // provide a method to take an existing Stmt (created on
1171 // perhaps a different Conn), and re-create it on this Conn if
1172 // necessary. Or, better: keep a map in DB of query string to
1173 // Stmts, and have Stmt.Execute do the right thing and
1174 // re-prepare if the Conn in use doesn't have that prepared
1175 // statement. But we'll want to avoid caching the statement
1176 // in the case where we only call conn.Prepare implicitly
1177 // (such as in db.Exec or tx.Exec), but the caller package
1178 // can't be holding a reference to the returned statement.
1179 // Perhaps just looking at the reference count (by noting
1180 // Stmt.Close) would be enough. We might also want a finalizer
1181 // on Stmt to drop the reference count.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001182 dc, err := tx.grabConn()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001183 if err != nil {
1184 return nil, err
1185 }
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001186
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001187 dc.Lock()
1188 si, err := dc.ci.Prepare(query)
1189 dc.Unlock()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001190 if err != nil {
1191 return nil, err
1192 }
1193
1194 stmt := &Stmt{
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001195 db: tx.db,
1196 tx: tx,
1197 txsi: &driverStmt{
1198 Locker: dc,
1199 si: si,
1200 },
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001201 query: query,
1202 }
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001203 tx.stmts.Lock()
1204 tx.stmts.v = append(tx.stmts.v, stmt)
1205 tx.stmts.Unlock()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001206 return stmt, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001207}
1208
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001209// Stmt returns a transaction-specific prepared statement from
1210// an existing statement.
1211//
1212// Example:
1213// updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
1214// ...
1215// tx, err := db.Begin()
1216// ...
1217// res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
Russ Cox3c9f60c2015-07-14 16:28:28 -04001218//
1219// The returned statement operates within the transaction and can no longer
1220// be used once the transaction has been committed or rolled back.
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001221func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
1222 // TODO(bradfitz): optimize this. Currently this re-prepares
1223 // each time. This is fine for now to illustrate the API but
1224 // we should really cache already-prepared statements
1225 // per-Conn. See also the big comment in Tx.Prepare.
1226
1227 if tx.db != stmt.db {
1228 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
1229 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001230 dc, err := tx.grabConn()
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001231 if err != nil {
1232 return &Stmt{stickyErr: err}
1233 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001234 dc.Lock()
1235 si, err := dc.ci.Prepare(stmt.query)
1236 dc.Unlock()
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001237 txs := &Stmt{
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001238 db: tx.db,
1239 tx: tx,
1240 txsi: &driverStmt{
1241 Locker: dc,
1242 si: si,
1243 },
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001244 query: stmt.query,
1245 stickyErr: err,
1246 }
Marko Tiikkaja5f739d92014-09-22 09:19:27 -04001247 tx.stmts.Lock()
1248 tx.stmts.v = append(tx.stmts.v, txs)
1249 tx.stmts.Unlock()
1250 return txs
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001251}
1252
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001253// Exec executes a query that doesn't return rows.
1254// For example: an INSERT and UPDATE.
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001255func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001256 dc, err := tx.grabConn()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001257 if err != nil {
1258 return nil, err
1259 }
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001260
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001261 if execer, ok := dc.ci.(driver.Execer); ok {
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -07001262 dargs, err := driverArgs(nil, args)
1263 if err != nil {
1264 return nil, err
1265 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001266 dc.Lock()
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -07001267 resi, err := execer.Exec(query, dargs)
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001268 dc.Unlock()
Andrew Balholmaca4a6c2012-02-10 09:19:22 +11001269 if err == nil {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001270 return driverResult{dc, resi}, nil
Andrew Balholmaca4a6c2012-02-10 09:19:22 +11001271 }
1272 if err != driver.ErrSkip {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001273 return nil, err
1274 }
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001275 }
1276
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001277 dc.Lock()
1278 si, err := dc.ci.Prepare(query)
1279 dc.Unlock()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001280 if err != nil {
1281 return nil, err
1282 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001283 defer withLock(dc, func() { si.Close() })
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -08001284
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001285 return resultFromStatement(driverStmt{dc, si}, args...)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001286}
1287
1288// Query executes a query that returns rows, typically a SELECT.
Russ Coxc2049d22011-11-01 22:04:37 -04001289func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
Brad Fitzpatricka7a803c2013-03-18 11:39:00 -07001290 dc, err := tx.grabConn()
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001291 if err != nil {
1292 return nil, err
1293 }
Brad Fitzpatricka7a803c2013-03-18 11:39:00 -07001294 releaseConn := func(error) {}
1295 return tx.db.queryConn(dc, releaseConn, query, args)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001296}
1297
1298// QueryRow executes a query that is expected to return at most one row.
1299// QueryRow always return a non-nil value. Errors are deferred until
1300// Row's Scan method is called.
1301func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001302 rows, err := tx.Query(query, args...)
1303 return &Row{rows: rows, err: err}
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001304}
1305
1306// connStmt is a prepared statement on a particular connection.
1307type connStmt struct {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001308 dc *driverConn
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001309 si driver.Stmt
1310}
1311
Russ Cox3c9f60c2015-07-14 16:28:28 -04001312// Stmt is a prepared statement.
1313// A Stmt is safe for concurrent use by multiple goroutines.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001314type Stmt struct {
1315 // Immutable:
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001316 db *DB // where we came from
1317 query string // that created the Stmt
1318 stickyErr error // if non-nil, this error is returned for all operations
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001319
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001320 closemu sync.RWMutex // held exclusively during close, for read otherwise.
1321
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001322 // If in a transaction, else both nil:
1323 tx *Tx
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001324 txsi *driverStmt
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001325
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001326 mu sync.Mutex // protects the rest of the fields
1327 closed bool
1328
1329 // css is a list of underlying driver statement interfaces
1330 // that are valid on particular connections. This is only
1331 // used if tx == nil and one is found that has idle
1332 // connections. If tx != nil, txsi is always used.
1333 css []connStmt
INADA Naoki1b61a972015-01-23 20:02:37 +09001334
1335 // lastNumClosed is copied from db.numClosed when Stmt is created
1336 // without tx and closed connections in css are removed.
1337 lastNumClosed uint64
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001338}
1339
1340// Exec executes a prepared statement with the given arguments and
1341// returns a Result summarizing the effect of the statement.
Russ Coxc2049d22011-11-01 22:04:37 -04001342func (s *Stmt) Exec(args ...interface{}) (Result, error) {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001343 s.closemu.RLock()
1344 defer s.closemu.RUnlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001345
Julien Schmidt762a9d92013-12-17 11:57:30 -08001346 var res Result
1347 for i := 0; i < maxBadConnRetries; i++ {
1348 dc, releaseConn, si, err := s.connStmt()
1349 if err != nil {
1350 if err == driver.ErrBadConn {
1351 continue
1352 }
1353 return nil, err
1354 }
1355
1356 res, err = resultFromStatement(driverStmt{dc, si}, args...)
1357 releaseConn(err)
1358 if err != driver.ErrBadConn {
1359 return res, err
1360 }
1361 }
1362 return nil, driver.ErrBadConn
Gwenael Treguier7f0449a2013-01-11 13:28:33 -08001363}
1364
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001365func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) {
1366 ds.Lock()
1367 want := ds.si.NumInput()
1368 ds.Unlock()
1369
Yasuhiro Matsumoto5e5c5c22011-11-15 16:29:43 -08001370 // -1 means the driver doesn't know how to count the number of
1371 // placeholders, so we won't sanity check input here and instead let the
1372 // driver deal with errors.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001373 if want != -1 && len(args) != want {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001374 return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001375 }
1376
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001377 dargs, err := driverArgs(&ds, args)
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -07001378 if err != nil {
1379 return nil, err
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001380 }
1381
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001382 ds.Lock()
1383 resi, err := ds.si.Exec(dargs)
1384 ds.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001385 if err != nil {
1386 return nil, err
1387 }
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001388 return driverResult{ds.Locker, resi}, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001389}
1390
INADA Naoki1b61a972015-01-23 20:02:37 +09001391// removeClosedStmtLocked removes closed conns in s.css.
1392//
1393// To avoid lock contention on DB.mu, we do it only when
1394// s.db.numClosed - s.lastNum is large enough.
1395func (s *Stmt) removeClosedStmtLocked() {
1396 t := len(s.css)/2 + 1
1397 if t > 10 {
1398 t = 10
1399 }
1400 dbClosed := atomic.LoadUint64(&s.db.numClosed)
1401 if dbClosed-s.lastNumClosed < uint64(t) {
1402 return
1403 }
1404
1405 s.db.mu.Lock()
1406 for i := 0; i < len(s.css); i++ {
1407 if s.css[i].dc.dbmuClosed {
1408 s.css[i] = s.css[len(s.css)-1]
1409 s.css = s.css[:len(s.css)-1]
1410 i--
1411 }
1412 }
1413 s.db.mu.Unlock()
1414 s.lastNumClosed = dbClosed
1415}
1416
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001417// connStmt returns a free driver connection on which to execute the
1418// statement, a function to call to release the connection, and a
1419// statement bound to that connection.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001420func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) {
Brad Fitzpatrick4435c8b2012-01-10 12:51:27 -08001421 if err = s.stickyErr; err != nil {
1422 return
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001423 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001424 s.mu.Lock()
1425 if s.closed {
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001426 s.mu.Unlock()
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001427 err = errors.New("sql: statement is closed")
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001428 return
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001429 }
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001430
1431 // In a transaction, we always use the connection that the
1432 // transaction was created on.
1433 if s.tx != nil {
1434 s.mu.Unlock()
1435 ci, err = s.tx.grabConn() // blocks, waiting for the connection.
1436 if err != nil {
1437 return
1438 }
Brad Fitzpatricka7a803c2013-03-18 11:39:00 -07001439 releaseConn = func(error) {}
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001440 return ci, releaseConn, s.txsi.si, nil
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001441 }
1442
INADA Naoki1b61a972015-01-23 20:02:37 +09001443 s.removeClosedStmtLocked()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001444 s.mu.Unlock()
1445
Marko Tiikkaja90e2e2b2014-09-02 09:08:41 -07001446 // TODO(bradfitz): or always wait for one? make configurable later?
Marko Tiikkajac468f9462015-03-27 19:45:12 +01001447 dc, err := s.db.conn(cachedOrNewConn)
Marko Tiikkaja90e2e2b2014-09-02 09:08:41 -07001448 if err != nil {
1449 return nil, nil, nil, err
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001450 }
1451
Marko Tiikkaja90e2e2b2014-09-02 09:08:41 -07001452 s.mu.Lock()
1453 for _, v := range s.css {
1454 if v.dc == dc {
1455 s.mu.Unlock()
1456 return dc, dc.releaseConn, v.si, nil
1457 }
1458 }
1459 s.mu.Unlock()
1460
1461 // No luck; we need to prepare the statement on this connection
1462 dc.Lock()
1463 si, err = dc.prepareLocked(s.query)
1464 dc.Unlock()
1465 if err != nil {
1466 s.db.putConn(dc, err)
1467 return nil, nil, nil, err
1468 }
1469 s.mu.Lock()
1470 cs := connStmt{dc, si}
1471 s.css = append(s.css, cs)
1472 s.mu.Unlock()
1473
1474 return dc, dc.releaseConn, si, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001475}
1476
1477// Query executes a prepared query statement with the given arguments
1478// and returns the query results as a *Rows.
Russ Coxc2049d22011-11-01 22:04:37 -04001479func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001480 s.closemu.RLock()
1481 defer s.closemu.RUnlock()
1482
Julien Schmidt762a9d92013-12-17 11:57:30 -08001483 var rowsi driver.Rows
1484 for i := 0; i < maxBadConnRetries; i++ {
1485 dc, releaseConn, si, err := s.connStmt()
1486 if err != nil {
1487 if err == driver.ErrBadConn {
1488 continue
1489 }
1490 return nil, err
1491 }
Yasuhiro Matsumoto5e5c5c22011-11-15 16:29:43 -08001492
Julien Schmidt762a9d92013-12-17 11:57:30 -08001493 rowsi, err = rowsiFromStatement(driverStmt{dc, si}, args...)
1494 if err == nil {
1495 // Note: ownership of ci passes to the *Rows, to be freed
1496 // with releaseConn.
1497 rows := &Rows{
1498 dc: dc,
1499 rowsi: rowsi,
1500 // releaseConn set below
1501 }
1502 s.db.addDep(s, rows)
1503 rows.releaseConn = func(err error) {
1504 releaseConn(err)
1505 s.db.removeDep(s, rows)
1506 }
1507 return rows, nil
1508 }
Julien Schmidt2968e232013-02-13 15:25:39 -08001509
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001510 releaseConn(err)
Julien Schmidt762a9d92013-12-17 11:57:30 -08001511 if err != driver.ErrBadConn {
1512 return nil, err
1513 }
Julien Schmidt2968e232013-02-13 15:25:39 -08001514 }
Julien Schmidt762a9d92013-12-17 11:57:30 -08001515 return nil, driver.ErrBadConn
Julien Schmidt2968e232013-02-13 15:25:39 -08001516}
1517
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001518func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) {
1519 ds.Lock()
1520 want := ds.si.NumInput()
1521 ds.Unlock()
1522
Yasuhiro Matsumoto5e5c5c22011-11-15 16:29:43 -08001523 // -1 means the driver doesn't know how to count the number of
1524 // placeholders, so we won't sanity check input here and instead let the
1525 // driver deal with errors.
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001526 if want != -1 && len(args) != want {
1527 return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001528 }
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -07001529
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001530 dargs, err := driverArgs(&ds, args)
Brad Fitzpatrick0a8005c2011-11-14 10:48:26 -08001531 if err != nil {
1532 return nil, err
1533 }
Brad Fitzpatrick93fe8c0c2012-05-29 11:09:09 -07001534
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001535 ds.Lock()
1536 rowsi, err := ds.si.Query(dargs)
1537 ds.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001538 if err != nil {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001539 return nil, err
1540 }
Julien Schmidt2968e232013-02-13 15:25:39 -08001541 return rowsi, nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001542}
1543
1544// QueryRow executes a prepared query statement with the given arguments.
1545// If an error occurs during the execution of the statement, that error will
1546// be returned by a call to Scan on the returned *Row, which is always non-nil.
1547// If the query selects no rows, the *Row's Scan will return ErrNoRows.
1548// Otherwise, the *Row's Scan scans the first selected row and discards
1549// the rest.
1550//
1551// Example usage:
1552//
1553// var name string
Brad Fitzpatrick6bdd7912012-02-10 10:20:49 +11001554// err := nameByUseridStmt.QueryRow(id).Scan(&name)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001555func (s *Stmt) QueryRow(args ...interface{}) *Row {
1556 rows, err := s.Query(args...)
1557 if err != nil {
1558 return &Row{err: err}
1559 }
1560 return &Row{rows: rows}
1561}
1562
1563// Close closes the statement.
Russ Coxc2049d22011-11-01 22:04:37 -04001564func (s *Stmt) Close() error {
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001565 s.closemu.Lock()
1566 defer s.closemu.Unlock()
1567
Brad Fitzpatricke77099d2011-11-28 11:00:32 -05001568 if s.stickyErr != nil {
1569 return s.stickyErr
1570 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001571 s.mu.Lock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001572 if s.closed {
Tad Glines41c5d8d2013-08-30 09:27:33 -07001573 s.mu.Unlock()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001574 return nil
1575 }
1576 s.closed = true
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001577
1578 if s.tx != nil {
1579 s.txsi.Close()
Tad Glines41c5d8d2013-08-30 09:27:33 -07001580 s.mu.Unlock()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001581 return nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001582 }
Tad Glines41c5d8d2013-08-30 09:27:33 -07001583 s.mu.Unlock()
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001584
1585 return s.db.removeDep(s, s)
1586}
1587
1588func (s *Stmt) finalClose() error {
Tad Glines41c5d8d2013-08-30 09:27:33 -07001589 s.mu.Lock()
1590 defer s.mu.Unlock()
1591 if s.css != nil {
1592 for _, v := range s.css {
1593 s.db.noteUnusedDriverStatement(v.dc, v.si)
1594 v.dc.removeOpenStmt(v.si)
1595 }
1596 s.css = nil
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001597 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001598 return nil
1599}
1600
1601// Rows is the result of a query. Its cursor starts before the first row
1602// of the result set. Use Next to advance through the rows:
1603//
1604// rows, err := db.Query("SELECT ...")
1605// ...
Nigel Tao50ca1a52014-03-25 13:32:18 +11001606// defer rows.Close()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001607// for rows.Next() {
1608// var id int
1609// var name string
1610// err = rows.Scan(&id, &name)
1611// ...
1612// }
Gustavo Niemeyerf2dc50b2011-11-04 09:50:20 -04001613// err = rows.Err() // get any error encountered during iteration
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001614// ...
1615type Rows struct {
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001616 dc *driverConn // owned; must call releaseConn when closed to release
Brad Fitzpatrick3297fc62012-03-10 10:00:02 -08001617 releaseConn func(error)
Brad Fitzpatrick8089e572011-11-02 11:46:04 -07001618 rowsi driver.Rows
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001619
Brad Fitzpatrick1c441e22012-01-13 15:25:07 -08001620 closed bool
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +11001621 lastcols []driver.Value
Nigel Taobc212652013-08-16 11:23:35 +10001622 lasterr error // non-nil only if closed is true
Julien Schmidt2968e232013-02-13 15:25:39 -08001623 closeStmt driver.Stmt // if non-nil, statement to Close on close
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001624}
1625
Marko Tiikkaja1f20ab12013-12-16 12:48:35 -08001626// Next prepares the next result row for reading with the Scan method. It
1627// returns true on success, or false if there is no next result row or an error
1628// happened while preparing it. Err should be consulted to distinguish between
1629// the two cases.
1630//
1631// Every call to Scan, even the first one, must be preceded by a call to Next.
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001632func (rs *Rows) Next() bool {
1633 if rs.closed {
1634 return false
1635 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001636 if rs.lastcols == nil {
Brad Fitzpatrick943f6cc2012-02-20 14:25:28 +11001637 rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001638 }
1639 rs.lasterr = rs.rowsi.Next(rs.lastcols)
Nigel Taobc212652013-08-16 11:23:35 +10001640 if rs.lasterr != nil {
Brad Fitzpatrick4435c8b2012-01-10 12:51:27 -08001641 rs.Close()
Nigel Taobc212652013-08-16 11:23:35 +10001642 return false
Brad Fitzpatrick4435c8b2012-01-10 12:51:27 -08001643 }
Nigel Taobc212652013-08-16 11:23:35 +10001644 return true
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001645}
1646
Gustavo Niemeyerf2dc50b2011-11-04 09:50:20 -04001647// Err returns the error, if any, that was encountered during iteration.
Nigel Taobc212652013-08-16 11:23:35 +10001648// Err may be called after an explicit or implicit Close.
Gustavo Niemeyerf2dc50b2011-11-04 09:50:20 -04001649func (rs *Rows) Err() error {
Russ Coxc2049d22011-11-01 22:04:37 -04001650 if rs.lasterr == io.EOF {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001651 return nil
1652 }
1653 return rs.lasterr
1654}
1655
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001656// Columns returns the column names.
1657// Columns returns an error if the rows are closed, or if the rows
1658// are from QueryRow and there was a deferred error.
1659func (rs *Rows) Columns() ([]string, error) {
1660 if rs.closed {
1661 return nil, errors.New("sql: Rows are closed")
1662 }
1663 if rs.rowsi == nil {
1664 return nil, errors.New("sql: no Rows available")
1665 }
1666 return rs.rowsi.Columns(), nil
1667}
1668
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001669// Scan copies the columns in the current row into the values pointed
Brad Fitzpatrickebc80132012-01-17 10:44:35 -08001670// at by dest.
1671//
1672// If an argument has type *[]byte, Scan saves in that argument a copy
1673// of the corresponding data. The copy is owned by the caller and can
1674// be modified and held indefinitely. The copy can be avoided by using
1675// an argument of type *RawBytes instead; see the documentation for
1676// RawBytes for restrictions on its use.
Brad Fitzpatrick9c060b82012-02-06 10:06:22 -08001677//
1678// If an argument has type *interface{}, Scan copies the value
1679// provided by the underlying driver without conversion. If the value
1680// is of type []byte, a copy is made and the caller owns the result.
Russ Coxc2049d22011-11-01 22:04:37 -04001681func (rs *Rows) Scan(dest ...interface{}) error {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001682 if rs.closed {
Nigel Taobc212652013-08-16 11:23:35 +10001683 return errors.New("sql: Rows are closed")
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001684 }
1685 if rs.lastcols == nil {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001686 return errors.New("sql: Scan called without calling Next")
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001687 }
1688 if len(dest) != len(rs.lastcols) {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001689 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001690 }
1691 for i, sv := range rs.lastcols {
1692 err := convertAssign(dest[i], sv)
1693 if err != nil {
Brad Fitzpatrickea51dd22011-12-15 10:14:57 -08001694 return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001695 }
1696 }
1697 return nil
1698}
1699
Brad Fitzpatrickca3ed9f2013-08-13 14:56:40 -07001700var rowsCloseHook func(*Rows, *error)
1701
Nigel Taobc212652013-08-16 11:23:35 +10001702// Close closes the Rows, preventing further enumeration. If Next returns
1703// false, the Rows are closed automatically and it will suffice to check the
1704// result of Err. Close is idempotent and does not affect the result of Err.
Russ Coxc2049d22011-11-01 22:04:37 -04001705func (rs *Rows) Close() error {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001706 if rs.closed {
1707 return nil
1708 }
1709 rs.closed = true
1710 err := rs.rowsi.Close()
Brad Fitzpatrickca3ed9f2013-08-13 14:56:40 -07001711 if fn := rowsCloseHook; fn != nil {
1712 fn(rs, &err)
1713 }
Brad Fitzpatrick1c441e22012-01-13 15:25:07 -08001714 if rs.closeStmt != nil {
1715 rs.closeStmt.Close()
1716 }
Brad Fitzpatrick36d3bef2013-04-15 14:06:41 -07001717 rs.releaseConn(err)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001718 return err
1719}
1720
1721// Row is the result of calling QueryRow to select a single row.
1722type Row struct {
1723 // One of these two will be non-nil:
Russ Coxc2049d22011-11-01 22:04:37 -04001724 err error // deferred error for easy chaining
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001725 rows *Rows
1726}
1727
1728// Scan copies the columns from the matched row into the values
1729// pointed at by dest. If more than one row matches the query,
1730// Scan uses the first row and discards the rest. If no row matches
1731// the query, Scan returns ErrNoRows.
Russ Coxc2049d22011-11-01 22:04:37 -04001732func (r *Row) Scan(dest ...interface{}) error {
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001733 if r.err != nil {
1734 return r.err
1735 }
Brad Fitzpatrick701f70a2012-01-12 11:23:33 -08001736
1737 // TODO(bradfitz): for now we need to defensively clone all
Gwenael Treguierc3954dd52012-03-10 15:21:44 -08001738 // []byte that the driver returned (not permitting
James P. Cooper2a22f352012-01-26 15:12:48 -08001739 // *RawBytes in Rows.Scan), since we're about to close
Brad Fitzpatrick701f70a2012-01-12 11:23:33 -08001740 // the Rows in our defer, when we return from this function.
1741 // the contract with the driver.Next(...) interface is that it
1742 // can return slices into read-only temporary memory that's
1743 // only valid until the next Scan/Close. But the TODO is that
1744 // for a lot of drivers, this copy will be unnecessary. We
1745 // should provide an optional interface for drivers to
1746 // implement to say, "don't worry, the []bytes that I return
1747 // from Next will not be modified again." (for instance, if
1748 // they were obtained from the network anyway) But for now we
1749 // don't care.
Alberto GarcĂ­a Hierro478f4b62013-10-16 09:17:25 -07001750 defer r.rows.Close()
Brad Fitzpatrick701f70a2012-01-12 11:23:33 -08001751 for _, dp := range dest {
Brad Fitzpatrickebc80132012-01-17 10:44:35 -08001752 if _, ok := dp.(*RawBytes); ok {
1753 return errors.New("sql: RawBytes isn't allowed on Row.Scan")
1754 }
Brad Fitzpatrick701f70a2012-01-12 11:23:33 -08001755 }
James P. Cooper2a22f352012-01-26 15:12:48 -08001756
James P. Cooper2a22f352012-01-26 15:12:48 -08001757 if !r.rows.Next() {
Marko Tiikkaja1f20ab12013-12-16 12:48:35 -08001758 if err := r.rows.Err(); err != nil {
1759 return err
1760 }
James P. Cooper2a22f352012-01-26 15:12:48 -08001761 return ErrNoRows
1762 }
1763 err := r.rows.Scan(dest...)
1764 if err != nil {
1765 return err
1766 }
Marko Tiikkaja1f20ab12013-12-16 12:48:35 -08001767 // Make sure the query can be processed to completion with no errors.
1768 if err := r.rows.Close(); err != nil {
1769 return err
1770 }
James P. Cooper2a22f352012-01-26 15:12:48 -08001771
Brad Fitzpatrick701f70a2012-01-12 11:23:33 -08001772 return nil
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001773}
1774
1775// A Result summarizes an executed SQL command.
1776type Result interface {
Brad Fitzpatrick7307ffa2013-10-29 17:38:43 -07001777 // LastInsertId returns the integer generated by the database
1778 // in response to a command. Typically this will be from an
1779 // "auto increment" column when inserting a new row. Not all
1780 // databases support this feature, and the syntax of such
1781 // statements varies.
Russ Coxc2049d22011-11-01 22:04:37 -04001782 LastInsertId() (int64, error)
Brad Fitzpatrick7307ffa2013-10-29 17:38:43 -07001783
1784 // RowsAffected returns the number of rows affected by an
1785 // update, insert, or delete. Not every database or database
1786 // driver may support this.
Russ Coxc2049d22011-11-01 22:04:37 -04001787 RowsAffected() (int64, error)
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001788}
1789
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001790type driverResult struct {
1791 sync.Locker // the *driverConn
1792 resi driver.Result
1793}
1794
1795func (dr driverResult) LastInsertId() (int64, error) {
1796 dr.Lock()
1797 defer dr.Unlock()
1798 return dr.resi.LastInsertId()
1799}
1800
1801func (dr driverResult) RowsAffected() (int64, error) {
1802 dr.Lock()
1803 defer dr.Unlock()
1804 return dr.resi.RowsAffected()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001805}
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001806
1807func stack() string {
Brad Fitzpatrick277047f2013-04-25 14:45:56 -07001808 var buf [2 << 10]byte
Brad Fitzpatrickf7a77162013-02-20 15:35:27 -08001809 return string(buf[:runtime.Stack(buf[:], false)])
1810}
Brad Fitzpatrickf28c8fb2013-03-14 15:01:45 -07001811
1812// withLock runs while holding lk.
1813func withLock(lk sync.Locker, fn func()) {
1814 lk.Lock()
1815 fn()
1816 lk.Unlock()
1817}