|
5 | 5 | "context" |
6 | 6 | "crypto/tls" |
7 | 7 | "fmt" |
| 8 | + "math/bits" |
8 | 9 | "net" |
9 | 10 | "runtime" |
10 | 11 | "runtime/debug" |
@@ -381,6 +382,21 @@ func (c *Conn) Begin() error { |
381 | 382 | return errors.Trace(err) |
382 | 383 | } |
383 | 384 |
|
| 385 | +func (c *Conn) BeginTx(readOnly bool, txIsolation string) error { |
| 386 | + if txIsolation != "" { |
| 387 | + if _, err := c.exec("SET TRANSACTION ISOLATION LEVEL " + txIsolation); err != nil { |
| 388 | + return errors.Trace(err) |
| 389 | + } |
| 390 | + } |
| 391 | + var err error |
| 392 | + if readOnly { |
| 393 | + _, err = c.exec("START TRANSACTION READ ONLY") |
| 394 | + } else { |
| 395 | + _, err = c.exec("START TRANSACTION") |
| 396 | + } |
| 397 | + return errors.Trace(err) |
| 398 | +} |
| 399 | + |
384 | 400 | func (c *Conn) Commit() error { |
385 | 401 | _, err := c.exec("COMMIT") |
386 | 402 | return errors.Trace(err) |
@@ -557,13 +573,10 @@ func (c *Conn) execSend(query string) error { |
557 | 573 | // separated by "|". Examples of capability names are CLIENT_DEPRECATE_EOF and CLIENT_PROTOCOL_41. |
558 | 574 | // These are defined as constants in the mysql package. |
559 | 575 | func (c *Conn) CapabilityString() string { |
560 | | - var caps []string |
561 | 576 | capability := c.capability |
562 | | - for i := 0; capability != 0; i++ { |
563 | | - field := uint32(1 << i) |
564 | | - if capability&field == 0 { |
565 | | - continue |
566 | | - } |
| 577 | + caps := make([]string, 0, bits.OnesCount32(capability)) |
| 578 | + for capability != 0 { |
| 579 | + field := uint32(1 << bits.TrailingZeros32(capability)) |
567 | 580 | capability ^= field |
568 | 581 |
|
569 | 582 | switch field { |
@@ -642,13 +655,10 @@ func (c *Conn) CapabilityString() string { |
642 | 655 | // StatusString returns a "|" separated list of status fields. Example status values are SERVER_QUERY_WAS_SLOW and SERVER_STATUS_AUTOCOMMIT. |
643 | 656 | // These are defined as constants in the mysql package. |
644 | 657 | func (c *Conn) StatusString() string { |
645 | | - var stats []string |
646 | 658 | status := c.status |
647 | | - for i := 0; status != 0; i++ { |
648 | | - field := uint16(1 << i) |
649 | | - if status&field == 0 { |
650 | | - continue |
651 | | - } |
| 659 | + stats := make([]string, 0, bits.OnesCount16(status)) |
| 660 | + for status != 0 { |
| 661 | + field := uint16(1 << bits.TrailingZeros16(status)) |
652 | 662 | status ^= field |
653 | 663 |
|
654 | 664 | switch field { |
|
0 commit comments