vitess.io/vitess@v0.16.2/go/mysql/charset.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "fmt" 21 "strconv" 22 23 binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" 24 "vitess.io/vitess/go/vt/proto/vtrpc" 25 "vitess.io/vitess/go/vt/vterrors" 26 ) 27 28 // This file contains utility methods for Conn objects. Only useful on the client 29 // side. 30 31 // ExecuteFetchMap returns a map from column names to cell data for a query 32 // that should return exactly 1 row. 33 func ExecuteFetchMap(conn *Conn, query string) (map[string]string, error) { 34 qr, err := conn.ExecuteFetch(query, 1, true) 35 if err != nil { 36 return nil, err 37 } 38 if len(qr.Rows) != 1 { 39 return nil, vterrors.Errorf(vtrpc.Code_OUT_OF_RANGE, "query %#v returned %d rows, expected 1", query, len(qr.Rows)) 40 } 41 if len(qr.Fields) != len(qr.Rows[0]) { 42 return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "query %#v returned %d column names, expected %d", query, len(qr.Fields), len(qr.Rows[0])) 43 } 44 45 rowMap := make(map[string]string) 46 for i, value := range qr.Rows[0] { 47 rowMap[qr.Fields[i].Name] = value.ToString() 48 } 49 return rowMap, nil 50 } 51 52 // GetCharset returns the current numerical values of the per-session character 53 // set variables. 54 func GetCharset(conn *Conn) (*binlogdatapb.Charset, error) { 55 // character_set_client 56 row, err := ExecuteFetchMap(conn, "SHOW COLLATION WHERE `charset`=@@session.character_set_client AND `default`='Yes'") 57 if err != nil { 58 return nil, err 59 } 60 client, err := strconv.ParseInt(row["Id"], 10, 16) 61 if err != nil { 62 return nil, err 63 } 64 65 // collation_connection 66 row, err = ExecuteFetchMap(conn, "SHOW COLLATION WHERE `collation`=@@session.collation_connection") 67 if err != nil { 68 return nil, err 69 } 70 connection, err := strconv.ParseInt(row["Id"], 10, 16) 71 if err != nil { 72 return nil, err 73 } 74 75 // collation_server 76 row, err = ExecuteFetchMap(conn, "SHOW COLLATION WHERE `collation`=@@session.collation_server") 77 if err != nil { 78 return nil, err 79 } 80 server, err := strconv.ParseInt(row["Id"], 10, 16) 81 if err != nil { 82 return nil, err 83 } 84 85 return &binlogdatapb.Charset{ 86 Client: int32(client), 87 Conn: int32(connection), 88 Server: int32(server), 89 }, nil 90 } 91 92 // SetCharset changes the per-session character set variables. 93 func SetCharset(conn *Conn, cs *binlogdatapb.Charset) error { 94 sql := fmt.Sprintf( 95 "SET @@session.character_set_client=%d, @@session.collation_connection=%d, @@session.collation_server=%d", 96 cs.Client, cs.Conn, cs.Server) 97 _, err := conn.ExecuteFetch(sql, 1, false) 98 return err 99 }