Skip to content

Commit c4063e5

Browse files
committed
Merge branch 'strict' into columntype
2 parents 57d5975 + cf724b8 commit c4063e5

26 files changed

+1456
-337
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
Icon?
77
ehthumbs.db
88
Thumbs.db
9+
.idea

.travis.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
sudo: false
22
language: go
33
go:
4-
- 1.2
5-
- 1.3
6-
- 1.4
74
- 1.5
85
- 1.6
96
- 1.7
107
- 1.8
8+
- 1.9
119
- tip
1210

11+
before_install:
12+
- go get golang.org/x/tools/cmd/cover
13+
- go get github.com/mattn/goveralls
14+
15+
before_script:
16+
- echo -e "[server]\ninnodb_log_file_size=256MB\ninnodb_buffer_pool_size=512MB\nmax_allowed_packet=16MB" | sudo tee -a /etc/mysql/my.cnf
17+
- sudo service mysql restart
18+
- .travis/wait_mysql.sh
19+
- mysql -e 'create database gotest;'
20+
1321
matrix:
1422
include:
1523
- env: DB=MYSQL57
1624
sudo: required
1725
dist: trusty
18-
go: 1.8
26+
go: 1.9
1927
services:
2028
- docker
2129
before_install:
30+
- go get golang.org/x/tools/cmd/cover
31+
- go get github.com/mattn/goveralls
2232
- docker pull mysql:5.7
2333
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
2434
mysql:5.7 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB
@@ -35,10 +45,12 @@ matrix:
3545
- env: DB=MARIA55
3646
sudo: required
3747
dist: trusty
38-
go: 1.8
48+
go: 1.9
3949
services:
4050
- docker
4151
before_install:
52+
- go get golang.org/x/tools/cmd/cover
53+
- go get github.com/mattn/goveralls
4254
- docker pull mariadb:5.5
4355
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
4456
mariadb:5.5 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB
@@ -55,10 +67,12 @@ matrix:
5567
- env: DB=MARIA10_1
5668
sudo: required
5769
dist: trusty
58-
go: 1.8
70+
go: 1.9
5971
services:
6072
- docker
6173
before_install:
74+
- go get golang.org/x/tools/cmd/cover
75+
- go get github.com/mattn/goveralls
6276
- docker pull mariadb:10.1
6377
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
6478
mariadb:10.1 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB
@@ -72,10 +86,8 @@ matrix:
7286
- export MYSQL_TEST_ADDR=127.0.0.1:3307
7387
- export MYSQL_TEST_CONCURRENT=1
7488

75-
76-
before_script:
77-
- mysql -e 'create database gotest;'
7889
script:
79-
- go test -v
90+
- go test -v -covermode=count -coverprofile=coverage.out
8091
- go vet ./...
8192
- test -z "$(gofmt -d -s . | tee /dev/stderr)"
93+
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci

AUTHORS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,33 @@
1212
# Individual Persons
1313

1414
Aaron Hopkins <go-sql-driver at die.net>
15+
Achille Roussel <achille.roussel at gmail.com>
1516
Arne Hormann <arnehormann at gmail.com>
1617
Asta Xie <xiemengjun at gmail.com>
18+
Bulat Gaifullin <gaifullinbf at gmail.com>
1719
Carlos Nieto <jose.carlos at menteslibres.net>
1820
Chris Moos <chris at tech9computers.com>
1921
Daniel Nichter <nil at codenode.com>
2022
Daniël van Eeden <git at myname.nl>
23+
Dave Protasowski <dprotaso at gmail.com>
2124
DisposaBoy <disposaboy at dby.me>
2225
Egor Smolyakov <egorsmkv at gmail.com>
26+
Evan Shaw <evan at vendhq.com>
2327
Frederick Mayle <frederickmayle at gmail.com>
2428
Gustavo Kristic <gkristic at gmail.com>
2529
Hanno Braun <mail at hannobraun.com>
2630
Henri Yandell <flamefew at gmail.com>
2731
Hirotaka Yamamoto <ymmt2005 at gmail.com>
32+
ICHINOSE Shogo <shogo82148 at gmail.com>
2833
INADA Naoki <songofacandy at gmail.com>
2934
Jacek Szwec <szwec.jacek at gmail.com>
3035
James Harr <james.harr at gmail.com>
36+
Jeff Hodges <jeff at somethingsimilar.com>
3137
Jian Zhen <zhenjl at gmail.com>
3238
Joshua Prunier <joshua.prunier at gmail.com>
3339
Julien Lefevre <julien.lefevr at gmail.com>
3440
Julien Schmidt <go-sql-driver at julienschmidt.com>
41+
Justin Nuß <nuss.justin at gmail.com>
3542
Kamil Dziedzic <kamil at klecza.pl>
3643
Kevin Malachowski <kevin at chowski.com>
3744
Lennart Rudolph <lrudolph at hmc.edu>
@@ -40,12 +47,16 @@ Lion Yang <lion at aosc.xyz>
4047
Luca Looz <luca.looz92 at gmail.com>
4148
Lucas Liu <extrafliu at gmail.com>
4249
Luke Scott <luke at webconnex.com>
50+
Maciej Zimnoch <maciej.zimnoch@codilime.com>
4351
Michael Woolnough <michael.woolnough at gmail.com>
4452
Nicola Peduzzi <thenikso at gmail.com>
4553
Olivier Mengué <dolmen at cpan.org>
54+
oscarzhao <oscarzhaosl at gmail.com>
4655
Paul Bonser <misterpib at gmail.com>
4756
Peter Schultz <peter.schultz at classmarkets.com>
57+
Rebecca Chin <rchin at pivotal.io>
4858
Runrioter Wung <runrioter at gmail.com>
59+
Shuode Li <elemount at qq.com>
4960
Soroush Pour <me at soroushjp.com>
5061
Stan Putrya <root.vagner at gmail.com>
5162
Stanley Gunawan <gunawan.stanley at gmail.com>
@@ -58,4 +69,6 @@ Zhenye Xie <xiezhenye at gmail.com>
5869

5970
Barracuda Networks, Inc.
6071
Google Inc.
72+
Keybase Inc.
73+
Pivotal Inc.
6174
Stripe Inc.

README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) pac
1919
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
2020
* [time.Time support](#timetime-support)
2121
* [Unicode support](#unicode-support)
22+
* [context.Context Support](#contextcontext-support)
2223
* [Testing / Development](#testing--development)
2324
* [License](#license)
2425

@@ -38,15 +39,15 @@ A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) pac
3839
* Optional placeholder interpolation
3940

4041
## Requirements
41-
* Go 1.2 or higher
42+
* Go 1.5 or higher
4243
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
4344

4445
---------------------------------------
4546

4647
## Installation
4748
Simple install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH "GOPATH") with the [go tool](https://golang.org/cmd/go/ "go command") from shell:
4849
```bash
49-
$ go get github.com/go-sql-driver/mysql
50+
$ go get -u github.com/go-sql-driver/mysql
5051
```
5152
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.
5253

@@ -100,7 +101,8 @@ See [net.Dial](https://golang.org/pkg/net/#Dial) for more information which netw
100101
In general you should use an Unix domain socket if available and TCP otherwise for best performance.
101102

102103
#### Address
103-
For TCP and UDP networks, addresses have the form `host:port`.
104+
For TCP and UDP networks, addresses have the form `host[:port]`.
105+
If `port` is omitted, the default port will be used.
104106
If `host` is a literal IPv6 address, it must be enclosed in square brackets.
105107
The functions [net.JoinHostPort](https://golang.org/pkg/net/#JoinHostPort) and [net.SplitHostPort](https://golang.org/pkg/net/#SplitHostPort) manipulate addresses in this form.
106108

@@ -137,9 +139,9 @@ Default: false
137139
```
138140
Type: bool
139141
Valid Values: true, false
140-
Default: false
142+
Default: true
141143
```
142-
`allowNativePasswords=true` allows the usage of the mysql native password method.
144+
`allowNativePasswords=false` disallows the usage of MySQL native password method.
143145

144146
##### `allowOldPasswords`
145147

@@ -267,19 +269,30 @@ Default: 0
267269

268270
I/O read timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
269271

270-
##### `strict`
272+
##### `rejectReadOnly`
271273

272274
```
273275
Type: bool
274276
Valid Values: true, false
275277
Default: false
276278
```
277279

278-
`strict=true` enables a driver-side strict mode in which MySQL warnings are treated as errors. This mode should not be used in production as it may lead to data corruption in certain situations.
279280

280-
A server-side strict mode, which is safe for production use, can be set via the [`sql_mode`](https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html) system variable.
281+
`rejectreadOnly=true` causes the driver to reject read-only connections. This
282+
is for a possible race condition during an automatic failover, where the mysql
283+
client gets connected to a read-only replica after the failover.
284+
285+
Note that this should be a fairly rare case, as an automatic failover normally
286+
happens when the primary is down, and the race condition shouldn't happen
287+
unless it comes back up online as soon as the failover is kicked off. On the
288+
other hand, when this happens, a MySQL application can get stuck on a
289+
read-only connection until restarted. It is however fairly easy to reproduce,
290+
for example, using a manual failover on AWS Aurora's MySQL-compatible cluster.
291+
292+
If you are not relying on read-only transactions to reject writes that aren't
293+
supposed to happen, setting this on some MySQL providers (such as AWS Aurora)
294+
is safer for failovers.
281295

282-
By default MySQL also treats notes as warnings. Use [`sql_notes=false`](http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sql_notes) to ignore notes.
283296

284297
##### `timeout`
285298

@@ -290,6 +303,7 @@ Default: OS default
290303

291304
Timeout for establishing connections, aka dial timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
292305

306+
293307
##### `tls`
294308

295309
```
@@ -300,6 +314,7 @@ Default: false
300314

301315
`tls=true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
302316

317+
303318
##### `writeTimeout`
304319

305320
```
@@ -318,9 +333,9 @@ Any other parameters are interpreted as system variables:
318333
* `<string_var>=%27<value>%27`: `SET <string_var>='<value>'`
319334

320335
Rules:
321-
* The values for string variables must be quoted with '
336+
* The values for string variables must be quoted with `'`.
322337
* The values must also be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed!
323-
(which implies values of string variables must be wrapped with `%27`)
338+
(which implies values of string variables must be wrapped with `%27`).
324339

325340
Examples:
326341
* `autocommit=1`: `SET autocommit=1`
@@ -400,7 +415,7 @@ See the [godoc of Go-MySQL-Driver](https://godoc.org/github.com/go-sql-driver/my
400415

401416

402417
### `time.Time` support
403-
The default internal output type of MySQL `DATE` and `DATETIME` values is `[]byte` which allows you to scan the value into a `[]byte`, `string` or `sql.RawBytes` variable in your programm.
418+
The default internal output type of MySQL `DATE` and `DATETIME` values is `[]byte` which allows you to scan the value into a `[]byte`, `string` or `sql.RawBytes` variable in your program.
404419

405420
However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` variables, which is the logical opposite in Go to `DATE` and `DATETIME` in MySQL. You can do that by changing the internal output type from `[]byte` to `time.Time` with the DSN parameter `parseTime=true`. You can set the default [`time.Time` location](https://golang.org/pkg/time/#Location) with the `loc` DSN parameter.
406421

@@ -418,6 +433,9 @@ Version 1.0 of the driver recommended adding `&charset=utf8` (alias for `SET NAM
418433

419434
See http://dev.mysql.com/doc/refman/5.7/en/charset-unicode.html for more details on MySQL's Unicode support.
420435

436+
## `context.Context` Support
437+
Go 1.8 added `database/sql` support for `context.Context`. This driver supports query timeouts and cancellation via contexts.
438+
See [context support in the database/sql package](https://golang.org/doc/go1.8#database_sql) for more details.
421439

422440
## Testing / Development
423441
To run the driver tests you may need to adjust the configuration. See the [Testing Wiki-Page](https://github.com/go-sql-driver/mysql/wiki/Testing "Testing") for details.
@@ -437,13 +455,13 @@ Mozilla summarizes the license scope as follows:
437455
438456

439457
That means:
440-
* You can **use** the **unchanged** source code both in private and commercially
441-
* When distributing, you **must publish** the source code of any **changed files** licensed under the MPL 2.0 under a) the MPL 2.0 itself or b) a compatible license (e.g. GPL 3.0 or Apache License 2.0)
442-
* You **needn't publish** the source code of your library as long as the files licensed under the MPL 2.0 are **unchanged**
458+
* You can **use** the **unchanged** source code both in private and commercially.
459+
* When distributing, you **must publish** the source code of any **changed files** licensed under the MPL 2.0 under a) the MPL 2.0 itself or b) a compatible license (e.g. GPL 3.0 or Apache License 2.0).
460+
* You **needn't publish** the source code of your library as long as the files licensed under the MPL 2.0 are **unchanged**.
443461

444462
Please read the [MPL 2.0 FAQ](https://www.mozilla.org/en-US/MPL/2.0/FAQ/) if you have further questions regarding the license.
445463

446-
You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE)
464+
You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE).
447465

448466
![Go Gopher and MySQL Dolphin](https://raw.github.com/wiki/go-sql-driver/mysql/go-mysql-driver_m.jpg "Golang Gopher transporting the MySQL Dolphin in a wheelbarrow")
449467

benchmark_go18_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build go1.8
10+
11+
package mysql
12+
13+
import (
14+
"context"
15+
"database/sql"
16+
"fmt"
17+
"runtime"
18+
"testing"
19+
)
20+
21+
func benchmarkQueryContext(b *testing.B, db *sql.DB, p int) {
22+
ctx, cancel := context.WithCancel(context.Background())
23+
defer cancel()
24+
db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
25+
26+
tb := (*TB)(b)
27+
stmt := tb.checkStmt(db.PrepareContext(ctx, "SELECT val FROM foo WHERE id=?"))
28+
defer stmt.Close()
29+
30+
b.SetParallelism(p)
31+
b.ReportAllocs()
32+
b.ResetTimer()
33+
b.RunParallel(func(pb *testing.PB) {
34+
var got string
35+
for pb.Next() {
36+
tb.check(stmt.QueryRow(1).Scan(&got))
37+
if got != "one" {
38+
b.Fatalf("query = %q; want one", got)
39+
}
40+
}
41+
})
42+
}
43+
44+
func BenchmarkQueryContext(b *testing.B) {
45+
db := initDB(b,
46+
"DROP TABLE IF EXISTS foo",
47+
"CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
48+
`INSERT INTO foo VALUES (1, "one")`,
49+
`INSERT INTO foo VALUES (2, "two")`,
50+
)
51+
defer db.Close()
52+
for _, p := range []int{1, 2, 3, 4} {
53+
b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
54+
benchmarkQueryContext(b, db, p)
55+
})
56+
}
57+
}
58+
59+
func benchmarkExecContext(b *testing.B, db *sql.DB, p int) {
60+
ctx, cancel := context.WithCancel(context.Background())
61+
defer cancel()
62+
db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
63+
64+
tb := (*TB)(b)
65+
stmt := tb.checkStmt(db.PrepareContext(ctx, "DO 1"))
66+
defer stmt.Close()
67+
68+
b.SetParallelism(p)
69+
b.ReportAllocs()
70+
b.ResetTimer()
71+
b.RunParallel(func(pb *testing.PB) {
72+
for pb.Next() {
73+
if _, err := stmt.ExecContext(ctx); err != nil {
74+
b.Fatal(err)
75+
}
76+
}
77+
})
78+
}
79+
80+
func BenchmarkExecContext(b *testing.B) {
81+
db := initDB(b,
82+
"DROP TABLE IF EXISTS foo",
83+
"CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
84+
`INSERT INTO foo VALUES (1, "one")`,
85+
`INSERT INTO foo VALUES (2, "two")`,
86+
)
87+
defer db.Close()
88+
for _, p := range []int{1, 2, 3, 4} {
89+
b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
90+
benchmarkQueryContext(b, db, p)
91+
})
92+
}
93+
}

benchmark_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ func initDB(b *testing.B, queries ...string) *sql.DB {
4848
db := tb.checkDB(sql.Open("mysql", dsn))
4949
for _, query := range queries {
5050
if _, err := db.Exec(query); err != nil {
51-
if w, ok := err.(MySQLWarnings); ok {
52-
b.Logf("warning on %q: %v", query, w)
53-
} else {
54-
b.Fatalf("error on %q: %v", query, err)
55-
}
51+
b.Fatalf("error on %q: %v", query, err)
5652
}
5753
}
5854
return db

0 commit comments

Comments
 (0)