@@ -17,6 +17,7 @@ package types
1717import (
1818 "context"
1919 "fmt"
20+ "github.com/dolthub/go-mysql-server/sql/values"
2021 "math"
2122 "math/bits"
2223 "reflect"
@@ -261,6 +262,35 @@ func (t SetType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Val
261262 return sqltypes .MakeTrusted (sqltypes .Set , val ), nil
262263}
263264
265+ func (t SetType ) ToSQLValue (ctx * sql.Context , v sql.Value , dest []byte ) (sqltypes.Value , error ) {
266+ if v .IsNull () {
267+ return sqltypes .NULL , nil
268+ }
269+
270+ bits := values .ReadUint64 (v .Val )
271+ value , err := t .BitsToString (bits )
272+ if err != nil {
273+ return sqltypes.Value {}, err
274+ }
275+
276+ resultCharset := ctx .GetCharacterSetResults ()
277+ if resultCharset == sql .CharacterSet_Unspecified || resultCharset == sql .CharacterSet_binary {
278+ resultCharset = t .collation .CharacterSet ()
279+ }
280+
281+ // TODO: write append style encoder
282+ res , ok := resultCharset .Encoder ().Encode (encodings .StringToBytes (value )) // TODO: use unsafe string to byte
283+ if ! ok {
284+ if len (value ) > 50 {
285+ value = value [:50 ]
286+ }
287+ value = strings .ToValidUTF8 (value , string (utf8 .RuneError ))
288+ return sqltypes.Value {}, sql .ErrCharSetFailedToEncode .New (resultCharset .Name (), utf8 .ValidString (value ), value )
289+ }
290+
291+ return sqltypes .MakeTrusted (sqltypes .Set , res ), nil
292+ }
293+
264294// String implements Type interface.
265295func (t SetType ) String () string {
266296 return t .StringWithTableCollation (sql .Collation_Default )
0 commit comments