vitess.io/vitess@v0.16.2/go/mysql/collations/remote/charset.go (about)

     1  /*
     2  Copyright 2021 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 remote
    18  
    19  import (
    20  	"encoding/hex"
    21  	"fmt"
    22  	"io"
    23  	"sync"
    24  
    25  	"vitess.io/vitess/go/bytes2"
    26  	"vitess.io/vitess/go/mysql"
    27  	"vitess.io/vitess/go/mysql/collations/internal/charset"
    28  )
    29  
    30  type Charset struct {
    31  	name string
    32  
    33  	mu   *sync.Mutex
    34  	conn *mysql.Conn
    35  	sql  bytes2.Buffer
    36  	hex  io.Writer
    37  }
    38  
    39  var _ charset.Charset = (*Charset)(nil)
    40  
    41  func makeRemoteCharset(conn *mysql.Conn, mu *sync.Mutex, csname string) *Charset {
    42  	cs := &Charset{
    43  		name: csname,
    44  		mu:   mu,
    45  		conn: conn,
    46  	}
    47  	cs.hex = hex.NewEncoder(&cs.sql)
    48  	return cs
    49  }
    50  
    51  func NewCharset(conn *mysql.Conn, csname string) *Charset {
    52  	return makeRemoteCharset(conn, &sync.Mutex{}, csname)
    53  }
    54  
    55  func (c *Charset) Name() string {
    56  	return c.name
    57  }
    58  
    59  func (c *Charset) IsSuperset(_ charset.Charset) bool {
    60  	return false
    61  }
    62  
    63  func (c *Charset) SupportsSupplementaryChars() bool {
    64  	return true
    65  }
    66  
    67  func (c *Charset) EncodeRune(dst []byte, r rune) int {
    68  	panic("unsupported: EncodeRune in remote.Charset (use Charset.Convert directly)")
    69  }
    70  
    71  func (c *Charset) DecodeRune(bytes []byte) (rune, int) {
    72  	panic("unsupported: DecodeRune in remote.Charset (use Charset.Convert directly)")
    73  }
    74  
    75  func (c *Charset) performConversion(dst []byte, dstCharset string, src []byte, srcCharset string) ([]byte, error) {
    76  	c.mu.Lock()
    77  	defer c.mu.Unlock()
    78  
    79  	c.sql.Reset()
    80  	c.sql.WriteString("SELECT CAST(CONVERT(_")
    81  	c.sql.WriteString(srcCharset)
    82  	c.sql.WriteString(" X'")
    83  	c.hex.Write(src)
    84  	c.sql.WriteString("' USING ")
    85  	c.sql.WriteString(dstCharset)
    86  	c.sql.WriteString(") AS binary)")
    87  
    88  	res, err := c.conn.ExecuteFetch(c.sql.StringUnsafe(), 1, false)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	if len(res.Rows) != 1 {
    93  		return nil, fmt.Errorf("unexpected result from MySQL: %d rows returned", len(res.Rows))
    94  	}
    95  	result, err := res.Rows[0][0].ToBytes()
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	if dst != nil {
   100  		return append(dst, result...), nil
   101  	}
   102  	return result, nil
   103  }
   104  
   105  func (c *Charset) EncodeFromUTF8(dst, src []byte) ([]byte, error) {
   106  	return c.performConversion(dst, c.name, src, "utf8mb4")
   107  }
   108  
   109  func (c *Charset) DecodeToUTF8(dst, src []byte) ([]byte, error) {
   110  	return c.performConversion(dst, "utf8mb4", src, c.name)
   111  }
   112  
   113  func (c *Charset) Convert(dst, src []byte, srcCharset charset.Charset) ([]byte, error) {
   114  	return c.performConversion(dst, c.name, src, srcCharset.Name())
   115  }