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