@@ -496,12 +496,6 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
496
496
return val , nil
497
497
}
498
498
499
- // ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
500
- // collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
501
- // value is converted to a string and the default collation is used. If the value is already a string then no additional
502
- // conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
503
- // original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
504
- // you must allocate a new byte slice and pass that new one in.
505
499
// convertToLongTextString safely converts a value to string using LongText.Convert with nil checking
506
500
func convertToLongTextString (ctx context.Context , val interface {}) (string , error ) {
507
501
converted , _ , err := LongText .Convert (ctx , val )
@@ -525,6 +519,12 @@ func convertEnumToString(ctx context.Context, val interface{}, enumType sql.Enum
525
519
return convertToLongTextString (ctx , val )
526
520
}
527
521
522
+ // ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
523
+ // collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
524
+ // value is converted to a string and the default collation is used. If the value is already a string then no additional
525
+ // conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
526
+ // original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
527
+ // you must allocate a new byte slice and pass that new one in.
528
528
func ConvertToCollatedString (ctx context.Context , val interface {}, typ sql.Type ) (string , sql.CollationID , error ) {
529
529
var content string
530
530
var collation sql.CollationID
@@ -539,13 +539,9 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
539
539
content = strVal
540
540
} else if byteVal , ok := val .([]byte ); ok {
541
541
content = encodings .BytesToString (byteVal )
542
- } else if IsEnum ( typ ) {
542
+ } else if enumType , ok := typ .(sql. EnumType ); ok {
543
543
// Handle enum types in string context - return the string value, not the index
544
- if enumType , ok := typ .(sql.EnumType ); ok {
545
- content , err = convertEnumToString (ctx , val , enumType )
546
- } else {
547
- content , err = convertToLongTextString (ctx , val )
548
- }
544
+ content , err = convertEnumToString (ctx , val , enumType )
549
545
if err != nil {
550
546
return "" , sql .Collation_Unspecified , err
551
547
}
@@ -560,12 +556,11 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
560
556
// Handle enum types in string context even without collation
561
557
if IsEnum (typ ) {
562
558
if enumType , ok := typ .(sql.EnumType ); ok {
563
- if enumVal , ok := val .(uint16 ); ok {
564
- if enumStr , exists := enumType .At (int (enumVal )); exists {
565
- content = enumStr
566
- return content , collation , nil
567
- }
559
+ content , err = convertEnumToString (ctx , val , enumType )
560
+ if err != nil {
561
+ return "" , sql .Collation_Unspecified , err
568
562
}
563
+ return content , collation , nil
569
564
}
570
565
}
571
566
content , err = convertToLongTextString (ctx , val )
0 commit comments