github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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 db.openerCh <- struct{}{} 722 } 723 } 724 725 // Runs in a separate goroutine, opens new connections when requested. 726 func (db *DB) connectionOpener() { 727 for range db.openerCh { 728 db.openNewConnection() 729 } 730 } 731 732 // Open one new connection 733 func (db *DB) openNewConnection() { 734 // maybeOpenNewConnctions has already executed db.numOpen++ before it sent 735 // on db.openerCh. This function must execute db.numOpen-- if the 736 // connection fails or is closed before returning. 737 ci, err := db.driver.Open(db.dsn) 738 db.mu.Lock() 739 defer db.mu.Unlock() 740 if db.closed { 741 if err == nil { 742 ci.Close() 743 } 744 db.numOpen-- 745 return 746 } 747 if err != nil { 748 db.numOpen-- 749 db.putConnDBLocked(nil, err) 750 db.maybeOpenNewConnections() 751 return 752 } 753 dc := &driverConn{ 754 db: db, 755 createdAt: nowFunc(), 756 ci: ci, 757 } 758 if db.putConnDBLocked(dc, err) { 759 db.addDepLocked(dc, dc) 760 } else { 761 db.numOpen-- 762 ci.Close() 763 } 764 } 765 766 // connRequest represents one request for a new connection 767 // When there are no idle connections available, DB.conn will create 768 // a new connRequest and put it on the db.connRequests list. 769 type connRequest struct { 770 conn *driverConn 771 err error 772 } 773 774 var errDBClosed = errors.New("sql: database is closed") 775 776 // conn returns a newly-opened or cached *driverConn. 777 func (db *DB) conn(strategy connReuseStrategy) (*driverConn, error) { 778 db.mu.Lock() 779 if db.closed { 780 db.mu.Unlock() 781 return nil, errDBClosed 782 } 783 lifetime := db.maxLifetime 784 785 // Prefer a free connection, if possible. 786 numFree := len(db.freeConn) 787 if strategy == cachedOrNewConn && numFree > 0 { 788 conn := db.freeConn[0] 789 copy(db.freeConn, db.freeConn[1:]) 790 db.freeConn = db.freeConn[:numFree-1] 791 conn.inUse = true 792 db.mu.Unlock() 793 if conn.expired(lifetime) { 794 conn.Close() 795 return nil, driver.ErrBadConn 796 } 797 return conn, nil 798 } 799 800 // Out of free connections or we were asked not to use one. If we're not 801 // allowed to open any more connections, make a request and wait. 802 if db.maxOpen > 0 && db.numOpen >= db.maxOpen { 803 // Make the connRequest channel. It's buffered so that the 804 // connectionOpener doesn't block while waiting for the req to be read. 805 req := make(chan connRequest, 1) 806 db.connRequests = append(db.connRequests, req) 807 db.mu.Unlock() 808 ret, ok := <-req 809 if !ok { 810 return nil, errDBClosed 811 } 812 if ret.err == nil && ret.conn.expired(lifetime) { 813 ret.conn.Close() 814 return nil, driver.ErrBadConn 815 } 816 return ret.conn, ret.err 817 } 818 819 db.numOpen++ // optimistically 820 db.mu.Unlock() 821 ci, err := db.driver.Open(db.dsn) 822 if err != nil { 823 db.mu.Lock() 824 db.numOpen-- // correct for earlier optimism 825 db.maybeOpenNewConnections() 826 db.mu.Unlock() 827 return nil, err 828 } 829 db.mu.Lock() 830 dc := &driverConn{ 831 db: db, 832 createdAt: nowFunc(), 833 ci: ci, 834 } 835 db.addDepLocked(dc, dc) 836 dc.inUse = true 837 db.mu.Unlock() 838 return dc, nil 839 } 840 841 // putConnHook is a hook for testing. 842 var putConnHook func(*DB, *driverConn) 843 844 // noteUnusedDriverStatement notes that si is no longer used and should 845 // be closed whenever possible (when c is next not in use), unless c is 846 // already closed. 847 func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) { 848 db.mu.Lock() 849 defer db.mu.Unlock() 850 if c.inUse { 851 c.onPut = append(c.onPut, func() { 852 si.Close() 853 }) 854 } else { 855 c.Lock() 856 defer c.Unlock() 857 if !c.finalClosed { 858 si.Close() 859 } 860 } 861 } 862 863 // debugGetPut determines whether getConn & putConn calls' stack traces 864 // are returned for more verbose crashes. 865 const debugGetPut = false 866 867 // putConn adds a connection to the db's free pool. 868 // err is optionally the last error that occurred on this connection. 869 func (db *DB) putConn(dc *driverConn, err error) { 870 db.mu.Lock() 871 if !dc.inUse { 872 if debugGetPut { 873 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc]) 874 } 875 panic("sql: connection returned that was never out") 876 } 877 if debugGetPut { 878 db.lastPut[dc] = stack() 879 } 880 dc.inUse = false 881 882 for _, fn := range dc.onPut { 883 fn() 884 } 885 dc.onPut = nil 886 887 if err == driver.ErrBadConn { 888 // Don't reuse bad connections. 889 // Since the conn is considered bad and is being discarded, treat it 890 // as closed. Don't decrement the open count here, finalClose will 891 // take care of that. 892 db.maybeOpenNewConnections() 893 db.mu.Unlock() 894 dc.Close() 895 return 896 } 897 if putConnHook != nil { 898 putConnHook(db, dc) 899 } 900 added := db.putConnDBLocked(dc, nil) 901 db.mu.Unlock() 902 903 if !added { 904 dc.Close() 905 } 906 } 907 908 // Satisfy a connRequest or put the driverConn in the idle pool and return true 909 // or return false. 910 // putConnDBLocked will satisfy a connRequest if there is one, or it will 911 // return the *driverConn to the freeConn list if err == nil and the idle 912 // connection limit will not be exceeded. 913 // If err != nil, the value of dc is ignored. 914 // If err == nil, then dc must not equal nil. 915 // If a connRequest was fulfilled or the *driverConn was placed in the 916 // freeConn list, then true is returned, otherwise false is returned. 917 func (db *DB) putConnDBLocked(dc *driverConn, err error) bool { 918 if db.maxOpen > 0 && db.numOpen > db.maxOpen { 919 return false 920 } 921 if c := len(db.connRequests); c > 0 { 922 req := db.connRequests[0] 923 // This copy is O(n) but in practice faster than a linked list. 924 // TODO: consider compacting it down less often and 925 // moving the base instead? 926 copy(db.connRequests, db.connRequests[1:]) 927 db.connRequests = db.connRequests[:c-1] 928 if err == nil { 929 dc.inUse = true 930 } 931 req <- connRequest{ 932 conn: dc, 933 err: err, 934 } 935 return true 936 } else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) { 937 db.freeConn = append(db.freeConn, dc) 938 db.startCleanerLocked() 939 return true 940 } 941 return false 942 } 943 944 // maxBadConnRetries is the number of maximum retries if the driver returns 945 // driver.ErrBadConn to signal a broken connection before forcing a new 946 // connection to be opened. 947 const maxBadConnRetries = 2 948 949 // Prepare creates a prepared statement for later queries or executions. 950 // Multiple queries or executions may be run concurrently from the 951 // returned statement. 952 // The caller must call the statement's Close method 953 // when the statement is no longer needed. 954 func (db *DB) Prepare(query string) (*Stmt, error) { 955 var stmt *Stmt 956 var err error 957 for i := 0; i < maxBadConnRetries; i++ { 958 stmt, err = db.prepare(query, cachedOrNewConn) 959 if err != driver.ErrBadConn { 960 break 961 } 962 } 963 if err == driver.ErrBadConn { 964 return db.prepare(query, alwaysNewConn) 965 } 966 return stmt, err 967 } 968 969 func (db *DB) prepare(query string, strategy connReuseStrategy) (*Stmt, error) { 970 // TODO: check if db.driver supports an optional 971 // driver.Preparer interface and call that instead, if so, 972 // otherwise we make a prepared statement that's bound 973 // to a connection, and to execute this prepared statement 974 // we either need to use this connection (if it's free), else 975 // get a new connection + re-prepare + execute on that one. 976 dc, err := db.conn(strategy) 977 if err != nil { 978 return nil, err 979 } 980 dc.Lock() 981 si, err := dc.prepareLocked(query) 982 dc.Unlock() 983 if err != nil { 984 db.putConn(dc, err) 985 return nil, err 986 } 987 stmt := &Stmt{ 988 db: db, 989 query: query, 990 css: []connStmt{{dc, si}}, 991 lastNumClosed: atomic.LoadUint64(&db.numClosed), 992 } 993 db.addDep(stmt, stmt) 994 db.putConn(dc, nil) 995 return stmt, nil 996 } 997 998 // Exec executes a query without returning any rows. 999 // The args are for any placeholder parameters in the query. 1000 func (db *DB) Exec(query string, args ...interface{}) (Result, error) { 1001 var res Result 1002 var err error 1003 for i := 0; i < maxBadConnRetries; i++ { 1004 res, err = db.exec(query, args, cachedOrNewConn) 1005 if err != driver.ErrBadConn { 1006 break 1007 } 1008 } 1009 if err == driver.ErrBadConn { 1010 return db.exec(query, args, alwaysNewConn) 1011 } 1012 return res, err 1013 } 1014 1015 func (db *DB) exec(query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) { 1016 dc, err := db.conn(strategy) 1017 if err != nil { 1018 return nil, err 1019 } 1020 defer func() { 1021 db.putConn(dc, err) 1022 }() 1023 1024 if execer, ok := dc.ci.(driver.Execer); ok { 1025 dargs, err := driverArgs(nil, args) 1026 if err != nil { 1027 return nil, err 1028 } 1029 dc.Lock() 1030 resi, err := execer.Exec(query, dargs) 1031 dc.Unlock() 1032 if err != driver.ErrSkip { 1033 if err != nil { 1034 return nil, err 1035 } 1036 return driverResult{dc, resi}, nil 1037 } 1038 } 1039 1040 dc.Lock() 1041 si, err := dc.ci.Prepare(query) 1042 dc.Unlock() 1043 if err != nil { 1044 return nil, err 1045 } 1046 defer withLock(dc, func() { si.Close() }) 1047 return resultFromStatement(driverStmt{dc, si}, args...) 1048 } 1049 1050 // Query executes a query that returns rows, typically a SELECT. 1051 // The args are for any placeholder parameters in the query. 1052 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) { 1053 var rows *Rows 1054 var err error 1055 for i := 0; i < maxBadConnRetries; i++ { 1056 rows, err = db.query(query, args, cachedOrNewConn) 1057 if err != driver.ErrBadConn { 1058 break 1059 } 1060 } 1061 if err == driver.ErrBadConn { 1062 return db.query(query, args, alwaysNewConn) 1063 } 1064 return rows, err 1065 } 1066 1067 func (db *DB) query(query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) { 1068 ci, err := db.conn(strategy) 1069 if err != nil { 1070 return nil, err 1071 } 1072 1073 return db.queryConn(ci, ci.releaseConn, query, args) 1074 } 1075 1076 // queryConn executes a query on the given connection. 1077 // The connection gets released by the releaseConn function. 1078 func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) { 1079 if queryer, ok := dc.ci.(driver.Queryer); ok { 1080 dargs, err := driverArgs(nil, args) 1081 if err != nil { 1082 releaseConn(err) 1083 return nil, err 1084 } 1085 dc.Lock() 1086 rowsi, err := queryer.Query(query, dargs) 1087 dc.Unlock() 1088 if err != driver.ErrSkip { 1089 if err != nil { 1090 releaseConn(err) 1091 return nil, err 1092 } 1093 // Note: ownership of dc passes to the *Rows, to be freed 1094 // with releaseConn. 1095 rows := &Rows{ 1096 dc: dc, 1097 releaseConn: releaseConn, 1098 rowsi: rowsi, 1099 } 1100 return rows, nil 1101 } 1102 } 1103 1104 dc.Lock() 1105 si, err := dc.ci.Prepare(query) 1106 dc.Unlock() 1107 if err != nil { 1108 releaseConn(err) 1109 return nil, err 1110 } 1111 1112 ds := driverStmt{dc, si} 1113 rowsi, err := rowsiFromStatement(ds, args...) 1114 if err != nil { 1115 dc.Lock() 1116 si.Close() 1117 dc.Unlock() 1118 releaseConn(err) 1119 return nil, err 1120 } 1121 1122 // Note: ownership of ci passes to the *Rows, to be freed 1123 // with releaseConn. 1124 rows := &Rows{ 1125 dc: dc, 1126 releaseConn: releaseConn, 1127 rowsi: rowsi, 1128 closeStmt: si, 1129 } 1130 return rows, nil 1131 } 1132 1133 // QueryRow executes a query that is expected to return at most one row. 1134 // QueryRow always returns a non-nil value. Errors are deferred until 1135 // Row's Scan method is called. 1136 func (db *DB) QueryRow(query string, args ...interface{}) *Row { 1137 rows, err := db.Query(query, args...) 1138 return &Row{rows: rows, err: err} 1139 } 1140 1141 // Begin starts a transaction. The isolation level is dependent on 1142 // the driver. 1143 func (db *DB) Begin() (*Tx, error) { 1144 var tx *Tx 1145 var err error 1146 for i := 0; i < maxBadConnRetries; i++ { 1147 tx, err = db.begin(cachedOrNewConn) 1148 if err != driver.ErrBadConn { 1149 break 1150 } 1151 } 1152 if err == driver.ErrBadConn { 1153 return db.begin(alwaysNewConn) 1154 } 1155 return tx, err 1156 } 1157 1158 func (db *DB) begin(strategy connReuseStrategy) (tx *Tx, err error) { 1159 dc, err := db.conn(strategy) 1160 if err != nil { 1161 return nil, err 1162 } 1163 dc.Lock() 1164 txi, err := dc.ci.Begin() 1165 dc.Unlock() 1166 if err != nil { 1167 db.putConn(dc, err) 1168 return nil, err 1169 } 1170 return &Tx{ 1171 db: db, 1172 dc: dc, 1173 txi: txi, 1174 }, nil 1175 } 1176 1177 // Driver returns the database's underlying driver. 1178 func (db *DB) Driver() driver.Driver { 1179 return db.driver 1180 } 1181 1182 // Tx is an in-progress database transaction. 1183 // 1184 // A transaction must end with a call to Commit or Rollback. 1185 // 1186 // After a call to Commit or Rollback, all operations on the 1187 // transaction fail with ErrTxDone. 1188 // 1189 // The statements prepared for a transaction by calling 1190 // the transaction's Prepare or Stmt methods are closed 1191 // by the call to Commit or Rollback. 1192 type Tx struct { 1193 db *DB 1194 1195 // dc is owned exclusively until Commit or Rollback, at which point 1196 // it's returned with putConn. 1197 dc *driverConn 1198 txi driver.Tx 1199 1200 // done transitions from false to true exactly once, on Commit 1201 // or Rollback. once done, all operations fail with 1202 // ErrTxDone. 1203 done bool 1204 1205 // All Stmts prepared for this transaction. These will be closed after the 1206 // transaction has been committed or rolled back. 1207 stmts struct { 1208 sync.Mutex 1209 v []*Stmt 1210 } 1211 } 1212 1213 var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back") 1214 1215 func (tx *Tx) close(err error) { 1216 if tx.done { 1217 panic("double close") // internal error 1218 } 1219 tx.done = true 1220 tx.db.putConn(tx.dc, err) 1221 tx.dc = nil 1222 tx.txi = nil 1223 } 1224 1225 func (tx *Tx) grabConn() (*driverConn, error) { 1226 if tx.done { 1227 return nil, ErrTxDone 1228 } 1229 return tx.dc, nil 1230 } 1231 1232 // Closes all Stmts prepared for this transaction. 1233 func (tx *Tx) closePrepared() { 1234 tx.stmts.Lock() 1235 for _, stmt := range tx.stmts.v { 1236 stmt.Close() 1237 } 1238 tx.stmts.Unlock() 1239 } 1240 1241 // Commit commits the transaction. 1242 func (tx *Tx) Commit() error { 1243 if tx.done { 1244 return ErrTxDone 1245 } 1246 tx.dc.Lock() 1247 err := tx.txi.Commit() 1248 tx.dc.Unlock() 1249 if err != driver.ErrBadConn { 1250 tx.closePrepared() 1251 } 1252 tx.close(err) 1253 return err 1254 } 1255 1256 // Rollback aborts the transaction. 1257 func (tx *Tx) Rollback() error { 1258 if tx.done { 1259 return ErrTxDone 1260 } 1261 tx.dc.Lock() 1262 err := tx.txi.Rollback() 1263 tx.dc.Unlock() 1264 if err != driver.ErrBadConn { 1265 tx.closePrepared() 1266 } 1267 tx.close(err) 1268 return err 1269 } 1270 1271 // Prepare creates a prepared statement for use within a transaction. 1272 // 1273 // The returned statement operates within the transaction and can no longer 1274 // be used once the transaction has been committed or rolled back. 1275 // 1276 // To use an existing prepared statement on this transaction, see Tx.Stmt. 1277 func (tx *Tx) Prepare(query string) (*Stmt, error) { 1278 // TODO(bradfitz): We could be more efficient here and either 1279 // provide a method to take an existing Stmt (created on 1280 // perhaps a different Conn), and re-create it on this Conn if 1281 // necessary. Or, better: keep a map in DB of query string to 1282 // Stmts, and have Stmt.Execute do the right thing and 1283 // re-prepare if the Conn in use doesn't have that prepared 1284 // statement. But we'll want to avoid caching the statement 1285 // in the case where we only call conn.Prepare implicitly 1286 // (such as in db.Exec or tx.Exec), but the caller package 1287 // can't be holding a reference to the returned statement. 1288 // Perhaps just looking at the reference count (by noting 1289 // Stmt.Close) would be enough. We might also want a finalizer 1290 // on Stmt to drop the reference count. 1291 dc, err := tx.grabConn() 1292 if err != nil { 1293 return nil, err 1294 } 1295 1296 dc.Lock() 1297 si, err := dc.ci.Prepare(query) 1298 dc.Unlock() 1299 if err != nil { 1300 return nil, err 1301 } 1302 1303 stmt := &Stmt{ 1304 db: tx.db, 1305 tx: tx, 1306 txsi: &driverStmt{ 1307 Locker: dc, 1308 si: si, 1309 }, 1310 query: query, 1311 } 1312 tx.stmts.Lock() 1313 tx.stmts.v = append(tx.stmts.v, stmt) 1314 tx.stmts.Unlock() 1315 return stmt, nil 1316 } 1317 1318 // Stmt returns a transaction-specific prepared statement from 1319 // an existing statement. 1320 // 1321 // Example: 1322 // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?") 1323 // ... 1324 // tx, err := db.Begin() 1325 // ... 1326 // res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203) 1327 // 1328 // The returned statement operates within the transaction and can no longer 1329 // be used once the transaction has been committed or rolled back. 1330 func (tx *Tx) Stmt(stmt *Stmt) *Stmt { 1331 // TODO(bradfitz): optimize this. Currently this re-prepares 1332 // each time. This is fine for now to illustrate the API but 1333 // we should really cache already-prepared statements 1334 // per-Conn. See also the big comment in Tx.Prepare. 1335 1336 if tx.db != stmt.db { 1337 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")} 1338 } 1339 dc, err := tx.grabConn() 1340 if err != nil { 1341 return &Stmt{stickyErr: err} 1342 } 1343 dc.Lock() 1344 si, err := dc.ci.Prepare(stmt.query) 1345 dc.Unlock() 1346 txs := &Stmt{ 1347 db: tx.db, 1348 tx: tx, 1349 txsi: &driverStmt{ 1350 Locker: dc, 1351 si: si, 1352 }, 1353 query: stmt.query, 1354 stickyErr: err, 1355 } 1356 tx.stmts.Lock() 1357 tx.stmts.v = append(tx.stmts.v, txs) 1358 tx.stmts.Unlock() 1359 return txs 1360 } 1361 1362 // Exec executes a query that doesn't return rows. 1363 // For example: an INSERT and UPDATE. 1364 func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) { 1365 dc, err := tx.grabConn() 1366 if err != nil { 1367 return nil, err 1368 } 1369 1370 if execer, ok := dc.ci.(driver.Execer); ok { 1371 dargs, err := driverArgs(nil, args) 1372 if err != nil { 1373 return nil, err 1374 } 1375 dc.Lock() 1376 resi, err := execer.Exec(query, dargs) 1377 dc.Unlock() 1378 if err == nil { 1379 return driverResult{dc, resi}, nil 1380 } 1381 if err != driver.ErrSkip { 1382 return nil, err 1383 } 1384 } 1385 1386 dc.Lock() 1387 si, err := dc.ci.Prepare(query) 1388 dc.Unlock() 1389 if err != nil { 1390 return nil, err 1391 } 1392 defer withLock(dc, func() { si.Close() }) 1393 1394 return resultFromStatement(driverStmt{dc, si}, args...) 1395 } 1396 1397 // Query executes a query that returns rows, typically a SELECT. 1398 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { 1399 dc, err := tx.grabConn() 1400 if err != nil { 1401 return nil, err 1402 } 1403 releaseConn := func(error) {} 1404 return tx.db.queryConn(dc, releaseConn, query, args) 1405 } 1406 1407 // QueryRow executes a query that is expected to return at most one row. 1408 // QueryRow always returns a non-nil value. Errors are deferred until 1409 // Row's Scan method is called. 1410 func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { 1411 rows, err := tx.Query(query, args...) 1412 return &Row{rows: rows, err: err} 1413 } 1414 1415 // connStmt is a prepared statement on a particular connection. 1416 type connStmt struct { 1417 dc *driverConn 1418 si driver.Stmt 1419 } 1420 1421 // Stmt is a prepared statement. 1422 // A Stmt is safe for concurrent use by multiple goroutines. 1423 type Stmt struct { 1424 // Immutable: 1425 db *DB // where we came from 1426 query string // that created the Stmt 1427 stickyErr error // if non-nil, this error is returned for all operations 1428 1429 closemu sync.RWMutex // held exclusively during close, for read otherwise. 1430 1431 // If in a transaction, else both nil: 1432 tx *Tx 1433 txsi *driverStmt 1434 1435 mu sync.Mutex // protects the rest of the fields 1436 closed bool 1437 1438 // css is a list of underlying driver statement interfaces 1439 // that are valid on particular connections. This is only 1440 // used if tx == nil and one is found that has idle 1441 // connections. If tx != nil, txsi is always used. 1442 css []connStmt 1443 1444 // lastNumClosed is copied from db.numClosed when Stmt is created 1445 // without tx and closed connections in css are removed. 1446 lastNumClosed uint64 1447 } 1448 1449 // Exec executes a prepared statement with the given arguments and 1450 // returns a Result summarizing the effect of the statement. 1451 func (s *Stmt) Exec(args ...interface{}) (Result, error) { 1452 s.closemu.RLock() 1453 defer s.closemu.RUnlock() 1454 1455 var res Result 1456 for i := 0; i < maxBadConnRetries; i++ { 1457 dc, releaseConn, si, err := s.connStmt() 1458 if err != nil { 1459 if err == driver.ErrBadConn { 1460 continue 1461 } 1462 return nil, err 1463 } 1464 1465 res, err = resultFromStatement(driverStmt{dc, si}, args...) 1466 releaseConn(err) 1467 if err != driver.ErrBadConn { 1468 return res, err 1469 } 1470 } 1471 return nil, driver.ErrBadConn 1472 } 1473 1474 func driverNumInput(ds driverStmt) int { 1475 ds.Lock() 1476 defer ds.Unlock() // in case NumInput panics 1477 return ds.si.NumInput() 1478 } 1479 1480 func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) { 1481 want := driverNumInput(ds) 1482 1483 // -1 means the driver doesn't know how to count the number of 1484 // placeholders, so we won't sanity check input here and instead let the 1485 // driver deal with errors. 1486 if want != -1 && len(args) != want { 1487 return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args)) 1488 } 1489 1490 dargs, err := driverArgs(&ds, args) 1491 if err != nil { 1492 return nil, err 1493 } 1494 1495 ds.Lock() 1496 defer ds.Unlock() 1497 resi, err := ds.si.Exec(dargs) 1498 if err != nil { 1499 return nil, err 1500 } 1501 return driverResult{ds.Locker, resi}, nil 1502 } 1503 1504 // removeClosedStmtLocked removes closed conns in s.css. 1505 // 1506 // To avoid lock contention on DB.mu, we do it only when 1507 // s.db.numClosed - s.lastNum is large enough. 1508 func (s *Stmt) removeClosedStmtLocked() { 1509 t := len(s.css)/2 + 1 1510 if t > 10 { 1511 t = 10 1512 } 1513 dbClosed := atomic.LoadUint64(&s.db.numClosed) 1514 if dbClosed-s.lastNumClosed < uint64(t) { 1515 return 1516 } 1517 1518 s.db.mu.Lock() 1519 for i := 0; i < len(s.css); i++ { 1520 if s.css[i].dc.dbmuClosed { 1521 s.css[i] = s.css[len(s.css)-1] 1522 s.css = s.css[:len(s.css)-1] 1523 i-- 1524 } 1525 } 1526 s.db.mu.Unlock() 1527 s.lastNumClosed = dbClosed 1528 } 1529 1530 // connStmt returns a free driver connection on which to execute the 1531 // statement, a function to call to release the connection, and a 1532 // statement bound to that connection. 1533 func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) { 1534 if err = s.stickyErr; err != nil { 1535 return 1536 } 1537 s.mu.Lock() 1538 if s.closed { 1539 s.mu.Unlock() 1540 err = errors.New("sql: statement is closed") 1541 return 1542 } 1543 1544 // In a transaction, we always use the connection that the 1545 // transaction was created on. 1546 if s.tx != nil { 1547 s.mu.Unlock() 1548 ci, err = s.tx.grabConn() // blocks, waiting for the connection. 1549 if err != nil { 1550 return 1551 } 1552 releaseConn = func(error) {} 1553 return ci, releaseConn, s.txsi.si, nil 1554 } 1555 1556 s.removeClosedStmtLocked() 1557 s.mu.Unlock() 1558 1559 // TODO(bradfitz): or always wait for one? make configurable later? 1560 dc, err := s.db.conn(cachedOrNewConn) 1561 if err != nil { 1562 return nil, nil, nil, err 1563 } 1564 1565 s.mu.Lock() 1566 for _, v := range s.css { 1567 if v.dc == dc { 1568 s.mu.Unlock() 1569 return dc, dc.releaseConn, v.si, nil 1570 } 1571 } 1572 s.mu.Unlock() 1573 1574 // No luck; we need to prepare the statement on this connection 1575 dc.Lock() 1576 si, err = dc.prepareLocked(s.query) 1577 dc.Unlock() 1578 if err != nil { 1579 s.db.putConn(dc, err) 1580 return nil, nil, nil, err 1581 } 1582 s.mu.Lock() 1583 cs := connStmt{dc, si} 1584 s.css = append(s.css, cs) 1585 s.mu.Unlock() 1586 1587 return dc, dc.releaseConn, si, nil 1588 } 1589 1590 // Query executes a prepared query statement with the given arguments 1591 // and returns the query results as a *Rows. 1592 func (s *Stmt) Query(args ...interface{}) (*Rows, error) { 1593 s.closemu.RLock() 1594 defer s.closemu.RUnlock() 1595 1596 var rowsi driver.Rows 1597 for i := 0; i < maxBadConnRetries; i++ { 1598 dc, releaseConn, si, err := s.connStmt() 1599 if err != nil { 1600 if err == driver.ErrBadConn { 1601 continue 1602 } 1603 return nil, err 1604 } 1605 1606 rowsi, err = rowsiFromStatement(driverStmt{dc, si}, args...) 1607 if err == nil { 1608 // Note: ownership of ci passes to the *Rows, to be freed 1609 // with releaseConn. 1610 rows := &Rows{ 1611 dc: dc, 1612 rowsi: rowsi, 1613 // releaseConn set below 1614 } 1615 s.db.addDep(s, rows) 1616 rows.releaseConn = func(err error) { 1617 releaseConn(err) 1618 s.db.removeDep(s, rows) 1619 } 1620 return rows, nil 1621 } 1622 1623 releaseConn(err) 1624 if err != driver.ErrBadConn { 1625 return nil, err 1626 } 1627 } 1628 return nil, driver.ErrBadConn 1629 } 1630 1631 func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) { 1632 ds.Lock() 1633 want := ds.si.NumInput() 1634 ds.Unlock() 1635 1636 // -1 means the driver doesn't know how to count the number of 1637 // placeholders, so we won't sanity check input here and instead let the 1638 // driver deal with errors. 1639 if want != -1 && len(args) != want { 1640 return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args)) 1641 } 1642 1643 dargs, err := driverArgs(&ds, args) 1644 if err != nil { 1645 return nil, err 1646 } 1647 1648 ds.Lock() 1649 rowsi, err := ds.si.Query(dargs) 1650 ds.Unlock() 1651 if err != nil { 1652 return nil, err 1653 } 1654 return rowsi, nil 1655 } 1656 1657 // QueryRow executes a prepared query statement with the given arguments. 1658 // If an error occurs during the execution of the statement, that error will 1659 // be returned by a call to Scan on the returned *Row, which is always non-nil. 1660 // If the query selects no rows, the *Row's Scan will return ErrNoRows. 1661 // Otherwise, the *Row's Scan scans the first selected row and discards 1662 // the rest. 1663 // 1664 // Example usage: 1665 // 1666 // var name string 1667 // err := nameByUseridStmt.QueryRow(id).Scan(&name) 1668 func (s *Stmt) QueryRow(args ...interface{}) *Row { 1669 rows, err := s.Query(args...) 1670 if err != nil { 1671 return &Row{err: err} 1672 } 1673 return &Row{rows: rows} 1674 } 1675 1676 // Close closes the statement. 1677 func (s *Stmt) Close() error { 1678 s.closemu.Lock() 1679 defer s.closemu.Unlock() 1680 1681 if s.stickyErr != nil { 1682 return s.stickyErr 1683 } 1684 s.mu.Lock() 1685 if s.closed { 1686 s.mu.Unlock() 1687 return nil 1688 } 1689 s.closed = true 1690 1691 if s.tx != nil { 1692 err := s.txsi.Close() 1693 s.mu.Unlock() 1694 return err 1695 } 1696 s.mu.Unlock() 1697 1698 return s.db.removeDep(s, s) 1699 } 1700 1701 func (s *Stmt) finalClose() error { 1702 s.mu.Lock() 1703 defer s.mu.Unlock() 1704 if s.css != nil { 1705 for _, v := range s.css { 1706 s.db.noteUnusedDriverStatement(v.dc, v.si) 1707 v.dc.removeOpenStmt(v.si) 1708 } 1709 s.css = nil 1710 } 1711 return nil 1712 } 1713 1714 // Rows is the result of a query. Its cursor starts before the first row 1715 // of the result set. Use Next to advance through the rows: 1716 // 1717 // rows, err := db.Query("SELECT ...") 1718 // ... 1719 // defer rows.Close() 1720 // for rows.Next() { 1721 // var id int 1722 // var name string 1723 // err = rows.Scan(&id, &name) 1724 // ... 1725 // } 1726 // err = rows.Err() // get any error encountered during iteration 1727 // ... 1728 type Rows struct { 1729 dc *driverConn // owned; must call releaseConn when closed to release 1730 releaseConn func(error) 1731 rowsi driver.Rows 1732 1733 closed bool 1734 lastcols []driver.Value 1735 lasterr error // non-nil only if closed is true 1736 closeStmt driver.Stmt // if non-nil, statement to Close on close 1737 } 1738 1739 // Next prepares the next result row for reading with the Scan method. It 1740 // returns true on success, or false if there is no next result row or an error 1741 // happened while preparing it. Err should be consulted to distinguish between 1742 // the two cases. 1743 // 1744 // Every call to Scan, even the first one, must be preceded by a call to Next. 1745 func (rs *Rows) Next() bool { 1746 if rs.closed { 1747 return false 1748 } 1749 if rs.lastcols == nil { 1750 rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns())) 1751 } 1752 rs.lasterr = rs.rowsi.Next(rs.lastcols) 1753 if rs.lasterr != nil { 1754 rs.Close() 1755 return false 1756 } 1757 return true 1758 } 1759 1760 // Err returns the error, if any, that was encountered during iteration. 1761 // Err may be called after an explicit or implicit Close. 1762 func (rs *Rows) Err() error { 1763 if rs.lasterr == io.EOF { 1764 return nil 1765 } 1766 return rs.lasterr 1767 } 1768 1769 // Columns returns the column names. 1770 // Columns returns an error if the rows are closed, or if the rows 1771 // are from QueryRow and there was a deferred error. 1772 func (rs *Rows) Columns() ([]string, error) { 1773 if rs.closed { 1774 return nil, errors.New("sql: Rows are closed") 1775 } 1776 if rs.rowsi == nil { 1777 return nil, errors.New("sql: no Rows available") 1778 } 1779 return rs.rowsi.Columns(), nil 1780 } 1781 1782 // Scan copies the columns in the current row into the values pointed 1783 // at by dest. The number of values in dest must be the same as the 1784 // number of columns in Rows. 1785 // 1786 // Scan converts columns read from the database into the following 1787 // common Go types and special types provided by the sql package: 1788 // 1789 // *string 1790 // *[]byte 1791 // *int, *int8, *int16, *int32, *int64 1792 // *uint, *uint8, *uint16, *uint32, *uint64 1793 // *bool 1794 // *float32, *float64 1795 // *interface{} 1796 // *RawBytes 1797 // any type implementing Scanner (see Scanner docs) 1798 // 1799 // In the most simple case, if the type of the value from the source 1800 // column is an integer, bool or string type T and dest is of type *T, 1801 // Scan simply assigns the value through the pointer. 1802 // 1803 // Scan also converts between string and numeric types, as long as no 1804 // information would be lost. While Scan stringifies all numbers 1805 // scanned from numeric database columns into *string, scans into 1806 // numeric types are checked for overflow. For example, a float64 with 1807 // value 300 or a string with value "300" can scan into a uint16, but 1808 // not into a uint8, though float64(255) or "255" can scan into a 1809 // uint8. One exception is that scans of some float64 numbers to 1810 // strings may lose information when stringifying. In general, scan 1811 // floating point columns into *float64. 1812 // 1813 // If a dest argument has type *[]byte, Scan saves in that argument a 1814 // copy of the corresponding data. The copy is owned by the caller and 1815 // can be modified and held indefinitely. The copy can be avoided by 1816 // using an argument of type *RawBytes instead; see the documentation 1817 // for RawBytes for restrictions on its use. 1818 // 1819 // If an argument has type *interface{}, Scan copies the value 1820 // provided by the underlying driver without conversion. When scanning 1821 // from a source value of type []byte to *interface{}, a copy of the 1822 // slice is made and the caller owns the result. 1823 // 1824 // Source values of type time.Time may be scanned into values of type 1825 // *time.Time, *interface{}, *string, or *[]byte. When converting to 1826 // the latter two, time.Format3339Nano is used. 1827 // 1828 // Source values of type bool may be scanned into types *bool, 1829 // *interface{}, *string, *[]byte, or *RawBytes. 1830 // 1831 // For scanning into *bool, the source may be true, false, 1, 0, or 1832 // string inputs parseable by strconv.ParseBool. 1833 func (rs *Rows) Scan(dest ...interface{}) error { 1834 if rs.closed { 1835 return errors.New("sql: Rows are closed") 1836 } 1837 if rs.lastcols == nil { 1838 return errors.New("sql: Scan called without calling Next") 1839 } 1840 if len(dest) != len(rs.lastcols) { 1841 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest)) 1842 } 1843 for i, sv := range rs.lastcols { 1844 err := convertAssign(dest[i], sv) 1845 if err != nil { 1846 return fmt.Errorf("sql: Scan error on column index %d: %v", i, err) 1847 } 1848 } 1849 return nil 1850 } 1851 1852 var rowsCloseHook func(*Rows, *error) 1853 1854 // Close closes the Rows, preventing further enumeration. If Next returns 1855 // false, the Rows are closed automatically and it will suffice to check the 1856 // result of Err. Close is idempotent and does not affect the result of Err. 1857 func (rs *Rows) Close() error { 1858 if rs.closed { 1859 return nil 1860 } 1861 rs.closed = true 1862 err := rs.rowsi.Close() 1863 if fn := rowsCloseHook; fn != nil { 1864 fn(rs, &err) 1865 } 1866 if rs.closeStmt != nil { 1867 rs.closeStmt.Close() 1868 } 1869 rs.releaseConn(err) 1870 return err 1871 } 1872 1873 // Row is the result of calling QueryRow to select a single row. 1874 type Row struct { 1875 // One of these two will be non-nil: 1876 err error // deferred error for easy chaining 1877 rows *Rows 1878 } 1879 1880 // Scan copies the columns from the matched row into the values 1881 // pointed at by dest. See the documentation on Rows.Scan for details. 1882 // If more than one row matches the query, 1883 // Scan uses the first row and discards the rest. If no row matches 1884 // the query, Scan returns ErrNoRows. 1885 func (r *Row) Scan(dest ...interface{}) error { 1886 if r.err != nil { 1887 return r.err 1888 } 1889 1890 // TODO(bradfitz): for now we need to defensively clone all 1891 // []byte that the driver returned (not permitting 1892 // *RawBytes in Rows.Scan), since we're about to close 1893 // the Rows in our defer, when we return from this function. 1894 // the contract with the driver.Next(...) interface is that it 1895 // can return slices into read-only temporary memory that's 1896 // only valid until the next Scan/Close. But the TODO is that 1897 // for a lot of drivers, this copy will be unnecessary. We 1898 // should provide an optional interface for drivers to 1899 // implement to say, "don't worry, the []bytes that I return 1900 // from Next will not be modified again." (for instance, if 1901 // they were obtained from the network anyway) But for now we 1902 // don't care. 1903 defer r.rows.Close() 1904 for _, dp := range dest { 1905 if _, ok := dp.(*RawBytes); ok { 1906 return errors.New("sql: RawBytes isn't allowed on Row.Scan") 1907 } 1908 } 1909 1910 if !r.rows.Next() { 1911 if err := r.rows.Err(); err != nil { 1912 return err 1913 } 1914 return ErrNoRows 1915 } 1916 err := r.rows.Scan(dest...) 1917 if err != nil { 1918 return err 1919 } 1920 // Make sure the query can be processed to completion with no errors. 1921 if err := r.rows.Close(); err != nil { 1922 return err 1923 } 1924 1925 return nil 1926 } 1927 1928 // A Result summarizes an executed SQL command. 1929 type Result interface { 1930 // LastInsertId returns the integer generated by the database 1931 // in response to a command. Typically this will be from an 1932 // "auto increment" column when inserting a new row. Not all 1933 // databases support this feature, and the syntax of such 1934 // statements varies. 1935 LastInsertId() (int64, error) 1936 1937 // RowsAffected returns the number of rows affected by an 1938 // update, insert, or delete. Not every database or database 1939 // driver may support this. 1940 RowsAffected() (int64, error) 1941 } 1942 1943 type driverResult struct { 1944 sync.Locker // the *driverConn 1945 resi driver.Result 1946 } 1947 1948 func (dr driverResult) LastInsertId() (int64, error) { 1949 dr.Lock() 1950 defer dr.Unlock() 1951 return dr.resi.LastInsertId() 1952 } 1953 1954 func (dr driverResult) RowsAffected() (int64, error) { 1955 dr.Lock() 1956 defer dr.Unlock() 1957 return dr.resi.RowsAffected() 1958 } 1959 1960 func stack() string { 1961 var buf [2 << 10]byte 1962 return string(buf[:runtime.Stack(buf[:], false)]) 1963 } 1964 1965 // withLock runs while holding lk. 1966 func withLock(lk sync.Locker, fn func()) { 1967 lk.Lock() 1968 defer lk.Unlock() // in case fn panics 1969 fn() 1970 }