فهرست منبع

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
Julien Schmidt 12 سال پیش
والد
کامیت
8f4b071ee8
5فایلهای تغییر یافته به همراه23 افزوده شده و 9 حذف شده
  1. 9 2
      README.md
  2. 0 1
      connection.go
  3. 10 0
      const.go
  4. 3 3
      driver_test.go
  5. 1 3
      packets.go

+ 9 - 2
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.

+ 0 - 1
connection.go

@@ -21,7 +21,6 @@ import (
 type mysqlConn struct {
 	cfg              *config
 	flags            clientFlag
-	charset          byte
 	cipher           []byte
 	netConn          net.Conn
 	buf              *buffer

+ 10 - 0
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
+)

+ 3 - 3
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)
 	}

+ 1 - 3
packets.go

@@ -178,8 +178,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]
@@ -254,7 +252,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