Skip to content

Commit 8f4b071

Browse files
committed
Merge pull request #93 from go-sql-driver/charset
Use UTF8 by default https://github.com/go-sql-driver/mysql/blob/master/README.md#unicode-support
2 parents d3fd057 + 48817fd commit 8f4b071

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
2121
* [Examples](#examples)
2222
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
2323
* [time.Time support](#timetime-support)
24+
* [Unicode support](#unicode-support)
2425
* [Testing / Development](#testing--development)
2526
* [License](#license)
2627

@@ -126,11 +127,11 @@ user@unix(/path/to/socket)/dbname
126127
```
127128

128129
```
129-
user:password@tcp(localhost:5555)/dbname?charset=utf8&autocommit=true
130+
user:password@tcp(localhost:5555)/dbname?autocommit=true
130131
```
131132

132133
```
133-
user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?charset=utf8mb4,utf8
134+
user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?tls=skip-verify&charset=utf8mb4,utf8
134135
```
135136

136137
```
@@ -154,6 +155,7 @@ To use a `io.Reader` a handler function must be registered with `mysql.RegisterR
154155

155156
See also the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation")
156157

158+
157159
### `time.Time` support
158160
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.
159161

@@ -164,6 +166,11 @@ However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` v
164166
Alternatively you can use the [`NullTime`](http://godoc.org/github.com/go-sql-driver/mysql#NullTime) type as the scan destination, which works with both `time.Time` and `string` / `[]byte`.
165167

166168

169+
### Unicode support
170+
Since version 1.1 Go-MySQL-Driver automatically uses the collation `utf8_general_ci` by default. Adding `&charset=utf8` (alias for `SET NAMES utf8`) to the DSN is not necessary anymore in most cases.
171+
172+
See http://dev.mysql.com/doc/refman/5.7/en/charset-unicode.html for more details on MySQL's Unicode support.
173+
167174

168175
## Testing / Development
169176
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.

connection.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
type mysqlConn struct {
2222
cfg *config
2323
flags clientFlag
24-
charset byte
2524
cipher []byte
2625
netConn net.Conn
2726
buf *buffer

const.go

+10
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,13 @@ const (
131131
flagUnknown3
132132
flagUnknown4
133133
)
134+
135+
const (
136+
collation_ascii_general_ci byte = 11
137+
collation_utf8_general_ci byte = 33
138+
collation_utf8mb4_general_ci byte = 45
139+
collation_utf8mb4_bin byte = 46
140+
collation_latin1_general_ci byte = 48
141+
collation_binary byte = 63
142+
collation_utf8mb4_unicode_ci byte = 224
143+
)

driver_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func init() {
4444
dbname := env("MYSQL_TEST_DBNAME", "gotest")
4545
charset = "charset=utf8"
4646
netAddr = fmt.Sprintf("%s(%s)", prot, addr)
47-
dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&strict=true&"+charset, user, pass, netAddr, dbname)
47+
dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&strict=true", user, pass, netAddr, dbname)
4848
c, err := net.Dial(prot, addr)
4949
if err == nil {
5050
available = true
@@ -103,7 +103,7 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)
103103

104104
func TestCharset(t *testing.T) {
105105
mustSetCharset := func(charsetParam, expected string) {
106-
db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
106+
db, err := sql.Open("mysql", dsn+"&"+charsetParam)
107107
if err != nil {
108108
t.Fatalf("Error on Open: %v", err)
109109
}
@@ -146,7 +146,7 @@ func TestFailingCharset(t *testing.T) {
146146
t.Logf("MySQL-Server not running on %s. Skipping TestFailingCharset", netAddr)
147147
return
148148
}
149-
db, err := sql.Open("mysql", strings.Replace(dsn, charset, "charset=none", 1))
149+
db, err := sql.Open("mysql", dsn+"&charset=none")
150150
if err != nil {
151151
t.Fatalf("Error on Open: %v", err)
152152
}

packets.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ func (mc *mysqlConn) readInitPacket() (err error) {
178178

179179
if len(data) > pos {
180180
// character set [1 byte]
181-
mc.charset = data[pos]
182-
183181
// status flags [2 bytes]
184182
// capability flags (upper 2 bytes) [2 bytes]
185183
// length of auth-plugin-data [1 byte]
@@ -254,7 +252,7 @@ func (mc *mysqlConn) writeAuthPacket() error {
254252
//data[11] = 0x00
255253

256254
// Charset [1 byte]
257-
data[12] = mc.charset
255+
data[12] = collation_utf8_general_ci
258256

259257
// SSL Connection Request Packet
260258
// http://dev.mysql.com/doc/internals/en/connection-phase.html#packet-Protocol::SSLRequest

0 commit comments

Comments
 (0)