github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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 // Drivers that do not support context cancelation will not return until 12 // after the query is completed. 13 // 14 // For usage examples, see the wiki page at 15 // https://golang.org/s/sqlwiki. 16 package sql 17 18 import ( 19 "context" 20 "database/sql/driver" 21 "errors" 22 "fmt" 23 "io" 24 "reflect" 25 "runtime" 26 "sort" 27 "sync" 28 "sync/atomic" 29 "time" 30 ) 31 32 var ( 33 driversMu sync.RWMutex 34 drivers = make(map[string]driver.Driver) 35 ) 36 37 // nowFunc returns the current time; it's overridden in tests. 38 var nowFunc = time.Now 39 40 // Register makes a database driver available by the provided name. 41 // If Register is called twice with the same name or if driver is nil, 42 // it panics. 43 func Register(name string, driver driver.Driver) { 44 driversMu.Lock() 45 defer driversMu.Unlock() 46 if driver == nil { 47 panic("sql: Register driver is nil") 48 } 49 if _, dup := drivers[name]; dup { 50 panic("sql: Register called twice for driver " + name) 51 } 52 drivers[name] = driver 53 } 54 55 func unregisterAllDrivers() { 56 driversMu.Lock() 57 defer driversMu.Unlock() 58 // For tests. 59 drivers = make(map[string]driver.Driver) 60 } 61 62 // Drivers returns a sorted list of the names of the registered drivers. 63 func Drivers() []string { 64 driversMu.RLock() 65 defer driversMu.RUnlock() 66 var list []string 67 for name := range drivers { 68 list = append(list, name) 69 } 70 sort.Strings(list) 71 return list 72 } 73 74 // A NamedArg is a named argument. NamedArg values may be used as 75 // arguments to Query or Exec and bind to the corresponding named 76 // parameter in the SQL statement. 77 // 78 // For a more concise way to create NamedArg values, see 79 // the Named function. 80 type NamedArg struct { 81 _Named_Fields_Required struct{} 82 83 // Name is the name of the parameter placeholder. 84 // 85 // If empty, the ordinal position in the argument list will be 86 // used. 87 // 88 // Name must omit any symbol prefix. 89 Name string 90 91 // Value is the value of the parameter. 92 // It may be assigned the same value types as the query 93 // arguments. 94 Value interface{} 95 } 96 97 // Named provides a more concise way to create NamedArg values. 98 // 99 // Example usage: 100 // 101 // db.ExecContext(ctx, ` 102 // delete from Invoice 103 // where 104 // TimeCreated < @end 105 // and TimeCreated >= @start;`, 106 // sql.Named("start", startTime), 107 // sql.Named("end", endTime), 108 // ) 109 func Named(name string, value interface{}) NamedArg { 110 // This method exists because the go1compat promise 111 // doesn't guarantee that structs don't grow more fields, 112 // so unkeyed struct literals are a vet error. Thus, we don't 113 // want to allow sql.NamedArg{name, value}. 114 return NamedArg{Name: name, Value: value} 115 } 116 117 // IsolationLevel is the transaction isolation level used in TxOptions. 118 type IsolationLevel int 119 120 // Various isolation levels that drivers may support in BeginTx. 121 // If a driver does not support a given isolation level an error may be returned. 122 // 123 // See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels. 124 const ( 125 LevelDefault IsolationLevel = iota 126 LevelReadUncommitted 127 LevelReadCommitted 128 LevelWriteCommitted 129 LevelRepeatableRead 130 LevelSnapshot 131 LevelSerializable 132 LevelLinearizable 133 ) 134 135 // TxOptions holds the transaction options to be used in DB.BeginTx. 136 type TxOptions struct { 137 // Isolation is the transaction isolation level. 138 // If zero, the driver or database's default level is used. 139 Isolation IsolationLevel 140 ReadOnly bool 141 } 142 143 // RawBytes is a byte slice that holds a reference to memory owned by 144 // the database itself. After a Scan into a RawBytes, the slice is only 145 // valid until the next call to Next, Scan, or Close. 146 type RawBytes []byte 147 148 // NullString represents a string that may be null. 149 // NullString implements the Scanner interface so 150 // it can be used as a scan destination: 151 // 152 // var s NullString 153 // err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s) 154 // ... 155 // if s.Valid { 156 // // use s.String 157 // } else { 158 // // NULL value 159 // } 160 // 161 type NullString struct { 162 String string 163 Valid bool // Valid is true if String is not NULL 164 } 165 166 // Scan implements the Scanner interface. 167 func (ns *NullString) Scan(value interface{}) error { 168 if value == nil { 169 ns.String, ns.Valid = "", false 170 return nil 171 } 172 ns.Valid = true 173 return convertAssign(&ns.String, value) 174 } 175 176 // Value implements the driver Valuer interface. 177 func (ns NullString) Value() (driver.Value, error) { 178 if !ns.Valid { 179 return nil, nil 180 } 181 return ns.String, nil 182 } 183 184 // NullInt64 represents an int64 that may be null. 185 // NullInt64 implements the Scanner interface so 186 // it can be used as a scan destination, similar to NullString. 187 type NullInt64 struct { 188 Int64 int64 189 Valid bool // Valid is true if Int64 is not NULL 190 } 191 192 // Scan implements the Scanner interface. 193 func (n *NullInt64) Scan(value interface{}) error { 194 if value == nil { 195 n.Int64, n.Valid = 0, false 196 return nil 197 } 198 n.Valid = true 199 return convertAssign(&n.Int64, value) 200 } 201 202 // Value implements the driver Valuer interface. 203 func (n NullInt64) Value() (driver.Value, error) { 204 if !n.Valid { 205 return nil, nil 206 } 207 return n.Int64, nil 208 } 209 210 // NullFloat64 represents a float64 that may be null. 211 // NullFloat64 implements the Scanner interface so 212 // it can be used as a scan destination, similar to NullString. 213 type NullFloat64 struct { 214 Float64 float64 215 Valid bool // Valid is true if Float64 is not NULL 216 } 217 218 // Scan implements the Scanner interface. 219 func (n *NullFloat64) Scan(value interface{}) error { 220 if value == nil { 221 n.Float64, n.Valid = 0, false 222 return nil 223 } 224 n.Valid = true 225 return convertAssign(&n.Float64, value) 226 } 227 228 // Value implements the driver Valuer interface. 229 func (n NullFloat64) Value() (driver.Value, error) { 230 if !n.Valid { 231 return nil, nil 232 } 233 return n.Float64, nil 234 } 235 236 // NullBool represents a bool that may be null. 237 // NullBool implements the Scanner interface so 238 // it can be used as a scan destination, similar to NullString. 239 type NullBool struct { 240 Bool bool 241 Valid bool // Valid is true if Bool is not NULL 242 } 243 244 // Scan implements the Scanner interface. 245 func (n *NullBool) Scan(value interface{}) error { 246 if value == nil { 247 n.Bool, n.Valid = false, false 248 return nil 249 } 250 n.Valid = true 251 return convertAssign(&n.Bool, value) 252 } 253 254 // Value implements the driver Valuer interface. 255 func (n NullBool) Value() (driver.Value, error) { 256 if !n.Valid { 257 return nil, nil 258 } 259 return n.Bool, nil 260 } 261 262 // Scanner is an interface used by Scan. 263 type Scanner interface { 264 // Scan assigns a value from a database driver. 265 // 266 // The src value will be of one of the following types: 267 // 268 // int64 269 // float64 270 // bool 271 // []byte 272 // string 273 // time.Time 274 // nil - for NULL values 275 // 276 // An error should be returned if the value cannot be stored 277 // without loss of information. 278 Scan(src interface{}) error 279 } 280 281 // Out may be used to retrieve OUTPUT value parameters from stored procedures. 282 // 283 // Not all drivers and databases support OUTPUT value parameters. 284 // 285 // Example usage: 286 // 287 // var outArg string 288 // _, err := db.ExecContext(ctx, "ProcName", sql.Named("Arg1", Out{Dest: &outArg})) 289 type Out struct { 290 _Named_Fields_Required struct{} 291 292 // Dest is a pointer to the value that will be set to the result of the 293 // stored procedure's OUTPUT parameter. 294 Dest interface{} 295 296 // In is whether the parameter is an INOUT parameter. If so, the input value to the stored 297 // procedure is the dereferenced value of Dest's pointer, which is then replaced with 298 // the output value. 299 In bool 300 } 301 302 // ErrNoRows is returned by Scan when QueryRow doesn't return a 303 // row. In such a case, QueryRow returns a placeholder *Row value that 304 // defers this error until a Scan. 305 var ErrNoRows = errors.New("sql: no rows in result set") 306 307 // DB is a database handle representing a pool of zero or more 308 // underlying connections. It's safe for concurrent use by multiple 309 // goroutines. 310 // 311 // The sql package creates and frees connections automatically; it 312 // also maintains a free pool of idle connections. If the database has 313 // a concept of per-connection state, such state can only be reliably 314 // observed within a transaction. Once DB.Begin is called, the 315 // returned Tx is bound to a single connection. Once Commit or 316 // Rollback is called on the transaction, that transaction's 317 // connection is returned to DB's idle connection pool. The pool size 318 // can be controlled with SetMaxIdleConns. 319 type DB struct { 320 driver driver.Driver 321 dsn string 322 // numClosed is an atomic counter which represents a total number of 323 // closed connections. Stmt.openStmt checks it before cleaning closed 324 // connections in Stmt.css. 325 numClosed uint64 326 327 mu sync.Mutex // protects following fields 328 freeConn []*driverConn 329 connRequests map[uint64]chan connRequest 330 nextRequest uint64 // Next key to use in connRequests. 331 numOpen int // number of opened and pending open connections 332 // Used to signal the need for new connections 333 // a goroutine running connectionOpener() reads on this chan and 334 // maybeOpenNewConnections sends on the chan (one send per needed connection) 335 // It is closed during db.Close(). The close tells the connectionOpener 336 // goroutine to exit. 337 openerCh chan struct{} 338 closed bool 339 dep map[finalCloser]depSet 340 lastPut map[*driverConn]string // stacktrace of last conn's put; debug only 341 maxIdle int // zero means defaultMaxIdleConns; negative means 0 342 maxOpen int // <= 0 means unlimited 343 maxLifetime time.Duration // maximum amount of time a connection may be reused 344 cleanerCh chan struct{} 345 } 346 347 // connReuseStrategy determines how (*DB).conn returns database connections. 348 type connReuseStrategy uint8 349 350 const ( 351 // alwaysNewConn forces a new connection to the database. 352 alwaysNewConn connReuseStrategy = iota 353 // cachedOrNewConn returns a cached connection, if available, else waits 354 // for one to become available (if MaxOpenConns has been reached) or 355 // creates a new database connection. 356 cachedOrNewConn 357 ) 358 359 // driverConn wraps a driver.Conn with a mutex, to 360 // be held during all calls into the Conn. (including any calls onto 361 // interfaces returned via that Conn, such as calls on Tx, Stmt, 362 // Result, Rows) 363 type driverConn struct { 364 db *DB 365 createdAt time.Time 366 367 sync.Mutex // guards following 368 ci driver.Conn 369 closed bool 370 finalClosed bool // ci.Close has been called 371 openStmt map[*driverStmt]bool 372 373 // guarded by db.mu 374 inUse bool 375 onPut []func() // code (with db.mu held) run when conn is next returned 376 dbmuClosed bool // same as closed, but guarded by db.mu, for removeClosedStmtLocked 377 } 378 379 func (dc *driverConn) releaseConn(err error) { 380 dc.db.putConn(dc, err) 381 } 382 383 func (dc *driverConn) removeOpenStmt(ds *driverStmt) { 384 dc.Lock() 385 defer dc.Unlock() 386 delete(dc.openStmt, ds) 387 } 388 389 func (dc *driverConn) expired(timeout time.Duration) bool { 390 if timeout <= 0 { 391 return false 392 } 393 return dc.createdAt.Add(timeout).Before(nowFunc()) 394 } 395 396 func (dc *driverConn) prepareLocked(ctx context.Context, query string) (*driverStmt, error) { 397 si, err := ctxDriverPrepare(ctx, dc.ci, query) 398 if err != nil { 399 return nil, err 400 } 401 402 // Track each driverConn's open statements, so we can close them 403 // before closing the conn. 404 // 405 // Wrap all driver.Stmt is *driverStmt to ensure they are only closed once. 406 if dc.openStmt == nil { 407 dc.openStmt = make(map[*driverStmt]bool) 408 } 409 ds := &driverStmt{Locker: dc, si: si} 410 dc.openStmt[ds] = true 411 412 return ds, nil 413 } 414 415 // the dc.db's Mutex is held. 416 func (dc *driverConn) closeDBLocked() func() error { 417 dc.Lock() 418 defer dc.Unlock() 419 if dc.closed { 420 return func() error { return errors.New("sql: duplicate driverConn close") } 421 } 422 dc.closed = true 423 return dc.db.removeDepLocked(dc, dc) 424 } 425 426 func (dc *driverConn) Close() error { 427 dc.Lock() 428 if dc.closed { 429 dc.Unlock() 430 return errors.New("sql: duplicate driverConn close") 431 } 432 dc.closed = true 433 dc.Unlock() // not defer; removeDep finalClose calls may need to lock 434 435 // And now updates that require holding dc.mu.Lock. 436 dc.db.mu.Lock() 437 dc.dbmuClosed = true 438 fn := dc.db.removeDepLocked(dc, dc) 439 dc.db.mu.Unlock() 440 return fn() 441 } 442 443 func (dc *driverConn) finalClose() error { 444 var err error 445 446 // Each *driverStmt has a lock to the dc. Copy the list out of the dc 447 // before calling close on each stmt. 448 var openStmt []*driverStmt 449 withLock(dc, func() { 450 openStmt = make([]*driverStmt, 0, len(dc.openStmt)) 451 for ds := range dc.openStmt { 452 openStmt = append(openStmt, ds) 453 } 454 dc.openStmt = nil 455 }) 456 for _, ds := range openStmt { 457 ds.Close() 458 } 459 withLock(dc, func() { 460 dc.finalClosed = true 461 err = dc.ci.Close() 462 dc.ci = nil 463 }) 464 465 dc.db.mu.Lock() 466 dc.db.numOpen-- 467 dc.db.maybeOpenNewConnections() 468 dc.db.mu.Unlock() 469 470 atomic.AddUint64(&dc.db.numClosed, 1) 471 return err 472 } 473 474 // driverStmt associates a driver.Stmt with the 475 // *driverConn from which it came, so the driverConn's lock can be 476 // held during calls. 477 type driverStmt struct { 478 sync.Locker // the *driverConn 479 si driver.Stmt 480 closed bool 481 closeErr error // return value of previous Close call 482 } 483 484 // Close ensures dirver.Stmt is only closed once any always returns the same 485 // result. 486 func (ds *driverStmt) Close() error { 487 ds.Lock() 488 defer ds.Unlock() 489 if ds.closed { 490 return ds.closeErr 491 } 492 ds.closed = true 493 ds.closeErr = ds.si.Close() 494 return ds.closeErr 495 } 496 497 // depSet is a finalCloser's outstanding dependencies 498 type depSet map[interface{}]bool // set of true bools 499 500 // The finalCloser interface is used by (*DB).addDep and related 501 // dependency reference counting. 502 type finalCloser interface { 503 // finalClose is called when the reference count of an object 504 // goes to zero. (*DB).mu is not held while calling it. 505 finalClose() error 506 } 507 508 // addDep notes that x now depends on dep, and x's finalClose won't be 509 // called until all of x's dependencies are removed with removeDep. 510 func (db *DB) addDep(x finalCloser, dep interface{}) { 511 //println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep)) 512 db.mu.Lock() 513 defer db.mu.Unlock() 514 db.addDepLocked(x, dep) 515 } 516 517 func (db *DB) addDepLocked(x finalCloser, dep interface{}) { 518 if db.dep == nil { 519 db.dep = make(map[finalCloser]depSet) 520 } 521 xdep := db.dep[x] 522 if xdep == nil { 523 xdep = make(depSet) 524 db.dep[x] = xdep 525 } 526 xdep[dep] = true 527 } 528 529 // removeDep notes that x no longer depends on dep. 530 // If x still has dependencies, nil is returned. 531 // If x no longer has any dependencies, its finalClose method will be 532 // called and its error value will be returned. 533 func (db *DB) removeDep(x finalCloser, dep interface{}) error { 534 db.mu.Lock() 535 fn := db.removeDepLocked(x, dep) 536 db.mu.Unlock() 537 return fn() 538 } 539 540 func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error { 541 //println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep)) 542 543 xdep, ok := db.dep[x] 544 if !ok { 545 panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x)) 546 } 547 548 l0 := len(xdep) 549 delete(xdep, dep) 550 551 switch len(xdep) { 552 case l0: 553 // Nothing removed. Shouldn't happen. 554 panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x)) 555 case 0: 556 // No more dependencies. 557 delete(db.dep, x) 558 return x.finalClose 559 default: 560 // Dependencies remain. 561 return func() error { return nil } 562 } 563 } 564 565 // This is the size of the connectionOpener request chan (DB.openerCh). 566 // This value should be larger than the maximum typical value 567 // used for db.maxOpen. If maxOpen is significantly larger than 568 // connectionRequestQueueSize then it is possible for ALL calls into the *DB 569 // to block until the connectionOpener can satisfy the backlog of requests. 570 var connectionRequestQueueSize = 1000000 571 572 // Open opens a database specified by its database driver name and a 573 // driver-specific data source name, usually consisting of at least a 574 // database name and connection information. 575 // 576 // Most users will open a database via a driver-specific connection 577 // helper function that returns a *DB. No database drivers are included 578 // in the Go standard library. See https://golang.org/s/sqldrivers for 579 // a list of third-party drivers. 580 // 581 // Open may just validate its arguments without creating a connection 582 // to the database. To verify that the data source name is valid, call 583 // Ping. 584 // 585 // The returned DB is safe for concurrent use by multiple goroutines 586 // and maintains its own pool of idle connections. Thus, the Open 587 // function should be called just once. It is rarely necessary to 588 // close a DB. 589 func Open(driverName, dataSourceName string) (*DB, error) { 590 driversMu.RLock() 591 driveri, ok := drivers[driverName] 592 driversMu.RUnlock() 593 if !ok { 594 return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName) 595 } 596 db := &DB{ 597 driver: driveri, 598 dsn: dataSourceName, 599 openerCh: make(chan struct{}, connectionRequestQueueSize), 600 lastPut: make(map[*driverConn]string), 601 connRequests: make(map[uint64]chan connRequest), 602 } 603 go db.connectionOpener() 604 return db, nil 605 } 606 607 func (db *DB) pingDC(ctx context.Context, dc *driverConn, release func(error)) error { 608 var err error 609 if pinger, ok := dc.ci.(driver.Pinger); ok { 610 withLock(dc, func() { 611 err = pinger.Ping(ctx) 612 }) 613 } 614 release(err) 615 return err 616 } 617 618 // PingContext verifies a connection to the database is still alive, 619 // establishing a connection if necessary. 620 func (db *DB) PingContext(ctx context.Context) error { 621 var dc *driverConn 622 var err error 623 624 for i := 0; i < maxBadConnRetries; i++ { 625 dc, err = db.conn(ctx, cachedOrNewConn) 626 if err != driver.ErrBadConn { 627 break 628 } 629 } 630 if err == driver.ErrBadConn { 631 dc, err = db.conn(ctx, alwaysNewConn) 632 } 633 if err != nil { 634 return err 635 } 636 637 return db.pingDC(ctx, dc, dc.releaseConn) 638 } 639 640 // Ping verifies a connection to the database is still alive, 641 // establishing a connection if necessary. 642 func (db *DB) Ping() error { 643 return db.PingContext(context.Background()) 644 } 645 646 // Close closes the database, releasing any open resources. 647 // 648 // It is rare to Close a DB, as the DB handle is meant to be 649 // long-lived and shared between many goroutines. 650 func (db *DB) Close() error { 651 db.mu.Lock() 652 if db.closed { // Make DB.Close idempotent 653 db.mu.Unlock() 654 return nil 655 } 656 close(db.openerCh) 657 if db.cleanerCh != nil { 658 close(db.cleanerCh) 659 } 660 var err error 661 fns := make([]func() error, 0, len(db.freeConn)) 662 for _, dc := range db.freeConn { 663 fns = append(fns, dc.closeDBLocked()) 664 } 665 db.freeConn = nil 666 db.closed = true 667 for _, req := range db.connRequests { 668 close(req) 669 } 670 db.mu.Unlock() 671 for _, fn := range fns { 672 err1 := fn() 673 if err1 != nil { 674 err = err1 675 } 676 } 677 return err 678 } 679 680 const defaultMaxIdleConns = 2 681 682 func (db *DB) maxIdleConnsLocked() int { 683 n := db.maxIdle 684 switch { 685 case n == 0: 686 // TODO(bradfitz): ask driver, if supported, for its default preference 687 return defaultMaxIdleConns 688 case n < 0: 689 return 0 690 default: 691 return n 692 } 693 } 694 695 // SetMaxIdleConns sets the maximum number of connections in the idle 696 // connection pool. 697 // 698 // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns 699 // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit 700 // 701 // If n <= 0, no idle connections are retained. 702 func (db *DB) SetMaxIdleConns(n int) { 703 db.mu.Lock() 704 if n > 0 { 705 db.maxIdle = n 706 } else { 707 // No idle connections. 708 db.maxIdle = -1 709 } 710 // Make sure maxIdle doesn't exceed maxOpen 711 if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen { 712 db.maxIdle = db.maxOpen 713 } 714 var closing []*driverConn 715 idleCount := len(db.freeConn) 716 maxIdle := db.maxIdleConnsLocked() 717 if idleCount > maxIdle { 718 closing = db.freeConn[maxIdle:] 719 db.freeConn = db.freeConn[:maxIdle] 720 } 721 db.mu.Unlock() 722 for _, c := range closing { 723 c.Close() 724 } 725 } 726 727 // SetMaxOpenConns sets the maximum number of open connections to the database. 728 // 729 // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than 730 // MaxIdleConns, then MaxIdleConns will be reduced to match the new 731 // MaxOpenConns limit 732 // 733 // If n <= 0, then there is no limit on the number of open connections. 734 // The default is 0 (unlimited). 735 func (db *DB) SetMaxOpenConns(n int) { 736 db.mu.Lock() 737 db.maxOpen = n 738 if n < 0 { 739 db.maxOpen = 0 740 } 741 syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen 742 db.mu.Unlock() 743 if syncMaxIdle { 744 db.SetMaxIdleConns(n) 745 } 746 } 747 748 // SetConnMaxLifetime sets the maximum amount of time a connection may be reused. 749 // 750 // Expired connections may be closed lazily before reuse. 751 // 752 // If d <= 0, connections are reused forever. 753 func (db *DB) SetConnMaxLifetime(d time.Duration) { 754 if d < 0 { 755 d = 0 756 } 757 db.mu.Lock() 758 // wake cleaner up when lifetime is shortened. 759 if d > 0 && d < db.maxLifetime && db.cleanerCh != nil { 760 select { 761 case db.cleanerCh <- struct{}{}: 762 default: 763 } 764 } 765 db.maxLifetime = d 766 db.startCleanerLocked() 767 db.mu.Unlock() 768 } 769 770 // startCleanerLocked starts connectionCleaner if needed. 771 func (db *DB) startCleanerLocked() { 772 if db.maxLifetime > 0 && db.numOpen > 0 && db.cleanerCh == nil { 773 db.cleanerCh = make(chan struct{}, 1) 774 go db.connectionCleaner(db.maxLifetime) 775 } 776 } 777 778 func (db *DB) connectionCleaner(d time.Duration) { 779 const minInterval = time.Second 780 781 if d < minInterval { 782 d = minInterval 783 } 784 t := time.NewTimer(d) 785 786 for { 787 select { 788 case <-t.C: 789 case <-db.cleanerCh: // maxLifetime was changed or db was closed. 790 } 791 792 db.mu.Lock() 793 d = db.maxLifetime 794 if db.closed || db.numOpen == 0 || d <= 0 { 795 db.cleanerCh = nil 796 db.mu.Unlock() 797 return 798 } 799 800 expiredSince := nowFunc().Add(-d) 801 var closing []*driverConn 802 for i := 0; i < len(db.freeConn); i++ { 803 c := db.freeConn[i] 804 if c.createdAt.Before(expiredSince) { 805 closing = append(closing, c) 806 last := len(db.freeConn) - 1 807 db.freeConn[i] = db.freeConn[last] 808 db.freeConn[last] = nil 809 db.freeConn = db.freeConn[:last] 810 i-- 811 } 812 } 813 db.mu.Unlock() 814 815 for _, c := range closing { 816 c.Close() 817 } 818 819 if d < minInterval { 820 d = minInterval 821 } 822 t.Reset(d) 823 } 824 } 825 826 // DBStats contains database statistics. 827 type DBStats struct { 828 // OpenConnections is the number of open connections to the database. 829 OpenConnections int 830 } 831 832 // Stats returns database statistics. 833 func (db *DB) Stats() DBStats { 834 db.mu.Lock() 835 stats := DBStats{ 836 OpenConnections: db.numOpen, 837 } 838 db.mu.Unlock() 839 return stats 840 } 841 842 // Assumes db.mu is locked. 843 // If there are connRequests and the connection limit hasn't been reached, 844 // then tell the connectionOpener to open new connections. 845 func (db *DB) maybeOpenNewConnections() { 846 numRequests := len(db.connRequests) 847 if db.maxOpen > 0 { 848 numCanOpen := db.maxOpen - db.numOpen 849 if numRequests > numCanOpen { 850 numRequests = numCanOpen 851 } 852 } 853 for numRequests > 0 { 854 db.numOpen++ // optimistically 855 numRequests-- 856 if db.closed { 857 return 858 } 859 db.openerCh <- struct{}{} 860 } 861 } 862 863 // Runs in a separate goroutine, opens new connections when requested. 864 func (db *DB) connectionOpener() { 865 for range db.openerCh { 866 db.openNewConnection() 867 } 868 } 869 870 // Open one new connection 871 func (db *DB) openNewConnection() { 872 // maybeOpenNewConnctions has already executed db.numOpen++ before it sent 873 // on db.openerCh. This function must execute db.numOpen-- if the 874 // connection fails or is closed before returning. 875 ci, err := db.driver.Open(db.dsn) 876 db.mu.Lock() 877 defer db.mu.Unlock() 878 if db.closed { 879 if err == nil { 880 ci.Close() 881 } 882 db.numOpen-- 883 return 884 } 885 if err != nil { 886 db.numOpen-- 887 db.putConnDBLocked(nil, err) 888 db.maybeOpenNewConnections() 889 return 890 } 891 dc := &driverConn{ 892 db: db, 893 createdAt: nowFunc(), 894 ci: ci, 895 } 896 if db.putConnDBLocked(dc, err) { 897 db.addDepLocked(dc, dc) 898 } else { 899 db.numOpen-- 900 ci.Close() 901 } 902 } 903 904 // connRequest represents one request for a new connection 905 // When there are no idle connections available, DB.conn will create 906 // a new connRequest and put it on the db.connRequests list. 907 type connRequest struct { 908 conn *driverConn 909 err error 910 } 911 912 var errDBClosed = errors.New("sql: database is closed") 913 914 // nextRequestKeyLocked returns the next connection request key. 915 // It is assumed that nextRequest will not overflow. 916 func (db *DB) nextRequestKeyLocked() uint64 { 917 next := db.nextRequest 918 db.nextRequest++ 919 return next 920 } 921 922 // conn returns a newly-opened or cached *driverConn. 923 func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) { 924 db.mu.Lock() 925 if db.closed { 926 db.mu.Unlock() 927 return nil, errDBClosed 928 } 929 // Check if the context is expired. 930 select { 931 default: 932 case <-ctx.Done(): 933 db.mu.Unlock() 934 return nil, ctx.Err() 935 } 936 lifetime := db.maxLifetime 937 938 // Prefer a free connection, if possible. 939 numFree := len(db.freeConn) 940 if strategy == cachedOrNewConn && numFree > 0 { 941 conn := db.freeConn[0] 942 copy(db.freeConn, db.freeConn[1:]) 943 db.freeConn = db.freeConn[:numFree-1] 944 conn.inUse = true 945 db.mu.Unlock() 946 if conn.expired(lifetime) { 947 conn.Close() 948 return nil, driver.ErrBadConn 949 } 950 return conn, nil 951 } 952 953 // Out of free connections or we were asked not to use one. If we're not 954 // allowed to open any more connections, make a request and wait. 955 if db.maxOpen > 0 && db.numOpen >= db.maxOpen { 956 // Make the connRequest channel. It's buffered so that the 957 // connectionOpener doesn't block while waiting for the req to be read. 958 req := make(chan connRequest, 1) 959 reqKey := db.nextRequestKeyLocked() 960 db.connRequests[reqKey] = req 961 db.mu.Unlock() 962 963 // Timeout the connection request with the context. 964 select { 965 case <-ctx.Done(): 966 // Remove the connection request and ensure no value has been sent 967 // on it after removing. 968 db.mu.Lock() 969 delete(db.connRequests, reqKey) 970 db.mu.Unlock() 971 select { 972 default: 973 case ret, ok := <-req: 974 if ok { 975 db.putConn(ret.conn, ret.err) 976 } 977 } 978 return nil, ctx.Err() 979 case ret, ok := <-req: 980 if !ok { 981 return nil, errDBClosed 982 } 983 if ret.err == nil && ret.conn.expired(lifetime) { 984 ret.conn.Close() 985 return nil, driver.ErrBadConn 986 } 987 return ret.conn, ret.err 988 } 989 } 990 991 db.numOpen++ // optimistically 992 db.mu.Unlock() 993 ci, err := db.driver.Open(db.dsn) 994 if err != nil { 995 db.mu.Lock() 996 db.numOpen-- // correct for earlier optimism 997 db.maybeOpenNewConnections() 998 db.mu.Unlock() 999 return nil, err 1000 } 1001 db.mu.Lock() 1002 dc := &driverConn{ 1003 db: db, 1004 createdAt: nowFunc(), 1005 ci: ci, 1006 inUse: true, 1007 } 1008 db.addDepLocked(dc, dc) 1009 db.mu.Unlock() 1010 return dc, nil 1011 } 1012 1013 // putConnHook is a hook for testing. 1014 var putConnHook func(*DB, *driverConn) 1015 1016 // noteUnusedDriverStatement notes that ds is no longer used and should 1017 // be closed whenever possible (when c is next not in use), unless c is 1018 // already closed. 1019 func (db *DB) noteUnusedDriverStatement(c *driverConn, ds *driverStmt) { 1020 db.mu.Lock() 1021 defer db.mu.Unlock() 1022 if c.inUse { 1023 c.onPut = append(c.onPut, func() { 1024 ds.Close() 1025 }) 1026 } else { 1027 c.Lock() 1028 fc := c.finalClosed 1029 c.Unlock() 1030 if !fc { 1031 ds.Close() 1032 } 1033 } 1034 } 1035 1036 // debugGetPut determines whether getConn & putConn calls' stack traces 1037 // are returned for more verbose crashes. 1038 const debugGetPut = false 1039 1040 // putConn adds a connection to the db's free pool. 1041 // err is optionally the last error that occurred on this connection. 1042 func (db *DB) putConn(dc *driverConn, err error) { 1043 db.mu.Lock() 1044 if !dc.inUse { 1045 if debugGetPut { 1046 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc]) 1047 } 1048 panic("sql: connection returned that was never out") 1049 } 1050 if debugGetPut { 1051 db.lastPut[dc] = stack() 1052 } 1053 dc.inUse = false 1054 1055 for _, fn := range dc.onPut { 1056 fn() 1057 } 1058 dc.onPut = nil 1059 1060 if err == driver.ErrBadConn { 1061 // Don't reuse bad connections. 1062 // Since the conn is considered bad and is being discarded, treat it 1063 // as closed. Don't decrement the open count here, finalClose will 1064 // take care of that. 1065 db.maybeOpenNewConnections() 1066 db.mu.Unlock() 1067 dc.Close() 1068 return 1069 } 1070 if putConnHook != nil { 1071 putConnHook(db, dc) 1072 } 1073 added := db.putConnDBLocked(dc, nil) 1074 db.mu.Unlock() 1075 1076 if !added { 1077 dc.Close() 1078 } 1079 } 1080 1081 // Satisfy a connRequest or put the driverConn in the idle pool and return true 1082 // or return false. 1083 // putConnDBLocked will satisfy a connRequest if there is one, or it will 1084 // return the *driverConn to the freeConn list if err == nil and the idle 1085 // connection limit will not be exceeded. 1086 // If err != nil, the value of dc is ignored. 1087 // If err == nil, then dc must not equal nil. 1088 // If a connRequest was fulfilled or the *driverConn was placed in the 1089 // freeConn list, then true is returned, otherwise false is returned. 1090 func (db *DB) putConnDBLocked(dc *driverConn, err error) bool { 1091 if db.closed { 1092 return false 1093 } 1094 if db.maxOpen > 0 && db.numOpen > db.maxOpen { 1095 return false 1096 } 1097 if c := len(db.connRequests); c > 0 { 1098 var req chan connRequest 1099 var reqKey uint64 1100 for reqKey, req = range db.connRequests { 1101 break 1102 } 1103 delete(db.connRequests, reqKey) // Remove from pending requests. 1104 if err == nil { 1105 dc.inUse = true 1106 } 1107 req <- connRequest{ 1108 conn: dc, 1109 err: err, 1110 } 1111 return true 1112 } else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) { 1113 db.freeConn = append(db.freeConn, dc) 1114 db.startCleanerLocked() 1115 return true 1116 } 1117 return false 1118 } 1119 1120 // maxBadConnRetries is the number of maximum retries if the driver returns 1121 // driver.ErrBadConn to signal a broken connection before forcing a new 1122 // connection to be opened. 1123 const maxBadConnRetries = 2 1124 1125 // PrepareContext creates a prepared statement for later queries or executions. 1126 // Multiple queries or executions may be run concurrently from the 1127 // returned statement. 1128 // The caller must call the statement's Close method 1129 // when the statement is no longer needed. 1130 // 1131 // The provided context is used for the preparation of the statement, not for the 1132 // execution of the statement. 1133 func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) { 1134 var stmt *Stmt 1135 var err error 1136 for i := 0; i < maxBadConnRetries; i++ { 1137 stmt, err = db.prepare(ctx, query, cachedOrNewConn) 1138 if err != driver.ErrBadConn { 1139 break 1140 } 1141 } 1142 if err == driver.ErrBadConn { 1143 return db.prepare(ctx, query, alwaysNewConn) 1144 } 1145 return stmt, err 1146 } 1147 1148 // Prepare creates a prepared statement for later queries or executions. 1149 // Multiple queries or executions may be run concurrently from the 1150 // returned statement. 1151 // The caller must call the statement's Close method 1152 // when the statement is no longer needed. 1153 func (db *DB) Prepare(query string) (*Stmt, error) { 1154 return db.PrepareContext(context.Background(), query) 1155 } 1156 1157 func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*Stmt, error) { 1158 // TODO: check if db.driver supports an optional 1159 // driver.Preparer interface and call that instead, if so, 1160 // otherwise we make a prepared statement that's bound 1161 // to a connection, and to execute this prepared statement 1162 // we either need to use this connection (if it's free), else 1163 // get a new connection + re-prepare + execute on that one. 1164 dc, err := db.conn(ctx, strategy) 1165 if err != nil { 1166 return nil, err 1167 } 1168 return db.prepareDC(ctx, dc, dc.releaseConn, query) 1169 } 1170 1171 func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error), query string) (*Stmt, error) { 1172 var ds *driverStmt 1173 var err error 1174 defer func() { 1175 release(err) 1176 }() 1177 withLock(dc, func() { 1178 ds, err = dc.prepareLocked(ctx, query) 1179 }) 1180 if err != nil { 1181 return nil, err 1182 } 1183 stmt := &Stmt{ 1184 db: db, 1185 query: query, 1186 css: []connStmt{{dc, ds}}, 1187 lastNumClosed: atomic.LoadUint64(&db.numClosed), 1188 } 1189 db.addDep(stmt, stmt) 1190 return stmt, nil 1191 } 1192 1193 // ExecContext executes a query without returning any rows. 1194 // The args are for any placeholder parameters in the query. 1195 func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) { 1196 var res Result 1197 var err error 1198 for i := 0; i < maxBadConnRetries; i++ { 1199 res, err = db.exec(ctx, query, args, cachedOrNewConn) 1200 if err != driver.ErrBadConn { 1201 break 1202 } 1203 } 1204 if err == driver.ErrBadConn { 1205 return db.exec(ctx, query, args, alwaysNewConn) 1206 } 1207 return res, err 1208 } 1209 1210 // Exec executes a query without returning any rows. 1211 // The args are for any placeholder parameters in the query. 1212 func (db *DB) Exec(query string, args ...interface{}) (Result, error) { 1213 return db.ExecContext(context.Background(), query, args...) 1214 } 1215 1216 func (db *DB) exec(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (Result, error) { 1217 dc, err := db.conn(ctx, strategy) 1218 if err != nil { 1219 return nil, err 1220 } 1221 return db.execDC(ctx, dc, dc.releaseConn, query, args) 1222 } 1223 1224 func (db *DB) execDC(ctx context.Context, dc *driverConn, release func(error), query string, args []interface{}) (res Result, err error) { 1225 defer func() { 1226 release(err) 1227 }() 1228 if execer, ok := dc.ci.(driver.Execer); ok { 1229 var dargs []driver.NamedValue 1230 dargs, err = driverArgs(dc.ci, nil, args) 1231 if err != nil { 1232 return nil, err 1233 } 1234 var resi driver.Result 1235 withLock(dc, func() { 1236 resi, err = ctxDriverExec(ctx, execer, query, dargs) 1237 }) 1238 if err != driver.ErrSkip { 1239 if err != nil { 1240 return nil, err 1241 } 1242 return driverResult{dc, resi}, nil 1243 } 1244 } 1245 1246 var si driver.Stmt 1247 withLock(dc, func() { 1248 si, err = ctxDriverPrepare(ctx, dc.ci, query) 1249 }) 1250 if err != nil { 1251 return nil, err 1252 } 1253 ds := &driverStmt{Locker: dc, si: si} 1254 defer ds.Close() 1255 return resultFromStatement(ctx, dc.ci, ds, args...) 1256 } 1257 1258 // QueryContext executes a query that returns rows, typically a SELECT. 1259 // The args are for any placeholder parameters in the query. 1260 func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) { 1261 var rows *Rows 1262 var err error 1263 for i := 0; i < maxBadConnRetries; i++ { 1264 rows, err = db.query(ctx, query, args, cachedOrNewConn) 1265 if err != driver.ErrBadConn { 1266 break 1267 } 1268 } 1269 if err == driver.ErrBadConn { 1270 return db.query(ctx, query, args, alwaysNewConn) 1271 } 1272 return rows, err 1273 } 1274 1275 // Query executes a query that returns rows, typically a SELECT. 1276 // The args are for any placeholder parameters in the query. 1277 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) { 1278 return db.QueryContext(context.Background(), query, args...) 1279 } 1280 1281 func (db *DB) query(ctx context.Context, query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) { 1282 dc, err := db.conn(ctx, strategy) 1283 if err != nil { 1284 return nil, err 1285 } 1286 1287 return db.queryDC(ctx, nil, dc, dc.releaseConn, query, args) 1288 } 1289 1290 // queryDC executes a query on the given connection. 1291 // The connection gets released by the releaseConn function. 1292 // The ctx context is from a query method and the txctx context is from an 1293 // optional transaction context. 1294 func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) { 1295 if queryer, ok := dc.ci.(driver.Queryer); ok { 1296 dargs, err := driverArgs(dc.ci, nil, args) 1297 if err != nil { 1298 releaseConn(err) 1299 return nil, err 1300 } 1301 var rowsi driver.Rows 1302 withLock(dc, func() { 1303 rowsi, err = ctxDriverQuery(ctx, queryer, query, dargs) 1304 }) 1305 if err != driver.ErrSkip { 1306 if err != nil { 1307 releaseConn(err) 1308 return nil, err 1309 } 1310 // Note: ownership of dc passes to the *Rows, to be freed 1311 // with releaseConn. 1312 rows := &Rows{ 1313 dc: dc, 1314 releaseConn: releaseConn, 1315 rowsi: rowsi, 1316 } 1317 rows.initContextClose(ctx, txctx) 1318 return rows, nil 1319 } 1320 } 1321 1322 var si driver.Stmt 1323 var err error 1324 withLock(dc, func() { 1325 si, err = ctxDriverPrepare(ctx, dc.ci, query) 1326 }) 1327 if err != nil { 1328 releaseConn(err) 1329 return nil, err 1330 } 1331 1332 ds := &driverStmt{Locker: dc, si: si} 1333 rowsi, err := rowsiFromStatement(ctx, dc.ci, ds, args...) 1334 if err != nil { 1335 ds.Close() 1336 releaseConn(err) 1337 return nil, err 1338 } 1339 1340 // Note: ownership of ci passes to the *Rows, to be freed 1341 // with releaseConn. 1342 rows := &Rows{ 1343 dc: dc, 1344 releaseConn: releaseConn, 1345 rowsi: rowsi, 1346 closeStmt: ds, 1347 } 1348 rows.initContextClose(ctx, txctx) 1349 return rows, nil 1350 } 1351 1352 // QueryRowContext executes a query that is expected to return at most one row. 1353 // QueryRowContext always returns a non-nil value. Errors are deferred until 1354 // Row's Scan method is called. 1355 func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row { 1356 rows, err := db.QueryContext(ctx, query, args...) 1357 return &Row{rows: rows, err: err} 1358 } 1359 1360 // QueryRow executes a query that is expected to return at most one row. 1361 // QueryRow always returns a non-nil value. Errors are deferred until 1362 // Row's Scan method is called. 1363 func (db *DB) QueryRow(query string, args ...interface{}) *Row { 1364 return db.QueryRowContext(context.Background(), query, args...) 1365 } 1366 1367 // BeginTx starts a transaction. 1368 // 1369 // The provided context is used until the transaction is committed or rolled back. 1370 // If the context is canceled, the sql package will roll back 1371 // the transaction. Tx.Commit will return an error if the context provided to 1372 // BeginTx is canceled. 1373 // 1374 // The provided TxOptions is optional and may be nil if defaults should be used. 1375 // If a non-default isolation level is used that the driver doesn't support, 1376 // an error will be returned. 1377 func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) { 1378 var tx *Tx 1379 var err error 1380 for i := 0; i < maxBadConnRetries; i++ { 1381 tx, err = db.begin(ctx, opts, cachedOrNewConn) 1382 if err != driver.ErrBadConn { 1383 break 1384 } 1385 } 1386 if err == driver.ErrBadConn { 1387 return db.begin(ctx, opts, alwaysNewConn) 1388 } 1389 return tx, err 1390 } 1391 1392 // Begin starts a transaction. The default isolation level is dependent on 1393 // the driver. 1394 func (db *DB) Begin() (*Tx, error) { 1395 return db.BeginTx(context.Background(), nil) 1396 } 1397 1398 func (db *DB) begin(ctx context.Context, opts *TxOptions, strategy connReuseStrategy) (tx *Tx, err error) { 1399 dc, err := db.conn(ctx, strategy) 1400 if err != nil { 1401 return nil, err 1402 } 1403 return db.beginDC(ctx, dc, dc.releaseConn, opts) 1404 } 1405 1406 // beginDC starts a transaction. The provided dc must be valid and ready to use. 1407 func (db *DB) beginDC(ctx context.Context, dc *driverConn, release func(error), opts *TxOptions) (tx *Tx, err error) { 1408 var txi driver.Tx 1409 withLock(dc, func() { 1410 txi, err = ctxDriverBegin(ctx, opts, dc.ci) 1411 }) 1412 if err != nil { 1413 release(err) 1414 return nil, err 1415 } 1416 1417 // Schedule the transaction to rollback when the context is cancelled. 1418 // The cancel function in Tx will be called after done is set to true. 1419 ctx, cancel := context.WithCancel(ctx) 1420 tx = &Tx{ 1421 db: db, 1422 dc: dc, 1423 releaseConn: release, 1424 txi: txi, 1425 cancel: cancel, 1426 ctx: ctx, 1427 } 1428 go tx.awaitDone() 1429 return tx, nil 1430 } 1431 1432 // Driver returns the database's underlying driver. 1433 func (db *DB) Driver() driver.Driver { 1434 return db.driver 1435 } 1436 1437 // ErrConnDone is returned by any operation that is performed on a connection 1438 // that has already been committed or rolled back. 1439 var ErrConnDone = errors.New("database/sql: connection is already closed") 1440 1441 // Conn returns a single connection by either opening a new connection 1442 // or returning an existing connection from the connection pool. Conn will 1443 // block until either a connection is returned or ctx is canceled. 1444 // Queries run on the same Conn will be run in the same database session. 1445 // 1446 // Every Conn must be returned to the database pool after use by 1447 // calling Conn.Close. 1448 func (db *DB) Conn(ctx context.Context) (*Conn, error) { 1449 var dc *driverConn 1450 var err error 1451 for i := 0; i < maxBadConnRetries; i++ { 1452 dc, err = db.conn(ctx, cachedOrNewConn) 1453 if err != driver.ErrBadConn { 1454 break 1455 } 1456 } 1457 if err == driver.ErrBadConn { 1458 dc, err = db.conn(ctx, cachedOrNewConn) 1459 } 1460 if err != nil { 1461 return nil, err 1462 } 1463 1464 conn := &Conn{ 1465 db: db, 1466 dc: dc, 1467 } 1468 return conn, nil 1469 } 1470 1471 // Conn represents a single database session rather a pool of database 1472 // sessions. Prefer running queries from DB unless there is a specific 1473 // need for a continuous single database session. 1474 // 1475 // A Conn must call Close to return the connection to the database pool 1476 // and may do so concurrently with a running query. 1477 // 1478 // After a call to Close, all operations on the 1479 // connection fail with ErrConnDone. 1480 type Conn struct { 1481 db *DB 1482 1483 // closemu prevents the connection from closing while there 1484 // is an active query. It is held for read during queries 1485 // and exclusively during close. 1486 closemu sync.RWMutex 1487 1488 // dc is owned until close, at which point 1489 // it's returned to the connection pool. 1490 dc *driverConn 1491 1492 // done transitions from 0 to 1 exactly once, on close. 1493 // Once done, all operations fail with ErrConnDone. 1494 // Use atomic operations on value when checking value. 1495 done int32 1496 } 1497 1498 func (c *Conn) grabConn() (*driverConn, error) { 1499 if atomic.LoadInt32(&c.done) != 0 { 1500 return nil, ErrConnDone 1501 } 1502 return c.dc, nil 1503 } 1504 1505 // PingContext verifies the connection to the database is still alive. 1506 func (c *Conn) PingContext(ctx context.Context) error { 1507 dc, err := c.grabConn() 1508 if err != nil { 1509 return err 1510 } 1511 1512 c.closemu.RLock() 1513 return c.db.pingDC(ctx, dc, c.closemuRUnlockCondReleaseConn) 1514 } 1515 1516 // ExecContext executes a query without returning any rows. 1517 // The args are for any placeholder parameters in the query. 1518 func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) { 1519 dc, err := c.grabConn() 1520 if err != nil { 1521 return nil, err 1522 } 1523 1524 c.closemu.RLock() 1525 return c.db.execDC(ctx, dc, c.closemuRUnlockCondReleaseConn, query, args) 1526 } 1527 1528 // QueryContext executes a query that returns rows, typically a SELECT. 1529 // The args are for any placeholder parameters in the query. 1530 func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) { 1531 dc, err := c.grabConn() 1532 if err != nil { 1533 return nil, err 1534 } 1535 1536 c.closemu.RLock() 1537 return c.db.queryDC(ctx, nil, dc, c.closemuRUnlockCondReleaseConn, query, args) 1538 } 1539 1540 // QueryRowContext executes a query that is expected to return at most one row. 1541 // QueryRowContext always returns a non-nil value. Errors are deferred until 1542 // Row's Scan method is called. 1543 func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row { 1544 rows, err := c.QueryContext(ctx, query, args...) 1545 return &Row{rows: rows, err: err} 1546 } 1547 1548 // PrepareContext creates a prepared statement for later queries or executions. 1549 // Multiple queries or executions may be run concurrently from the 1550 // returned statement. 1551 // The caller must call the statement's Close method 1552 // when the statement is no longer needed. 1553 // 1554 // The provided context is used for the preparation of the statement, not for the 1555 // execution of the statement. 1556 func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error) { 1557 dc, err := c.grabConn() 1558 if err != nil { 1559 return nil, err 1560 } 1561 1562 c.closemu.RLock() 1563 return c.db.prepareDC(ctx, dc, c.closemuRUnlockCondReleaseConn, query) 1564 } 1565 1566 // BeginTx starts a transaction. 1567 // 1568 // The provided context is used until the transaction is committed or rolled back. 1569 // If the context is canceled, the sql package will roll back 1570 // the transaction. Tx.Commit will return an error if the context provided to 1571 // BeginTx is canceled. 1572 // 1573 // The provided TxOptions is optional and may be nil if defaults should be used. 1574 // If a non-default isolation level is used that the driver doesn't support, 1575 // an error will be returned. 1576 func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) { 1577 dc, err := c.grabConn() 1578 if err != nil { 1579 return nil, err 1580 } 1581 1582 c.closemu.RLock() 1583 return c.db.beginDC(ctx, dc, c.closemuRUnlockCondReleaseConn, opts) 1584 } 1585 1586 // closemuRUnlockCondReleaseConn read unlocks closemu 1587 // as the sql operation is done with the dc. 1588 func (c *Conn) closemuRUnlockCondReleaseConn(err error) { 1589 c.closemu.RUnlock() 1590 if err == driver.ErrBadConn { 1591 c.close(err) 1592 } 1593 } 1594 1595 func (c *Conn) close(err error) error { 1596 if !atomic.CompareAndSwapInt32(&c.done, 0, 1) { 1597 return ErrConnDone 1598 } 1599 1600 // Lock around releasing the driver connection 1601 // to ensure all queries have been stopped before doing so. 1602 c.closemu.Lock() 1603 defer c.closemu.Unlock() 1604 1605 c.dc.releaseConn(err) 1606 c.dc = nil 1607 c.db = nil 1608 return err 1609 } 1610 1611 // Close returns the connection to the connection pool. 1612 // All operations after a Close will return with ErrConnDone. 1613 // Close is safe to call concurrently with other operations and will 1614 // block until all other operations finish. It may be useful to first 1615 // cancel any used context and then call close directly after. 1616 func (c *Conn) Close() error { 1617 return c.close(nil) 1618 } 1619 1620 // Tx is an in-progress database transaction. 1621 // 1622 // A transaction must end with a call to Commit or Rollback. 1623 // 1624 // After a call to Commit or Rollback, all operations on the 1625 // transaction fail with ErrTxDone. 1626 // 1627 // The statements prepared for a transaction by calling 1628 // the transaction's Prepare or Stmt methods are closed 1629 // by the call to Commit or Rollback. 1630 type Tx struct { 1631 db *DB 1632 1633 // closemu prevents the transaction from closing while there 1634 // is an active query. It is held for read during queries 1635 // and exclusively during close. 1636 closemu sync.RWMutex 1637 1638 // dc is owned exclusively until Commit or Rollback, at which point 1639 // it's returned with putConn. 1640 dc *driverConn 1641 txi driver.Tx 1642 1643 // releaseConn is called once the Tx is closed to release 1644 // any held driverConn back to the pool. 1645 releaseConn func(error) 1646 1647 // done transitions from 0 to 1 exactly once, on Commit 1648 // or Rollback. once done, all operations fail with 1649 // ErrTxDone. 1650 // Use atomic operations on value when checking value. 1651 done int32 1652 1653 // All Stmts prepared for this transaction. These will be closed after the 1654 // transaction has been committed or rolled back. 1655 stmts struct { 1656 sync.Mutex 1657 v []*Stmt 1658 } 1659 1660 // cancel is called after done transitions from 0 to 1. 1661 cancel func() 1662 1663 // ctx lives for the life of the transaction. 1664 ctx context.Context 1665 } 1666 1667 // awaitDone blocks until the context in Tx is canceled and rolls back 1668 // the transaction if it's not already done. 1669 func (tx *Tx) awaitDone() { 1670 // Wait for either the transaction to be committed or rolled 1671 // back, or for the associated context to be closed. 1672 <-tx.ctx.Done() 1673 1674 // Discard and close the connection used to ensure the 1675 // transaction is closed and the resources are released. This 1676 // rollback does nothing if the transaction has already been 1677 // committed or rolled back. 1678 tx.rollback(true) 1679 } 1680 1681 func (tx *Tx) isDone() bool { 1682 return atomic.LoadInt32(&tx.done) != 0 1683 } 1684 1685 // ErrTxDone is returned by any operation that is performed on a transaction 1686 // that has already been committed or rolled back. 1687 var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back") 1688 1689 // close returns the connection to the pool and 1690 // must only be called by Tx.rollback or Tx.Commit. 1691 func (tx *Tx) close(err error) { 1692 tx.cancel() 1693 1694 tx.closemu.Lock() 1695 defer tx.closemu.Unlock() 1696 1697 tx.releaseConn(err) 1698 tx.dc = nil 1699 tx.txi = nil 1700 } 1701 1702 // hookTxGrabConn specifies an optional hook to be called on 1703 // a successful call to (*Tx).grabConn. For tests. 1704 var hookTxGrabConn func() 1705 1706 func (tx *Tx) grabConn(ctx context.Context) (*driverConn, error) { 1707 select { 1708 default: 1709 case <-ctx.Done(): 1710 return nil, ctx.Err() 1711 } 1712 if tx.isDone() { 1713 return nil, ErrTxDone 1714 } 1715 if hookTxGrabConn != nil { // test hook 1716 hookTxGrabConn() 1717 } 1718 return tx.dc, nil 1719 } 1720 1721 // closemuRUnlockRelease is used as a func(error) method value in 1722 // ExecContext and QueryContext. Unlocking in the releaseConn keeps 1723 // the driver conn from being returned to the connection pool until 1724 // the Rows has been closed. 1725 func (tx *Tx) closemuRUnlockRelease(error) { 1726 tx.closemu.RUnlock() 1727 } 1728 1729 // Closes all Stmts prepared for this transaction. 1730 func (tx *Tx) closePrepared() { 1731 tx.stmts.Lock() 1732 defer tx.stmts.Unlock() 1733 for _, stmt := range tx.stmts.v { 1734 stmt.Close() 1735 } 1736 } 1737 1738 // Commit commits the transaction. 1739 func (tx *Tx) Commit() error { 1740 if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) { 1741 return ErrTxDone 1742 } 1743 select { 1744 default: 1745 case <-tx.ctx.Done(): 1746 return tx.ctx.Err() 1747 } 1748 var err error 1749 withLock(tx.dc, func() { 1750 err = tx.txi.Commit() 1751 }) 1752 if err != driver.ErrBadConn { 1753 tx.closePrepared() 1754 } 1755 tx.close(err) 1756 return err 1757 } 1758 1759 // rollback aborts the transaction and optionally forces the pool to discard 1760 // the connection. 1761 func (tx *Tx) rollback(discardConn bool) error { 1762 if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) { 1763 return ErrTxDone 1764 } 1765 var err error 1766 withLock(tx.dc, func() { 1767 err = tx.txi.Rollback() 1768 }) 1769 if err != driver.ErrBadConn { 1770 tx.closePrepared() 1771 } 1772 if discardConn { 1773 err = driver.ErrBadConn 1774 } 1775 tx.close(err) 1776 return err 1777 } 1778 1779 // Rollback aborts the transaction. 1780 func (tx *Tx) Rollback() error { 1781 return tx.rollback(false) 1782 } 1783 1784 // Prepare creates a prepared statement for use within a transaction. 1785 // 1786 // The returned statement operates within the transaction and will be closed 1787 // when the transaction has been committed or rolled back. 1788 // 1789 // To use an existing prepared statement on this transaction, see Tx.Stmt. 1790 // 1791 // The provided context will be used for the preparation of the context, not 1792 // for the execution of the returned statement. The returned statement 1793 // will run in the transaction context. 1794 func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) { 1795 tx.closemu.RLock() 1796 defer tx.closemu.RUnlock() 1797 1798 dc, err := tx.grabConn(ctx) 1799 if err != nil { 1800 return nil, err 1801 } 1802 1803 var si driver.Stmt 1804 withLock(dc, func() { 1805 si, err = ctxDriverPrepare(ctx, dc.ci, query) 1806 }) 1807 if err != nil { 1808 return nil, err 1809 } 1810 1811 stmt := &Stmt{ 1812 db: tx.db, 1813 tx: tx, 1814 txds: &driverStmt{ 1815 Locker: dc, 1816 si: si, 1817 }, 1818 query: query, 1819 } 1820 tx.stmts.Lock() 1821 tx.stmts.v = append(tx.stmts.v, stmt) 1822 tx.stmts.Unlock() 1823 return stmt, nil 1824 } 1825 1826 // Prepare creates a prepared statement for use within a transaction. 1827 // 1828 // The returned statement operates within the transaction and can no longer 1829 // be used once the transaction has been committed or rolled back. 1830 // 1831 // To use an existing prepared statement on this transaction, see Tx.Stmt. 1832 func (tx *Tx) Prepare(query string) (*Stmt, error) { 1833 return tx.PrepareContext(context.Background(), query) 1834 } 1835 1836 // StmtContext returns a transaction-specific prepared statement from 1837 // an existing statement. 1838 // 1839 // Example: 1840 // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?") 1841 // ... 1842 // tx, err := db.Begin() 1843 // ... 1844 // res, err := tx.StmtContext(ctx, updateMoney).Exec(123.45, 98293203) 1845 // 1846 // The returned statement operates within the transaction and will be closed 1847 // when the transaction has been committed or rolled back. 1848 func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt { 1849 tx.closemu.RLock() 1850 defer tx.closemu.RUnlock() 1851 1852 if tx.db != stmt.db { 1853 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")} 1854 } 1855 dc, err := tx.grabConn(ctx) 1856 if err != nil { 1857 return &Stmt{stickyErr: err} 1858 } 1859 var si driver.Stmt 1860 var parentStmt *Stmt 1861 stmt.mu.Lock() 1862 if stmt.closed || stmt.tx != nil { 1863 // If the statement has been closed or already belongs to a 1864 // transaction, we can't reuse it in this connection. 1865 // Since tx.StmtContext should never need to be called with a 1866 // Stmt already belonging to tx, we ignore this edge case and 1867 // re-prepare the statement in this case. No need to add 1868 // code-complexity for this. 1869 stmt.mu.Unlock() 1870 withLock(dc, func() { 1871 si, err = ctxDriverPrepare(ctx, dc.ci, stmt.query) 1872 }) 1873 if err != nil { 1874 return &Stmt{stickyErr: err} 1875 } 1876 } else { 1877 stmt.removeClosedStmtLocked() 1878 // See if the statement has already been prepared on this connection, 1879 // and reuse it if possible. 1880 for _, v := range stmt.css { 1881 if v.dc == dc { 1882 si = v.ds.si 1883 break 1884 } 1885 } 1886 1887 stmt.mu.Unlock() 1888 1889 if si == nil { 1890 cs, err := stmt.prepareOnConnLocked(ctx, dc) 1891 if err != nil { 1892 return &Stmt{stickyErr: err} 1893 } 1894 si = cs.si 1895 } 1896 parentStmt = stmt 1897 } 1898 1899 txs := &Stmt{ 1900 db: tx.db, 1901 tx: tx, 1902 txds: &driverStmt{ 1903 Locker: dc, 1904 si: si, 1905 }, 1906 parentStmt: parentStmt, 1907 query: stmt.query, 1908 } 1909 if parentStmt != nil { 1910 tx.db.addDep(parentStmt, txs) 1911 } 1912 tx.stmts.Lock() 1913 tx.stmts.v = append(tx.stmts.v, txs) 1914 tx.stmts.Unlock() 1915 return txs 1916 } 1917 1918 // Stmt returns a transaction-specific prepared statement from 1919 // an existing statement. 1920 // 1921 // Example: 1922 // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?") 1923 // ... 1924 // tx, err := db.Begin() 1925 // ... 1926 // res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203) 1927 // 1928 // The returned statement operates within the transaction and will be closed 1929 // when the transaction has been committed or rolled back. 1930 func (tx *Tx) Stmt(stmt *Stmt) *Stmt { 1931 return tx.StmtContext(context.Background(), stmt) 1932 } 1933 1934 // ExecContext executes a query that doesn't return rows. 1935 // For example: an INSERT and UPDATE. 1936 func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) { 1937 tx.closemu.RLock() 1938 1939 dc, err := tx.grabConn(ctx) 1940 if err != nil { 1941 tx.closemu.RUnlock() 1942 return nil, err 1943 } 1944 return tx.db.execDC(ctx, dc, tx.closemuRUnlockRelease, query, args) 1945 } 1946 1947 // Exec executes a query that doesn't return rows. 1948 // For example: an INSERT and UPDATE. 1949 func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) { 1950 return tx.ExecContext(context.Background(), query, args...) 1951 } 1952 1953 // QueryContext executes a query that returns rows, typically a SELECT. 1954 func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) { 1955 tx.closemu.RLock() 1956 1957 dc, err := tx.grabConn(ctx) 1958 if err != nil { 1959 tx.closemu.RUnlock() 1960 return nil, err 1961 } 1962 1963 return tx.db.queryDC(ctx, tx.ctx, dc, tx.closemuRUnlockRelease, query, args) 1964 } 1965 1966 // Query executes a query that returns rows, typically a SELECT. 1967 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { 1968 return tx.QueryContext(context.Background(), query, args...) 1969 } 1970 1971 // QueryRowContext executes a query that is expected to return at most one row. 1972 // QueryRowContext always returns a non-nil value. Errors are deferred until 1973 // Row's Scan method is called. 1974 func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row { 1975 rows, err := tx.QueryContext(ctx, query, args...) 1976 return &Row{rows: rows, err: err} 1977 } 1978 1979 // QueryRow executes a query that is expected to return at most one row. 1980 // QueryRow always returns a non-nil value. Errors are deferred until 1981 // Row's Scan method is called. 1982 func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { 1983 return tx.QueryRowContext(context.Background(), query, args...) 1984 } 1985 1986 // connStmt is a prepared statement on a particular connection. 1987 type connStmt struct { 1988 dc *driverConn 1989 ds *driverStmt 1990 } 1991 1992 // Stmt is a prepared statement. 1993 // A Stmt is safe for concurrent use by multiple goroutines. 1994 type Stmt struct { 1995 // Immutable: 1996 db *DB // where we came from 1997 query string // that created the Stmt 1998 stickyErr error // if non-nil, this error is returned for all operations 1999 2000 closemu sync.RWMutex // held exclusively during close, for read otherwise. 2001 2002 // If in a transaction, else both nil: 2003 tx *Tx 2004 txds *driverStmt 2005 2006 // parentStmt is set when a transaction-specific statement 2007 // is requested from an identical statement prepared on the same 2008 // conn. parentStmt is used to track the dependency of this statement 2009 // on its originating ("parent") statement so that parentStmt may 2010 // be closed by the user without them having to know whether or not 2011 // any transactions are still using it. 2012 parentStmt *Stmt 2013 2014 mu sync.Mutex // protects the rest of the fields 2015 closed bool 2016 2017 // css is a list of underlying driver statement interfaces 2018 // that are valid on particular connections. This is only 2019 // used if tx == nil and one is found that has idle 2020 // connections. If tx != nil, txds is always used. 2021 css []connStmt 2022 2023 // lastNumClosed is copied from db.numClosed when Stmt is created 2024 // without tx and closed connections in css are removed. 2025 lastNumClosed uint64 2026 } 2027 2028 // ExecContext executes a prepared statement with the given arguments and 2029 // returns a Result summarizing the effect of the statement. 2030 func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error) { 2031 s.closemu.RLock() 2032 defer s.closemu.RUnlock() 2033 2034 var res Result 2035 strategy := cachedOrNewConn 2036 for i := 0; i < maxBadConnRetries+1; i++ { 2037 if i == maxBadConnRetries { 2038 strategy = alwaysNewConn 2039 } 2040 dc, releaseConn, ds, err := s.connStmt(ctx, strategy) 2041 if err != nil { 2042 if err == driver.ErrBadConn { 2043 continue 2044 } 2045 return nil, err 2046 } 2047 2048 res, err = resultFromStatement(ctx, dc.ci, ds, args...) 2049 releaseConn(err) 2050 if err != driver.ErrBadConn { 2051 return res, err 2052 } 2053 } 2054 return nil, driver.ErrBadConn 2055 } 2056 2057 // Exec executes a prepared statement with the given arguments and 2058 // returns a Result summarizing the effect of the statement. 2059 func (s *Stmt) Exec(args ...interface{}) (Result, error) { 2060 return s.ExecContext(context.Background(), args...) 2061 } 2062 2063 func resultFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...interface{}) (Result, error) { 2064 dargs, err := driverArgs(ci, ds, args) 2065 if err != nil { 2066 return nil, err 2067 } 2068 2069 ds.Lock() 2070 defer ds.Unlock() 2071 2072 resi, err := ctxDriverStmtExec(ctx, ds.si, dargs) 2073 if err != nil { 2074 return nil, err 2075 } 2076 return driverResult{ds.Locker, resi}, nil 2077 } 2078 2079 // removeClosedStmtLocked removes closed conns in s.css. 2080 // 2081 // To avoid lock contention on DB.mu, we do it only when 2082 // s.db.numClosed - s.lastNum is large enough. 2083 func (s *Stmt) removeClosedStmtLocked() { 2084 t := len(s.css)/2 + 1 2085 if t > 10 { 2086 t = 10 2087 } 2088 dbClosed := atomic.LoadUint64(&s.db.numClosed) 2089 if dbClosed-s.lastNumClosed < uint64(t) { 2090 return 2091 } 2092 2093 s.db.mu.Lock() 2094 for i := 0; i < len(s.css); i++ { 2095 if s.css[i].dc.dbmuClosed { 2096 s.css[i] = s.css[len(s.css)-1] 2097 s.css = s.css[:len(s.css)-1] 2098 i-- 2099 } 2100 } 2101 s.db.mu.Unlock() 2102 s.lastNumClosed = dbClosed 2103 } 2104 2105 // connStmt returns a free driver connection on which to execute the 2106 // statement, a function to call to release the connection, and a 2107 // statement bound to that connection. 2108 func (s *Stmt) connStmt(ctx context.Context, strategy connReuseStrategy) (ci *driverConn, releaseConn func(error), ds *driverStmt, err error) { 2109 if err = s.stickyErr; err != nil { 2110 return 2111 } 2112 s.mu.Lock() 2113 if s.closed { 2114 s.mu.Unlock() 2115 err = errors.New("sql: statement is closed") 2116 return 2117 } 2118 2119 // In a transaction, we always use the connection that the 2120 // transaction was created on. 2121 if s.tx != nil { 2122 s.mu.Unlock() 2123 ci, err = s.tx.grabConn(ctx) // blocks, waiting for the connection. 2124 if err != nil { 2125 return 2126 } 2127 releaseConn = func(error) {} 2128 return ci, releaseConn, s.txds, nil 2129 } 2130 2131 s.removeClosedStmtLocked() 2132 s.mu.Unlock() 2133 2134 dc, err := s.db.conn(ctx, strategy) 2135 if err != nil { 2136 return nil, nil, nil, err 2137 } 2138 2139 s.mu.Lock() 2140 for _, v := range s.css { 2141 if v.dc == dc { 2142 s.mu.Unlock() 2143 return dc, dc.releaseConn, v.ds, nil 2144 } 2145 } 2146 s.mu.Unlock() 2147 2148 // No luck; we need to prepare the statement on this connection 2149 withLock(dc, func() { 2150 ds, err = s.prepareOnConnLocked(ctx, dc) 2151 }) 2152 if err != nil { 2153 dc.releaseConn(err) 2154 return nil, nil, nil, err 2155 } 2156 2157 return dc, dc.releaseConn, ds, nil 2158 } 2159 2160 // prepareOnConnLocked prepares the query in Stmt s on dc and adds it to the list of 2161 // open connStmt on the statement. It assumes the caller is holding the lock on dc. 2162 func (s *Stmt) prepareOnConnLocked(ctx context.Context, dc *driverConn) (*driverStmt, error) { 2163 si, err := dc.prepareLocked(ctx, s.query) 2164 if err != nil { 2165 return nil, err 2166 } 2167 cs := connStmt{dc, si} 2168 s.mu.Lock() 2169 s.css = append(s.css, cs) 2170 s.mu.Unlock() 2171 return cs.ds, nil 2172 } 2173 2174 // QueryContext executes a prepared query statement with the given arguments 2175 // and returns the query results as a *Rows. 2176 func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) { 2177 s.closemu.RLock() 2178 defer s.closemu.RUnlock() 2179 2180 var rowsi driver.Rows 2181 strategy := cachedOrNewConn 2182 for i := 0; i < maxBadConnRetries+1; i++ { 2183 if i == maxBadConnRetries { 2184 strategy = alwaysNewConn 2185 } 2186 dc, releaseConn, ds, err := s.connStmt(ctx, strategy) 2187 if err != nil { 2188 if err == driver.ErrBadConn { 2189 continue 2190 } 2191 return nil, err 2192 } 2193 2194 rowsi, err = rowsiFromStatement(ctx, dc.ci, ds, args...) 2195 if err == nil { 2196 // Note: ownership of ci passes to the *Rows, to be freed 2197 // with releaseConn. 2198 rows := &Rows{ 2199 dc: dc, 2200 rowsi: rowsi, 2201 // releaseConn set below 2202 } 2203 // addDep must be added before initContextClose or it could attempt 2204 // to removeDep before it has been added. 2205 s.db.addDep(s, rows) 2206 2207 // releaseConn must be set before initContextClose or it could 2208 // release the connection before it is set. 2209 rows.releaseConn = func(err error) { 2210 releaseConn(err) 2211 s.db.removeDep(s, rows) 2212 } 2213 var txctx context.Context 2214 if s.tx != nil { 2215 txctx = s.tx.ctx 2216 } 2217 rows.initContextClose(ctx, txctx) 2218 return rows, nil 2219 } 2220 2221 releaseConn(err) 2222 if err != driver.ErrBadConn { 2223 return nil, err 2224 } 2225 } 2226 return nil, driver.ErrBadConn 2227 } 2228 2229 // Query executes a prepared query statement with the given arguments 2230 // and returns the query results as a *Rows. 2231 func (s *Stmt) Query(args ...interface{}) (*Rows, error) { 2232 return s.QueryContext(context.Background(), args...) 2233 } 2234 2235 func rowsiFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...interface{}) (driver.Rows, error) { 2236 var want int 2237 withLock(ds, func() { 2238 want = ds.si.NumInput() 2239 }) 2240 2241 // -1 means the driver doesn't know how to count the number of 2242 // placeholders, so we won't sanity check input here and instead let the 2243 // driver deal with errors. 2244 if want != -1 && len(args) != want { 2245 return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args)) 2246 } 2247 2248 dargs, err := driverArgs(ci, ds, args) 2249 if err != nil { 2250 return nil, err 2251 } 2252 2253 ds.Lock() 2254 defer ds.Unlock() 2255 2256 rowsi, err := ctxDriverStmtQuery(ctx, ds.si, dargs) 2257 if err != nil { 2258 return nil, err 2259 } 2260 return rowsi, nil 2261 } 2262 2263 // QueryRowContext executes a prepared query statement with the given arguments. 2264 // If an error occurs during the execution of the statement, that error will 2265 // be returned by a call to Scan on the returned *Row, which is always non-nil. 2266 // If the query selects no rows, the *Row's Scan will return ErrNoRows. 2267 // Otherwise, the *Row's Scan scans the first selected row and discards 2268 // the rest. 2269 // 2270 // Example usage: 2271 // 2272 // var name string 2273 // err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name) 2274 func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row { 2275 rows, err := s.QueryContext(ctx, args...) 2276 if err != nil { 2277 return &Row{err: err} 2278 } 2279 return &Row{rows: rows} 2280 } 2281 2282 // QueryRow executes a prepared query statement with the given arguments. 2283 // If an error occurs during the execution of the statement, that error will 2284 // be returned by a call to Scan on the returned *Row, which is always non-nil. 2285 // If the query selects no rows, the *Row's Scan will return ErrNoRows. 2286 // Otherwise, the *Row's Scan scans the first selected row and discards 2287 // the rest. 2288 // 2289 // Example usage: 2290 // 2291 // var name string 2292 // err := nameByUseridStmt.QueryRow(id).Scan(&name) 2293 func (s *Stmt) QueryRow(args ...interface{}) *Row { 2294 return s.QueryRowContext(context.Background(), args...) 2295 } 2296 2297 // Close closes the statement. 2298 func (s *Stmt) Close() error { 2299 s.closemu.Lock() 2300 defer s.closemu.Unlock() 2301 2302 if s.stickyErr != nil { 2303 return s.stickyErr 2304 } 2305 s.mu.Lock() 2306 if s.closed { 2307 s.mu.Unlock() 2308 return nil 2309 } 2310 s.closed = true 2311 s.mu.Unlock() 2312 2313 if s.tx == nil { 2314 return s.db.removeDep(s, s) 2315 } 2316 2317 if s.parentStmt != nil { 2318 // If parentStmt is set, we must not close s.txds since it's stored 2319 // in the css array of the parentStmt. 2320 return s.db.removeDep(s.parentStmt, s) 2321 } 2322 return s.txds.Close() 2323 } 2324 2325 func (s *Stmt) finalClose() error { 2326 s.mu.Lock() 2327 defer s.mu.Unlock() 2328 if s.css != nil { 2329 for _, v := range s.css { 2330 s.db.noteUnusedDriverStatement(v.dc, v.ds) 2331 v.dc.removeOpenStmt(v.ds) 2332 } 2333 s.css = nil 2334 } 2335 return nil 2336 } 2337 2338 // Rows is the result of a query. Its cursor starts before the first row 2339 // of the result set. Use Next to advance through the rows: 2340 // 2341 // rows, err := db.Query("SELECT ...") 2342 // ... 2343 // defer rows.Close() 2344 // for rows.Next() { 2345 // var id int 2346 // var name string 2347 // err = rows.Scan(&id, &name) 2348 // ... 2349 // } 2350 // err = rows.Err() // get any error encountered during iteration 2351 // ... 2352 type Rows struct { 2353 dc *driverConn // owned; must call releaseConn when closed to release 2354 releaseConn func(error) 2355 rowsi driver.Rows 2356 cancel func() // called when Rows is closed, may be nil. 2357 closeStmt *driverStmt // if non-nil, statement to Close on close 2358 2359 // closemu prevents Rows from closing while there 2360 // is an active streaming result. It is held for read during non-close operations 2361 // and exclusively during close. 2362 // 2363 // closemu guards lasterr and closed. 2364 closemu sync.RWMutex 2365 closed bool 2366 lasterr error // non-nil only if closed is true 2367 2368 // lastcols is only used in Scan, Next, and NextResultSet which are expected 2369 // not to be called concurrently. 2370 lastcols []driver.Value 2371 } 2372 2373 func (rs *Rows) initContextClose(ctx, txctx context.Context) { 2374 ctx, rs.cancel = context.WithCancel(ctx) 2375 go rs.awaitDone(ctx, txctx) 2376 } 2377 2378 // awaitDone blocks until either ctx or txctx is canceled. The ctx is provided 2379 // from the query context and is canceled when the query Rows is closed. 2380 // If the query was issued in a transaction, the transaction's context 2381 // is also provided in txctx to ensure Rows is closed if the Tx is closed. 2382 func (rs *Rows) awaitDone(ctx, txctx context.Context) { 2383 var txctxDone <-chan struct{} 2384 if txctx != nil { 2385 txctxDone = txctx.Done() 2386 } 2387 select { 2388 case <-ctx.Done(): 2389 case <-txctxDone: 2390 } 2391 rs.close(ctx.Err()) 2392 } 2393 2394 // Next prepares the next result row for reading with the Scan method. It 2395 // returns true on success, or false if there is no next result row or an error 2396 // happened while preparing it. Err should be consulted to distinguish between 2397 // the two cases. 2398 // 2399 // Every call to Scan, even the first one, must be preceded by a call to Next. 2400 func (rs *Rows) Next() bool { 2401 var doClose, ok bool 2402 withLock(rs.closemu.RLocker(), func() { 2403 doClose, ok = rs.nextLocked() 2404 }) 2405 if doClose { 2406 rs.Close() 2407 } 2408 return ok 2409 } 2410 2411 func (rs *Rows) nextLocked() (doClose, ok bool) { 2412 if rs.closed { 2413 return false, false 2414 } 2415 if rs.lastcols == nil { 2416 rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns())) 2417 } 2418 rs.lasterr = rs.rowsi.Next(rs.lastcols) 2419 if rs.lasterr != nil { 2420 // Close the connection if there is a driver error. 2421 if rs.lasterr != io.EOF { 2422 return true, false 2423 } 2424 nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet) 2425 if !ok { 2426 return true, false 2427 } 2428 // The driver is at the end of the current result set. 2429 // Test to see if there is another result set after the current one. 2430 // Only close Rows if there is no further result sets to read. 2431 if !nextResultSet.HasNextResultSet() { 2432 doClose = true 2433 } 2434 return doClose, false 2435 } 2436 return false, true 2437 } 2438 2439 // NextResultSet prepares the next result set for reading. It returns true if 2440 // there is further result sets, or false if there is no further result set 2441 // or if there is an error advancing to it. The Err method should be consulted 2442 // to distinguish between the two cases. 2443 // 2444 // After calling NextResultSet, the Next method should always be called before 2445 // scanning. If there are further result sets they may not have rows in the result 2446 // set. 2447 func (rs *Rows) NextResultSet() bool { 2448 var doClose bool 2449 defer func() { 2450 if doClose { 2451 rs.Close() 2452 } 2453 }() 2454 rs.closemu.RLock() 2455 defer rs.closemu.RUnlock() 2456 2457 if rs.closed { 2458 return false 2459 } 2460 2461 rs.lastcols = nil 2462 nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet) 2463 if !ok { 2464 doClose = true 2465 return false 2466 } 2467 rs.lasterr = nextResultSet.NextResultSet() 2468 if rs.lasterr != nil { 2469 doClose = true 2470 return false 2471 } 2472 return true 2473 } 2474 2475 // Err returns the error, if any, that was encountered during iteration. 2476 // Err may be called after an explicit or implicit Close. 2477 func (rs *Rows) Err() error { 2478 rs.closemu.RLock() 2479 defer rs.closemu.RUnlock() 2480 if rs.lasterr == io.EOF { 2481 return nil 2482 } 2483 return rs.lasterr 2484 } 2485 2486 // Columns returns the column names. 2487 // Columns returns an error if the rows are closed, or if the rows 2488 // are from QueryRow and there was a deferred error. 2489 func (rs *Rows) Columns() ([]string, error) { 2490 rs.closemu.RLock() 2491 defer rs.closemu.RUnlock() 2492 if rs.closed { 2493 return nil, errors.New("sql: Rows are closed") 2494 } 2495 if rs.rowsi == nil { 2496 return nil, errors.New("sql: no Rows available") 2497 } 2498 return rs.rowsi.Columns(), nil 2499 } 2500 2501 // ColumnTypes returns column information such as column type, length, 2502 // and nullable. Some information may not be available from some drivers. 2503 func (rs *Rows) ColumnTypes() ([]*ColumnType, error) { 2504 rs.closemu.RLock() 2505 defer rs.closemu.RUnlock() 2506 if rs.closed { 2507 return nil, errors.New("sql: Rows are closed") 2508 } 2509 if rs.rowsi == nil { 2510 return nil, errors.New("sql: no Rows available") 2511 } 2512 return rowsColumnInfoSetup(rs.rowsi), nil 2513 } 2514 2515 // ColumnType contains the name and type of a column. 2516 type ColumnType struct { 2517 name string 2518 2519 hasNullable bool 2520 hasLength bool 2521 hasPrecisionScale bool 2522 2523 nullable bool 2524 length int64 2525 databaseType string 2526 precision int64 2527 scale int64 2528 scanType reflect.Type 2529 } 2530 2531 // Name returns the name or alias of the column. 2532 func (ci *ColumnType) Name() string { 2533 return ci.name 2534 } 2535 2536 // Length returns the column type length for variable length column types such 2537 // as text and binary field types. If the type length is unbounded the value will 2538 // be math.MaxInt64 (any database limits will still apply). 2539 // If the column type is not variable length, such as an int, or if not supported 2540 // by the driver ok is false. 2541 func (ci *ColumnType) Length() (length int64, ok bool) { 2542 return ci.length, ci.hasLength 2543 } 2544 2545 // DecimalSize returns the scale and precision of a decimal type. 2546 // If not applicable or if not supported ok is false. 2547 func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool) { 2548 return ci.precision, ci.scale, ci.hasPrecisionScale 2549 } 2550 2551 // ScanType returns a Go type suitable for scanning into using Rows.Scan. 2552 // If a driver does not support this property ScanType will return 2553 // the type of an empty interface. 2554 func (ci *ColumnType) ScanType() reflect.Type { 2555 return ci.scanType 2556 } 2557 2558 // Nullable returns whether the column may be null. 2559 // If a driver does not support this property ok will be false. 2560 func (ci *ColumnType) Nullable() (nullable, ok bool) { 2561 return ci.nullable, ci.hasNullable 2562 } 2563 2564 // DatabaseTypeName returns the database system name of the column type. If an empty 2565 // string is returned the driver type name is not supported. 2566 // Consult your driver documentation for a list of driver data types. Length specifiers 2567 // are not included. 2568 // Common type include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", "BIGINT". 2569 func (ci *ColumnType) DatabaseTypeName() string { 2570 return ci.databaseType 2571 } 2572 2573 func rowsColumnInfoSetup(rowsi driver.Rows) []*ColumnType { 2574 names := rowsi.Columns() 2575 2576 list := make([]*ColumnType, len(names)) 2577 for i := range list { 2578 ci := &ColumnType{ 2579 name: names[i], 2580 } 2581 list[i] = ci 2582 2583 if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok { 2584 ci.scanType = prop.ColumnTypeScanType(i) 2585 } else { 2586 ci.scanType = reflect.TypeOf(new(interface{})).Elem() 2587 } 2588 if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok { 2589 ci.databaseType = prop.ColumnTypeDatabaseTypeName(i) 2590 } 2591 if prop, ok := rowsi.(driver.RowsColumnTypeLength); ok { 2592 ci.length, ci.hasLength = prop.ColumnTypeLength(i) 2593 } 2594 if prop, ok := rowsi.(driver.RowsColumnTypeNullable); ok { 2595 ci.nullable, ci.hasNullable = prop.ColumnTypeNullable(i) 2596 } 2597 if prop, ok := rowsi.(driver.RowsColumnTypePrecisionScale); ok { 2598 ci.precision, ci.scale, ci.hasPrecisionScale = prop.ColumnTypePrecisionScale(i) 2599 } 2600 } 2601 return list 2602 } 2603 2604 // Scan copies the columns in the current row into the values pointed 2605 // at by dest. The number of values in dest must be the same as the 2606 // number of columns in Rows. 2607 // 2608 // Scan converts columns read from the database into the following 2609 // common Go types and special types provided by the sql package: 2610 // 2611 // *string 2612 // *[]byte 2613 // *int, *int8, *int16, *int32, *int64 2614 // *uint, *uint8, *uint16, *uint32, *uint64 2615 // *bool 2616 // *float32, *float64 2617 // *interface{} 2618 // *RawBytes 2619 // any type implementing Scanner (see Scanner docs) 2620 // 2621 // In the most simple case, if the type of the value from the source 2622 // column is an integer, bool or string type T and dest is of type *T, 2623 // Scan simply assigns the value through the pointer. 2624 // 2625 // Scan also converts between string and numeric types, as long as no 2626 // information would be lost. While Scan stringifies all numbers 2627 // scanned from numeric database columns into *string, scans into 2628 // numeric types are checked for overflow. For example, a float64 with 2629 // value 300 or a string with value "300" can scan into a uint16, but 2630 // not into a uint8, though float64(255) or "255" can scan into a 2631 // uint8. One exception is that scans of some float64 numbers to 2632 // strings may lose information when stringifying. In general, scan 2633 // floating point columns into *float64. 2634 // 2635 // If a dest argument has type *[]byte, Scan saves in that argument a 2636 // copy of the corresponding data. The copy is owned by the caller and 2637 // can be modified and held indefinitely. The copy can be avoided by 2638 // using an argument of type *RawBytes instead; see the documentation 2639 // for RawBytes for restrictions on its use. 2640 // 2641 // If an argument has type *interface{}, Scan copies the value 2642 // provided by the underlying driver without conversion. When scanning 2643 // from a source value of type []byte to *interface{}, a copy of the 2644 // slice is made and the caller owns the result. 2645 // 2646 // Source values of type time.Time may be scanned into values of type 2647 // *time.Time, *interface{}, *string, or *[]byte. When converting to 2648 // the latter two, time.Format3339Nano is used. 2649 // 2650 // Source values of type bool may be scanned into types *bool, 2651 // *interface{}, *string, *[]byte, or *RawBytes. 2652 // 2653 // For scanning into *bool, the source may be true, false, 1, 0, or 2654 // string inputs parseable by strconv.ParseBool. 2655 func (rs *Rows) Scan(dest ...interface{}) error { 2656 rs.closemu.RLock() 2657 if rs.closed { 2658 rs.closemu.RUnlock() 2659 return errors.New("sql: Rows are closed") 2660 } 2661 rs.closemu.RUnlock() 2662 2663 if rs.lastcols == nil { 2664 return errors.New("sql: Scan called without calling Next") 2665 } 2666 if len(dest) != len(rs.lastcols) { 2667 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest)) 2668 } 2669 for i, sv := range rs.lastcols { 2670 err := convertAssign(dest[i], sv) 2671 if err != nil { 2672 return fmt.Errorf("sql: Scan error on column index %d: %v", i, err) 2673 } 2674 } 2675 return nil 2676 } 2677 2678 // rowsCloseHook returns a function so tests may install the 2679 // hook through a test only mutex. 2680 var rowsCloseHook = func() func(*Rows, *error) { return nil } 2681 2682 // Close closes the Rows, preventing further enumeration. If Next is called 2683 // and returns false and there are no further result sets, 2684 // the Rows are closed automatically and it will suffice to check the 2685 // result of Err. Close is idempotent and does not affect the result of Err. 2686 func (rs *Rows) Close() error { 2687 return rs.close(nil) 2688 } 2689 2690 func (rs *Rows) close(err error) error { 2691 rs.closemu.Lock() 2692 defer rs.closemu.Unlock() 2693 2694 if rs.closed { 2695 return nil 2696 } 2697 rs.closed = true 2698 2699 if rs.lasterr == nil { 2700 rs.lasterr = err 2701 } 2702 2703 err = rs.rowsi.Close() 2704 if fn := rowsCloseHook(); fn != nil { 2705 fn(rs, &err) 2706 } 2707 if rs.cancel != nil { 2708 rs.cancel() 2709 } 2710 2711 if rs.closeStmt != nil { 2712 rs.closeStmt.Close() 2713 } 2714 rs.releaseConn(err) 2715 return err 2716 } 2717 2718 // Row is the result of calling QueryRow to select a single row. 2719 type Row struct { 2720 // One of these two will be non-nil: 2721 err error // deferred error for easy chaining 2722 rows *Rows 2723 } 2724 2725 // Scan copies the columns from the matched row into the values 2726 // pointed at by dest. See the documentation on Rows.Scan for details. 2727 // If more than one row matches the query, 2728 // Scan uses the first row and discards the rest. If no row matches 2729 // the query, Scan returns ErrNoRows. 2730 func (r *Row) Scan(dest ...interface{}) error { 2731 if r.err != nil { 2732 return r.err 2733 } 2734 2735 // TODO(bradfitz): for now we need to defensively clone all 2736 // []byte that the driver returned (not permitting 2737 // *RawBytes in Rows.Scan), since we're about to close 2738 // the Rows in our defer, when we return from this function. 2739 // the contract with the driver.Next(...) interface is that it 2740 // can return slices into read-only temporary memory that's 2741 // only valid until the next Scan/Close. But the TODO is that 2742 // for a lot of drivers, this copy will be unnecessary. We 2743 // should provide an optional interface for drivers to 2744 // implement to say, "don't worry, the []bytes that I return 2745 // from Next will not be modified again." (for instance, if 2746 // they were obtained from the network anyway) But for now we 2747 // don't care. 2748 defer r.rows.Close() 2749 for _, dp := range dest { 2750 if _, ok := dp.(*RawBytes); ok { 2751 return errors.New("sql: RawBytes isn't allowed on Row.Scan") 2752 } 2753 } 2754 2755 if !r.rows.Next() { 2756 if err := r.rows.Err(); err != nil { 2757 return err 2758 } 2759 return ErrNoRows 2760 } 2761 err := r.rows.Scan(dest...) 2762 if err != nil { 2763 return err 2764 } 2765 // Make sure the query can be processed to completion with no errors. 2766 if err := r.rows.Close(); err != nil { 2767 return err 2768 } 2769 2770 return nil 2771 } 2772 2773 // A Result summarizes an executed SQL command. 2774 type Result interface { 2775 // LastInsertId returns the integer generated by the database 2776 // in response to a command. Typically this will be from an 2777 // "auto increment" column when inserting a new row. Not all 2778 // databases support this feature, and the syntax of such 2779 // statements varies. 2780 LastInsertId() (int64, error) 2781 2782 // RowsAffected returns the number of rows affected by an 2783 // update, insert, or delete. Not every database or database 2784 // driver may support this. 2785 RowsAffected() (int64, error) 2786 } 2787 2788 type driverResult struct { 2789 sync.Locker // the *driverConn 2790 resi driver.Result 2791 } 2792 2793 func (dr driverResult) LastInsertId() (int64, error) { 2794 dr.Lock() 2795 defer dr.Unlock() 2796 return dr.resi.LastInsertId() 2797 } 2798 2799 func (dr driverResult) RowsAffected() (int64, error) { 2800 dr.Lock() 2801 defer dr.Unlock() 2802 return dr.resi.RowsAffected() 2803 } 2804 2805 func stack() string { 2806 var buf [2 << 10]byte 2807 return string(buf[:runtime.Stack(buf[:], false)]) 2808 } 2809 2810 // withLock runs while holding lk. 2811 func withLock(lk sync.Locker, fn func()) { 2812 lk.Lock() 2813 defer lk.Unlock() // in case fn panics 2814 fn() 2815 }