Skip to content

Commit c2d7e96

Browse files
committed
Export ParseDSN and Config
Fixes go-sql-driver#224
1 parent d164b60 commit c2d7e96

11 files changed

+518
-484
lines changed

benchmark_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ func BenchmarkRoundtripBin(b *testing.B) {
216216

217217
func BenchmarkInterpolation(b *testing.B) {
218218
mc := &mysqlConn{
219-
cfg: &config{
220-
interpolateParams: true,
221-
loc: time.UTC,
219+
cfg: &Config{
220+
InterpolateParams: true,
221+
Loc: time.UTC,
222222
},
223223
maxPacketAllowed: maxPacketSize,
224224
maxWriteSize: maxPacketSize - 1,

connection.go

+5-47
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
package mysql
1010

1111
import (
12-
"crypto/tls"
1312
"database/sql/driver"
14-
"errors"
1513
"net"
1614
"strconv"
1715
"strings"
@@ -23,7 +21,7 @@ type mysqlConn struct {
2321
netConn net.Conn
2422
affectedRows uint64
2523
insertId uint64
26-
cfg *config
24+
cfg *Config
2725
maxPacketAllowed int
2826
maxWriteSize int
2927
flags clientFlag
@@ -33,28 +31,9 @@ type mysqlConn struct {
3331
strict bool
3432
}
3533

36-
type config struct {
37-
user string
38-
passwd string
39-
net string
40-
addr string
41-
dbname string
42-
params map[string]string
43-
loc *time.Location
44-
tls *tls.Config
45-
timeout time.Duration
46-
collation uint8
47-
allowAllFiles bool
48-
allowOldPasswords bool
49-
allowCleartextPasswords bool
50-
clientFoundRows bool
51-
columnsWithAlias bool
52-
interpolateParams bool
53-
}
54-
5534
// Handles parameters set in DSN after the connection is established
5635
func (mc *mysqlConn) handleParams() (err error) {
57-
for param, val := range mc.cfg.params {
36+
for param, val := range mc.cfg.Params {
5837
switch param {
5938
// Charset
6039
case "charset":
@@ -70,27 +49,6 @@ func (mc *mysqlConn) handleParams() (err error) {
7049
return
7150
}
7251

73-
// time.Time parsing
74-
case "parseTime":
75-
var isBool bool
76-
mc.parseTime, isBool = readBool(val)
77-
if !isBool {
78-
return errors.New("Invalid Bool value: " + val)
79-
}
80-
81-
// Strict mode
82-
case "strict":
83-
var isBool bool
84-
mc.strict, isBool = readBool(val)
85-
if !isBool {
86-
return errors.New("Invalid Bool value: " + val)
87-
}
88-
89-
// Compression
90-
case "compress":
91-
err = errors.New("Compression not implemented yet")
92-
return
93-
9452
// System Vars
9553
default:
9654
err = mc.exec("SET " + param + "=" + val + "")
@@ -217,7 +175,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
217175
if v.IsZero() {
218176
buf = append(buf, "'0000-00-00'"...)
219177
} else {
220-
v := v.In(mc.cfg.loc)
178+
v := v.In(mc.cfg.Loc)
221179
v = v.Add(time.Nanosecond * 500) // To round under microsecond
222180
year := v.Year()
223181
year100 := year / 100
@@ -298,7 +256,7 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
298256
return nil, driver.ErrBadConn
299257
}
300258
if len(args) != 0 {
301-
if !mc.cfg.interpolateParams {
259+
if !mc.cfg.InterpolateParams {
302260
return nil, driver.ErrSkip
303261
}
304262
// try to interpolate the parameters to save extra roundtrips for preparing and closing a statement
@@ -349,7 +307,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
349307
return nil, driver.ErrBadConn
350308
}
351309
if len(args) != 0 {
352-
if !mc.cfg.interpolateParams {
310+
if !mc.cfg.InterpolateParams {
353311
return nil, driver.ErrSkip
354312
}
355313
// try client-side prepare to reduce roundtrip

driver.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,19 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
5353
maxPacketAllowed: maxPacketSize,
5454
maxWriteSize: maxPacketSize - 1,
5555
}
56-
mc.cfg, err = parseDSN(dsn)
56+
mc.cfg, err = ParseDSN(dsn)
5757
if err != nil {
5858
return nil, err
5959
}
60+
mc.parseTime = mc.cfg.ParseTime
61+
mc.strict = mc.cfg.Strict
6062

6163
// Connect to Server
62-
if dial, ok := dials[mc.cfg.net]; ok {
63-
mc.netConn, err = dial(mc.cfg.addr)
64+
if dial, ok := dials[mc.cfg.Net]; ok {
65+
mc.netConn, err = dial(mc.cfg.Addr)
6466
} else {
65-
nd := net.Dialer{Timeout: mc.cfg.timeout}
66-
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
67+
nd := net.Dialer{Timeout: mc.cfg.Timeout}
68+
mc.netConn, err = nd.Dial(mc.cfg.Net, mc.cfg.Addr)
6769
}
6870
if err != nil {
6971
return nil, err
@@ -136,15 +138,15 @@ func handleAuthResult(mc *mysqlConn, cipher []byte) error {
136138
}
137139

138140
// Retry auth if configured to do so.
139-
if mc.cfg.allowOldPasswords && err == ErrOldPassword {
141+
if mc.cfg.AllowOldPasswords && err == ErrOldPassword {
140142
// Retry with old authentication method. Note: there are edge cases
141143
// where this should work but doesn't; this is currently "wontfix":
142144
// https://github.com/go-sql-driver/mysql/issues/184
143145
if err = mc.writeOldAuthPacket(cipher); err != nil {
144146
return err
145147
}
146148
err = mc.readResultOK()
147-
} else if mc.cfg.allowCleartextPasswords && err == ErrCleartextPassword {
149+
} else if mc.cfg.AllowCleartextPasswords && err == ErrCleartextPassword {
148150
// Retry with clear text password for
149151
// http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
150152
// http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html

driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func runTests(t *testing.T, dsn string, tests ...func(dbt *DBTest)) {
9191

9292
dsn2 := dsn + "&interpolateParams=true"
9393
var db2 *sql.DB
94-
if _, err := parseDSN(dsn2); err != errInvalidDSNUnsafeCollation {
94+
if _, err := ParseDSN(dsn2); err != errInvalidDSNUnsafeCollation {
9595
db2, err = sql.Open("mysql", dsn2)
9696
if err != nil {
9797
t.Fatalf("Error connecting: %s", err.Error())

0 commit comments

Comments
 (0)