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