github.com/euank/go@v0.0.0-20160829210321-495514729181/src/database/sql/sql.go (about) 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. 4 5 // Package sql provides a generic interface around SQL (or SQL-like) 6 // databases. 7 // 8 // The sql package must be used in conjunction with a database driver. 9 // See https://golang.org/s/sqldrivers for a list of drivers. 10 // 11 // For more usage examples, see the wiki page at 12 // https://golang.org/s/sqlwiki. 13 package sql 14 15 import ( 16 "database/sql/driver" 17 "errors" 18 "fmt" 19 "io" 20 "runtime" 21 "sort" 22 "sync" 23 "sync/atomic" 24 "time" 25 ) 26 27 var ( 28 driversMu sync.RWMutex 29 drivers = make(map[string]driver.Driver) 30 ) 31 32 // nowFunc returns the current time; it's overridden in tests. 33 var nowFunc = time.Now 34 35 // Register makes a database driver available by the provided name. 36 // If Register is called twice with the same name or if driver is nil, 37 // it panics. 38 func Register(name string, driver driver.Driver) { 39 driversMu.Lock() 40 defer driversMu.Unlock() 41 if driver == nil { 42 panic("sql: Register driver is nil") 43 } 44 if _, dup := drivers[name]; dup { 45 panic("sql: Register called twice for driver " + name) 46 } 47 drivers[name] = driver 48 } 49 50 func unregisterAllDrivers() { 51 driversMu.Lock() 52 defer driversMu.Unlock() 53 // For tests. 54 drivers = make(map[string]driver.Driver) 55 } 56 57 // Drivers returns a sorted list of the names of the registered drivers. 58 func Drivers() []string { 59 driversMu.RLock() 60 defer driversMu.RUnlock() 61 var list []string 62 for name := range drivers { 63 list = append(list, name) 64 } 65 sort.Strings(list) 66 return list 67 } 68 69 // RawBytes is a byte slice that holds a reference to memory owned by 70 // the database itself. After a Scan into a RawBytes, the slice is only 71 // valid until the next call to Next, Scan, or Close. 72 type RawBytes []byte 73 74 // NullString represents a string that may be null. 75 // NullString implements the Scanner interface so 76 // it can be used as a scan destination: 77 // 78 // var s NullString 79 // err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s) 80 // ... 81 // if s.Valid { 82 // // use s.String 83 // } else { 84 // // NULL value 85 // } 86 // 87 type NullString struct { 88 String string 89 Valid bool // Valid is true if String is not NULL 90 } 91 92 // Scan implements the Scanner interface. 93 func (ns *NullString) Scan(value interface{}) error { 94 if value == nil { 95 ns.String, ns.Valid = "", false 96 return nil 97 } 98 ns.Valid = true 99 return convertAssign(&ns.String, value) 100 } 101 102 // Value implements the driver Valuer interface. 103 func (ns NullString) Value() (driver.Value, error) { 104 if !ns.Valid { 105 return nil, nil 106 } 107 return ns.String, nil 108 } 109 110 // NullInt64 represents an int64 that may be null. 111 // NullInt64 implements the Scanner interface so 112 // it can be used as a scan destination, similar to NullString. 113 type NullInt64 struct { 114 Int64 int64 115 Valid bool // Valid is true if Int64 is not NULL 116 } 117 118 // Scan implements the Scanner interface. 119 func (n *NullInt64) Scan(value interface{}) error { 120 if value == nil { 121 n.Int64, n.Valid = 0, false 122 return nil 123 } 124 n.Valid = true 125 return convertAssign(&n.Int64, value) 126 } 127 128 // Value implements the driver Valuer interface. 129 func (n NullInt64) Value() (driver.Value, error) { 130 if !n.Valid { 131 return nil, nil 132 } 133 return n.Int64, nil 134 } 135 136 // NullFloat64 represents a float64 that may be null. 137 // NullFloat64 implements the Scanner interface so 138 // it can be used as a scan destination, similar to NullString. 139 type NullFloat64 struct { 140 Float64 float64 141 Valid bool // Valid is true if Float64 is not NULL 142 } 143 144 // Scan implements the Scanner interface. 145 func (n *NullFloat64) Scan(value interface{}) error { 146 if value == nil { 147 n.Float64, n.Valid = 0, false 148 return nil 149 } 150 n.Valid = true 151 return convertAssign(&n.Float64, value) 152 } 153 154 // Value implements the driver Valuer interface. 155 func (n NullFloat64) Value() (driver.Value, error) { 156 if !n.Valid { 157 return nil, nil 158 } 159 return n.Float64, nil 160 } 161 162 // NullBool represents a bool that may be null. 163 // NullBool implements the Scanner interface so 164 // it can be used as a scan destination, similar to NullString. 165 type NullBool struct { 166 Bool bool 167 Valid bool // Valid is true if Bool is not NULL 168 } 169 170 // Scan implements the Scanner interface. 171 func (n *NullBool) Scan(value interface{}) error { 172 if value == nil { 173 n.Bool, n.Valid = false, false 174 return nil 175 } 176 n.Valid = true 177 return convertAssign(&n.Bool, value) 178 } 179 180 // Value implements the driver Valuer interface. 181 func (n NullBool) Value() (driver.Value, error) { 182 if !n.Valid { 183 return nil, nil 184 } 185 return n.Bool, nil 186 } 187 188 // Scanner is an interface used by Scan. 189 type Scanner interface { 190 // Scan assigns a value from a database driver. 191 // 192 // The src value will be of one of the following types: 193 // 194 // int64 195 // float64 196 // bool 197 // []byte 198 // string 199 // time.Time 200 // nil - for NULL values 201 // 202 // An error should be returned if the value cannot be stored 203 // without loss of information. 204 Scan(src interface{}) error 205 } 206 207 // ErrNoRows is returned by Scan when QueryRow doesn't return a 208 // row. In such a case, QueryRow returns a placeholder *Row value that 209 // defers this error until a Scan. 210 var ErrNoRows = errors.New("sql: no rows in result set") 211 212 // DB is a database handle representing a pool of zero or more 213 // underlying connections. It's safe for concurrent use by multiple 214 // goroutines. 215 // 216 // The sql package creates and frees connections automatically; it 217 // also maintains a free pool of idle connections. If the database has 218 // a concept of per-connection state, such state can only be reliably 219 // observed within a transaction. Once DB.Begin is called, the 220 // returned Tx is bound to a single connection. Once Commit or 221 // Rollback is called on the transaction, that transaction's 222 // connection is returned to DB's idle connection pool. The pool size 223 // can be controlled with SetMaxIdleConns. 224 type DB struct { 225 driver driver.Driver 226 dsn string 227 // numClosed is an atomic counter which represents a total number of 228 // closed connections. Stmt.openStmt checks it before cleaning closed 229 // connections in Stmt.css. 230 numClosed uint64 231 232 mu sync.Mutex // protects following fields 233 freeConn []*driverConn 234 connRequests []chan connRequest 235 numOpen int // number of opened and pending open connections 236 // Used to signal the need for new connections 237 // a goroutine running connectionOpener() reads on this chan and 238 // maybeOpenNewConnections sends on the chan (one send per needed connection) 239 // It is closed during db.Close(). The close tells the connectionOpener 240 // goroutine to exit. 241 openerCh chan struct{} 242 closed bool 243 dep map[finalCloser]depSet 244 lastPut map[*driverConn]string // stacktrace of last conn's put; debug only 245 maxIdle int // zero means defaultMaxIdleConns; negative means 0 246 maxOpen int // <= 0 means unlimited 247 maxLifetime time.Duration // maximum amount of time a connection may be reused 248 cleanerCh chan struct{} 249 } 250 251 // connReuseStrategy determines how (*DB).conn returns database connections. 252 type connReuseStrategy uint8 253 254 const ( 255 // alwaysNewConn forces a new connection to the database. 256 alwaysNewConn connReuseStrategy = iota 257 // cachedOrNewConn returns a cached connection, if available, else waits 258 // for one to become available (if MaxOpenConns has been reached) or 259 // creates a new database connection. 260 cachedOrNewConn 261 ) 262 263 // driverConn wraps a driver.Conn with a mutex, to 264 // be held during all calls into the Conn. (including any calls onto 265 // interfaces returned via that Conn, such as calls on Tx, Stmt, 266 // Result, Rows) 267 type driverConn struct { 268 db *DB 269 createdAt time.Time 270 271 sync.Mutex // guards following 272 ci driver.Conn 273 closed bool 274 finalClosed bool // ci.Close has been called 275 openStmt map[driver.Stmt]bool 276 277 // guarded by db.mu 278 inUse bool 279 onPut []func() // code (with db.mu held) run when conn is next returned 280 dbmuClosed bool // same as closed, but guarded by db.mu, for removeClosedStmtLocked 281 } 282 283 func (dc *driverConn) releaseConn(err error) { 284 dc.db.putConn(dc, err) 285 } 286 287 func (dc *driverConn) removeOpenStmt(si driver.Stmt) { 288 dc.Lock() 289 defer dc.Unlock() 290 delete(dc.openStmt, si) 291 } 292 293 func (dc *driverConn) expired(timeout time.Duration) bool { 294 if timeout <= 0 { 295 return false 296 } 297 return dc.createdAt.Add(timeout).Before(nowFunc()) 298 } 299 300 func (dc *driverConn) prepareLocked(query string) (driver.Stmt, error) { 301 si, err := dc.ci.Prepare(query) 302 if err == nil { 303 // Track each driverConn's open statements, so we can close them 304 // before closing the conn. 305 // 306 // TODO(bradfitz): let drivers opt out of caring about 307 // stmt closes if the conn is about to close anyway? For now 308 // do the safe thing, in case stmts need to be closed. 309 // 310 // TODO(bradfitz): after Go 1.2, closing driver.Stmts 311 // should be moved to driverStmt, using unique 312 // *driverStmts everywhere (including from 313 // *Stmt.connStmt, instead of returning a 314 // driver.Stmt), using driverStmt as a pointer 315 // everywhere, and making it a finalCloser. 316 if dc.openStmt == nil { 317 dc.openStmt = make(map[driver.Stmt]bool) 318 } 319 dc.openStmt[si] = true 320 } 321 return si, err 322 } 323 324 // the dc.db's Mutex is held. 325 func (dc *driverConn) closeDBLocked() func() error { 326 dc.Lock() 327 defer dc.Unlock() 328 if dc.closed { 329 return func() error { return errors.New("sql: duplicate driverConn close") } 330 } 331 dc.closed = true 332 return dc.db.removeDepLocked(dc, dc) 333 } 334 335 func (dc *driverConn) Close() error { 336 dc.Lock() 337 if dc.closed { 338 dc.Unlock() 339 return errors.New("sql: duplicate driverConn close") 340 } 341 dc.closed = true 342 dc.Unlock() // not defer; removeDep finalClose calls may need to lock 343 344 // And now updates that require holding dc.mu.Lock. 345 dc.db.mu.Lock() 346 dc.dbmuClosed = true 347 fn := dc.db.removeDepLocked(dc, dc) 348 dc.db.mu.Unlock() 349 return fn() 350 } 351 352 func (dc *driverConn) finalClose() error { 353 dc.Lock() 354 355 for si := range dc.openStmt { 356 si.Close() 357 } 358 dc.openStmt = nil 359 360 err := dc.ci.Close() 361 dc.ci = nil 362 dc.finalClosed = true 363 dc.Unlock() 364 365 dc.db.mu.Lock() 366 dc.db.numOpen-- 367 dc.db.maybeOpenNewConnections() 368 dc.db.mu.Unlock() 369 370 atomic.AddUint64(&dc.db.numClosed, 1) 371 return err 372 } 373 374 // driverStmt associates a driver.Stmt with the 375 // *driverConn from which it came, so the driverConn's lock can be 376 // held during calls. 377 type driverStmt struct { 378 sync.Locker // the *driverConn 379 si driver.Stmt 380 } 381 382 func (ds *driverStmt) Close() error { 383 ds.Lock() 384 defer ds.Unlock() 385 return ds.si.Close() 386 } 387 388 // depSet is a finalCloser's outstanding dependencies 389 type depSet map[interface{}]bool // set of true bools 390 391 // The finalCloser interface is used by (*DB).addDep and related 392 // dependency reference counting. 393 type finalCloser interface { 394 // finalClose is called when the reference count of an object 395 // goes to zero. (*DB).mu is not held while calling it. 396 finalClose() error 397 } 398 399 // addDep notes that x now depends on dep, and x's finalClose won't be 400 // called until all of x's dependencies are removed with removeDep. 401 func (db *DB) addDep(x finalCloser, dep interface{}) { 402 //println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep)) 403 db.mu.Lock() 404 defer db.mu.Unlock() 405 db.addDepLocked(x, dep) 406 } 407 408 func (db *DB) addDepLocked(x finalCloser, dep interface{}) { 409 if db.dep == nil { 410 db.dep = make(map[finalCloser]depSet) 411 } 412 xdep := db.dep[x] 413 if xdep == nil { 414 xdep = make(depSet) 415 db.dep[x] = xdep 416 } 417 xdep[dep] = true 418 } 419 420 // removeDep notes that x no longer depends on dep. 421 // If x still has dependencies, nil is returned. 422 // If x no longer has any dependencies, its finalClose method will be 423 // called and its error value will be returned. 424 func (db *DB) removeDep(x finalCloser, dep interface{}) error { 425 db.mu.Lock() 426 fn := db.removeDepLocked(x, dep) 427 db.mu.Unlock() 428 return fn() 429 } 430 431 func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error { 432 //println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep)) 433 434 xdep, ok := db.dep[x] 435 if !ok { 436 panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x)) 437 } 438 439 l0 := len(xdep) 440 delete(xdep, dep) 441 442 switch len(xdep) { 443 case l0: 444 // Nothing removed. Shouldn't happen. 445 panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x)) 446 case 0: 447 // No more dependencies. 448 delete(db.dep, x) 449 return x.finalClose 450 default: 451 // Dependencies remain. 452 return func() error { return nil } 453 } 454 } 455 456 // This is the size of the connectionOpener request chan (DB.openerCh). 457 // This value should be larger than the maximum typical value 458 // used for db.maxOpen. If maxOpen is significantly larger than 459 // connectionRequestQueueSize then it is possible for ALL calls into the *DB 460 // to block until the connectionOpener can satisfy the backlog of requests. 461 var connectionRequestQueueSize = 1000000 462 463 // Open opens a database specified by its database driver name and a 464 // driver-specific data source name, usually consisting of at least a 465 // database name and connection information. 466 // 467 // Most users will open a database via a driver-specific connection 468 // helper function that returns a *DB. No database drivers are included 469 // in the Go standard library. See https://golang.org/s/sqldrivers for 470 // a list of third-party drivers. 471 // 472 // Open may just validate its arguments without creating a connection 473 // to the database. To verify that the data source name is valid, call 474 // Ping. 475 // 476 // The returned DB is safe for concurrent use by multiple goroutines 477 // and maintains its own pool of idle connections. Thus, the Open 478 // function should be called just once. It is rarely necessary to 479 // close a DB. 480 func Open(driverName, dataSourceName string) (*DB, error) { 481 driversMu.RLock() 482 driveri, ok := drivers[driverName] 483 driversMu.RUnlock() 484 if !ok { 485 return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName) 486 } 487 db := &DB{ 488 driver: driveri, 489 dsn: dataSourceName, 490 openerCh: make(chan struct{}, connectionRequestQueueSize), 491 lastPut: make(map[*driverConn]string), 492 } 493 go db.connectionOpener() 494 return db, nil 495 } 496 497 // Ping verifies a connection to the database is still alive, 498 // establishing a connection if necessary. 499 func (db *DB) Ping() error { 500 // TODO(bradfitz): give drivers an optional hook to implement 501 // this in a more efficient or more reliable way, if they 502 // have one. 503 dc, err := db.conn(cachedOrNewConn) 504 if err != nil { 505 return err 506 } 507 db.putConn(dc, nil) 508 return nil 509 } 510 511 // Close closes the database, releasing any open resources. 512 // 513 // It is rare to Close a DB, as the DB handle is meant to be 514 // long-lived and shared between many goroutines. 515 func (db *DB) Close() error { 516 db.mu.Lock() 517 if db.closed { // Make DB.Close idempotent 518 db.mu.Unlock() 519 return nil 520 } 521 close(db.openerCh) 522 if db.cleanerCh != nil { 523 close(db.cleanerCh) 524 } 525 var err error 526 fns := make([]func() error, 0, len(db.freeConn)) 527 for _, dc := range db.freeConn { 528 fns = append(fns, dc.closeDBLocked()) 529 } 530 db.freeConn = nil 531 db.closed = true 532 for _, req := range db.connRequests { 533 close(req) 534 } 535 db.mu.Unlock() 536 for _, fn := range fns { 537 err1 := fn() 538 if err1 != nil { 539 err = err1 540 } 541 } 542 return err 543 } 544 545 const defaultMaxIdleConns = 2 546 547 func (db *DB) maxIdleConnsLocked() int { 548 n := db.maxIdle 549 switch { 550 case n == 0: 551 // TODO(bradfitz): ask driver, if supported, for its default preference 552 return defaultMaxIdleConns 553 case n < 0: 554 return 0 555 default: 556 return n 557 } 558 } 559 560 // SetMaxIdleConns sets the maximum number of connections in the idle 561 // connection pool. 562 // 563 // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns 564 // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit 565 // 566 // If n <= 0, no idle connections are retained. 567 func (db *DB) SetMaxIdleConns(n int) { 568 db.mu.Lock() 569 if n > 0 { 570 db.maxIdle = n 571 } else { 572 // No idle connections. 573 db.maxIdle = -1 574 } 575 // Make sure maxIdle doesn't exceed maxOpen 576 if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen { 577 db.maxIdle = db.maxOpen 578 } 579 var closing []*driverConn 580 idleCount := len(db.freeConn) 581 maxIdle := db.maxIdleConnsLocked() 582 if idleCount > maxIdle { 583 closing = db.freeConn[maxIdle:] 584 db.freeConn = db.freeConn[:maxIdle] 585 } 586 db.mu.Unlock() 587 for _, c := range closing { 588 c.Close() 589 } 590 } 591 592 // SetMaxOpenConns sets the maximum number of open connections to the database. 593 // 594 // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than 595 // MaxIdleConns, then MaxIdleConns will be reduced to match the new 596 // MaxOpenConns limit 597 // 598 // If n <= 0, then there is no limit on the number of open connections. 599 // The default is 0 (unlimited). 600 func (db *DB) SetMaxOpenConns(n int) { 601 db.mu.Lock() 602 db.maxOpen = n 603 if n < 0 { 604 db.maxOpen = 0 605 } 606 syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen 607 db.mu.Unlock() 608 if syncMaxIdle { 609 db.SetMaxIdleConns(n) 610 } 611 } 612 613 // SetConnMaxLifetime sets the maximum amount of time a connection may be reused. 614 // 615 // Expired connections may be closed lazily before reuse. 616 // 617 // If d <= 0, connections are reused forever. 618 func (db *DB) SetConnMaxLifetime(d time.Duration) { 619 if d < 0 { 620 d = 0 621 } 622 db.mu.Lock() 623 // wake cleaner up when lifetime is shortened. 624 if d > 0 && d < db.maxLifetime && db.cleanerCh != nil { 625 select { 626 case db.cleanerCh <- struct{}{}: 627 default: 628 } 629 } 630 db.maxLifetime = d 631 db.startCleanerLocked() 632 db.mu.Unlock() 633 } 634 635 // startCleanerLocked starts connectionCleaner if needed. 636 func (db *DB) startCleanerLocked() { 637 if db.maxLifetime > 0 && db.numOpen > 0 && db.cleanerCh == nil { 638 db.cleanerCh = make(chan struct{}, 1) 639 go db.connectionCleaner(db.maxLifetime) 640 } 641 } 642 643 func (db *DB) connectionCleaner(d time.Duration) { 644 const minInterval = time.Second 645 646 if d < minInterval { 647 d = minInterval 648 } 649 t := time.NewTimer(d) 650 651 for { 652 select { 653 case <-t.C: 654 case <-db.cleanerCh: // maxLifetime was changed or db was closed. 655 } 656 657 db.mu.Lock() 658 d = db.maxLifetime 659 if db.closed || db.numOpen == 0 || d <= 0 { 660 db.cleanerCh = nil 661 db.mu.Unlock() 662 return 663 } 664 665 expiredSince := nowFunc().Add(-d) 666 var closing []*driverConn 667 for i := 0; i < len(db.freeConn); i++ { 668 c := db.freeConn[i] 669 if c.createdAt.Before(expiredSince) { 670 closing = append(closing, c) 671 last := len(db.freeConn) - 1 672 db.freeConn[i] = db.freeConn[last] 673 db.freeConn[last] = nil 674 db.freeConn = db.freeConn[:last] 675 i-- 676 } 677 } 678 db.mu.Unlock() 679 680 for _, c := range closing { 681 c.Close() 682 } 683 684 if d < minInterval { 685 d = minInterval 686 } 687 t.Reset(d) 688 } 689 } 690 691 // DBStats contains database statistics. 692 type DBStats struct { 693 // OpenConnections is the number of open connections to the database. 694 OpenConnections int 695 } 696 697 // Stats returns database statistics. 698 func (db *DB) Stats() DBStats { 699 db.mu.Lock() 700 stats := DBStats{ 701 OpenConnections: db.numOpen, 702 } 703 db.mu.Unlock() 704 return stats 705 } 706 707 // Assumes db.mu is locked. 708 // If there are connRequests and the connection limit hasn't been reached, 709 // then tell the connectionOpener to open new connections. 710 func (db *DB) maybeOpenNewConnections() { 711 numRequests := len(db.connRequests) 712 if db.maxOpen > 0 { 713 numCanOpen := db.maxOpen - db.numOpen 714 if numRequests > numCanOpen { 715 numRequests = numCanOpen 716 } 717 } 718 for numRequests > 0 { 719 db.numOpen++ // optimistically 720 numRequests-- 721 if db.closed { 722 return 723 } 724 db.openerCh <- struct{}{} 725 } 726 } 727 728 // Runs in a separate goroutine, opens new connections when requested. 729 func (db *DB) connectionOpener() { 730 for range db.openerCh { 731 db.openNewConnection() 732 } 733 } 734 735 // Open one new connection 736 func (db *DB) openNewConnection() { 737 // maybeOpenNewConnctions has already executed db.numOpen++ before it sent 738 // on db.openerCh. This function must execute db.numOpen-- if the 739 // connection fails or is closed before returning. 740 ci, err := db.driver.Open(db.dsn) 741 db.mu.Lock() 742 defer db.mu.Unlock() 743 if db.closed { 744 if err == nil { 745 ci.Close() 746 } 747 db.numOpen-- 748 return 749 } 750 if err != nil { 751 db.numOpen-- 752 db.putConnDBLocked(nil, err) 753 db.maybeOpenNewConnections() 754 return 755 } 756 dc := &driverConn{ 757 db: db, 758 createdAt: nowFunc(), 759 ci: ci, 760 } 761 if db.putConnDBLocked(dc, err) { 762 db.addDepLocked(dc, dc) 763 } else { 764 db.numOpen-- 765 ci.Close() 766 } 767 } 768 769 // connRequest represents one request for a new connection 770 // When there are no idle connections available, DB.conn will create 771 // a new connRequest and put it on the db.connRequests list. 772 type connRequest struct { 773 conn *driverConn 774 err error 775 } 776 777 var errDBClosed = errors.New("sql: database is closed") 778 779 // conn returns a newly-opened or cached *driverConn. 780 func (db *DB) conn(strategy connReuseStrategy) (*driverConn, error) { 781 db.mu.Lock() 782 if db.closed { 783 db.mu.Unlock() 784 return nil, errDBClosed 785 } 786 lifetime := db.maxLifetime 787 788 // Prefer a free connection, if possible. 789 numFree := len(db.freeConn) 790 if strategy == cachedOrNewConn && numFree > 0 { 791 conn := db.freeConn[0] 792 copy(db.freeConn, db.freeConn[1:]) 793 db.freeConn = db.freeConn[:numFree-1] 794 conn.inUse = true 795 db.mu.Unlock() 796 if conn.expired(lifetime) { 797 conn.Close() 798 return nil, driver.ErrBadConn 799 } 800 return conn, nil 801 } 802 803 // Out of free connections or we were asked not to use one. If we're not 804 // allowed to open any more connections, make a request and wait. 805 if db.maxOpen > 0 && db.numOpen >= db.maxOpen { 806 // Make the connRequest channel. It's buffered so that the 807 // connectionOpener doesn't block while waiting for the req to be read. 808 req := make(chan connRequest, 1) 809 db.connRequests = append(db.connRequests, req) 810 db.mu.Unlock() 811 ret, ok := <-req 812 if !ok { 813 return nil, errDBClosed 814 } 815 if ret.err == nil && ret.conn.expired(lifetime) { 816 ret.conn.Close() 817 return nil, driver.ErrBadConn 818 } 819 return ret.conn, ret.err 820 } 821 822 db.numOpen++ // optimistically 823 db.mu.Unlock() 824 ci, err := db.driver.Open(db.dsn) 825 if err != nil { 826 db.mu.Lock() 827 db.numOpen-- // correct for earlier optimism 828 db.maybeOpenNewConnections() 829 db.mu.Unlock() 830 return nil, err 831 } 832 db.mu.Lock() 833 dc := &driverConn{ 834 db: db, 835 createdAt: nowFunc(), 836 ci: ci, 837 } 838 db.addDepLocked(dc, dc) 839 dc.inUse = true 840 db.mu.Unlock() 841 return dc, nil 842 } 843 844 // putConnHook is a hook for testing. 845 var putConnHook func(*DB, *driverConn) 846 847 // noteUnusedDriverStatement notes that si is no longer used and should 848 // be closed whenever possible (when c is next not in use), unless c is 849 // already closed. 850 func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) { 851 db.mu.Lock() 852 defer db.mu.Unlock() 853 if c.inUse { 854 c.onPut = append(c.onPut, func() { 855 si.Close() 856 }) 857 } else { 858 c.Lock() 859 defer c.Unlock() 860 if !c.finalClosed { 861 si.Close() 862 } 863 } 864 } 865 866 // debugGetPut determines whether getConn & putConn calls' stack traces 867 // are returned for more verbose crashes. 868 const debugGetPut = false 869 870 // putConn adds a connection to the db's free pool. 871 // err is optionally the last error that occurred on this connection. 872 func (db *DB) putConn(dc *driverConn, err error) { 873 db.mu.Lock() 874 if !dc.inUse { 875 if debugGetPut { 876 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc]) 877 } 878 panic("sql: connection returned that was never out") 879 } 880 if debugGetPut { 881 db.lastPut[dc] = stack() 882 } 883 dc.inUse = false 884 885 for _, fn := range dc.onPut { 886 fn() 887 } 888 dc.onPut = nil 889 890 if err == driver.ErrBadConn { 891 // Don't reuse bad connections. 892 // Since the conn is considered bad and is being discarded, treat it 893 // as closed. Don't decrement the open count here, finalClose will 894 // take care of that. 895 db.maybeOpenNewConnections() 896 db.mu.Unlock() 897 dc.Close() 898 return 899 } 900 if putConnHook != nil { 901 putConnHook(db, dc) 902 } 903 added := db.putConnDBLocked(dc, nil) 904 db.mu.Unlock() 905 906 if !added { 907 dc.Close() 908 } 909 } 910 911 // Satisfy a connRequest or put the driverConn in the idle pool and return true 912 // or return false. 913 // putConnDBLocked will satisfy a connRequest if there is one, or it will 914 // return the *driverConn to the freeConn list if err == nil and the idle 915 // connection limit will not be exceeded. 916 // If err != nil, the value of dc is ignored. 917 // If err == nil, then dc must not equal nil. 918 // If a connRequest was fulfilled or the *driverConn was placed in the 919 // freeConn list, then true is returned, otherwise false is returned. 920 func (db *DB) putConnDBLocked(dc *driverConn, err error) bool { 921 if db.closed { 922 return false 923 } 924 if db.maxOpen > 0 && db.numOpen > db.maxOpen { 925 return false 926 } 927 if c := len(db.connRequests); c > 0 { 928 req := db.connRequests[0] 929 // This copy is O(n) but in practice faster than a linked list. 930 // TODO: consider compacting it down less often and 931 // moving the base instead? 932 copy(db.connRequests, db.connRequests[1:]) 933 db.connRequests = db.connRequests[:c-1] 934 if err == nil { 935 dc.inUse = true 936 } 937 req <- connRequest{ 938 conn: dc, 939 err: err, 940 } 941 return true 942 } else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) { 943 db.freeConn = append(db.freeConn, dc) 944 db.startCleanerLocked() 945 return true 946 } 947 return false 948 } 949 950 // maxBadConnRetries is the number of maximum retries if the driver returns 951 // driver.ErrBadConn to signal a broken connection before forcing a new 952 // connection to be opened. 953 const maxBadConnRetries = 2 954 955 // Prepare creates a prepared statement for later queries or executions. 956 // Multiple queries or executions may be run concurrently from the 957 // returned statement. 958 // The caller must call the statement's Close method 959 // when the statement is no longer needed. 960 func (db *DB) Prepare(query string) (*Stmt, error) { 961 var stmt *Stmt 962 var err error 963 for i := 0; i < maxBadConnRetries; i++ { 964 stmt, err = db.prepare(query, cachedOrNewConn) 965 if err != driver.ErrBadConn { 966 break 967 } 968 } 969 if err == driver.ErrBadConn { 970 return db.prepare(query, alwaysNewConn) 971 } 972 return stmt, err 973 } 974 975 func (db *DB) prepare(query string, strategy connReuseStrategy) (*Stmt, error) { 976 // TODO: check if db.driver supports an optional 977 // driver.Preparer interface and call that instead, if so, 978 // otherwise we make a prepared statement that's bound 979 // to a connection, and to execute this prepared statement 980 // we either need to use this connection (if it's free), else 981 // get a new connection + re-prepare + execute on that one. 982 dc, err := db.conn(strategy) 983 if err != nil { 984 return nil, err 985 } 986 var si driver.Stmt 987 withLock(dc, func() { 988 si, err = dc.prepareLocked(query) 989 }) 990 if err != nil { 991 db.putConn(dc, err) 992 return nil, err 993 } 994 stmt := &Stmt{ 995 db: db, 996 query: query, 997 css: []connStmt{{dc, si}}, 998 lastNumClosed: atomic.LoadUint64(&db.numClosed), 999 } 1000 db.addDep(stmt, stmt) 1001 db.putConn(dc, nil) 1002 return stmt, nil 1003 } 1004 1005 // Exec executes a query without returning any rows. 1006 // The args are for any placeholder parameters in the query. 1007 func (db *DB) Exec(query string, args ...interface{}) (Result, error) { 1008 var res Result 1009 var err error 1010 for i := 0; i < maxBadConnRetries; i++ { 1011 res, err = db.exec(query, args, cachedOrNewConn) 1012 if err != driver.ErrBadConn { 1013 break 1014 } 1015 } 1016 if err == driver.ErrBadConn { 1017 return db.exec(query, args, alwaysNewConn) 1018 } 1019 return res, err 1020 } 1021 1022 func (db *DB) exec(query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) { 1023 dc, err := db.conn(strategy) 1024 if err != nil { 1025 return nil, err 1026 } 1027 defer func() { 1028 db.putConn(dc, err) 1029 }() 1030 1031 if execer, ok := dc.ci.(driver.Execer); ok { 1032 var dargs []driver.Value 1033 dargs, err = driverArgs(nil, args) 1034 if err != nil { 1035 return nil, err 1036 } 1037 var resi driver.Result 1038 withLock(dc, func() { 1039 resi, err = execer.Exec(query, dargs) 1040 }) 1041 if err != driver.ErrSkip { 1042 if err != nil { 1043 return nil, err 1044 } 1045 return driverResult{dc, resi}, nil 1046 } 1047 } 1048 1049 var si driver.Stmt 1050 withLock(dc, func() { 1051 si, err = dc.ci.Prepare(query) 1052 }) 1053 if err != nil { 1054 return nil, err 1055 } 1056 defer withLock(dc, func() { si.Close() }) 1057 return resultFromStatement(driverStmt{dc, si}, args...) 1058 } 1059 1060 // Query executes a query that returns rows, typically a SELECT. 1061 // The args are for any placeholder parameters in the query. 1062 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) { 1063 var rows *Rows 1064 var err error 1065 for i := 0; i < maxBadConnRetries; i++ { 1066 rows, err = db.query(query, args, cachedOrNewConn) 1067 if err != driver.ErrBadConn { 1068 break 1069 } 1070 } 1071 if err == driver.ErrBadConn { 1072 return db.query(query, args, alwaysNewConn) 1073 } 1074 return rows, err 1075 } 1076 1077 func (db *DB) query(query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) { 1078 ci, err := db.conn(strategy) 1079 if err != nil { 1080 return nil, err 1081 } 1082 1083 return db.queryConn(ci, ci.releaseConn, query, args) 1084 } 1085 1086 // queryConn executes a query on the given connection. 1087 // The connection gets released by the releaseConn function. 1088 func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) { 1089 if queryer, ok := dc.ci.(driver.Queryer); ok { 1090 dargs, err := driverArgs(nil, args) 1091 if err != nil { 1092 releaseConn(err) 1093 return nil, err 1094 } 1095 var rowsi driver.Rows 1096 withLock(dc, func() { 1097 rowsi, err = queryer.Query(query, dargs) 1098 }) 1099 if err != driver.ErrSkip { 1100 if err != nil { 1101 releaseConn(err) 1102 return nil, err 1103 } 1104 // Note: ownership of dc passes to the *Rows, to be freed 1105 // with releaseConn. 1106 rows := &Rows{ 1107 dc: dc, 1108 releaseConn: releaseConn, 1109 rowsi: rowsi, 1110 } 1111 return rows, nil 1112 } 1113 } 1114 1115 var si driver.Stmt 1116 var err error 1117 withLock(dc, func() { 1118 si, err = dc.ci.Prepare(query) 1119 }) 1120 if err != nil { 1121 releaseConn(err) 1122 return nil, err 1123 } 1124 1125 ds := driverStmt{dc, si} 1126 rowsi, err := rowsiFromStatement(ds, args...) 1127 if err != nil { 1128 withLock(dc, func() { 1129 si.Close() 1130 }) 1131 releaseConn(err) 1132 return nil, err 1133 } 1134 1135 // Note: ownership of ci passes to the *Rows, to be freed 1136 // with releaseConn. 1137 rows := &Rows{ 1138 dc: dc, 1139 releaseConn: releaseConn, 1140 rowsi: rowsi, 1141 closeStmt: si, 1142 } 1143 return rows, nil 1144 } 1145 1146 // QueryRow executes a query that is expected to return at most one row. 1147 // QueryRow always returns a non-nil value. Errors are deferred until 1148 // Row's Scan method is called. 1149 func (db *DB) QueryRow(query string, args ...interface{}) *Row { 1150 rows, err := db.Query(query, args...) 1151 return &Row{rows: rows, err: err} 1152 } 1153 1154 // Begin starts a transaction. The isolation level is dependent on 1155 // the driver. 1156 func (db *DB) Begin() (*Tx, error) { 1157 var tx *Tx 1158 var err error 1159 for i := 0; i < maxBadConnRetries; i++ { 1160 tx, err = db.begin(cachedOrNewConn) 1161 if err != driver.ErrBadConn { 1162 break 1163 } 1164 } 1165 if err == driver.ErrBadConn { 1166 return db.begin(alwaysNewConn) 1167 } 1168 return tx, err 1169 } 1170 1171 func (db *DB) begin(strategy connReuseStrategy) (tx *Tx, err error) { 1172 dc, err := db.conn(strategy) 1173 if err != nil { 1174 return nil, err 1175 } 1176 var txi driver.Tx 1177 withLock(dc, func() { 1178 txi, err = dc.ci.Begin() 1179 }) 1180 if err != nil { 1181 db.putConn(dc, err) 1182 return nil, err 1183 } 1184 return &Tx{ 1185 db: db, 1186 dc: dc, 1187 txi: txi, 1188 }, nil 1189 } 1190 1191 // Driver returns the database's underlying driver. 1192 func (db *DB) Driver() driver.Driver { 1193 return db.driver 1194 } 1195 1196 // Tx is an in-progress database transaction. 1197 // 1198 // A transaction must end with a call to Commit or Rollback. 1199 // 1200 // After a call to Commit or Rollback, all operations on the 1201 // transaction fail with ErrTxDone. 1202 // 1203 // The statements prepared for a transaction by calling 1204 // the transaction's Prepare or Stmt methods are closed 1205 // by the call to Commit or Rollback. 1206 type Tx struct { 1207 db *DB 1208 1209 // dc is owned exclusively until Commit or Rollback, at which point 1210 // it's returned with putConn. 1211 dc *driverConn 1212 txi driver.Tx 1213 1214 // done transitions from false to true exactly once, on Commit 1215 // or Rollback. once done, all operations fail with 1216 // ErrTxDone. 1217 done bool 1218 1219 // All Stmts prepared for this transaction. These will be closed after the 1220 // transaction has been committed or rolled back. 1221 stmts struct { 1222 sync.Mutex 1223 v []*Stmt 1224 } 1225 } 1226 1227 var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back") 1228 1229 func (tx *Tx) close(err error) { 1230 if tx.done { 1231 panic("double close") // internal error 1232 } 1233 tx.done = true 1234 tx.db.putConn(tx.dc, err) 1235 tx.dc = nil 1236 tx.txi = nil 1237 } 1238 1239 func (tx *Tx) grabConn() (*driverConn, error) { 1240 if tx.done { 1241 return nil, ErrTxDone 1242 } 1243 return tx.dc, nil 1244 } 1245 1246 // Closes all Stmts prepared for this transaction. 1247 func (tx *Tx) closePrepared() { 1248 tx.stmts.Lock() 1249 defer tx.stmts.Unlock() 1250 for _, stmt := range tx.stmts.v { 1251 stmt.Close() 1252 } 1253 } 1254 1255 // Commit commits the transaction. 1256 func (tx *Tx) Commit() error { 1257 if tx.done { 1258 return ErrTxDone 1259 } 1260 var err error 1261 withLock(tx.dc, func() { 1262 err = tx.txi.Commit() 1263 }) 1264 if err != driver.ErrBadConn { 1265 tx.closePrepared() 1266 } 1267 tx.close(err) 1268 return err 1269 } 1270 1271 // Rollback aborts the transaction. 1272 func (tx *Tx) Rollback() error { 1273 if tx.done { 1274 return ErrTxDone 1275 } 1276 var err error 1277 withLock(tx.dc, func() { 1278 err = tx.txi.Rollback() 1279 }) 1280 if err != driver.ErrBadConn { 1281 tx.closePrepared() 1282 } 1283 tx.close(err) 1284 return err 1285 } 1286 1287 // Prepare creates a prepared statement for use within a transaction. 1288 // 1289 // The returned statement operates within the transaction and can no longer 1290 // be used once the transaction has been committed or rolled back. 1291 // 1292 // To use an existing prepared statement on this transaction, see Tx.Stmt. 1293 func (tx *Tx) Prepare(query string) (*Stmt, error) { 1294 // TODO(bradfitz): We could be more efficient here and either 1295 // provide a method to take an existing Stmt (created on 1296 // perhaps a different Conn), and re-create it on this Conn if 1297 // necessary. Or, better: keep a map in DB of query string to 1298 // Stmts, and have Stmt.Execute do the right thing and 1299 // re-prepare if the Conn in use doesn't have that prepared 1300 // statement. But we'll want to avoid caching the statement 1301 // in the case where we only call conn.Prepare implicitly 1302 // (such as in db.Exec or tx.Exec), but the caller package 1303 // can't be holding a reference to the returned statement. 1304 // Perhaps just looking at the reference count (by noting 1305 // Stmt.Close) would be enough. We might also want a finalizer 1306 // on Stmt to drop the reference count. 1307 dc, err := tx.grabConn() 1308 if err != nil { 1309 return nil, err 1310 } 1311 1312 var si driver.Stmt 1313 withLock(dc, func() { 1314 si, err = dc.ci.Prepare(query) 1315 }) 1316 if err != nil { 1317 return nil, err 1318 } 1319 1320 stmt := &Stmt{ 1321 db: tx.db, 1322 tx: tx, 1323 txsi: &driverStmt{ 1324 Locker: dc, 1325 si: si, 1326 }, 1327 query: query, 1328 } 1329 tx.stmts.Lock() 1330 tx.stmts.v = append(tx.stmts.v, stmt) 1331 tx.stmts.Unlock() 1332 return stmt, nil 1333 } 1334 1335 // Stmt returns a transaction-specific prepared statement from 1336 // an existing statement. 1337 // 1338 // Example: 1339 // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?") 1340 // ... 1341 // tx, err := db.Begin() 1342 // ... 1343 // res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203) 1344 // 1345 // The returned statement operates within the transaction and can no longer 1346 // be used once the transaction has been committed or rolled back. 1347 func (tx *Tx) Stmt(stmt *Stmt) *Stmt { 1348 // TODO(bradfitz): optimize this. Currently this re-prepares 1349 // each time. This is fine for now to illustrate the API but 1350 // we should really cache already-prepared statements 1351 // per-Conn. See also the big comment in Tx.Prepare. 1352 1353 if tx.db != stmt.db { 1354 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")} 1355 } 1356 dc, err := tx.grabConn() 1357 if err != nil { 1358 return &Stmt{stickyErr: err} 1359 } 1360 var si driver.Stmt 1361 withLock(dc, func() { 1362 si, err = dc.ci.Prepare(stmt.query) 1363 }) 1364 txs := &Stmt{ 1365 db: tx.db, 1366 tx: tx, 1367 txsi: &driverStmt{ 1368 Locker: dc, 1369 si: si, 1370 }, 1371 query: stmt.query, 1372 stickyErr: err, 1373 } 1374 tx.stmts.Lock() 1375 tx.stmts.v = append(tx.stmts.v, txs) 1376 tx.stmts.Unlock() 1377 return txs 1378 } 1379 1380 // Exec executes a query that doesn't return rows. 1381 // For example: an INSERT and UPDATE. 1382 func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) { 1383 dc, err := tx.grabConn() 1384 if err != nil { 1385 return nil, err 1386 } 1387 1388 if execer, ok := dc.ci.(driver.Execer); ok { 1389 dargs, err := driverArgs(nil, args) 1390 if err != nil { 1391 return nil, err 1392 } 1393 var resi driver.Result 1394 withLock(dc, func() { 1395 resi, err = execer.Exec(query, dargs) 1396 }) 1397 if err == nil { 1398 return driverResult{dc, resi}, nil 1399 } 1400 if err != driver.ErrSkip { 1401 return nil, err 1402 } 1403 } 1404 1405 var si driver.Stmt 1406 withLock(dc, func() { 1407 si, err = dc.ci.Prepare(query) 1408 }) 1409 if err != nil { 1410 return nil, err 1411 } 1412 defer withLock(dc, func() { si.Close() }) 1413 1414 return resultFromStatement(driverStmt{dc, si}, args...) 1415 } 1416 1417 // Query executes a query that returns rows, typically a SELECT. 1418 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { 1419 dc, err := tx.grabConn() 1420 if err != nil { 1421 return nil, err 1422 } 1423 releaseConn := func(error) {} 1424 return tx.db.queryConn(dc, releaseConn, query, args) 1425 } 1426 1427 // QueryRow executes a query that is expected to return at most one row. 1428 // QueryRow always returns a non-nil value. Errors are deferred until 1429 // Row's Scan method is called. 1430 func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { 1431 rows, err := tx.Query(query, args...) 1432 return &Row{rows: rows, err: err} 1433 } 1434 1435 // connStmt is a prepared statement on a particular connection. 1436 type connStmt struct { 1437 dc *driverConn 1438 si driver.Stmt 1439 } 1440 1441 // Stmt is a prepared statement. 1442 // A Stmt is safe for concurrent use by multiple goroutines. 1443 type Stmt struct { 1444 // Immutable: 1445 db *DB // where we came from 1446 query string // that created the Stmt 1447 stickyErr error // if non-nil, this error is returned for all operations 1448 1449 closemu sync.RWMutex // held exclusively during close, for read otherwise. 1450 1451 // If in a transaction, else both nil: 1452 tx *Tx 1453 txsi *driverStmt 1454 1455 mu sync.Mutex // protects the rest of the fields 1456 closed bool 1457 1458 // css is a list of underlying driver statement interfaces 1459 // that are valid on particular connections. This is only 1460 // used if tx == nil and one is found that has idle 1461 // connections. If tx != nil, txsi is always used. 1462 css []connStmt 1463 1464 // lastNumClosed is copied from db.numClosed when Stmt is created 1465 // without tx and closed connections in css are removed. 1466 lastNumClosed uint64 1467 } 1468 1469 // Exec executes a prepared statement with the given arguments and 1470 // returns a Result summarizing the effect of the statement. 1471 func (s *Stmt) Exec(args ...interface{}) (Result, error) { 1472 s.closemu.RLock() 1473 defer s.closemu.RUnlock() 1474 1475 var res Result 1476 for i := 0; i < maxBadConnRetries; i++ { 1477 dc, releaseConn, si, err := s.connStmt() 1478 if err != nil { 1479 if err == driver.ErrBadConn { 1480 continue 1481 } 1482 return nil, err 1483 } 1484 1485 res, err = resultFromStatement(driverStmt{dc, si}, args...) 1486 releaseConn(err) 1487 if err != driver.ErrBadConn { 1488 return res, err 1489 } 1490 } 1491 return nil, driver.ErrBadConn 1492 } 1493 1494 func driverNumInput(ds driverStmt) int { 1495 ds.Lock() 1496 defer ds.Unlock() // in case NumInput panics 1497 return ds.si.NumInput() 1498 } 1499 1500 func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) { 1501 want := driverNumInput(ds) 1502 1503 // -1 means the driver doesn't know how to count the number of 1504 // placeholders, so we won't sanity check input here and instead let the 1505 // driver deal with errors. 1506 if want != -1 && len(args) != want { 1507 return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args)) 1508 } 1509 1510 dargs, err := driverArgs(&ds, args) 1511 if err != nil { 1512 return nil, err 1513 } 1514 1515 ds.Lock() 1516 defer ds.Unlock() 1517 resi, err := ds.si.Exec(dargs) 1518 if err != nil { 1519 return nil, err 1520 } 1521 return driverResult{ds.Locker, resi}, nil 1522 } 1523 1524 // removeClosedStmtLocked removes closed conns in s.css. 1525 // 1526 // To avoid lock contention on DB.mu, we do it only when 1527 // s.db.numClosed - s.lastNum is large enough. 1528 func (s *Stmt) removeClosedStmtLocked() { 1529 t := len(s.css)/2 + 1 1530 if t > 10 { 1531 t = 10 1532 } 1533 dbClosed := atomic.LoadUint64(&s.db.numClosed) 1534 if dbClosed-s.lastNumClosed < uint64(t) { 1535 return 1536 } 1537 1538 s.db.mu.Lock() 1539 for i := 0; i < len(s.css); i++ { 1540 if s.css[i].dc.dbmuClosed { 1541 s.css[i] = s.css[len(s.css)-1] 1542 s.css = s.css[:len(s.css)-1] 1543 i-- 1544 } 1545 } 1546 s.db.mu.Unlock() 1547 s.lastNumClosed = dbClosed 1548 } 1549 1550 // connStmt returns a free driver connection on which to execute the 1551 // statement, a function to call to release the connection, and a 1552 // statement bound to that connection. 1553 func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) { 1554 if err = s.stickyErr; err != nil { 1555 return 1556 } 1557 s.mu.Lock() 1558 if s.closed { 1559 s.mu.Unlock() 1560 err = errors.New("sql: statement is closed") 1561 return 1562 } 1563 1564 // In a transaction, we always use the connection that the 1565 // transaction was created on. 1566 if s.tx != nil { 1567 s.mu.Unlock() 1568 ci, err = s.tx.grabConn() // blocks, waiting for the connection. 1569 if err != nil { 1570 return 1571 } 1572 releaseConn = func(error) {} 1573 return ci, releaseConn, s.txsi.si, nil 1574 } 1575 1576 s.removeClosedStmtLocked() 1577 s.mu.Unlock() 1578 1579 // TODO(bradfitz): or always wait for one? make configurable later? 1580 dc, err := s.db.conn(cachedOrNewConn) 1581 if err != nil { 1582 return nil, nil, nil, err 1583 } 1584 1585 s.mu.Lock() 1586 for _, v := range s.css { 1587 if v.dc == dc { 1588 s.mu.Unlock() 1589 return dc, dc.releaseConn, v.si, nil 1590 } 1591 } 1592 s.mu.Unlock() 1593 1594 // No luck; we need to prepare the statement on this connection 1595 withLock(dc, func() { 1596 si, err = dc.prepareLocked(s.query) 1597 }) 1598 if err != nil { 1599 s.db.putConn(dc, err) 1600 return nil, nil, nil, err 1601 } 1602 s.mu.Lock() 1603 cs := connStmt{dc, si} 1604 s.css = append(s.css, cs) 1605 s.mu.Unlock() 1606 1607 return dc, dc.releaseConn, si, nil 1608 } 1609 1610 // Query executes a prepared query statement with the given arguments 1611 // and returns the query results as a *Rows. 1612 func (s *Stmt) Query(args ...interface{}) (*Rows, error) { 1613 s.closemu.RLock() 1614 defer s.closemu.RUnlock() 1615 1616 var rowsi driver.Rows 1617 for i := 0; i < maxBadConnRetries; i++ { 1618 dc, releaseConn, si, err := s.connStmt() 1619 if err != nil { 1620 if err == driver.ErrBadConn { 1621 continue 1622 } 1623 return nil, err 1624 } 1625 1626 rowsi, err = rowsiFromStatement(driverStmt{dc, si}, args...) 1627 if err == nil { 1628 // Note: ownership of ci passes to the *Rows, to be freed 1629 // with releaseConn. 1630 rows := &Rows{ 1631 dc: dc, 1632 rowsi: rowsi, 1633 // releaseConn set below 1634 } 1635 s.db.addDep(s, rows) 1636 rows.releaseConn = func(err error) { 1637 releaseConn(err) 1638 s.db.removeDep(s, rows) 1639 } 1640 return rows, nil 1641 } 1642 1643 releaseConn(err) 1644 if err != driver.ErrBadConn { 1645 return nil, err 1646 } 1647 } 1648 return nil, driver.ErrBadConn 1649 } 1650 1651 func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) { 1652 var want int 1653 withLock(ds, func() { 1654 want = ds.si.NumInput() 1655 }) 1656 1657 // -1 means the driver doesn't know how to count the number of 1658 // placeholders, so we won't sanity check input here and instead let the 1659 // driver deal with errors. 1660 if want != -1 && len(args) != want { 1661 return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args)) 1662 } 1663 1664 dargs, err := driverArgs(&ds, args) 1665 if err != nil { 1666 return nil, err 1667 } 1668 1669 ds.Lock() 1670 defer ds.Unlock() 1671 rowsi, err := ds.si.Query(dargs) 1672 if err != nil { 1673 return nil, err 1674 } 1675 return rowsi, nil 1676 } 1677 1678 // QueryRow executes a prepared query statement with the given arguments. 1679 // If an error occurs during the execution of the statement, that error will 1680 // be returned by a call to Scan on the returned *Row, which is always non-nil. 1681 // If the query selects no rows, the *Row's Scan will return ErrNoRows. 1682 // Otherwise, the *Row's Scan scans the first selected row and discards 1683 // the rest. 1684 // 1685 // Example usage: 1686 // 1687 // var name string 1688 // err := nameByUseridStmt.QueryRow(id).Scan(&name) 1689 func (s *Stmt) QueryRow(args ...interface{}) *Row { 1690 rows, err := s.Query(args...) 1691 if err != nil { 1692 return &Row{err: err} 1693 } 1694 return &Row{rows: rows} 1695 } 1696 1697 // Close closes the statement. 1698 func (s *Stmt) Close() error { 1699 s.closemu.Lock() 1700 defer s.closemu.Unlock() 1701 1702 if s.stickyErr != nil { 1703 return s.stickyErr 1704 } 1705 s.mu.Lock() 1706 if s.closed { 1707 s.mu.Unlock() 1708 return nil 1709 } 1710 s.closed = true 1711 1712 if s.tx != nil { 1713 defer s.mu.Unlock() 1714 return s.txsi.Close() 1715 } 1716 s.mu.Unlock() 1717 1718 return s.db.removeDep(s, s) 1719 } 1720 1721 func (s *Stmt) finalClose() error { 1722 s.mu.Lock() 1723 defer s.mu.Unlock() 1724 if s.css != nil { 1725 for _, v := range s.css { 1726 s.db.noteUnusedDriverStatement(v.dc, v.si) 1727 v.dc.removeOpenStmt(v.si) 1728 } 1729 s.css = nil 1730 } 1731 return nil 1732 } 1733 1734 // Rows is the result of a query. Its cursor starts before the first row 1735 // of the result set. Use Next to advance through the rows: 1736 // 1737 // rows, err := db.Query("SELECT ...") 1738 // ... 1739 // defer rows.Close() 1740 // for rows.Next() { 1741 // var id int 1742 // var name string 1743 // err = rows.Scan(&id, &name) 1744 // ... 1745 // } 1746 // err = rows.Err() // get any error encountered during iteration 1747 // ... 1748 type Rows struct { 1749 dc *driverConn // owned; must call releaseConn when closed to release 1750 releaseConn func(error) 1751 rowsi driver.Rows 1752 1753 closed bool 1754 lastcols []driver.Value 1755 lasterr error // non-nil only if closed is true 1756 closeStmt driver.Stmt // if non-nil, statement to Close on close 1757 } 1758 1759 // Next prepares the next result row for reading with the Scan method. It 1760 // returns true on success, or false if there is no next result row or an error 1761 // happened while preparing it. Err should be consulted to distinguish between 1762 // the two cases. 1763 // 1764 // Every call to Scan, even the first one, must be preceded by a call to Next. 1765 func (rs *Rows) Next() bool { 1766 if rs.closed { 1767 return false 1768 } 1769 if rs.lastcols == nil { 1770 rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns())) 1771 } 1772 rs.lasterr = rs.rowsi.Next(rs.lastcols) 1773 if rs.lasterr != nil { 1774 rs.Close() 1775 return false 1776 } 1777 return true 1778 } 1779 1780 // Err returns the error, if any, that was encountered during iteration. 1781 // Err may be called after an explicit or implicit Close. 1782 func (rs *Rows) Err() error { 1783 if rs.lasterr == io.EOF { 1784 return nil 1785 } 1786 return rs.lasterr 1787 } 1788 1789 // Columns returns the column names. 1790 // Columns returns an error if the rows are closed, or if the rows 1791 // are from QueryRow and there was a deferred error. 1792 func (rs *Rows) Columns() ([]string, error) { 1793 if rs.closed { 1794 return nil, errors.New("sql: Rows are closed") 1795 } 1796 if rs.rowsi == nil { 1797 return nil, errors.New("sql: no Rows available") 1798 } 1799 return rs.rowsi.Columns(), nil 1800 } 1801 1802 // Scan copies the columns in the current row into the values pointed 1803 // at by dest. The number of values in dest must be the same as the 1804 // number of columns in Rows. 1805 // 1806 // Scan converts columns read from the database into the following 1807 // common Go types and special types provided by the sql package: 1808 // 1809 // *string 1810 // *[]byte 1811 // *int, *int8, *int16, *int32, *int64 1812 // *uint, *uint8, *uint16, *uint32, *uint64 1813 // *bool 1814 // *float32, *float64 1815 // *interface{} 1816 // *RawBytes 1817 // any type implementing Scanner (see Scanner docs) 1818 // 1819 // In the most simple case, if the type of the value from the source 1820 // column is an integer, bool or string type T and dest is of type *T, 1821 // Scan simply assigns the value through the pointer. 1822 // 1823 // Scan also converts between string and numeric types, as long as no 1824 // information would be lost. While Scan stringifies all numbers 1825 // scanned from numeric database columns into *string, scans into 1826 // numeric types are checked for overflow. For example, a float64 with 1827 // value 300 or a string with value "300" can scan into a uint16, but 1828 // not into a uint8, though float64(255) or "255" can scan into a 1829 // uint8. One exception is that scans of some float64 numbers to 1830 // strings may lose information when stringifying. In general, scan 1831 // floating point columns into *float64. 1832 // 1833 // If a dest argument has type *[]byte, Scan saves in that argument a 1834 // copy of the corresponding data. The copy is owned by the caller and 1835 // can be modified and held indefinitely. The copy can be avoided by 1836 // using an argument of type *RawBytes instead; see the documentation 1837 // for RawBytes for restrictions on its use. 1838 // 1839 // If an argument has type *interface{}, Scan copies the value 1840 // provided by the underlying driver without conversion. When scanning 1841 // from a source value of type []byte to *interface{}, a copy of the 1842 // slice is made and the caller owns the result. 1843 // 1844 // Source values of type time.Time may be scanned into values of type 1845 // *time.Time, *interface{}, *string, or *[]byte. When converting to 1846 // the latter two, time.Format3339Nano is used. 1847 // 1848 // Source values of type bool may be scanned into types *bool, 1849 // *interface{}, *string, *[]byte, or *RawBytes. 1850 // 1851 // For scanning into *bool, the source may be true, false, 1, 0, or 1852 // string inputs parseable by strconv.ParseBool. 1853 func (rs *Rows) Scan(dest ...interface{}) error { 1854 if rs.closed { 1855 return errors.New("sql: Rows are closed") 1856 } 1857 if rs.lastcols == nil { 1858 return errors.New("sql: Scan called without calling Next") 1859 } 1860 if len(dest) != len(rs.lastcols) { 1861 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest)) 1862 } 1863 for i, sv := range rs.lastcols { 1864 err := convertAssign(dest[i], sv) 1865 if err != nil { 1866 return fmt.Errorf("sql: Scan error on column index %d: %v", i, err) 1867 } 1868 } 1869 return nil 1870 } 1871 1872 var rowsCloseHook func(*Rows, *error) 1873 1874 // Close closes the Rows, preventing further enumeration. If Next returns 1875 // false, the Rows are closed automatically and it will suffice to check the 1876 // result of Err. Close is idempotent and does not affect the result of Err. 1877 func (rs *Rows) Close() error { 1878 if rs.closed { 1879 return nil 1880 } 1881 rs.closed = true 1882 err := rs.rowsi.Close() 1883 if fn := rowsCloseHook; fn != nil { 1884 fn(rs, &err) 1885 } 1886 if rs.closeStmt != nil { 1887 rs.closeStmt.Close() 1888 } 1889 rs.releaseConn(err) 1890 return err 1891 } 1892 1893 // Row is the result of calling QueryRow to select a single row. 1894 type Row struct { 1895 // One of these two will be non-nil: 1896 err error // deferred error for easy chaining 1897 rows *Rows 1898 } 1899 1900 // Scan copies the columns from the matched row into the values 1901 // pointed at by dest. See the documentation on Rows.Scan for details. 1902 // If more than one row matches the query, 1903 // Scan uses the first row and discards the rest. If no row matches 1904 // the query, Scan returns ErrNoRows. 1905 func (r *Row) Scan(dest ...interface{}) error { 1906 if r.err != nil { 1907 return r.err 1908 } 1909 1910 // TODO(bradfitz): for now we need to defensively clone all 1911 // []byte that the driver returned (not permitting 1912 // *RawBytes in Rows.Scan), since we're about to close 1913 // the Rows in our defer, when we return from this function. 1914 // the contract with the driver.Next(...) interface is that it 1915 // can return slices into read-only temporary memory that's 1916 // only valid until the next Scan/Close. But the TODO is that 1917 // for a lot of drivers, this copy will be unnecessary. We 1918 // should provide an optional interface for drivers to 1919 // implement to say, "don't worry, the []bytes that I return 1920 // from Next will not be modified again." (for instance, if 1921 // they were obtained from the network anyway) But for now we 1922 // don't care. 1923 defer r.rows.Close() 1924 for _, dp := range dest { 1925 if _, ok := dp.(*RawBytes); ok { 1926 return errors.New("sql: RawBytes isn't allowed on Row.Scan") 1927 } 1928 } 1929 1930 if !r.rows.Next() { 1931 if err := r.rows.Err(); err != nil { 1932 return err 1933 } 1934 return ErrNoRows 1935 } 1936 err := r.rows.Scan(dest...) 1937 if err != nil { 1938 return err 1939 } 1940 // Make sure the query can be processed to completion with no errors. 1941 if err := r.rows.Close(); err != nil { 1942 return err 1943 } 1944 1945 return nil 1946 } 1947 1948 // A Result summarizes an executed SQL command. 1949 type Result interface { 1950 // LastInsertId returns the integer generated by the database 1951 // in response to a command. Typically this will be from an 1952 // "auto increment" column when inserting a new row. Not all 1953 // databases support this feature, and the syntax of such 1954 // statements varies. 1955 LastInsertId() (int64, error) 1956 1957 // RowsAffected returns the number of rows affected by an 1958 // update, insert, or delete. Not every database or database 1959 // driver may support this. 1960 RowsAffected() (int64, error) 1961 } 1962 1963 type driverResult struct { 1964 sync.Locker // the *driverConn 1965 resi driver.Result 1966 } 1967 1968 func (dr driverResult) LastInsertId() (int64, error) { 1969 dr.Lock() 1970 defer dr.Unlock() 1971 return dr.resi.LastInsertId() 1972 } 1973 1974 func (dr driverResult) RowsAffected() (int64, error) { 1975 dr.Lock() 1976 defer dr.Unlock() 1977 return dr.resi.RowsAffected() 1978 } 1979 1980 func stack() string { 1981 var buf [2 << 10]byte 1982 return string(buf[:runtime.Stack(buf[:], false)]) 1983 } 1984 1985 // withLock runs while holding lk. 1986 func withLock(lk sync.Locker, fn func()) { 1987 lk.Lock() 1988 defer lk.Unlock() // in case fn panics 1989 fn() 1990 }