1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
39 var nowFunc = time.Now
40
41
42
43
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
60 drivers = make(map[string]driver.Driver)
61 }
62
63
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
76
77
78
79
80
81 type NamedArg struct {
82 _NamedFieldsRequired struct{}
83
84
85
86
87
88
89
90 Name string
91
92
93
94
95 Value any
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110 func Named(name string, value any) NamedArg {
111
112
113
114
115 return NamedArg{Name: name, Value: value}
116 }
117
118
119 type IsolationLevel int
120
121
122
123
124
125 const (
126 LevelDefault IsolationLevel = iota
127 LevelReadUncommitted
128 LevelReadCommitted
129 LevelWriteCommitted
130 LevelRepeatableRead
131 LevelSnapshot
132 LevelSerializable
133 LevelLinearizable
134 )
135
136
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
163 type TxOptions struct {
164
165
166 Isolation IsolationLevel
167 ReadOnly bool
168 }
169
170
171
172
173 type RawBytes []byte
174
175
176
177
178
179
180
181
182
183
184
185
186
187 type NullString struct {
188 String string
189 Valid bool
190 }
191
192
193 func (ns *NullString) Scan(value any) error {
194 if value == nil {
195 ns.String, ns.Valid = "", false
196 return nil
197 }
198 ns.Valid = true
199 return convertAssign(&ns.String, value)
200 }
201
202
203 func (ns NullString) Value() (driver.Value, error) {
204 if !ns.Valid {
205 return nil, nil
206 }
207 return ns.String, nil
208 }
209
210
211
212
213 type NullInt64 struct {
214 Int64 int64
215 Valid bool
216 }
217
218
219 func (n *NullInt64) Scan(value any) error {
220 if value == nil {
221 n.Int64, n.Valid = 0, false
222 return nil
223 }
224 n.Valid = true
225 return convertAssign(&n.Int64, value)
226 }
227
228
229 func (n NullInt64) Value() (driver.Value, error) {
230 if !n.Valid {
231 return nil, nil
232 }
233 return n.Int64, nil
234 }
235
236
237
238
239 type NullInt32 struct {
240 Int32 int32
241 Valid bool
242 }
243
244
245 func (n *NullInt32) Scan(value any) error {
246 if value == nil {
247 n.Int32, n.Valid = 0, false
248 return nil
249 }
250 n.Valid = true
251 return convertAssign(&n.Int32, value)
252 }
253
254
255 func (n NullInt32) Value() (driver.Value, error) {
256 if !n.Valid {
257 return nil, nil
258 }
259 return int64(n.Int32), nil
260 }
261
262
263
264
265 type NullInt16 struct {
266 Int16 int16
267 Valid bool
268 }
269
270
271 func (n *NullInt16) Scan(value any) error {
272 if value == nil {
273 n.Int16, n.Valid = 0, false
274 return nil
275 }
276 err := convertAssign(&n.Int16, value)
277 n.Valid = err == nil
278 return err
279 }
280
281
282 func (n NullInt16) Value() (driver.Value, error) {
283 if !n.Valid {
284 return nil, nil
285 }
286 return int64(n.Int16), nil
287 }
288
289
290
291
292 type NullByte struct {
293 Byte byte
294 Valid bool
295 }
296
297
298 func (n *NullByte) Scan(value any) error {
299 if value == nil {
300 n.Byte, n.Valid = 0, false
301 return nil
302 }
303 err := convertAssign(&n.Byte, value)
304 n.Valid = err == nil
305 return err
306 }
307
308
309 func (n NullByte) Value() (driver.Value, error) {
310 if !n.Valid {
311 return nil, nil
312 }
313 return int64(n.Byte), nil
314 }
315
316
317
318
319 type NullFloat64 struct {
320 Float64 float64
321 Valid bool
322 }
323
324
325 func (n *NullFloat64) Scan(value any) error {
326 if value == nil {
327 n.Float64, n.Valid = 0, false
328 return nil
329 }
330 n.Valid = true
331 return convertAssign(&n.Float64, value)
332 }
333
334
335 func (n NullFloat64) Value() (driver.Value, error) {
336 if !n.Valid {
337 return nil, nil
338 }
339 return n.Float64, nil
340 }
341
342
343
344
345 type NullBool struct {
346 Bool bool
347 Valid bool
348 }
349
350
351 func (n *NullBool) Scan(value any) error {
352 if value == nil {
353 n.Bool, n.Valid = false, false
354 return nil
355 }
356 n.Valid = true
357 return convertAssign(&n.Bool, value)
358 }
359
360
361 func (n NullBool) Value() (driver.Value, error) {
362 if !n.Valid {
363 return nil, nil
364 }
365 return n.Bool, nil
366 }
367
368
369
370
371 type NullTime struct {
372 Time time.Time
373 Valid bool
374 }
375
376
377 func (n *NullTime) Scan(value any) error {
378 if value == nil {
379 n.Time, n.Valid = time.Time{}, false
380 return nil
381 }
382 n.Valid = true
383 return convertAssign(&n.Time, value)
384 }
385
386
387 func (n NullTime) Value() (driver.Value, error) {
388 if !n.Valid {
389 return nil, nil
390 }
391 return n.Time, nil
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406 type Null[T any] struct {
407 V T
408 Valid bool
409 }
410
411 func (n *Null[T]) Scan(value any) error {
412 if value == nil {
413 n.V, n.Valid = *new(T), false
414 return nil
415 }
416 n.Valid = true
417 return convertAssign(&n.V, value)
418 }
419
420 func (n Null[T]) Value() (driver.Value, error) {
421 if !n.Valid {
422 return nil, nil
423 }
424 return n.V, nil
425 }
426
427
428 type Scanner interface {
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 Scan(src any) error
448 }
449
450
451
452
453
454
455
456
457
458 type Out struct {
459 _NamedFieldsRequired struct{}
460
461
462
463 Dest any
464
465
466
467
468 In bool
469 }
470
471
472
473
474 var ErrNoRows = errors.New("sql: no rows in result set")
475
476
477
478
479
480
481
482
483
484
485
486
487
488 type DB struct {
489
490 waitDuration atomic.Int64
491
492 connector driver.Connector
493
494
495
496 numClosed atomic.Uint64
497
498 mu sync.Mutex
499 freeConn []*driverConn
500 connRequests map[uint64]chan connRequest
501 nextRequest uint64
502 numOpen int
503
504
505
506
507
508 openerCh chan struct{}
509 closed bool
510 dep map[finalCloser]depSet
511 lastPut map[*driverConn]string
512 maxIdleCount int
513 maxOpen int
514 maxLifetime time.Duration
515 maxIdleTime time.Duration
516 cleanerCh chan struct{}
517 waitCount int64
518 maxIdleClosed int64
519 maxIdleTimeClosed int64
520 maxLifetimeClosed int64
521
522 stop func()
523 }
524
525
526 type connReuseStrategy uint8
527
528 const (
529
530 alwaysNewConn connReuseStrategy = iota
531
532
533
534 cachedOrNewConn
535 )
536
537
538
539
540
541 type driverConn struct {
542 db *DB
543 createdAt time.Time
544
545 sync.Mutex
546 ci driver.Conn
547 needReset bool
548 closed bool
549 finalClosed bool
550 openStmt map[*driverStmt]bool
551
552
553 inUse bool
554 returnedAt time.Time
555 onPut []func()
556 dbmuClosed bool
557 }
558
559 func (dc *driverConn) releaseConn(err error) {
560 dc.db.putConn(dc, err, true)
561 }
562
563 func (dc *driverConn) removeOpenStmt(ds *driverStmt) {
564 dc.Lock()
565 defer dc.Unlock()
566 delete(dc.openStmt, ds)
567 }
568
569 func (dc *driverConn) expired(timeout time.Duration) bool {
570 if timeout <= 0 {
571 return false
572 }
573 return dc.createdAt.Add(timeout).Before(nowFunc())
574 }
575
576
577
578 func (dc *driverConn) resetSession(ctx context.Context) error {
579 dc.Lock()
580 defer dc.Unlock()
581
582 if !dc.needReset {
583 return nil
584 }
585 if cr, ok := dc.ci.(driver.SessionResetter); ok {
586 return cr.ResetSession(ctx)
587 }
588 return nil
589 }
590
591
592
593 func (dc *driverConn) validateConnection(needsReset bool) bool {
594 dc.Lock()
595 defer dc.Unlock()
596
597 if needsReset {
598 dc.needReset = true
599 }
600 if cv, ok := dc.ci.(driver.Validator); ok {
601 return cv.IsValid()
602 }
603 return true
604 }
605
606
607
608 func (dc *driverConn) prepareLocked(ctx context.Context, cg stmtConnGrabber, query string) (*driverStmt, error) {
609 si, err := ctxDriverPrepare(ctx, dc.ci, query)
610 if err != nil {
611 return nil, err
612 }
613 ds := &driverStmt{Locker: dc, si: si}
614
615
616 if cg != nil {
617 return ds, nil
618 }
619
620
621
622
623
624 if dc.openStmt == nil {
625 dc.openStmt = make(map[*driverStmt]bool)
626 }
627 dc.openStmt[ds] = true
628 return ds, nil
629 }
630
631
632 func (dc *driverConn) closeDBLocked() func() error {
633 dc.Lock()
634 defer dc.Unlock()
635 if dc.closed {
636 return func() error { return errors.New("sql: duplicate driverConn close") }
637 }
638 dc.closed = true
639 return dc.db.removeDepLocked(dc, dc)
640 }
641
642 func (dc *driverConn) Close() error {
643 dc.Lock()
644 if dc.closed {
645 dc.Unlock()
646 return errors.New("sql: duplicate driverConn close")
647 }
648 dc.closed = true
649 dc.Unlock()
650
651
652 dc.db.mu.Lock()
653 dc.dbmuClosed = true
654 fn := dc.db.removeDepLocked(dc, dc)
655 dc.db.mu.Unlock()
656 return fn()
657 }
658
659 func (dc *driverConn) finalClose() error {
660 var err error
661
662
663
664 var openStmt []*driverStmt
665 withLock(dc, func() {
666 openStmt = make([]*driverStmt, 0, len(dc.openStmt))
667 for ds := range dc.openStmt {
668 openStmt = append(openStmt, ds)
669 }
670 dc.openStmt = nil
671 })
672 for _, ds := range openStmt {
673 ds.Close()
674 }
675 withLock(dc, func() {
676 dc.finalClosed = true
677 err = dc.ci.Close()
678 dc.ci = nil
679 })
680
681 dc.db.mu.Lock()
682 dc.db.numOpen--
683 dc.db.maybeOpenNewConnections()
684 dc.db.mu.Unlock()
685
686 dc.db.numClosed.Add(1)
687 return err
688 }
689
690
691
692
693 type driverStmt struct {
694 sync.Locker
695 si driver.Stmt
696 closed bool
697 closeErr error
698 }
699
700
701
702 func (ds *driverStmt) Close() error {
703 ds.Lock()
704 defer ds.Unlock()
705 if ds.closed {
706 return ds.closeErr
707 }
708 ds.closed = true
709 ds.closeErr = ds.si.Close()
710 return ds.closeErr
711 }
712
713
714 type depSet map[any]bool
715
716
717
718 type finalCloser interface {
719
720
721 finalClose() error
722 }
723
724
725
726 func (db *DB) addDep(x finalCloser, dep any) {
727 db.mu.Lock()
728 defer db.mu.Unlock()
729 db.addDepLocked(x, dep)
730 }
731
732 func (db *DB) addDepLocked(x finalCloser, dep any) {
733 if db.dep == nil {
734 db.dep = make(map[finalCloser]depSet)
735 }
736 xdep := db.dep[x]
737 if xdep == nil {
738 xdep = make(depSet)
739 db.dep[x] = xdep
740 }
741 xdep[dep] = true
742 }
743
744
745
746
747
748 func (db *DB) removeDep(x finalCloser, dep any) error {
749 db.mu.Lock()
750 fn := db.removeDepLocked(x, dep)
751 db.mu.Unlock()
752 return fn()
753 }
754
755 func (db *DB) removeDepLocked(x finalCloser, dep any) func() error {
756 xdep, ok := db.dep[x]
757 if !ok {
758 panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
759 }
760
761 l0 := len(xdep)
762 delete(xdep, dep)
763
764 switch len(xdep) {
765 case l0:
766
767 panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
768 case 0:
769
770 delete(db.dep, x)
771 return x.finalClose
772 default:
773
774 return func() error { return nil }
775 }
776 }
777
778
779
780
781
782
783 var connectionRequestQueueSize = 1000000
784
785 type dsnConnector struct {
786 dsn string
787 driver driver.Driver
788 }
789
790 func (t dsnConnector) Connect(_ context.Context) (driver.Conn, error) {
791 return t.driver.Open(t.dsn)
792 }
793
794 func (t dsnConnector) Driver() driver.Driver {
795 return t.driver
796 }
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814 func OpenDB(c driver.Connector) *DB {
815 ctx, cancel := context.WithCancel(context.Background())
816 db := &DB{
817 connector: c,
818 openerCh: make(chan struct{}, connectionRequestQueueSize),
819 lastPut: make(map[*driverConn]string),
820 connRequests: make(map[uint64]chan connRequest),
821 stop: cancel,
822 }
823
824 go db.connectionOpener(ctx)
825
826 return db
827 }
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846 func Open(driverName, dataSourceName string) (*DB, error) {
847 driversMu.RLock()
848 driveri, ok := drivers[driverName]
849 driversMu.RUnlock()
850 if !ok {
851 return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
852 }
853
854 if driverCtx, ok := driveri.(driver.DriverContext); ok {
855 connector, err := driverCtx.OpenConnector(dataSourceName)
856 if err != nil {
857 return nil, err
858 }
859 return OpenDB(connector), nil
860 }
861
862 return OpenDB(dsnConnector{dsn: dataSourceName, driver: driveri}), nil
863 }
864
865 func (db *DB) pingDC(ctx context.Context, dc *driverConn, release func(error)) error {
866 var err error
867 if pinger, ok := dc.ci.(driver.Pinger); ok {
868 withLock(dc, func() {
869 err = pinger.Ping(ctx)
870 })
871 }
872 release(err)
873 return err
874 }
875
876
877
878 func (db *DB) PingContext(ctx context.Context) error {
879 var dc *driverConn
880 var err error
881
882 err = db.retry(func(strategy connReuseStrategy) error {
883 dc, err = db.conn(ctx, strategy)
884 return err
885 })
886
887 if err != nil {
888 return err
889 }
890
891 return db.pingDC(ctx, dc, dc.releaseConn)
892 }
893
894
895
896
897
898
899 func (db *DB) Ping() error {
900 return db.PingContext(context.Background())
901 }
902
903
904
905
906
907
908
909 func (db *DB) Close() error {
910 db.mu.Lock()
911 if db.closed {
912 db.mu.Unlock()
913 return nil
914 }
915 if db.cleanerCh != nil {
916 close(db.cleanerCh)
917 }
918 var err error
919 fns := make([]func() error, 0, len(db.freeConn))
920 for _, dc := range db.freeConn {
921 fns = append(fns, dc.closeDBLocked())
922 }
923 db.freeConn = nil
924 db.closed = true
925 for _, req := range db.connRequests {
926 close(req)
927 }
928 db.mu.Unlock()
929 for _, fn := range fns {
930 err1 := fn()
931 if err1 != nil {
932 err = err1
933 }
934 }
935 db.stop()
936 if c, ok := db.connector.(io.Closer); ok {
937 err1 := c.Close()
938 if err1 != nil {
939 err = err1
940 }
941 }
942 return err
943 }
944
945 const defaultMaxIdleConns = 2
946
947 func (db *DB) maxIdleConnsLocked() int {
948 n := db.maxIdleCount
949 switch {
950 case n == 0:
951
952 return defaultMaxIdleConns
953 case n < 0:
954 return 0
955 default:
956 return n
957 }
958 }
959
960 func (db *DB) shortestIdleTimeLocked() time.Duration {
961 if db.maxIdleTime <= 0 {
962 return db.maxLifetime
963 }
964 if db.maxLifetime <= 0 {
965 return db.maxIdleTime
966 }
967 return min(db.maxIdleTime, db.maxLifetime)
968 }
969
970
971
972
973
974
975
976
977
978
979
980 func (db *DB) SetMaxIdleConns(n int) {
981 db.mu.Lock()
982 if n > 0 {
983 db.maxIdleCount = n
984 } else {
985
986 db.maxIdleCount = -1
987 }
988
989 if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
990 db.maxIdleCount = db.maxOpen
991 }
992 var closing []*driverConn
993 idleCount := len(db.freeConn)
994 maxIdle := db.maxIdleConnsLocked()
995 if idleCount > maxIdle {
996 closing = db.freeConn[maxIdle:]
997 db.freeConn = db.freeConn[:maxIdle]
998 }
999 db.maxIdleClosed += int64(len(closing))
1000 db.mu.Unlock()
1001 for _, c := range closing {
1002 c.Close()
1003 }
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 func (db *DB) SetMaxOpenConns(n int) {
1015 db.mu.Lock()
1016 db.maxOpen = n
1017 if n < 0 {
1018 db.maxOpen = 0
1019 }
1020 syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
1021 db.mu.Unlock()
1022 if syncMaxIdle {
1023 db.SetMaxIdleConns(n)
1024 }
1025 }
1026
1027
1028
1029
1030
1031
1032 func (db *DB) SetConnMaxLifetime(d time.Duration) {
1033 if d < 0 {
1034 d = 0
1035 }
1036 db.mu.Lock()
1037
1038 if d > 0 && d < db.maxLifetime && db.cleanerCh != nil {
1039 select {
1040 case db.cleanerCh <- struct{}{}:
1041 default:
1042 }
1043 }
1044 db.maxLifetime = d
1045 db.startCleanerLocked()
1046 db.mu.Unlock()
1047 }
1048
1049
1050
1051
1052
1053
1054 func (db *DB) SetConnMaxIdleTime(d time.Duration) {
1055 if d < 0 {
1056 d = 0
1057 }
1058 db.mu.Lock()
1059 defer db.mu.Unlock()
1060
1061
1062 if d > 0 && d < db.maxIdleTime && db.cleanerCh != nil {
1063 select {
1064 case db.cleanerCh <- struct{}{}:
1065 default:
1066 }
1067 }
1068 db.maxIdleTime = d
1069 db.startCleanerLocked()
1070 }
1071
1072
1073 func (db *DB) startCleanerLocked() {
1074 if (db.maxLifetime > 0 || db.maxIdleTime > 0) && db.numOpen > 0 && db.cleanerCh == nil {
1075 db.cleanerCh = make(chan struct{}, 1)
1076 go db.connectionCleaner(db.shortestIdleTimeLocked())
1077 }
1078 }
1079
1080 func (db *DB) connectionCleaner(d time.Duration) {
1081 const minInterval = time.Second
1082
1083 if d < minInterval {
1084 d = minInterval
1085 }
1086 t := time.NewTimer(d)
1087
1088 for {
1089 select {
1090 case <-t.C:
1091 case <-db.cleanerCh:
1092 }
1093
1094 db.mu.Lock()
1095
1096 d = db.shortestIdleTimeLocked()
1097 if db.closed || db.numOpen == 0 || d <= 0 {
1098 db.cleanerCh = nil
1099 db.mu.Unlock()
1100 return
1101 }
1102
1103 d, closing := db.connectionCleanerRunLocked(d)
1104 db.mu.Unlock()
1105 for _, c := range closing {
1106 c.Close()
1107 }
1108
1109 if d < minInterval {
1110 d = minInterval
1111 }
1112
1113 if !t.Stop() {
1114 select {
1115 case <-t.C:
1116 default:
1117 }
1118 }
1119 t.Reset(d)
1120 }
1121 }
1122
1123
1124
1125
1126 func (db *DB) connectionCleanerRunLocked(d time.Duration) (time.Duration, []*driverConn) {
1127 var idleClosing int64
1128 var closing []*driverConn
1129 if db.maxIdleTime > 0 {
1130
1131
1132 idleSince := nowFunc().Add(-db.maxIdleTime)
1133 last := len(db.freeConn) - 1
1134 for i := last; i >= 0; i-- {
1135 c := db.freeConn[i]
1136 if c.returnedAt.Before(idleSince) {
1137 i++
1138 closing = db.freeConn[:i:i]
1139 db.freeConn = db.freeConn[i:]
1140 idleClosing = int64(len(closing))
1141 db.maxIdleTimeClosed += idleClosing
1142 break
1143 }
1144 }
1145
1146 if len(db.freeConn) > 0 {
1147 c := db.freeConn[0]
1148 if d2 := c.returnedAt.Sub(idleSince); d2 < d {
1149
1150
1151 d = d2
1152 }
1153 }
1154 }
1155
1156 if db.maxLifetime > 0 {
1157 expiredSince := nowFunc().Add(-db.maxLifetime)
1158 for i := 0; i < len(db.freeConn); i++ {
1159 c := db.freeConn[i]
1160 if c.createdAt.Before(expiredSince) {
1161 closing = append(closing, c)
1162
1163 last := len(db.freeConn) - 1
1164
1165
1166 copy(db.freeConn[i:], db.freeConn[i+1:])
1167 db.freeConn[last] = nil
1168 db.freeConn = db.freeConn[:last]
1169 i--
1170 } else if d2 := c.createdAt.Sub(expiredSince); d2 < d {
1171
1172
1173 d = d2
1174 }
1175 }
1176 db.maxLifetimeClosed += int64(len(closing)) - idleClosing
1177 }
1178
1179 return d, closing
1180 }
1181
1182
1183 type DBStats struct {
1184 MaxOpenConnections int
1185
1186
1187 OpenConnections int
1188 InUse int
1189 Idle int
1190
1191
1192 WaitCount int64
1193 WaitDuration time.Duration
1194 MaxIdleClosed int64
1195 MaxIdleTimeClosed int64
1196 MaxLifetimeClosed int64
1197 }
1198
1199
1200 func (db *DB) Stats() DBStats {
1201 wait := db.waitDuration.Load()
1202
1203 db.mu.Lock()
1204 defer db.mu.Unlock()
1205
1206 stats := DBStats{
1207 MaxOpenConnections: db.maxOpen,
1208
1209 Idle: len(db.freeConn),
1210 OpenConnections: db.numOpen,
1211 InUse: db.numOpen - len(db.freeConn),
1212
1213 WaitCount: db.waitCount,
1214 WaitDuration: time.Duration(wait),
1215 MaxIdleClosed: db.maxIdleClosed,
1216 MaxIdleTimeClosed: db.maxIdleTimeClosed,
1217 MaxLifetimeClosed: db.maxLifetimeClosed,
1218 }
1219 return stats
1220 }
1221
1222
1223
1224
1225 func (db *DB) maybeOpenNewConnections() {
1226 numRequests := len(db.connRequests)
1227 if db.maxOpen > 0 {
1228 numCanOpen := db.maxOpen - db.numOpen
1229 if numRequests > numCanOpen {
1230 numRequests = numCanOpen
1231 }
1232 }
1233 for numRequests > 0 {
1234 db.numOpen++
1235 numRequests--
1236 if db.closed {
1237 return
1238 }
1239 db.openerCh <- struct{}{}
1240 }
1241 }
1242
1243
1244 func (db *DB) connectionOpener(ctx context.Context) {
1245 for {
1246 select {
1247 case <-ctx.Done():
1248 return
1249 case <-db.openerCh:
1250 db.openNewConnection(ctx)
1251 }
1252 }
1253 }
1254
1255
1256 func (db *DB) openNewConnection(ctx context.Context) {
1257
1258
1259
1260 ci, err := db.connector.Connect(ctx)
1261 db.mu.Lock()
1262 defer db.mu.Unlock()
1263 if db.closed {
1264 if err == nil {
1265 ci.Close()
1266 }
1267 db.numOpen--
1268 return
1269 }
1270 if err != nil {
1271 db.numOpen--
1272 db.putConnDBLocked(nil, err)
1273 db.maybeOpenNewConnections()
1274 return
1275 }
1276 dc := &driverConn{
1277 db: db,
1278 createdAt: nowFunc(),
1279 returnedAt: nowFunc(),
1280 ci: ci,
1281 }
1282 if db.putConnDBLocked(dc, err) {
1283 db.addDepLocked(dc, dc)
1284 } else {
1285 db.numOpen--
1286 ci.Close()
1287 }
1288 }
1289
1290
1291
1292
1293 type connRequest struct {
1294 conn *driverConn
1295 err error
1296 }
1297
1298 var errDBClosed = errors.New("sql: database is closed")
1299
1300
1301
1302 func (db *DB) nextRequestKeyLocked() uint64 {
1303 next := db.nextRequest
1304 db.nextRequest++
1305 return next
1306 }
1307
1308
1309 func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) {
1310 db.mu.Lock()
1311 if db.closed {
1312 db.mu.Unlock()
1313 return nil, errDBClosed
1314 }
1315
1316 select {
1317 default:
1318 case <-ctx.Done():
1319 db.mu.Unlock()
1320 return nil, ctx.Err()
1321 }
1322 lifetime := db.maxLifetime
1323
1324
1325 last := len(db.freeConn) - 1
1326 if strategy == cachedOrNewConn && last >= 0 {
1327
1328
1329 conn := db.freeConn[last]
1330 db.freeConn = db.freeConn[:last]
1331 conn.inUse = true
1332 if conn.expired(lifetime) {
1333 db.maxLifetimeClosed++
1334 db.mu.Unlock()
1335 conn.Close()
1336 return nil, driver.ErrBadConn
1337 }
1338 db.mu.Unlock()
1339
1340
1341 if err := conn.resetSession(ctx); errors.Is(err, driver.ErrBadConn) {
1342 conn.Close()
1343 return nil, err
1344 }
1345
1346 return conn, nil
1347 }
1348
1349
1350
1351 if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
1352
1353
1354 req := make(chan connRequest, 1)
1355 reqKey := db.nextRequestKeyLocked()
1356 db.connRequests[reqKey] = req
1357 db.waitCount++
1358 db.mu.Unlock()
1359
1360 waitStart := nowFunc()
1361
1362
1363 select {
1364 case <-ctx.Done():
1365
1366
1367 db.mu.Lock()
1368 delete(db.connRequests, reqKey)
1369 db.mu.Unlock()
1370
1371 db.waitDuration.Add(int64(time.Since(waitStart)))
1372
1373 select {
1374 default:
1375 case ret, ok := <-req:
1376 if ok && ret.conn != nil {
1377 db.putConn(ret.conn, ret.err, false)
1378 }
1379 }
1380 return nil, ctx.Err()
1381 case ret, ok := <-req:
1382 db.waitDuration.Add(int64(time.Since(waitStart)))
1383
1384 if !ok {
1385 return nil, errDBClosed
1386 }
1387
1388
1389
1390
1391
1392
1393 if strategy == cachedOrNewConn && ret.err == nil && ret.conn.expired(lifetime) {
1394 db.mu.Lock()
1395 db.maxLifetimeClosed++
1396 db.mu.Unlock()
1397 ret.conn.Close()
1398 return nil, driver.ErrBadConn
1399 }
1400 if ret.conn == nil {
1401 return nil, ret.err
1402 }
1403
1404
1405 if err := ret.conn.resetSession(ctx); errors.Is(err, driver.ErrBadConn) {
1406 ret.conn.Close()
1407 return nil, err
1408 }
1409 return ret.conn, ret.err
1410 }
1411 }
1412
1413 db.numOpen++
1414 db.mu.Unlock()
1415 ci, err := db.connector.Connect(ctx)
1416 if err != nil {
1417 db.mu.Lock()
1418 db.numOpen--
1419 db.maybeOpenNewConnections()
1420 db.mu.Unlock()
1421 return nil, err
1422 }
1423 db.mu.Lock()
1424 dc := &driverConn{
1425 db: db,
1426 createdAt: nowFunc(),
1427 returnedAt: nowFunc(),
1428 ci: ci,
1429 inUse: true,
1430 }
1431 db.addDepLocked(dc, dc)
1432 db.mu.Unlock()
1433 return dc, nil
1434 }
1435
1436
1437 var putConnHook func(*DB, *driverConn)
1438
1439
1440
1441
1442 func (db *DB) noteUnusedDriverStatement(c *driverConn, ds *driverStmt) {
1443 db.mu.Lock()
1444 defer db.mu.Unlock()
1445 if c.inUse {
1446 c.onPut = append(c.onPut, func() {
1447 ds.Close()
1448 })
1449 } else {
1450 c.Lock()
1451 fc := c.finalClosed
1452 c.Unlock()
1453 if !fc {
1454 ds.Close()
1455 }
1456 }
1457 }
1458
1459
1460
1461 const debugGetPut = false
1462
1463
1464
1465 func (db *DB) putConn(dc *driverConn, err error, resetSession bool) {
1466 if !errors.Is(err, driver.ErrBadConn) {
1467 if !dc.validateConnection(resetSession) {
1468 err = driver.ErrBadConn
1469 }
1470 }
1471 db.mu.Lock()
1472 if !dc.inUse {
1473 db.mu.Unlock()
1474 if debugGetPut {
1475 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
1476 }
1477 panic("sql: connection returned that was never out")
1478 }
1479
1480 if !errors.Is(err, driver.ErrBadConn) && dc.expired(db.maxLifetime) {
1481 db.maxLifetimeClosed++
1482 err = driver.ErrBadConn
1483 }
1484 if debugGetPut {
1485 db.lastPut[dc] = stack()
1486 }
1487 dc.inUse = false
1488 dc.returnedAt = nowFunc()
1489
1490 for _, fn := range dc.onPut {
1491 fn()
1492 }
1493 dc.onPut = nil
1494
1495 if errors.Is(err, driver.ErrBadConn) {
1496
1497
1498
1499
1500 db.maybeOpenNewConnections()
1501 db.mu.Unlock()
1502 dc.Close()
1503 return
1504 }
1505 if putConnHook != nil {
1506 putConnHook(db, dc)
1507 }
1508 added := db.putConnDBLocked(dc, nil)
1509 db.mu.Unlock()
1510
1511 if !added {
1512 dc.Close()
1513 return
1514 }
1515 }
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526 func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
1527 if db.closed {
1528 return false
1529 }
1530 if db.maxOpen > 0 && db.numOpen > db.maxOpen {
1531 return false
1532 }
1533 if c := len(db.connRequests); c > 0 {
1534 var req chan connRequest
1535 var reqKey uint64
1536 for reqKey, req = range db.connRequests {
1537 break
1538 }
1539 delete(db.connRequests, reqKey)
1540 if err == nil {
1541 dc.inUse = true
1542 }
1543 req <- connRequest{
1544 conn: dc,
1545 err: err,
1546 }
1547 return true
1548 } else if err == nil && !db.closed {
1549 if db.maxIdleConnsLocked() > len(db.freeConn) {
1550 db.freeConn = append(db.freeConn, dc)
1551 db.startCleanerLocked()
1552 return true
1553 }
1554 db.maxIdleClosed++
1555 }
1556 return false
1557 }
1558
1559
1560
1561
1562 const maxBadConnRetries = 2
1563
1564 func (db *DB) retry(fn func(strategy connReuseStrategy) error) error {
1565 for i := int64(0); i < maxBadConnRetries; i++ {
1566 err := fn(cachedOrNewConn)
1567
1568 if err == nil || !errors.Is(err, driver.ErrBadConn) {
1569 return err
1570 }
1571 }
1572
1573 return fn(alwaysNewConn)
1574 }
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584 func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
1585 var stmt *Stmt
1586 var err error
1587
1588 err = db.retry(func(strategy connReuseStrategy) error {
1589 stmt, err = db.prepare(ctx, query, strategy)
1590 return err
1591 })
1592
1593 return stmt, err
1594 }
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604 func (db *DB) Prepare(query string) (*Stmt, error) {
1605 return db.PrepareContext(context.Background(), query)
1606 }
1607
1608 func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*Stmt, error) {
1609
1610
1611
1612
1613
1614
1615 dc, err := db.conn(ctx, strategy)
1616 if err != nil {
1617 return nil, err
1618 }
1619 return db.prepareDC(ctx, dc, dc.releaseConn, nil, query)
1620 }
1621
1622
1623
1624
1625 func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error), cg stmtConnGrabber, query string) (*Stmt, error) {
1626 var ds *driverStmt
1627 var err error
1628 defer func() {
1629 release(err)
1630 }()
1631 withLock(dc, func() {
1632 ds, err = dc.prepareLocked(ctx, cg, query)
1633 })
1634 if err != nil {
1635 return nil, err
1636 }
1637 stmt := &Stmt{
1638 db: db,
1639 query: query,
1640 cg: cg,
1641 cgds: ds,
1642 }
1643
1644
1645
1646
1647 if cg == nil {
1648 stmt.css = []connStmt{{dc, ds}}
1649 stmt.lastNumClosed = db.numClosed.Load()
1650 db.addDep(stmt, stmt)
1651 }
1652 return stmt, nil
1653 }
1654
1655
1656
1657 func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
1658 var res Result
1659 var err error
1660
1661 err = db.retry(func(strategy connReuseStrategy) error {
1662 res, err = db.exec(ctx, query, args, strategy)
1663 return err
1664 })
1665
1666 return res, err
1667 }
1668
1669
1670
1671
1672
1673
1674 func (db *DB) Exec(query string, args ...any) (Result, error) {
1675 return db.ExecContext(context.Background(), query, args...)
1676 }
1677
1678 func (db *DB) exec(ctx context.Context, query string, args []any, strategy connReuseStrategy) (Result, error) {
1679 dc, err := db.conn(ctx, strategy)
1680 if err != nil {
1681 return nil, err
1682 }
1683 return db.execDC(ctx, dc, dc.releaseConn, query, args)
1684 }
1685
1686 func (db *DB) execDC(ctx context.Context, dc *driverConn, release func(error), query string, args []any) (res Result, err error) {
1687 defer func() {
1688 release(err)
1689 }()
1690 execerCtx, ok := dc.ci.(driver.ExecerContext)
1691 var execer driver.Execer
1692 if !ok {
1693 execer, ok = dc.ci.(driver.Execer)
1694 }
1695 if ok {
1696 var nvdargs []driver.NamedValue
1697 var resi driver.Result
1698 withLock(dc, func() {
1699 nvdargs, err = driverArgsConnLocked(dc.ci, nil, args)
1700 if err != nil {
1701 return
1702 }
1703 resi, err = ctxDriverExec(ctx, execerCtx, execer, query, nvdargs)
1704 })
1705 if err != driver.ErrSkip {
1706 if err != nil {
1707 return nil, err
1708 }
1709 return driverResult{dc, resi}, nil
1710 }
1711 }
1712
1713 var si driver.Stmt
1714 withLock(dc, func() {
1715 si, err = ctxDriverPrepare(ctx, dc.ci, query)
1716 })
1717 if err != nil {
1718 return nil, err
1719 }
1720 ds := &driverStmt{Locker: dc, si: si}
1721 defer ds.Close()
1722 return resultFromStatement(ctx, dc.ci, ds, args...)
1723 }
1724
1725
1726
1727 func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
1728 var rows *Rows
1729 var err error
1730
1731 err = db.retry(func(strategy connReuseStrategy) error {
1732 rows, err = db.query(ctx, query, args, strategy)
1733 return err
1734 })
1735
1736 return rows, err
1737 }
1738
1739
1740
1741
1742
1743
1744 func (db *DB) Query(query string, args ...any) (*Rows, error) {
1745 return db.QueryContext(context.Background(), query, args...)
1746 }
1747
1748 func (db *DB) query(ctx context.Context, query string, args []any, strategy connReuseStrategy) (*Rows, error) {
1749 dc, err := db.conn(ctx, strategy)
1750 if err != nil {
1751 return nil, err
1752 }
1753
1754 return db.queryDC(ctx, nil, dc, dc.releaseConn, query, args)
1755 }
1756
1757
1758
1759
1760
1761 func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []any) (*Rows, error) {
1762 queryerCtx, ok := dc.ci.(driver.QueryerContext)
1763 var queryer driver.Queryer
1764 if !ok {
1765 queryer, ok = dc.ci.(driver.Queryer)
1766 }
1767 if ok {
1768 var nvdargs []driver.NamedValue
1769 var rowsi driver.Rows
1770 var err error
1771 withLock(dc, func() {
1772 nvdargs, err = driverArgsConnLocked(dc.ci, nil, args)
1773 if err != nil {
1774 return
1775 }
1776 rowsi, err = ctxDriverQuery(ctx, queryerCtx, queryer, query, nvdargs)
1777 })
1778 if err != driver.ErrSkip {
1779 if err != nil {
1780 releaseConn(err)
1781 return nil, err
1782 }
1783
1784
1785 rows := &Rows{
1786 dc: dc,
1787 releaseConn: releaseConn,
1788 rowsi: rowsi,
1789 }
1790 rows.initContextClose(ctx, txctx)
1791 return rows, nil
1792 }
1793 }
1794
1795 var si driver.Stmt
1796 var err error
1797 withLock(dc, func() {
1798 si, err = ctxDriverPrepare(ctx, dc.ci, query)
1799 })
1800 if err != nil {
1801 releaseConn(err)
1802 return nil, err
1803 }
1804
1805 ds := &driverStmt{Locker: dc, si: si}
1806 rowsi, err := rowsiFromStatement(ctx, dc.ci, ds, args...)
1807 if err != nil {
1808 ds.Close()
1809 releaseConn(err)
1810 return nil, err
1811 }
1812
1813
1814
1815 rows := &Rows{
1816 dc: dc,
1817 releaseConn: releaseConn,
1818 rowsi: rowsi,
1819 closeStmt: ds,
1820 }
1821 rows.initContextClose(ctx, txctx)
1822 return rows, nil
1823 }
1824
1825
1826
1827
1828
1829
1830
1831 func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
1832 rows, err := db.QueryContext(ctx, query, args...)
1833 return &Row{rows: rows, err: err}
1834 }
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845 func (db *DB) QueryRow(query string, args ...any) *Row {
1846 return db.QueryRowContext(context.Background(), query, args...)
1847 }
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859 func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
1860 var tx *Tx
1861 var err error
1862
1863 err = db.retry(func(strategy connReuseStrategy) error {
1864 tx, err = db.begin(ctx, opts, strategy)
1865 return err
1866 })
1867
1868 return tx, err
1869 }
1870
1871
1872
1873
1874
1875
1876 func (db *DB) Begin() (*Tx, error) {
1877 return db.BeginTx(context.Background(), nil)
1878 }
1879
1880 func (db *DB) begin(ctx context.Context, opts *TxOptions, strategy connReuseStrategy) (tx *Tx, err error) {
1881 dc, err := db.conn(ctx, strategy)
1882 if err != nil {
1883 return nil, err
1884 }
1885 return db.beginDC(ctx, dc, dc.releaseConn, opts)
1886 }
1887
1888
1889 func (db *DB) beginDC(ctx context.Context, dc *driverConn, release func(error), opts *TxOptions) (tx *Tx, err error) {
1890 var txi driver.Tx
1891 keepConnOnRollback := false
1892 withLock(dc, func() {
1893 _, hasSessionResetter := dc.ci.(driver.SessionResetter)
1894 _, hasConnectionValidator := dc.ci.(driver.Validator)
1895 keepConnOnRollback = hasSessionResetter && hasConnectionValidator
1896 txi, err = ctxDriverBegin(ctx, opts, dc.ci)
1897 })
1898 if err != nil {
1899 release(err)
1900 return nil, err
1901 }
1902
1903
1904
1905 ctx, cancel := context.WithCancel(ctx)
1906 tx = &Tx{
1907 db: db,
1908 dc: dc,
1909 releaseConn: release,
1910 txi: txi,
1911 cancel: cancel,
1912 keepConnOnRollback: keepConnOnRollback,
1913 ctx: ctx,
1914 }
1915 go tx.awaitDone()
1916 return tx, nil
1917 }
1918
1919
1920 func (db *DB) Driver() driver.Driver {
1921 return db.connector.Driver()
1922 }
1923
1924
1925
1926 var ErrConnDone = errors.New("sql: connection is already closed")
1927
1928
1929
1930
1931
1932
1933
1934
1935 func (db *DB) Conn(ctx context.Context) (*Conn, error) {
1936 var dc *driverConn
1937 var err error
1938
1939 err = db.retry(func(strategy connReuseStrategy) error {
1940 dc, err = db.conn(ctx, strategy)
1941 return err
1942 })
1943
1944 if err != nil {
1945 return nil, err
1946 }
1947
1948 conn := &Conn{
1949 db: db,
1950 dc: dc,
1951 }
1952 return conn, nil
1953 }
1954
1955 type releaseConn func(error)
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966 type Conn struct {
1967 db *DB
1968
1969
1970
1971
1972 closemu sync.RWMutex
1973
1974
1975
1976 dc *driverConn
1977
1978
1979
1980 done atomic.Bool
1981
1982 releaseConnOnce sync.Once
1983
1984
1985 releaseConnCache releaseConn
1986 }
1987
1988
1989
1990 func (c *Conn) grabConn(context.Context) (*driverConn, releaseConn, error) {
1991 if c.done.Load() {
1992 return nil, nil, ErrConnDone
1993 }
1994 c.releaseConnOnce.Do(func() {
1995 c.releaseConnCache = c.closemuRUnlockCondReleaseConn
1996 })
1997 c.closemu.RLock()
1998 return c.dc, c.releaseConnCache, nil
1999 }
2000
2001
2002 func (c *Conn) PingContext(ctx context.Context) error {
2003 dc, release, err := c.grabConn(ctx)
2004 if err != nil {
2005 return err
2006 }
2007 return c.db.pingDC(ctx, dc, release)
2008 }
2009
2010
2011
2012 func (c *Conn) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
2013 dc, release, err := c.grabConn(ctx)
2014 if err != nil {
2015 return nil, err
2016 }
2017 return c.db.execDC(ctx, dc, release, query, args)
2018 }
2019
2020
2021
2022 func (c *Conn) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
2023 dc, release, err := c.grabConn(ctx)
2024 if err != nil {
2025 return nil, err
2026 }
2027 return c.db.queryDC(ctx, nil, dc, release, query, args)
2028 }
2029
2030
2031
2032
2033
2034
2035
2036 func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
2037 rows, err := c.QueryContext(ctx, query, args...)
2038 return &Row{rows: rows, err: err}
2039 }
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049 func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
2050 dc, release, err := c.grabConn(ctx)
2051 if err != nil {
2052 return nil, err
2053 }
2054 return c.db.prepareDC(ctx, dc, release, c, query)
2055 }
2056
2057
2058
2059
2060
2061
2062 func (c *Conn) Raw(f func(driverConn any) error) (err error) {
2063 var dc *driverConn
2064 var release releaseConn
2065
2066
2067 dc, release, err = c.grabConn(nil)
2068 if err != nil {
2069 return
2070 }
2071 fPanic := true
2072 dc.Mutex.Lock()
2073 defer func() {
2074 dc.Mutex.Unlock()
2075
2076
2077
2078
2079 if fPanic {
2080 err = driver.ErrBadConn
2081 }
2082 release(err)
2083 }()
2084 err = f(dc.ci)
2085 fPanic = false
2086
2087 return
2088 }
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100 func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
2101 dc, release, err := c.grabConn(ctx)
2102 if err != nil {
2103 return nil, err
2104 }
2105 return c.db.beginDC(ctx, dc, release, opts)
2106 }
2107
2108
2109
2110 func (c *Conn) closemuRUnlockCondReleaseConn(err error) {
2111 c.closemu.RUnlock()
2112 if errors.Is(err, driver.ErrBadConn) {
2113 c.close(err)
2114 }
2115 }
2116
2117 func (c *Conn) txCtx() context.Context {
2118 return nil
2119 }
2120
2121 func (c *Conn) close(err error) error {
2122 if !c.done.CompareAndSwap(false, true) {
2123 return ErrConnDone
2124 }
2125
2126
2127
2128 c.closemu.Lock()
2129 defer c.closemu.Unlock()
2130
2131 c.dc.releaseConn(err)
2132 c.dc = nil
2133 c.db = nil
2134 return err
2135 }
2136
2137
2138
2139
2140
2141
2142 func (c *Conn) Close() error {
2143 return c.close(nil)
2144 }
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156 type Tx struct {
2157 db *DB
2158
2159
2160
2161
2162 closemu sync.RWMutex
2163
2164
2165
2166 dc *driverConn
2167 txi driver.Tx
2168
2169
2170
2171 releaseConn func(error)
2172
2173
2174
2175
2176 done atomic.Bool
2177
2178
2179
2180
2181 keepConnOnRollback bool
2182
2183
2184
2185 stmts struct {
2186 sync.Mutex
2187 v []*Stmt
2188 }
2189
2190
2191 cancel func()
2192
2193
2194 ctx context.Context
2195 }
2196
2197
2198
2199 func (tx *Tx) awaitDone() {
2200
2201
2202 <-tx.ctx.Done()
2203
2204
2205
2206
2207
2208
2209
2210 discardConnection := !tx.keepConnOnRollback
2211 tx.rollback(discardConnection)
2212 }
2213
2214 func (tx *Tx) isDone() bool {
2215 return tx.done.Load()
2216 }
2217
2218
2219
2220 var ErrTxDone = errors.New("sql: transaction has already been committed or rolled back")
2221
2222
2223
2224
2225 func (tx *Tx) close(err error) {
2226 tx.releaseConn(err)
2227 tx.dc = nil
2228 tx.txi = nil
2229 }
2230
2231
2232
2233 var hookTxGrabConn func()
2234
2235 func (tx *Tx) grabConn(ctx context.Context) (*driverConn, releaseConn, error) {
2236 select {
2237 default:
2238 case <-ctx.Done():
2239 return nil, nil, ctx.Err()
2240 }
2241
2242
2243
2244 tx.closemu.RLock()
2245 if tx.isDone() {
2246 tx.closemu.RUnlock()
2247 return nil, nil, ErrTxDone
2248 }
2249 if hookTxGrabConn != nil {
2250 hookTxGrabConn()
2251 }
2252 return tx.dc, tx.closemuRUnlockRelease, nil
2253 }
2254
2255 func (tx *Tx) txCtx() context.Context {
2256 return tx.ctx
2257 }
2258
2259
2260
2261
2262
2263 func (tx *Tx) closemuRUnlockRelease(error) {
2264 tx.closemu.RUnlock()
2265 }
2266
2267
2268 func (tx *Tx) closePrepared() {
2269 tx.stmts.Lock()
2270 defer tx.stmts.Unlock()
2271 for _, stmt := range tx.stmts.v {
2272 stmt.Close()
2273 }
2274 }
2275
2276
2277 func (tx *Tx) Commit() error {
2278
2279
2280
2281 select {
2282 default:
2283 case <-tx.ctx.Done():
2284 if tx.done.Load() {
2285 return ErrTxDone
2286 }
2287 return tx.ctx.Err()
2288 }
2289 if !tx.done.CompareAndSwap(false, true) {
2290 return ErrTxDone
2291 }
2292
2293
2294
2295
2296
2297 tx.cancel()
2298 tx.closemu.Lock()
2299 tx.closemu.Unlock()
2300
2301 var err error
2302 withLock(tx.dc, func() {
2303 err = tx.txi.Commit()
2304 })
2305 if !errors.Is(err, driver.ErrBadConn) {
2306 tx.closePrepared()
2307 }
2308 tx.close(err)
2309 return err
2310 }
2311
2312 var rollbackHook func()
2313
2314
2315
2316 func (tx *Tx) rollback(discardConn bool) error {
2317 if !tx.done.CompareAndSwap(false, true) {
2318 return ErrTxDone
2319 }
2320
2321 if rollbackHook != nil {
2322 rollbackHook()
2323 }
2324
2325
2326
2327
2328
2329 tx.cancel()
2330 tx.closemu.Lock()
2331 tx.closemu.Unlock()
2332
2333 var err error
2334 withLock(tx.dc, func() {
2335 err = tx.txi.Rollback()
2336 })
2337 if !errors.Is(err, driver.ErrBadConn) {
2338 tx.closePrepared()
2339 }
2340 if discardConn {
2341 err = driver.ErrBadConn
2342 }
2343 tx.close(err)
2344 return err
2345 }
2346
2347
2348 func (tx *Tx) Rollback() error {
2349 return tx.rollback(false)
2350 }
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362 func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
2363 dc, release, err := tx.grabConn(ctx)
2364 if err != nil {
2365 return nil, err
2366 }
2367
2368 stmt, err := tx.db.prepareDC(ctx, dc, release, tx, query)
2369 if err != nil {
2370 return nil, err
2371 }
2372 tx.stmts.Lock()
2373 tx.stmts.v = append(tx.stmts.v, stmt)
2374 tx.stmts.Unlock()
2375 return stmt, nil
2376 }
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387 func (tx *Tx) Prepare(query string) (*Stmt, error) {
2388 return tx.PrepareContext(context.Background(), query)
2389 }
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407 func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
2408 dc, release, err := tx.grabConn(ctx)
2409 if err != nil {
2410 return &Stmt{stickyErr: err}
2411 }
2412 defer release(nil)
2413
2414 if tx.db != stmt.db {
2415 return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
2416 }
2417 var si driver.Stmt
2418 var parentStmt *Stmt
2419 stmt.mu.Lock()
2420 if stmt.closed || stmt.cg != nil {
2421
2422
2423
2424
2425
2426
2427 stmt.mu.Unlock()
2428 withLock(dc, func() {
2429 si, err = ctxDriverPrepare(ctx, dc.ci, stmt.query)
2430 })
2431 if err != nil {
2432 return &Stmt{stickyErr: err}
2433 }
2434 } else {
2435 stmt.removeClosedStmtLocked()
2436
2437
2438 for _, v := range stmt.css {
2439 if v.dc == dc {
2440 si = v.ds.si
2441 break
2442 }
2443 }
2444
2445 stmt.mu.Unlock()
2446
2447 if si == nil {
2448 var ds *driverStmt
2449 withLock(dc, func() {
2450 ds, err = stmt.prepareOnConnLocked(ctx, dc)
2451 })
2452 if err != nil {
2453 return &Stmt{stickyErr: err}
2454 }
2455 si = ds.si
2456 }
2457 parentStmt = stmt
2458 }
2459
2460 txs := &Stmt{
2461 db: tx.db,
2462 cg: tx,
2463 cgds: &driverStmt{
2464 Locker: dc,
2465 si: si,
2466 },
2467 parentStmt: parentStmt,
2468 query: stmt.query,
2469 }
2470 if parentStmt != nil {
2471 tx.db.addDep(parentStmt, txs)
2472 }
2473 tx.stmts.Lock()
2474 tx.stmts.v = append(tx.stmts.v, txs)
2475 tx.stmts.Unlock()
2476 return txs
2477 }
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495 func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
2496 return tx.StmtContext(context.Background(), stmt)
2497 }
2498
2499
2500
2501 func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
2502 dc, release, err := tx.grabConn(ctx)
2503 if err != nil {
2504 return nil, err
2505 }
2506 return tx.db.execDC(ctx, dc, release, query, args)
2507 }
2508
2509
2510
2511
2512
2513
2514 func (tx *Tx) Exec(query string, args ...any) (Result, error) {
2515 return tx.ExecContext(context.Background(), query, args...)
2516 }
2517
2518
2519 func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
2520 dc, release, err := tx.grabConn(ctx)
2521 if err != nil {
2522 return nil, err
2523 }
2524
2525 return tx.db.queryDC(ctx, tx.ctx, dc, release, query, args)
2526 }
2527
2528
2529
2530
2531
2532 func (tx *Tx) Query(query string, args ...any) (*Rows, error) {
2533 return tx.QueryContext(context.Background(), query, args...)
2534 }
2535
2536
2537
2538
2539
2540
2541
2542 func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
2543 rows, err := tx.QueryContext(ctx, query, args...)
2544 return &Row{rows: rows, err: err}
2545 }
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556 func (tx *Tx) QueryRow(query string, args ...any) *Row {
2557 return tx.QueryRowContext(context.Background(), query, args...)
2558 }
2559
2560
2561 type connStmt struct {
2562 dc *driverConn
2563 ds *driverStmt
2564 }
2565
2566
2567
2568 type stmtConnGrabber interface {
2569
2570
2571 grabConn(context.Context) (*driverConn, releaseConn, error)
2572
2573
2574
2575
2576 txCtx() context.Context
2577 }
2578
2579 var (
2580 _ stmtConnGrabber = &Tx{}
2581 _ stmtConnGrabber = &Conn{}
2582 )
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593 type Stmt struct {
2594
2595 db *DB
2596 query string
2597 stickyErr error
2598
2599 closemu sync.RWMutex
2600
2601
2602
2603
2604
2605
2606 cg stmtConnGrabber
2607 cgds *driverStmt
2608
2609
2610
2611
2612
2613
2614
2615 parentStmt *Stmt
2616
2617 mu sync.Mutex
2618 closed bool
2619
2620
2621
2622
2623
2624 css []connStmt
2625
2626
2627
2628 lastNumClosed uint64
2629 }
2630
2631
2632
2633 func (s *Stmt) ExecContext(ctx context.Context, args ...any) (Result, error) {
2634 s.closemu.RLock()
2635 defer s.closemu.RUnlock()
2636
2637 var res Result
2638 err := s.db.retry(func(strategy connReuseStrategy) error {
2639 dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
2640 if err != nil {
2641 return err
2642 }
2643
2644 res, err = resultFromStatement(ctx, dc.ci, ds, args...)
2645 releaseConn(err)
2646 return err
2647 })
2648
2649 return res, err
2650 }
2651
2652
2653
2654
2655
2656
2657 func (s *Stmt) Exec(args ...any) (Result, error) {
2658 return s.ExecContext(context.Background(), args...)
2659 }
2660
2661 func resultFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...any) (Result, error) {
2662 ds.Lock()
2663 defer ds.Unlock()
2664
2665 dargs, err := driverArgsConnLocked(ci, ds, args)
2666 if err != nil {
2667 return nil, err
2668 }
2669
2670 resi, err := ctxDriverStmtExec(ctx, ds.si, dargs)
2671 if err != nil {
2672 return nil, err
2673 }
2674 return driverResult{ds.Locker, resi}, nil
2675 }
2676
2677
2678
2679
2680
2681 func (s *Stmt) removeClosedStmtLocked() {
2682 t := len(s.css)/2 + 1
2683 if t > 10 {
2684 t = 10
2685 }
2686 dbClosed := s.db.numClosed.Load()
2687 if dbClosed-s.lastNumClosed < uint64(t) {
2688 return
2689 }
2690
2691 s.db.mu.Lock()
2692 for i := 0; i < len(s.css); i++ {
2693 if s.css[i].dc.dbmuClosed {
2694 s.css[i] = s.css[len(s.css)-1]
2695 s.css = s.css[:len(s.css)-1]
2696 i--
2697 }
2698 }
2699 s.db.mu.Unlock()
2700 s.lastNumClosed = dbClosed
2701 }
2702
2703
2704
2705
2706 func (s *Stmt) connStmt(ctx context.Context, strategy connReuseStrategy) (dc *driverConn, releaseConn func(error), ds *driverStmt, err error) {
2707 if err = s.stickyErr; err != nil {
2708 return
2709 }
2710 s.mu.Lock()
2711 if s.closed {
2712 s.mu.Unlock()
2713 err = errors.New("sql: statement is closed")
2714 return
2715 }
2716
2717
2718
2719 if s.cg != nil {
2720 s.mu.Unlock()
2721 dc, releaseConn, err = s.cg.grabConn(ctx)
2722 if err != nil {
2723 return
2724 }
2725 return dc, releaseConn, s.cgds, nil
2726 }
2727
2728 s.removeClosedStmtLocked()
2729 s.mu.Unlock()
2730
2731 dc, err = s.db.conn(ctx, strategy)
2732 if err != nil {
2733 return nil, nil, nil, err
2734 }
2735
2736 s.mu.Lock()
2737 for _, v := range s.css {
2738 if v.dc == dc {
2739 s.mu.Unlock()
2740 return dc, dc.releaseConn, v.ds, nil
2741 }
2742 }
2743 s.mu.Unlock()
2744
2745
2746 withLock(dc, func() {
2747 ds, err = s.prepareOnConnLocked(ctx, dc)
2748 })
2749 if err != nil {
2750 dc.releaseConn(err)
2751 return nil, nil, nil, err
2752 }
2753
2754 return dc, dc.releaseConn, ds, nil
2755 }
2756
2757
2758
2759 func (s *Stmt) prepareOnConnLocked(ctx context.Context, dc *driverConn) (*driverStmt, error) {
2760 si, err := dc.prepareLocked(ctx, s.cg, s.query)
2761 if err != nil {
2762 return nil, err
2763 }
2764 cs := connStmt{dc, si}
2765 s.mu.Lock()
2766 s.css = append(s.css, cs)
2767 s.mu.Unlock()
2768 return cs.ds, nil
2769 }
2770
2771
2772
2773 func (s *Stmt) QueryContext(ctx context.Context, args ...any) (*Rows, error) {
2774 s.closemu.RLock()
2775 defer s.closemu.RUnlock()
2776
2777 var rowsi driver.Rows
2778 var rows *Rows
2779
2780 err := s.db.retry(func(strategy connReuseStrategy) error {
2781 dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
2782 if err != nil {
2783 return err
2784 }
2785
2786 rowsi, err = rowsiFromStatement(ctx, dc.ci, ds, args...)
2787 if err == nil {
2788
2789
2790 rows = &Rows{
2791 dc: dc,
2792 rowsi: rowsi,
2793
2794 }
2795
2796
2797 s.db.addDep(s, rows)
2798
2799
2800
2801 rows.releaseConn = func(err error) {
2802 releaseConn(err)
2803 s.db.removeDep(s, rows)
2804 }
2805 var txctx context.Context
2806 if s.cg != nil {
2807 txctx = s.cg.txCtx()
2808 }
2809 rows.initContextClose(ctx, txctx)
2810 return nil
2811 }
2812
2813 releaseConn(err)
2814 return err
2815 })
2816
2817 return rows, err
2818 }
2819
2820
2821
2822
2823
2824
2825 func (s *Stmt) Query(args ...any) (*Rows, error) {
2826 return s.QueryContext(context.Background(), args...)
2827 }
2828
2829 func rowsiFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...any) (driver.Rows, error) {
2830 ds.Lock()
2831 defer ds.Unlock()
2832 dargs, err := driverArgsConnLocked(ci, ds, args)
2833 if err != nil {
2834 return nil, err
2835 }
2836 return ctxDriverStmtQuery(ctx, ds.si, dargs)
2837 }
2838
2839
2840
2841
2842
2843
2844
2845 func (s *Stmt) QueryRowContext(ctx context.Context, args ...any) *Row {
2846 rows, err := s.QueryContext(ctx, args...)
2847 if err != nil {
2848 return &Row{err: err}
2849 }
2850 return &Row{rows: rows}
2851 }
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867 func (s *Stmt) QueryRow(args ...any) *Row {
2868 return s.QueryRowContext(context.Background(), args...)
2869 }
2870
2871
2872 func (s *Stmt) Close() error {
2873 s.closemu.Lock()
2874 defer s.closemu.Unlock()
2875
2876 if s.stickyErr != nil {
2877 return s.stickyErr
2878 }
2879 s.mu.Lock()
2880 if s.closed {
2881 s.mu.Unlock()
2882 return nil
2883 }
2884 s.closed = true
2885 txds := s.cgds
2886 s.cgds = nil
2887
2888 s.mu.Unlock()
2889
2890 if s.cg == nil {
2891 return s.db.removeDep(s, s)
2892 }
2893
2894 if s.parentStmt != nil {
2895
2896
2897 return s.db.removeDep(s.parentStmt, s)
2898 }
2899 return txds.Close()
2900 }
2901
2902 func (s *Stmt) finalClose() error {
2903 s.mu.Lock()
2904 defer s.mu.Unlock()
2905 if s.css != nil {
2906 for _, v := range s.css {
2907 s.db.noteUnusedDriverStatement(v.dc, v.ds)
2908 v.dc.removeOpenStmt(v.ds)
2909 }
2910 s.css = nil
2911 }
2912 return nil
2913 }
2914
2915
2916
2917 type Rows struct {
2918 dc *driverConn
2919 releaseConn func(error)
2920 rowsi driver.Rows
2921 cancel func()
2922 closeStmt *driverStmt
2923
2924 contextDone atomic.Pointer[error]
2925
2926
2927
2928
2929
2930
2931 closemu sync.RWMutex
2932 closed bool
2933 lasterr error
2934
2935
2936
2937 lastcols []driver.Value
2938
2939
2940
2941
2942
2943
2944
2945
2946 closemuScanHold bool
2947
2948
2949
2950
2951
2952 hitEOF bool
2953 }
2954
2955
2956
2957 func (rs *Rows) lasterrOrErrLocked(err error) error {
2958 if rs.lasterr != nil && rs.lasterr != io.EOF {
2959 return rs.lasterr
2960 }
2961 return err
2962 }
2963
2964
2965
2966 var bypassRowsAwaitDone = false
2967
2968 func (rs *Rows) initContextClose(ctx, txctx context.Context) {
2969 if ctx.Done() == nil && (txctx == nil || txctx.Done() == nil) {
2970 return
2971 }
2972 if bypassRowsAwaitDone {
2973 return
2974 }
2975 closectx, cancel := context.WithCancel(ctx)
2976 rs.cancel = cancel
2977 go rs.awaitDone(ctx, txctx, closectx)
2978 }
2979
2980
2981
2982
2983
2984
2985 func (rs *Rows) awaitDone(ctx, txctx, closectx context.Context) {
2986 var txctxDone <-chan struct{}
2987 if txctx != nil {
2988 txctxDone = txctx.Done()
2989 }
2990 select {
2991 case <-ctx.Done():
2992 err := ctx.Err()
2993 rs.contextDone.Store(&err)
2994 case <-txctxDone:
2995 err := txctx.Err()
2996 rs.contextDone.Store(&err)
2997 case <-closectx.Done():
2998
2999
3000 }
3001 rs.close(ctx.Err())
3002 }
3003
3004
3005
3006
3007
3008
3009
3010 func (rs *Rows) Next() bool {
3011
3012
3013
3014 rs.closemuRUnlockIfHeldByScan()
3015
3016 if rs.contextDone.Load() != nil {
3017 return false
3018 }
3019
3020 var doClose, ok bool
3021 withLock(rs.closemu.RLocker(), func() {
3022 doClose, ok = rs.nextLocked()
3023 })
3024 if doClose {
3025 rs.Close()
3026 }
3027 if doClose && !ok {
3028 rs.hitEOF = true
3029 }
3030 return ok
3031 }
3032
3033 func (rs *Rows) nextLocked() (doClose, ok bool) {
3034 if rs.closed {
3035 return false, false
3036 }
3037
3038
3039
3040 rs.dc.Lock()
3041 defer rs.dc.Unlock()
3042
3043 if rs.lastcols == nil {
3044 rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
3045 }
3046
3047 rs.lasterr = rs.rowsi.Next(rs.lastcols)
3048 if rs.lasterr != nil {
3049
3050 if rs.lasterr != io.EOF {
3051 return true, false
3052 }
3053 nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
3054 if !ok {
3055 return true, false
3056 }
3057
3058
3059
3060 if !nextResultSet.HasNextResultSet() {
3061 doClose = true
3062 }
3063 return doClose, false
3064 }
3065 return false, true
3066 }
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076 func (rs *Rows) NextResultSet() bool {
3077
3078
3079
3080 rs.closemuRUnlockIfHeldByScan()
3081
3082 var doClose bool
3083 defer func() {
3084 if doClose {
3085 rs.Close()
3086 }
3087 }()
3088 rs.closemu.RLock()
3089 defer rs.closemu.RUnlock()
3090
3091 if rs.closed {
3092 return false
3093 }
3094
3095 rs.lastcols = nil
3096 nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
3097 if !ok {
3098 doClose = true
3099 return false
3100 }
3101
3102
3103
3104 rs.dc.Lock()
3105 defer rs.dc.Unlock()
3106
3107 rs.lasterr = nextResultSet.NextResultSet()
3108 if rs.lasterr != nil {
3109 doClose = true
3110 return false
3111 }
3112 return true
3113 }
3114
3115
3116
3117 func (rs *Rows) Err() error {
3118
3119
3120
3121
3122 if !rs.hitEOF {
3123 if errp := rs.contextDone.Load(); errp != nil {
3124 return *errp
3125 }
3126 }
3127
3128 rs.closemu.RLock()
3129 defer rs.closemu.RUnlock()
3130 return rs.lasterrOrErrLocked(nil)
3131 }
3132
3133 var errRowsClosed = errors.New("sql: Rows are closed")
3134 var errNoRows = errors.New("sql: no Rows available")
3135
3136
3137
3138 func (rs *Rows) Columns() ([]string, error) {
3139 rs.closemu.RLock()
3140 defer rs.closemu.RUnlock()
3141 if rs.closed {
3142 return nil, rs.lasterrOrErrLocked(errRowsClosed)
3143 }
3144 if rs.rowsi == nil {
3145 return nil, rs.lasterrOrErrLocked(errNoRows)
3146 }
3147 rs.dc.Lock()
3148 defer rs.dc.Unlock()
3149
3150 return rs.rowsi.Columns(), nil
3151 }
3152
3153
3154
3155 func (rs *Rows) ColumnTypes() ([]*ColumnType, error) {
3156 rs.closemu.RLock()
3157 defer rs.closemu.RUnlock()
3158 if rs.closed {
3159 return nil, rs.lasterrOrErrLocked(errRowsClosed)
3160 }
3161 if rs.rowsi == nil {
3162 return nil, rs.lasterrOrErrLocked(errNoRows)
3163 }
3164 rs.dc.Lock()
3165 defer rs.dc.Unlock()
3166
3167 return rowsColumnInfoSetupConnLocked(rs.rowsi), nil
3168 }
3169
3170
3171 type ColumnType struct {
3172 name string
3173
3174 hasNullable bool
3175 hasLength bool
3176 hasPrecisionScale bool
3177
3178 nullable bool
3179 length int64
3180 databaseType string
3181 precision int64
3182 scale int64
3183 scanType reflect.Type
3184 }
3185
3186
3187 func (ci *ColumnType) Name() string {
3188 return ci.name
3189 }
3190
3191
3192
3193
3194
3195
3196 func (ci *ColumnType) Length() (length int64, ok bool) {
3197 return ci.length, ci.hasLength
3198 }
3199
3200
3201
3202 func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool) {
3203 return ci.precision, ci.scale, ci.hasPrecisionScale
3204 }
3205
3206
3207
3208
3209 func (ci *ColumnType) ScanType() reflect.Type {
3210 return ci.scanType
3211 }
3212
3213
3214
3215 func (ci *ColumnType) Nullable() (nullable, ok bool) {
3216 return ci.nullable, ci.hasNullable
3217 }
3218
3219
3220
3221
3222
3223
3224
3225 func (ci *ColumnType) DatabaseTypeName() string {
3226 return ci.databaseType
3227 }
3228
3229 func rowsColumnInfoSetupConnLocked(rowsi driver.Rows) []*ColumnType {
3230 names := rowsi.Columns()
3231
3232 list := make([]*ColumnType, len(names))
3233 for i := range list {
3234 ci := &ColumnType{
3235 name: names[i],
3236 }
3237 list[i] = ci
3238
3239 if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok {
3240 ci.scanType = prop.ColumnTypeScanType(i)
3241 } else {
3242 ci.scanType = reflect.TypeFor[any]()
3243 }
3244 if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok {
3245 ci.databaseType = prop.ColumnTypeDatabaseTypeName(i)
3246 }
3247 if prop, ok := rowsi.(driver.RowsColumnTypeLength); ok {
3248 ci.length, ci.hasLength = prop.ColumnTypeLength(i)
3249 }
3250 if prop, ok := rowsi.(driver.RowsColumnTypeNullable); ok {
3251 ci.nullable, ci.hasNullable = prop.ColumnTypeNullable(i)
3252 }
3253 if prop, ok := rowsi.(driver.RowsColumnTypePrecisionScale); ok {
3254 ci.precision, ci.scale, ci.hasPrecisionScale = prop.ColumnTypePrecisionScale(i)
3255 }
3256 }
3257 return list
3258 }
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320 func (rs *Rows) Scan(dest ...any) error {
3321 if rs.closemuScanHold {
3322
3323
3324 return fmt.Errorf("sql: Scan called without calling Next (closemuScanHold)")
3325 }
3326 rs.closemu.RLock()
3327
3328 if rs.lasterr != nil && rs.lasterr != io.EOF {
3329 rs.closemu.RUnlock()
3330 return rs.lasterr
3331 }
3332 if rs.closed {
3333 err := rs.lasterrOrErrLocked(errRowsClosed)
3334 rs.closemu.RUnlock()
3335 return err
3336 }
3337
3338 if scanArgsContainRawBytes(dest) {
3339 rs.closemuScanHold = true
3340 } else {
3341 rs.closemu.RUnlock()
3342 }
3343
3344 if rs.lastcols == nil {
3345 rs.closemuRUnlockIfHeldByScan()
3346 return errors.New("sql: Scan called without calling Next")
3347 }
3348 if len(dest) != len(rs.lastcols) {
3349 rs.closemuRUnlockIfHeldByScan()
3350 return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
3351 }
3352
3353 for i, sv := range rs.lastcols {
3354 err := convertAssignRows(dest[i], sv, rs)
3355 if err != nil {
3356 rs.closemuRUnlockIfHeldByScan()
3357 return fmt.Errorf(`sql: Scan error on column index %d, name %q: %w`, i, rs.rowsi.Columns()[i], err)
3358 }
3359 }
3360 return nil
3361 }
3362
3363
3364
3365 func (rs *Rows) closemuRUnlockIfHeldByScan() {
3366 if rs.closemuScanHold {
3367 rs.closemuScanHold = false
3368 rs.closemu.RUnlock()
3369 }
3370 }
3371
3372 func scanArgsContainRawBytes(args []any) bool {
3373 for _, a := range args {
3374 if _, ok := a.(*RawBytes); ok {
3375 return true
3376 }
3377 }
3378 return false
3379 }
3380
3381
3382
3383 var rowsCloseHook = func() func(*Rows, *error) { return nil }
3384
3385
3386
3387
3388
3389 func (rs *Rows) Close() error {
3390
3391
3392
3393 rs.closemuRUnlockIfHeldByScan()
3394
3395 return rs.close(nil)
3396 }
3397
3398 func (rs *Rows) close(err error) error {
3399 rs.closemu.Lock()
3400 defer rs.closemu.Unlock()
3401
3402 if rs.closed {
3403 return nil
3404 }
3405 rs.closed = true
3406
3407 if rs.lasterr == nil {
3408 rs.lasterr = err
3409 }
3410
3411 withLock(rs.dc, func() {
3412 err = rs.rowsi.Close()
3413 })
3414 if fn := rowsCloseHook(); fn != nil {
3415 fn(rs, &err)
3416 }
3417 if rs.cancel != nil {
3418 rs.cancel()
3419 }
3420
3421 if rs.closeStmt != nil {
3422 rs.closeStmt.Close()
3423 }
3424 rs.releaseConn(err)
3425
3426 rs.lasterr = rs.lasterrOrErrLocked(err)
3427 return err
3428 }
3429
3430
3431 type Row struct {
3432
3433 err error
3434 rows *Rows
3435 }
3436
3437
3438
3439
3440
3441
3442 func (r *Row) Scan(dest ...any) error {
3443 if r.err != nil {
3444 return r.err
3445 }
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460 defer r.rows.Close()
3461 for _, dp := range dest {
3462 if _, ok := dp.(*RawBytes); ok {
3463 return errors.New("sql: RawBytes isn't allowed on Row.Scan")
3464 }
3465 }
3466
3467 if !r.rows.Next() {
3468 if err := r.rows.Err(); err != nil {
3469 return err
3470 }
3471 return ErrNoRows
3472 }
3473 err := r.rows.Scan(dest...)
3474 if err != nil {
3475 return err
3476 }
3477
3478 return r.rows.Close()
3479 }
3480
3481
3482
3483
3484
3485 func (r *Row) Err() error {
3486 return r.err
3487 }
3488
3489
3490 type Result interface {
3491
3492
3493
3494
3495
3496 LastInsertId() (int64, error)
3497
3498
3499
3500
3501 RowsAffected() (int64, error)
3502 }
3503
3504 type driverResult struct {
3505 sync.Locker
3506 resi driver.Result
3507 }
3508
3509 func (dr driverResult) LastInsertId() (int64, error) {
3510 dr.Lock()
3511 defer dr.Unlock()
3512 return dr.resi.LastInsertId()
3513 }
3514
3515 func (dr driverResult) RowsAffected() (int64, error) {
3516 dr.Lock()
3517 defer dr.Unlock()
3518 return dr.resi.RowsAffected()
3519 }
3520
3521 func stack() string {
3522 var buf [2 << 10]byte
3523 return string(buf[:runtime.Stack(buf[:], false)])
3524 }
3525
3526
3527 func withLock(lk sync.Locker, fn func()) {
3528 lk.Lock()
3529 defer lk.Unlock()
3530 fn()
3531 }
3532
View as plain text