forked from labstack/echo
/
db.go
executable file
·1135 lines (968 loc) · 26.1 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Adverax. All Rights Reserved.
// This file is part of project
//
// http://github.com/adverax/echo
//
// Licensed under the MIT (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/adverax/echo/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/adverax/echo/data"
"github.com/adverax/echo/generic"
)
// Single dataSource node
type DSN struct {
Host string `json:"host"` // Host address
Port uint16 `json:"port"` // Host port
Database string `json:"database"` // Database name
Username string `json:"username"` // User name
Password string `json:"password"` // User password
Params map[string]string `json:"params"` // Other parameters
}
func (dsn *DSN) AddParam(key string, value string) {
if dsn.Params == nil {
dsn.Params = map[string]string{
key: value,
}
} else {
dsn.Params[key] = value
}
}
func (dsn *DSN) Open(
driver string,
activator Activator,
) (DB, error) {
return Open(
DSC{
Driver: driver,
DSN: []*DSN{dsn},
},
activator,
)
}
func (dsn *DSN) openSQL(driver string) (*sql.DB, Adapter, error) {
adapter, err := adapters.find(driver)
if err != nil {
return nil, nil, err
}
db, err := sql.Open(driver, adapter.MakeConnectionString(dsn))
if err != nil {
return nil, nil, err
}
return db, adapter, nil
}
// DataSource nodes cluster (first node is master)
type DSC struct {
Driver string `json:"Driver"`
DbId DbId `json:"-"`
DSN []*DSN `json:"dsn"`
}
func (dsc *DSC) Primary() DSN {
if len(dsc.DSN) == 0 {
return DSN{}
}
return *dsc.DSN[0]
}
func (dsc *DSC) String() (string, error) {
adapter, err := adapters.find(dsc.Driver)
if err != nil {
return "", err
}
var list = make([]string, len(dsc.DSN))
for i, dsn := range dsc.DSN {
list[i] = adapter.MakeConnectionString(dsn)
}
return strings.Join(list, ";"), nil
}
func (dsc *DSC) Open(
activator Activator,
) (DB, error) {
return Open(*dsc, activator)
}
type Adapter interface {
// Get driver name
Driver() string
//Get database name
DatabaseName(db DB) (name string, err error)
// Make connection string for open database
MakeConnectionString(dsn *DSN) string
// Check error for deadlock criteria
IsDeadlock(db DB, err error) bool
// Acquire local lock
LockLocal(ctx context.Context, tx Tx, latch string, timeout int) error
// Release local lock
UnlockLocal(ctx context.Context, tx Tx, latch string) error
// Acquire local lock
LockGlobal(ctx context.Context, tx Tx, latch string, timeout int) error
// Release local lock
UnlockGlobal(ctx context.Context, tx Tx, latch string) error
}
type Activator func(dsc DSC) (DB, error)
type adapterRegistry map[string]Adapter
func (registry adapterRegistry) find(driver string) (Adapter, error) {
if adapter, ok := registry[driver]; ok {
return adapter, nil
}
return nil, ErrUnknownDriver
}
var adapters = make(adapterRegistry, 8)
func Register(driver string, adapter Adapter) {
adapters[driver] = adapter
}
// IsolationLevel is the transaction isolation level used in TxOptions.
type IsolationLevel int
// Various isolation levels that drivers may support in BeginTx.
// If a driver does not support a given isolation level an error may be returned.
//
// See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
const (
LevelDefault IsolationLevel = iota
LevelReadUncommitted
LevelReadCommitted
LevelWriteCommitted
LevelRepeatableRead
LevelSnapshot
LevelSerializable
LevelLinearizable
)
type HalfMetrics struct {
Count int32 `json:"count"` // Count of executed queries
Time int64 `json:"time"` // Elapsed time (microseconds)
}
// Metrics of database
type Metrics struct {
Query HalfMetrics `json:"query"`
Exec HalfMetrics `json:"exec"`
Transact HalfMetrics `json:"transact"`
}
func (metrics *Metrics) beginQuery() int64 {
return time.Now().UnixNano()
}
func (metrics *Metrics) endQuery(started int64) {
atomic.AddInt32(&metrics.Query.Count, 1)
atomic.AddInt64(&metrics.Query.Time, time.Now().UnixNano()-started)
}
func (metrics *Metrics) beginExec() int64 {
return time.Now().UnixNano()
}
func (metrics *Metrics) endExec(started int64) {
atomic.AddInt32(&metrics.Exec.Count, 1)
atomic.AddInt64(&metrics.Exec.Time, time.Now().UnixNano()-started)
}
func (metrics *Metrics) beginTransact() int64 {
return time.Now().UnixNano()
}
func (metrics *Metrics) endTransact(started int64) {
atomic.AddInt32(&metrics.Transact.Count, 1)
atomic.AddInt64(&metrics.Transact.Time, time.Now().UnixNano()-started)
}
func (metrics *Metrics) GetMetrics() Metrics {
return Metrics{
Query: HalfMetrics{
Count: atomic.LoadInt32(&metrics.Query.Count),
Time: atomic.LoadInt64(&metrics.Query.Time),
},
Exec: HalfMetrics{
Count: atomic.LoadInt32(&metrics.Exec.Count),
Time: atomic.LoadInt64(&metrics.Exec.Time),
},
Transact: HalfMetrics{
Count: atomic.LoadInt32(&metrics.Transact.Count),
Time: atomic.LoadInt64(&metrics.Transact.Time),
},
}
}
func (metrics *Metrics) Audit(auditor interface{}) error {
if a, ok := auditor.(Auditor); ok {
return a.AuditDatabase(metrics.GetMetrics())
}
return nil
}
type Auditor interface {
AuditDatabase(Metrics Metrics) error
}
// TxOptions holds the transaction options to be used in DB.BeginTx.
type TxOptions = sql.TxOptions
// Scope is abstract processor
type Scope interface {
Begin() (Tx, error)
BeginTx(ctx context.Context, opts *TxOptions) (Tx, error)
Exec(query string, args ...interface{}) (Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
Query(query string, args ...interface{}) (Rows, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
QueryRow(query string, args ...interface{}) Row
QueryRowContext(ctx context.Context, query string, args ...interface{}) Row
DbId() DbId
Adapter() Adapter
}
// Composer is interface for coordinate threads
type Composer interface {
Add(delta int)
Done()
Abort() <-chan struct{}
}
type composer struct {
sync.WaitGroup
stop chan struct{}
}
// Stop and wait termination all processes
func (composer *composer) Close() {
close(composer.stop)
composer.WaitGroup.Wait()
}
// Get chan for stop handling
func (composer *composer) Abort() <-chan struct{} {
return composer.stop
}
// DB is a logical database with multiple underlying physical databases
// forming a single master multiple slaves topology.
// Reads and writes are automatically directed to the correct physical db.
type DB interface {
Scope
Composer
Close(ctx context.Context) error
Driver() driver.Driver
Ping() error
Slave() DB
Master() DB
Prepare(query string) (Stmt, error)
PrepareContext(ctx context.Context, query string) (Stmt, error)
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
SetConnMaxLifetime(d time.Duration)
IsCluster() bool
GetMetrics() Metrics
Audit(auditor interface{}) error
Interface(detective func(interface{}) interface{}) (interface{}, bool)
DSC() DSC
beginQuery() int64
endQuery(started int64)
beginExec() int64
endExec(started int64)
beginTransact() int64
endTransact(started int64)
}
// Scanner is an interface used by Scan.
type Scanner = sql.Scanner
// Transaction
type Tx interface {
Scope
Level() int16
Commit() error
Rollback() error
}
// Abstract row fetcher
type Fetcher interface {
Scan(dest ...interface{}) error
}
// Array of abstract data
type Array []interface{}
type Arrays []Array
func (a Array) Scan(dest ...interface{}) error {
for i, d := range dest {
err := generic.ConvertAssign(d, a[i])
if err != nil {
return err
}
}
return nil
}
// Executor result
type Result interface {
LastInsertId() (int64, error)
RowsAffected() (int64, error)
}
// Abstract row
type Row interface {
Scan(dest ...interface{}) error
}
// Record set
type Rows interface {
Err() error
Next() bool
Columns() ([]string, error)
Close() error
Scan(dest ...interface{}) error
}
// Named arguments
type Args map[string]interface{}
// Extract args to usage in Query/Exec
func (args *Args) Extract() []sql.NamedArg {
res := make([]sql.NamedArg, len(*args))
var i int
for key, val := range *args {
res[i] = sql.NamedArg{Name: key, Value: val}
i++
}
return res
}
type row struct {
db DB
r *sql.Row
}
func (row *row) Scan(dest ...interface{}) error {
err := row.r.Scan(dest...)
if err != nil {
return recode(err)
}
return nil
}
type rows struct {
db DB
rs *sql.Rows
started int64
}
func (rows *rows) Scan(dest ...interface{}) error {
err := rows.rs.Scan(dest...)
if err != nil {
return recode(err)
}
return nil
}
func (rows *rows) Close() error {
rows.db.endQuery(rows.started)
return recode(rows.rs.Close())
}
func (rows *rows) Err() error {
err := rows.rs.Err()
if err != nil {
return recode(err)
}
return nil
}
func (rows *rows) Next() bool {
return rows.rs.Next()
}
func (rows *rows) Columns() ([]string, error) {
res, err := rows.rs.Columns()
if err != nil {
return nil, err
}
return res, err
}
type result struct {
db DB
res sql.Result
}
func (res *result) LastInsertId() (int64, error) {
r, err := res.res.LastInsertId()
if err != nil {
return 0, err
}
return r, nil
}
func (res *result) RowsAffected() (int64, error) {
r, err := res.res.RowsAffected()
if err != nil {
return 0, err
}
return r, nil
}
// Wrapper for physical database
type database1 struct {
db *sql.DB
dsc DSC
dbId DbId
adapter Adapter
*Metrics
composer
}
func (db *database1) DSC() DSC {
return db.dsc
}
func (db *database1) DbId() DbId {
return db.dbId
}
func (db *database1) Adapter() Adapter {
return db.adapter
}
// Close closes all physical databases concurrently, releasing any open resources.
func (db *database1) Close(ctx context.Context) error {
db.composer.Close()
return db.db.Close()
}
// Driver returns the physical database's underlying driver.
func (db *database1) Driver() driver.Driver {
return db.db.Driver()
}
// Begin starts a transaction on the master. The isolation level is dependent on the driver.
func (db *database1) Begin() (Tx, error) {
started := db.beginTransact()
t, err := db.db.Begin()
if err != nil {
return nil, err
}
return &tx{db: db, trans: t, started: started}, nil
}
func (db *database1) BeginTx(ctx context.Context, opts *TxOptions) (Tx, error) {
started := db.beginTransact()
t, err := db.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &tx{db: db, trans: t, started: started}, nil
}
// Exec executes a query without returning any rows.
// The args are for any named parameters in the query.
// Exec uses the master as the underlying physical db.
func (db *database1) Exec(query string, args ...interface{}) (Result, error) {
started := db.beginExec()
res, err := db.db.Exec(query, args...)
db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: db, res: res}, nil
}
func (db *database1) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
started := db.beginExec()
res, err := db.db.ExecContext(ctx, query, args...)
db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: db, res: res}, nil
}
// Prepare creates a prepared statement for later queries or executions
// on each physical database, concurrently.
func (db *database1) Prepare(query string) (Stmt, error) {
s, err := db.db.Prepare(query)
if err != nil {
return nil, err
}
return &stmt1{database: db, stmt: s}, nil
}
func (db *database1) PrepareContext(ctx context.Context, query string) (Stmt, error) {
s, err := db.db.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &stmt1{database: db, stmt: s}, nil
}
// Query executes a query that returns rows, typically a SELECT.
// The args are for any parameters in the query.
// Query uses a slave as the physical db.
func (db *database1) Query(query string, args ...interface{}) (Rows, error) {
started := db.beginQuery()
rs, err := db.db.Query(query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: db, rs: rs, started: started}, nil
}
func (db *database1) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error) {
started := db.beginQuery()
rs, err := db.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: db, rs: rs, started: started}, nil
}
// QueryRow executes a query that is expected to return at most one row.
// QueryRow always return a non-nil value.
// Errors are deferred until Row's Scan method is called.
// QueryRow uses a slave as the physical db.
func (db *database1) QueryRow(query string, args ...interface{}) Row {
started := db.beginQuery()
r := db.db.QueryRow(query, args...)
db.endQuery(started)
return &row{db: db, r: r}
}
func (db *database1) QueryRowContext(ctx context.Context, query string, args ...interface{}) Row {
started := db.beginQuery()
r := db.db.QueryRowContext(ctx, query, args...)
db.endQuery(started)
return &row{db: db, r: r}
}
// Ping verifies if a connection to database is still alive,
// establishing a connection if necessary.
func (db *database1) Ping() error {
return db.db.Ping()
}
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool for each underlying physical db.
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the
// new MaxIdleConns will be reduced to match the MaxOpenConns limit
// If n <= 0, no idle connections are retained.
func (db *database1) SetMaxIdleConns(n int) {
db.db.SetMaxIdleConns(n)
}
// SetMaxOpenConns sets the maximum number of open connections
// to each physical database.
// If MaxIdleConns is greater than 0 and the new MaxOpenConns
// is less than MaxIdleConns, then MaxIdleConns will be reduced to match
// the new MaxOpenConns limit. If n <= 0, then there is no limit on the number
// of open connections. The default is 0 (unlimited).
func (db *database1) SetMaxOpenConns(n int) {
db.db.SetMaxOpenConns(n)
}
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
// Expired connections may be closed lazily before reuse.
// If d <= 0, connections are reused forever.
func (db *database1) SetConnMaxLifetime(d time.Duration) {
db.db.SetConnMaxLifetime(d)
}
// Slave returns one of the physical databases which is a slave
func (db *database1) Slave() DB {
return db
}
// Master returns the master physical database
func (db *database1) Master() DB {
return db
}
func (db *database1) IsCluster() bool {
return false
}
func (db *database1) Interface(
detective func(interface{}) interface{},
) (interface{}, bool) {
res := detective(db)
if res != nil {
return res, true
}
return nil, false
}
// Cluster database
type database2 struct {
pdbs []*sql.DB // Physical databases
dsc DSC
count uint64 // Monotonically incrementing counter on each query
dbId DbId
adapter Adapter
*Metrics
composer
}
func (db *database2) DSC() DSC {
return db.dsc
}
func (db *database2) DbId() DbId {
return db.dbId
}
func (db *database2) Adapter() Adapter {
return db.adapter
}
// Close closes all physical databases concurrently, releasing any open resources.
func (db *database2) Close(ctx context.Context) error {
db.composer.Close()
return scatter(
len(db.pdbs),
func(i int) error {
err := db.pdbs[i].Close()
return err
},
)
}
// Driver returns the physical database's underlying driver.
func (db *database2) Driver() driver.Driver {
return db.pdbs[0].Driver()
}
// Begin starts a transaction on the master. The isolation level is dependent on the driver.
func (db *database2) Begin() (Tx, error) {
started := db.beginTransact()
t, err := db.pdbs[0].Begin()
if err != nil {
return nil, err
}
return &tx{db: db, trans: t, started: started}, nil
}
func (db *database2) BeginTx(ctx context.Context, opts *TxOptions) (Tx, error) {
started := db.beginTransact()
t, err := db.pdbs[0].BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &tx{db: db, trans: t, started: started}, nil
}
// Exec executes a query without returning any rows.
// The args are for any named parameters in the query.
// Exec uses the master as the underlying physical db.
func (db *database2) Exec(query string, args ...interface{}) (Result, error) {
started := db.beginExec()
res, err := db.pdbs[0].Exec(query, args...)
db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: db, res: res}, nil
}
func (db *database2) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
started := db.beginExec()
res, err := db.pdbs[0].ExecContext(ctx, query, args...)
db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: db, res: res}, nil
}
// Ping verifies if a connection to each physical database is still alive,
// establishing a connection if necessary.
func (db *database2) Ping() error {
return scatter(
len(db.pdbs),
func(i int) error {
err := db.pdbs[i].Ping()
return err
},
)
}
// Prepare creates a prepared statement for later queries or executions
// on each physical database, concurrently.
func (db *database2) Prepare(query string) (Stmt, error) {
stmts := make([]*sql.Stmt, len(db.pdbs))
err := scatter(
len(db.pdbs),
func(i int) (err error) {
stmts[i], err = db.pdbs[i].Prepare(query)
return err
},
)
if err != nil {
return nil, err
}
return &stmt2{db: db, stmts: stmts}, nil
}
func (db *database2) PrepareContext(ctx context.Context, query string) (Stmt, error) {
stmts := make([]*sql.Stmt, len(db.pdbs))
err := scatter(
len(db.pdbs),
func(i int) (err error) {
stmts[i], err = db.pdbs[i].PrepareContext(ctx, query)
return err
},
)
if err != nil {
return nil, err
}
return &stmt2{db: db, stmts: stmts}, nil
}
// Query executes a query that returns rows, typically a SELECT.
// The args are for any parameters in the query.
// Query uses a slave as the physical db.
func (db *database2) Query(query string, args ...interface{}) (Rows, error) {
started := db.beginQuery()
rs, err := db.pdbs[db.slave(len(db.pdbs))].Query(query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: db, rs: rs, started: started}, nil
}
func (db *database2) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error) {
started := db.beginQuery()
rs, err := db.pdbs[db.slave(len(db.pdbs))].QueryContext(ctx, query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: db, rs: rs, started: started}, nil
}
// QueryRow executes a query that is expected to return at most one row.
// QueryRow always return a non-nil value.
// Errors are deferred until Row's Scan method is called.
// QueryRow uses a slave as the physical db.
func (db *database2) QueryRow(query string, args ...interface{}) Row {
started := db.beginQuery()
r := db.pdbs[db.slave(len(db.pdbs))].QueryRow(query, args...)
db.endQuery(started)
return &row{db: db, r: r}
}
func (db *database2) QueryRowContext(ctx context.Context, query string, args ...interface{}) Row {
started := db.beginQuery()
r := db.pdbs[db.slave(len(db.pdbs))].QueryRowContext(ctx, query, args...)
db.endQuery(started)
return &row{db: db, r: r}
}
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool for each underlying physical db.
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the
// new MaxIdleConns will be reduced to match the MaxOpenConns limit
// If n <= 0, no idle connections are retained.
func (db *database2) SetMaxIdleConns(n int) {
for i := range db.pdbs {
db.pdbs[i].SetMaxIdleConns(n)
}
}
// SetMaxOpenConns sets the maximum number of open connections
// to each physical database.
// If MaxIdleConns is greater than 0 and the new MaxOpenConns
// is less than MaxIdleConns, then MaxIdleConns will be reduced to match
// the new MaxOpenConns limit. If n <= 0, then there is no limit on the number
// of open connections. The default is 0 (unlimited).
func (db *database2) SetMaxOpenConns(n int) {
for i := range db.pdbs {
db.pdbs[i].SetMaxOpenConns(n)
}
}
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
// Expired connections may be closed lazily before reuse.
// If d <= 0, connections are reused forever.
func (db *database2) SetConnMaxLifetime(d time.Duration) {
for i := range db.pdbs {
db.pdbs[i].SetConnMaxLifetime(d)
}
}
// Slave returns one of the physical databases which is a slave
func (db *database2) Slave() DB {
return &database1{
db: db.pdbs[db.slave(len(db.pdbs))],
Metrics: db.Metrics,
}
}
// Master returns the master physical database
func (db *database2) Master() DB {
return &database1{db: db.pdbs[0]}
}
func (db *database2) slave(n int) int {
if n <= 1 {
return 0
}
return int(1 + (atomic.AddUint64(&db.count, 1) % uint64(n-1)))
}
func (db *database2) IsCluster() bool {
return true
}
func (db *database2) Interface(
detective func(interface{}) interface{},
) (interface{}, bool) {
res := detective(db)
if res != nil {
return res, true
}
return nil, false
}
type tx struct {
db DB
trans *sql.Tx
level int16
started int64
}
func (t *tx) DbId() DbId {
return t.db.DbId()
}
func (t *tx) Adapter() Adapter {
return t.db.Adapter()
}
func (t *tx) Level() int16 {
return t.level
}
func (t *tx) Begin() (Tx, error) {
res := &txx{tx{db: t.db, trans: t.trans, level: t.level + 1}, true}
query := "SAVEPOINT " + res.getSavePoint()
_, err := t.Exec(query)
if err != nil {
return nil, err
}
return res, nil
}
func (t *tx) BeginTx(ctx context.Context, opts *TxOptions) (Tx, error) {
res := &txx{tx{db: t.db, trans: t.trans, level: t.level + 1}, true}
query := "SAVEPOINT " + res.getSavePoint()
_, err := t.ExecContext(ctx, query)
if err != nil {
return nil, err
}
return res, nil
}
func (t *tx) Commit() error {
err := t.trans.Commit()
t.db.endTransact(t.started)
if err != nil {
return err
}
return nil
}
func (t *tx) Rollback() error {
err := t.trans.Rollback()
if err != nil {
return err
}
return nil
}
func (t *tx) Exec(query string, args ...interface{}) (Result, error) {
started := t.db.beginExec()
res, err := t.trans.Exec(query, args...)
t.db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: t.db, res: res}, nil
}
func (t *tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error) {
started := t.db.beginExec()
res, err := t.trans.ExecContext(ctx, query, args...)
t.db.endExec(started)
if err != nil {
return nil, err
}
return &result{db: t.db, res: res}, nil
}
func (t *tx) Query(query string, args ...interface{}) (Rows, error) {
started := t.db.beginQuery()
rs, err := t.trans.Query(query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: t.db, rs: rs, started: started}, nil
}
func (t *tx) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error) {
started := t.db.beginQuery()
rs, err := t.trans.QueryContext(ctx, query, args...)
if err != nil {
return nil, recode(err)
}
return &rows{db: t.db, rs: rs, started: started}, nil
}
func (t *tx) QueryRow(query string, args ...interface{}) Row {
started := t.db.beginQuery()
r := t.trans.QueryRow(query, args...)
t.db.endQuery(started)
return &row{db: t.db, r: r}
}
func (t *tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) Row {
started := t.db.beginQuery()
r := t.trans.QueryRowContext(ctx, query, args...)
t.db.endQuery(started)
return &row{db: t.db, r: r}
}
func (t *tx) getSavePoint() string {
return fmt.Sprintf("trans%d", t.level)
}
type txx struct {
tx
active bool
}
func (t *txx) DbId() DbId {
return t.db.DbId()
}