Skip to content

Commit 8e75469

Browse files
committed
add a parameter to mc.Begin and reuse it.
1 parent 23c5e4d commit 8e75469

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

connection.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,20 @@ func (mc *mysqlConn) handleParams() (err error) {
8282
}
8383

8484
func (mc *mysqlConn) Begin() (driver.Tx, error) {
85+
return mc.begin(false)
86+
}
87+
88+
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
8589
if mc.closed.IsSet() {
8690
errLog.Print(ErrInvalidConn)
8791
return nil, driver.ErrBadConn
8892
}
89-
err := mc.exec("START TRANSACTION")
93+
var err error
94+
if readOnly {
95+
err = mc.exec("START TRANSACTION READ ONLY")
96+
} else {
97+
err = mc.exec("START TRANSACTION")
98+
}
9099
if err == nil {
91100
return &mysqlTx{mc}, err
92101
}

connection_go18.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,7 @@ func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
5656
}
5757
}
5858

59-
if opts.ReadOnly {
60-
return mc.beginReadOnly()
61-
}
62-
return mc.Begin()
63-
}
64-
65-
func (mc *mysqlConn) beginReadOnly() (driver.Tx, error) {
66-
if mc.closed.IsSet() {
67-
errLog.Print(ErrInvalidConn)
68-
return nil, driver.ErrBadConn
69-
}
70-
err := mc.exec("START TRANSACTION READ ONLY")
71-
if err != nil {
72-
return nil, err
73-
}
74-
75-
return &mysqlTx{mc}, nil
59+
return mc.begin(opts.ReadOnly)
7660
}
7761

7862
func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {

0 commit comments

Comments
 (0)