@@ -20,12 +20,13 @@ import (
20
20
"time"
21
21
)
22
22
23
- type mysqlConn struct {
23
+ type MysqlConn struct {
24
24
buf buffer
25
25
netConn net.Conn
26
26
rawConn net.Conn // underlying connection when netConn is TLS connection.
27
27
affectedRows uint64
28
28
insertId uint64
29
+ lastMessage string
29
30
cfg * Config
30
31
maxAllowedPacket int
31
32
maxWriteSize int
@@ -45,8 +46,18 @@ type mysqlConn struct {
45
46
closed atomicBool // set when conn is closed, before closech is closed
46
47
}
47
48
49
+ // RowsAffected returns the number of rows affected by the query.
50
+ func (mc * MysqlConn ) RowsAffected () uint64 {
51
+ return mc .affectedRows
52
+ }
53
+
54
+ // LastMessage returns the database's last message.
55
+ func (mc * MysqlConn ) LastMessage () string {
56
+ return mc .lastMessage
57
+ }
58
+
48
59
// Handles parameters set in DSN after the connection is established
49
- func (mc * mysqlConn ) handleParams () (err error ) {
60
+ func (mc * MysqlConn ) handleParams () (err error ) {
50
61
var cmdSet strings.Builder
51
62
for param , val := range mc .cfg .Params {
52
63
switch param {
@@ -89,7 +100,7 @@ func (mc *mysqlConn) handleParams() (err error) {
89
100
return
90
101
}
91
102
92
- func (mc * mysqlConn ) markBadConn (err error ) error {
103
+ func (mc * MysqlConn ) markBadConn (err error ) error {
93
104
if mc == nil {
94
105
return err
95
106
}
@@ -99,11 +110,11 @@ func (mc *mysqlConn) markBadConn(err error) error {
99
110
return driver .ErrBadConn
100
111
}
101
112
102
- func (mc * mysqlConn ) Begin () (driver.Tx , error ) {
113
+ func (mc * MysqlConn ) Begin () (driver.Tx , error ) {
103
114
return mc .begin (false )
104
115
}
105
116
106
- func (mc * mysqlConn ) begin (readOnly bool ) (driver.Tx , error ) {
117
+ func (mc * MysqlConn ) begin (readOnly bool ) (driver.Tx , error ) {
107
118
if mc .closed .Load () {
108
119
errLog .Print (ErrInvalidConn )
109
120
return nil , driver .ErrBadConn
@@ -121,7 +132,7 @@ func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
121
132
return nil , mc .markBadConn (err )
122
133
}
123
134
124
- func (mc * mysqlConn ) Close () (err error ) {
135
+ func (mc * MysqlConn ) Close () (err error ) {
125
136
// Makes Close idempotent
126
137
if ! mc .closed .Load () {
127
138
err = mc .writeCommandPacket (comQuit )
@@ -136,7 +147,7 @@ func (mc *mysqlConn) Close() (err error) {
136
147
// function after successfully authentication, call Close instead. This function
137
148
// is called before auth or on auth failure because MySQL will have already
138
149
// closed the network connection.
139
- func (mc * mysqlConn ) cleanup () {
150
+ func (mc * MysqlConn ) cleanup () {
140
151
if mc .closed .Swap (true ) {
141
152
return
142
153
}
@@ -151,7 +162,7 @@ func (mc *mysqlConn) cleanup() {
151
162
}
152
163
}
153
164
154
- func (mc * mysqlConn ) error () error {
165
+ func (mc * MysqlConn ) error () error {
155
166
if mc .closed .Load () {
156
167
if err := mc .canceled .Value (); err != nil {
157
168
return err
@@ -161,7 +172,7 @@ func (mc *mysqlConn) error() error {
161
172
return nil
162
173
}
163
174
164
- func (mc * mysqlConn ) Prepare (query string ) (driver.Stmt , error ) {
175
+ func (mc * MysqlConn ) Prepare (query string ) (driver.Stmt , error ) {
165
176
if mc .closed .Load () {
166
177
errLog .Print (ErrInvalidConn )
167
178
return nil , driver .ErrBadConn
@@ -195,7 +206,7 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
195
206
return stmt , err
196
207
}
197
208
198
- func (mc * mysqlConn ) interpolateParams (query string , args []driver.Value ) (string , error ) {
209
+ func (mc * MysqlConn ) interpolateParams (query string , args []driver.Value ) (string , error ) {
199
210
// Number of ? should be same to len(args)
200
211
if strings .Count (query , "?" ) != len (args ) {
201
212
return "" , driver .ErrSkip
@@ -294,7 +305,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
294
305
return string (buf ), nil
295
306
}
296
307
297
- func (mc * mysqlConn ) Exec (query string , args []driver.Value ) (driver.Result , error ) {
308
+ func (mc * MysqlConn ) Exec (query string , args []driver.Value ) (driver.Result , error ) {
298
309
if mc .closed .Load () {
299
310
errLog .Print (ErrInvalidConn )
300
311
return nil , driver .ErrBadConn
@@ -312,6 +323,7 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
312
323
}
313
324
mc .affectedRows = 0
314
325
mc .insertId = 0
326
+ mc .lastMessage = ""
315
327
316
328
err := mc .exec (query )
317
329
if err == nil {
@@ -324,7 +336,7 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
324
336
}
325
337
326
338
// Internal function to execute commands
327
- func (mc * mysqlConn ) exec (query string ) error {
339
+ func (mc * MysqlConn ) exec (query string ) error {
328
340
// Send command
329
341
if err := mc .writeCommandPacketStr (comQuery , query ); err != nil {
330
342
return mc .markBadConn (err )
@@ -351,11 +363,11 @@ func (mc *mysqlConn) exec(query string) error {
351
363
return mc .discardResults ()
352
364
}
353
365
354
- func (mc * mysqlConn ) Query (query string , args []driver.Value ) (driver.Rows , error ) {
366
+ func (mc * MysqlConn ) Query (query string , args []driver.Value ) (driver.Rows , error ) {
355
367
return mc .query (query , args )
356
368
}
357
369
358
- func (mc * mysqlConn ) query (query string , args []driver.Value ) (* textRows , error ) {
370
+ func (mc * MysqlConn ) query (query string , args []driver.Value ) (* textRows , error ) {
359
371
if mc .closed .Load () {
360
372
errLog .Print (ErrInvalidConn )
361
373
return nil , driver .ErrBadConn
@@ -371,6 +383,11 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
371
383
}
372
384
query = prepared
373
385
}
386
+
387
+ mc .affectedRows = 0
388
+ mc .insertId = 0
389
+ mc .lastMessage = ""
390
+
374
391
// Send command
375
392
err := mc .writeCommandPacketStr (comQuery , query )
376
393
if err == nil {
@@ -402,7 +419,7 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
402
419
403
420
// Gets the value of the given MySQL System Variable
404
421
// The returned byte slice is only valid until the next read
405
- func (mc * mysqlConn ) getSystemVar (name string ) ([]byte , error ) {
422
+ func (mc * MysqlConn ) getSystemVar (name string ) ([]byte , error ) {
406
423
// Send command
407
424
if err := mc .writeCommandPacketStr (comQuery , "SELECT @@" + name ); err != nil {
408
425
return nil , err
@@ -431,13 +448,13 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
431
448
}
432
449
433
450
// finish is called when the query has canceled.
434
- func (mc * mysqlConn ) cancel (err error ) {
451
+ func (mc * MysqlConn ) cancel (err error ) {
435
452
mc .canceled .Set (err )
436
453
mc .cleanup ()
437
454
}
438
455
439
456
// finish is called when the query has succeeded.
440
- func (mc * mysqlConn ) finish () {
457
+ func (mc * MysqlConn ) finish () {
441
458
if ! mc .watching || mc .finished == nil {
442
459
return
443
460
}
@@ -449,7 +466,7 @@ func (mc *mysqlConn) finish() {
449
466
}
450
467
451
468
// Ping implements driver.Pinger interface
452
- func (mc * mysqlConn ) Ping (ctx context.Context ) (err error ) {
469
+ func (mc * MysqlConn ) Ping (ctx context.Context ) (err error ) {
453
470
if mc .closed .Load () {
454
471
errLog .Print (ErrInvalidConn )
455
472
return driver .ErrBadConn
@@ -468,7 +485,7 @@ func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
468
485
}
469
486
470
487
// BeginTx implements driver.ConnBeginTx interface
471
- func (mc * mysqlConn ) BeginTx (ctx context.Context , opts driver.TxOptions ) (driver.Tx , error ) {
488
+ func (mc * MysqlConn ) BeginTx (ctx context.Context , opts driver.TxOptions ) (driver.Tx , error ) {
472
489
if mc .closed .Load () {
473
490
return nil , driver .ErrBadConn
474
491
}
@@ -492,7 +509,7 @@ func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
492
509
return mc .begin (opts .ReadOnly )
493
510
}
494
511
495
- func (mc * mysqlConn ) QueryContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
512
+ func (mc * MysqlConn ) QueryContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
496
513
dargs , err := namedValueToValue (args )
497
514
if err != nil {
498
515
return nil , err
@@ -511,7 +528,7 @@ func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driv
511
528
return rows , err
512
529
}
513
530
514
- func (mc * mysqlConn ) ExecContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Result , error ) {
531
+ func (mc * MysqlConn ) ExecContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Result , error ) {
515
532
dargs , err := namedValueToValue (args )
516
533
if err != nil {
517
534
return nil , err
@@ -525,7 +542,7 @@ func (mc *mysqlConn) ExecContext(ctx context.Context, query string, args []drive
525
542
return mc .Exec (query , dargs )
526
543
}
527
544
528
- func (mc * mysqlConn ) PrepareContext (ctx context.Context , query string ) (driver.Stmt , error ) {
545
+ func (mc * MysqlConn ) PrepareContext (ctx context.Context , query string ) (driver.Stmt , error ) {
529
546
if err := mc .watchCancel (ctx ); err != nil {
530
547
return nil , err
531
548
}
@@ -578,7 +595,7 @@ func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue
578
595
return stmt .Exec (dargs )
579
596
}
580
597
581
- func (mc * mysqlConn ) watchCancel (ctx context.Context ) error {
598
+ func (mc * MysqlConn ) watchCancel (ctx context.Context ) error {
582
599
if mc .watching {
583
600
// Reach here if canceled,
584
601
// so the connection is already invalid
@@ -603,7 +620,7 @@ func (mc *mysqlConn) watchCancel(ctx context.Context) error {
603
620
return nil
604
621
}
605
622
606
- func (mc * mysqlConn ) startWatcher () {
623
+ func (mc * MysqlConn ) startWatcher () {
607
624
watcher := make (chan context.Context , 1 )
608
625
mc .watcher = watcher
609
626
finished := make (chan struct {})
@@ -628,14 +645,14 @@ func (mc *mysqlConn) startWatcher() {
628
645
}()
629
646
}
630
647
631
- func (mc * mysqlConn ) CheckNamedValue (nv * driver.NamedValue ) (err error ) {
648
+ func (mc * MysqlConn ) CheckNamedValue (nv * driver.NamedValue ) (err error ) {
632
649
nv .Value , err = converter {}.ConvertValue (nv .Value )
633
650
return
634
651
}
635
652
636
653
// ResetSession implements driver.SessionResetter.
637
654
// (From Go 1.10)
638
- func (mc * mysqlConn ) ResetSession (ctx context.Context ) error {
655
+ func (mc * MysqlConn ) ResetSession (ctx context.Context ) error {
639
656
if mc .closed .Load () {
640
657
return driver .ErrBadConn
641
658
}
@@ -645,6 +662,6 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
645
662
646
663
// IsValid implements driver.Validator interface
647
664
// (From Go 1.15)
648
- func (mc * mysqlConn ) IsValid () bool {
665
+ func (mc * MysqlConn ) IsValid () bool {
649
666
return ! mc .closed .Load ()
650
667
}
0 commit comments