diff --git a/README.md b/README.md index 0d166b9d8..e5db9de9c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 ``` ``` @@ -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. @@ -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. diff --git a/connection.go b/connection.go index 6a2316c8f..8436f0383 100644 --- a/connection.go +++ b/connection.go @@ -21,7 +21,6 @@ import ( type mysqlConn struct { cfg *config flags clientFlag - charset byte cipher []byte netConn net.Conn buf *buffer diff --git a/const.go b/const.go index 3822e3d18..852fec55f 100644 --- a/const.go +++ b/const.go @@ -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 +) diff --git a/driver_test.go b/driver_test.go index 74a69d2a8..51546f024 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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 @@ -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) } @@ -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) } diff --git a/packets.go b/packets.go index 5ad3dee16..dbd512f92 100644 --- a/packets.go +++ b/packets.go @@ -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] @@ -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