Skip to content

Commit acb04ff

Browse files
committedJan 20, 2016
Merge pull request go-sql-driver#410 from go-sql-driver/lint
Lint
2 parents 72ea5d0 + 280e61d commit acb04ff

8 files changed

+152
-151
lines changed
 

‎benchmark_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func initDB(b *testing.B, queries ...string) *sql.DB {
4949
for _, query := range queries {
5050
if _, err := db.Exec(query); err != nil {
5151
if w, ok := err.(MySQLWarnings); ok {
52-
b.Logf("Warning on %q: %v", query, w)
52+
b.Logf("warning on %q: %v", query, w)
5353
} else {
54-
b.Fatalf("Error on %q: %v", query, err)
54+
b.Fatalf("error on %q: %v", query, err)
5555
}
5656
}
5757
}

‎driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
55
// You can obtain one at http://mozilla.org/MPL/2.0/.
66

7-
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
7+
// Package mysql provides a MySQL driver for Go's database/sql package
88
//
99
// The driver should be used via the database/sql package:
1010
//
@@ -22,7 +22,7 @@ import (
2222
"net"
2323
)
2424

25-
// This struct is exported to make the driver directly accessible.
25+
// MySQLDriver is exported to make the driver directly accessible.
2626
// In general the driver is used via the database/sql package.
2727
type MySQLDriver struct{}
2828

‎driver_test.go

+82-82
Large diffs are not rendered by default.

‎dsn.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
)
2020

2121
var (
22-
errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
23-
errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
24-
errInvalidDSNNoSlash = errors.New("Invalid DSN: Missing the slash separating the database name")
25-
errInvalidDSNUnsafeCollation = errors.New("Invalid DSN: interpolateParams can be used with ascii, latin1, utf8 and utf8mb4 charset")
22+
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
23+
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
24+
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
25+
errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
2626
)
2727

2828
// Config is a configuration parsed from a DSN string
@@ -141,7 +141,7 @@ func ParseDSN(dsn string) (cfg *Config, err error) {
141141
case "unix":
142142
cfg.Addr = "/tmp/mysql.sock"
143143
default:
144-
return nil, errors.New("Default addr for network '" + cfg.Net + "' unknown")
144+
return nil, errors.New("default addr for network '" + cfg.Net + "' unknown")
145145
}
146146

147147
}
@@ -166,31 +166,31 @@ func parseDSNParams(cfg *Config, params string) (err error) {
166166
var isBool bool
167167
cfg.AllowAllFiles, isBool = readBool(value)
168168
if !isBool {
169-
return fmt.Errorf("Invalid Bool value: %s", value)
169+
return errors.New("invalid bool value: " + value)
170170
}
171171

172172
// Use cleartext authentication mode (MySQL 5.5.10+)
173173
case "allowCleartextPasswords":
174174
var isBool bool
175175
cfg.AllowCleartextPasswords, isBool = readBool(value)
176176
if !isBool {
177-
return fmt.Errorf("Invalid Bool value: %s", value)
177+
return errors.New("invalid bool value: " + value)
178178
}
179179

180180
// Use old authentication mode (pre MySQL 4.1)
181181
case "allowOldPasswords":
182182
var isBool bool
183183
cfg.AllowOldPasswords, isBool = readBool(value)
184184
if !isBool {
185-
return fmt.Errorf("Invalid Bool value: %s", value)
185+
return errors.New("invalid bool value: " + value)
186186
}
187187

188188
// Switch "rowsAffected" mode
189189
case "clientFoundRows":
190190
var isBool bool
191191
cfg.ClientFoundRows, isBool = readBool(value)
192192
if !isBool {
193-
return fmt.Errorf("Invalid Bool value: %s", value)
193+
return errors.New("invalid bool value: " + value)
194194
}
195195

196196
// Collation
@@ -210,19 +210,19 @@ func parseDSNParams(cfg *Config, params string) (err error) {
210210
var isBool bool
211211
cfg.ColumnsWithAlias, isBool = readBool(value)
212212
if !isBool {
213-
return fmt.Errorf("Invalid Bool value: %s", value)
213+
return errors.New("invalid bool value: " + value)
214214
}
215215

216216
// Compression
217217
case "compress":
218-
return errors.New("Compression not implemented yet")
218+
return errors.New("compression not implemented yet")
219219

220220
// Enable client side placeholder substitution
221221
case "interpolateParams":
222222
var isBool bool
223223
cfg.InterpolateParams, isBool = readBool(value)
224224
if !isBool {
225-
return fmt.Errorf("Invalid Bool value: %s", value)
225+
return errors.New("invalid bool value: " + value)
226226
}
227227

228228
// Time Location
@@ -240,7 +240,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
240240
var isBool bool
241241
cfg.ParseTime, isBool = readBool(value)
242242
if !isBool {
243-
return errors.New("Invalid Bool value: " + value)
243+
return errors.New("invalid bool value: " + value)
244244
}
245245

246246
// I/O read Timeout
@@ -255,7 +255,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
255255
var isBool bool
256256
cfg.Strict, isBool = readBool(value)
257257
if !isBool {
258-
return errors.New("Invalid Bool value: " + value)
258+
return errors.New("invalid bool value: " + value)
259259
}
260260

261261
// Dial Timeout
@@ -273,7 +273,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
273273
cfg.TLS = &tls.Config{}
274274
}
275275
} else if value, err := url.QueryUnescape(value); err != nil {
276-
return fmt.Errorf("Invalid value for tls config name: %v", err)
276+
return fmt.Errorf("invalid value for TLS config name: %v", err)
277277
} else {
278278
if strings.ToLower(value) == "skip-verify" {
279279
cfg.TLS = &tls.Config{InsecureSkipVerify: true}
@@ -287,7 +287,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
287287

288288
cfg.TLS = tlsConfig
289289
} else {
290-
return fmt.Errorf("Invalid value / unknown config name: %s", value)
290+
return errors.New("invalid value / unknown config name: " + value)
291291
}
292292
}
293293

‎errors.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import (
1919

2020
// Various errors the driver might return. Can change between driver versions.
2121
var (
22-
ErrInvalidConn = errors.New("Invalid Connection")
23-
ErrMalformPkt = errors.New("Malformed Packet")
24-
ErrNoTLS = errors.New("TLS encryption requested but server does not support TLS")
25-
ErrOldPassword = errors.New("This user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
26-
ErrCleartextPassword = errors.New("This user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN.")
27-
ErrUnknownPlugin = errors.New("The authentication plugin is not supported.")
28-
ErrOldProtocol = errors.New("MySQL-Server does not support required Protocol 41+")
29-
ErrPktSync = errors.New("Commands out of sync. You can't run this command now")
30-
ErrPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
31-
ErrPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
32-
ErrBusyBuffer = errors.New("Busy buffer")
22+
ErrInvalidConn = errors.New("invalid connection")
23+
ErrMalformPkt = errors.New("malformed packet")
24+
ErrNoTLS = errors.New("TLS requested but server does not support TLS")
25+
ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
26+
ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
27+
ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
28+
ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
29+
ErrPktSync = errors.New("commands out of sync. You can't run this command now")
30+
ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
31+
ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
32+
ErrBusyBuffer = errors.New("busy buffer")
3333
)
3434

35-
var errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
35+
var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
3636

3737
// Logger is used to log critical error messages.
3838
type Logger interface {

‎infile.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
139139
} else if fileSize <= mc.maxPacketAllowed {
140140
data = make([]byte, 4+mc.maxWriteSize)
141141
} else {
142-
err = fmt.Errorf("Local File '%s' too large: Size: %d, Max: %d", name, fileSize, mc.maxPacketAllowed)
142+
err = fmt.Errorf("local file '%s' too large: size: %d, max: %d", name, fileSize, mc.maxPacketAllowed)
143143
}
144144
}
145145
}
146146
} else {
147-
err = fmt.Errorf("Local File '%s' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files", name)
147+
err = fmt.Errorf("local file '%s' is not registered", name)
148148
}
149149
}
150150

@@ -175,8 +175,8 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
175175
// read OK packet
176176
if err == nil {
177177
return mc.readResultOK()
178-
} else {
179-
mc.readPacket()
180178
}
179+
180+
mc.readPacket()
181181
return err
182182
}

‎packets.go

+20-21
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
4747
if data[3] != mc.sequence {
4848
if data[3] > mc.sequence {
4949
return nil, ErrPktSyncMul
50-
} else {
51-
return nil, ErrPktSync
5250
}
51+
return nil, ErrPktSync
5352
}
5453
mc.sequence++
5554

@@ -146,7 +145,7 @@ func (mc *mysqlConn) readInitPacket() ([]byte, error) {
146145
// protocol version [1 byte]
147146
if data[0] < minProtocolVersion {
148147
return nil, fmt.Errorf(
149-
"Unsupported MySQL Protocol Version %d. Protocol Version %d or higher is required",
148+
"unsupported protocol version %d. Version %d or higher is required",
150149
data[0],
151150
minProtocolVersion,
152151
)
@@ -539,13 +538,13 @@ func (mc *mysqlConn) handleOkPacket(data []byte) error {
539538
// warning count [2 bytes]
540539
if !mc.strict {
541540
return nil
542-
} else {
543-
pos := 1 + n + m + 2
544-
if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
545-
return mc.getWarnings()
546-
}
547-
return nil
548541
}
542+
543+
pos := 1 + n + m + 2
544+
if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
545+
return mc.getWarnings()
546+
}
547+
return nil
549548
}
550549

551550
// Read Packets as Field Packets until EOF-Packet or an Error appears
@@ -564,7 +563,7 @@ func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
564563
if i == count {
565564
return columns, nil
566565
}
567-
return nil, fmt.Errorf("ColumnsCount mismatch n:%d len:%d", count, len(columns))
566+
return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
568567
}
569568

570569
// Catalog
@@ -742,13 +741,13 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
742741
// Warning count [16 bit uint]
743742
if !stmt.mc.strict {
744743
return columnCount, nil
745-
} else {
746-
// Check for warnings count > 0, only available in MySQL > 4.1
747-
if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
748-
return columnCount, stmt.mc.getWarnings()
749-
}
750-
return columnCount, nil
751744
}
745+
746+
// Check for warnings count > 0, only available in MySQL > 4.1
747+
if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
748+
return columnCount, stmt.mc.getWarnings()
749+
}
750+
return columnCount, nil
752751
}
753752
return 0, err
754753
}
@@ -810,7 +809,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
810809
func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
811810
if len(args) != stmt.paramCount {
812811
return fmt.Errorf(
813-
"Arguments count mismatch (Got: %d Has: %d)",
812+
"argument count mismatch (got: %d; has: %d)",
814813
len(args),
815814
stmt.paramCount,
816815
)
@@ -996,7 +995,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
996995
paramValues = append(paramValues, val...)
997996

998997
default:
999-
return fmt.Errorf("Can't convert type: %T", arg)
998+
return fmt.Errorf("can not convert type: %T", arg)
1000999
}
10011000
}
10021001

@@ -1144,7 +1143,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
11441143
dstlen = 8 + 1 + decimals
11451144
default:
11461145
return fmt.Errorf(
1147-
"MySQL protocol error, illegal decimals value %d",
1146+
"protocol error, illegal decimals value %d",
11481147
rows.columns[i].decimals,
11491148
)
11501149
}
@@ -1163,7 +1162,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
11631162
dstlen = 19 + 1 + decimals
11641163
default:
11651164
return fmt.Errorf(
1166-
"MySQL protocol error, illegal decimals value %d",
1165+
"protocol error, illegal decimals value %d",
11671166
rows.columns[i].decimals,
11681167
)
11691168
}
@@ -1180,7 +1179,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
11801179

11811180
// Please report if this happens!
11821181
default:
1183-
return fmt.Errorf("Unknown FieldType %d", rows.columns[i].fieldType)
1182+
return fmt.Errorf("unknown field type %d", rows.columns[i].fieldType)
11841183
}
11851184
}
11861185

‎utils.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ var (
2323
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
2424
)
2525

26-
func init() {
27-
tlsConfigRegister = make(map[string]*tls.Config)
28-
}
29-
3026
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
3127
// Use the key as a value in the DSN where tls=value.
3228
//
@@ -52,7 +48,11 @@ func init() {
5248
//
5349
func RegisterTLSConfig(key string, config *tls.Config) error {
5450
if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
55-
return fmt.Errorf("Key '%s' is reserved", key)
51+
return fmt.Errorf("key '%s' is reserved", key)
52+
}
53+
54+
if tlsConfigRegister == nil {
55+
tlsConfigRegister = make(map[string]*tls.Config)
5656
}
5757

5858
tlsConfigRegister[key] = config
@@ -61,7 +61,9 @@ func RegisterTLSConfig(key string, config *tls.Config) error {
6161

6262
// DeregisterTLSConfig removes the tls.Config associated with key.
6363
func DeregisterTLSConfig(key string) {
64-
delete(tlsConfigRegister, key)
64+
if tlsConfigRegister != nil {
65+
delete(tlsConfigRegister, key)
66+
}
6567
}
6668

6769
// Returns the bool value of the input.
@@ -258,7 +260,7 @@ func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
258260
}
259261
t, err = time.Parse(timeFormat[:len(str)], str)
260262
default:
261-
err = fmt.Errorf("Invalid Time-String: %s", str)
263+
err = fmt.Errorf("invalid time string: %s", str)
262264
return
263265
}
264266

@@ -307,7 +309,7 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
307309
loc,
308310
), nil
309311
}
310-
return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
312+
return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
311313
}
312314

313315
// zeroDateTime is used in formatBinaryDateTime to avoid an allocation
@@ -342,7 +344,7 @@ func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value
342344
switch len(src) {
343345
case 8, 12:
344346
default:
345-
return nil, fmt.Errorf("Invalid TIME-packet length %d", len(src))
347+
return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
346348
}
347349
// +2 to enable negative time and 100+ hours
348350
dst = make([]byte, 0, length+2)
@@ -376,7 +378,7 @@ func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value
376378
if length > 10 {
377379
t += "TIME"
378380
}
379-
return nil, fmt.Errorf("illegal %s-packet length %d", t, len(src))
381+
return nil, fmt.Errorf("illegal %s packet length %d", t, len(src))
380382
}
381383
dst = make([]byte, 0, length)
382384
// start with the date
@@ -642,7 +644,7 @@ func escapeBytesBackslash(buf, v []byte) []byte {
642644
pos += 2
643645
default:
644646
buf[pos] = c
645-
pos += 1
647+
pos++
646648
}
647649
}
648650

@@ -687,7 +689,7 @@ func escapeStringBackslash(buf []byte, v string) []byte {
687689
pos += 2
688690
default:
689691
buf[pos] = c
690-
pos += 1
692+
pos++
691693
}
692694
}
693695

0 commit comments

Comments
 (0)
Please sign in to comment.