Skip to content

Use UTF8 by default #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
* [Examples](#examples)
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
* [time.Time support](#timetime-support)
* [Unicode support](#unicode-support)
* [Testing / Development](#testing--development)
* [License](#license)

Expand Down Expand Up @@ -126,11 +127,11 @@ user@unix(/path/to/socket)/dbname
```

```
user:password@tcp(localhost:5555)/dbname?charset=utf8&autocommit=true
user:password@tcp(localhost:5555)/dbname?autocommit=true
```

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

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

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


### `time.Time` support
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.

Expand All @@ -164,6 +166,11 @@ However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` v
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`.


### Unicode support
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.

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


## Testing / Development
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.
Expand Down
1 change: 0 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
type mysqlConn struct {
cfg *config
flags clientFlag
charset byte
cipher []byte
netConn net.Conn
buf *buffer
Expand Down
10 changes: 10 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ const (
flagUnknown3
flagUnknown4
)

const (
collation_ascii_general_ci byte = 11
collation_utf8_general_ci byte = 33
collation_utf8mb4_general_ci byte = 45
collation_utf8mb4_bin byte = 46
collation_latin1_general_ci byte = 48
collation_binary byte = 63
collation_utf8mb4_unicode_ci byte = 224
)
6 changes: 3 additions & 3 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
dbname := env("MYSQL_TEST_DBNAME", "gotest")
charset = "charset=utf8"
netAddr = fmt.Sprintf("%s(%s)", prot, addr)
dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&strict=true&"+charset, user, pass, netAddr, dbname)
dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&strict=true", user, pass, netAddr, dbname)
c, err := net.Dial(prot, addr)
if err == nil {
available = true
Expand Down Expand Up @@ -103,7 +103,7 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)

func TestCharset(t *testing.T) {
mustSetCharset := func(charsetParam, expected string) {
db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
db, err := sql.Open("mysql", dsn+"&"+charsetParam)
if err != nil {
t.Fatalf("Error on Open: %v", err)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestFailingCharset(t *testing.T) {
t.Logf("MySQL-Server not running on %s. Skipping TestFailingCharset", netAddr)
return
}
db, err := sql.Open("mysql", strings.Replace(dsn, charset, "charset=none", 1))
db, err := sql.Open("mysql", dsn+"&charset=none")
if err != nil {
t.Fatalf("Error on Open: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ func (mc *mysqlConn) readInitPacket() (err error) {

if len(data) > pos {
// character set [1 byte]
mc.charset = data[pos]

// status flags [2 bytes]
// capability flags (upper 2 bytes) [2 bytes]
// length of auth-plugin-data [1 byte]
Expand Down Expand Up @@ -252,7 +250,7 @@ func (mc *mysqlConn) writeAuthPacket() error {
//data[11] = 0x00

// Charset [1 byte]
data[12] = mc.charset
data[12] = collation_utf8_general_ci

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