Skip to content

Commit fc441ea

Browse files
committed
added set conversion to ConvertToCollatedString
1 parent 16be347 commit fc441ea

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

sql/types/strings.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func convertToLongTextString(ctx context.Context, val interface{}) (string, erro
509509
}
510510

511511
// convertEnumToString converts an enum value to its string representation
512-
func convertEnumToString(ctx context.Context, val interface{}, enumType sql.EnumType) (string, error) {
512+
func convertEnumToString(ctx context.Context, val interface{}, enumType EnumType) (string, error) {
513513
if enumVal, ok := val.(uint16); ok {
514514
if enumStr, exists := enumType.At(int(enumVal)); exists {
515515
return enumStr, nil
@@ -519,6 +519,14 @@ func convertEnumToString(ctx context.Context, val interface{}, enumType sql.Enum
519519
return convertToLongTextString(ctx, val)
520520
}
521521

522+
// convertSetToString converts a set value to its string representation
523+
func convertSetToString(ctx context.Context, val interface{}, setType SetType) (string, error) {
524+
if setVal, ok := val.(uint64); ok {
525+
return setType.BitsToString(setVal)
526+
}
527+
return convertToLongTextString(ctx, val)
528+
}
529+
522530
// ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
523531
// collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
524532
// value is converted to a string and the default collation is used. If the value is already a string then no additional
@@ -539,28 +547,27 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
539547
content = strVal
540548
} else if byteVal, ok := val.([]byte); ok {
541549
content = encodings.BytesToString(byteVal)
542-
} else if enumType, ok := typ.(sql.EnumType); ok {
543-
// Handle enum types in string context - return the string value, not the index
544-
content, err = convertEnumToString(ctx, val, enumType)
545-
if err != nil {
546-
return "", sql.Collation_Unspecified, err
547-
}
548550
} else {
549-
content, err = convertToLongTextString(ctx, val)
550-
if err != nil {
551-
return "", sql.Collation_Unspecified, err
551+
switch typ := typ.(type) {
552+
case EnumType:
553+
content, err = convertEnumToString(ctx, val, typ)
554+
if err != nil {
555+
return "", sql.Collation_Unspecified, err
556+
}
557+
case SetType:
558+
content, err = convertSetToString(ctx, val, typ)
559+
if err != nil {
560+
return "", sql.Collation_Unspecified, err
561+
}
562+
default:
563+
content, err = convertToLongTextString(ctx, val)
564+
if err != nil {
565+
return "", sql.Collation_Unspecified, err
566+
}
552567
}
553568
}
554569
} else {
555570
collation = sql.Collation_Default
556-
// Handle enum types in string context even without collation
557-
if enumType, ok := typ.(sql.EnumType); ok {
558-
content, err = convertEnumToString(ctx, val, enumType)
559-
if err != nil {
560-
return "", sql.Collation_Unspecified, err
561-
}
562-
return content, collation, nil
563-
}
564571
content, err = convertToLongTextString(ctx, val)
565572
if err != nil {
566573
return "", sql.Collation_Unspecified, err

0 commit comments

Comments
 (0)