@@ -502,6 +502,29 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
502
502
// conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
503
503
// original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
504
504
// you must allocate a new byte slice and pass that new one in.
505
+ // convertToLongTextString safely converts a value to string using LongText.Convert with nil checking
506
+ func convertToLongTextString (ctx context.Context , val interface {}) (string , error ) {
507
+ converted , _ , err := LongText .Convert (ctx , val )
508
+ if err != nil {
509
+ return "" , err
510
+ }
511
+ if converted == nil {
512
+ return "" , nil
513
+ }
514
+ return converted .(string ), nil
515
+ }
516
+
517
+ // convertEnumToString converts an enum value to its string representation
518
+ func convertEnumToString (ctx context.Context , val interface {}, enumType sql.EnumType ) (string , error ) {
519
+ if enumVal , ok := val .(uint16 ); ok {
520
+ if enumStr , exists := enumType .At (int (enumVal )); exists {
521
+ return enumStr , nil
522
+ }
523
+ return "" , nil
524
+ }
525
+ return convertToLongTextString (ctx , val )
526
+ }
527
+
505
528
func ConvertToCollatedString (ctx context.Context , val interface {}, typ sql.Type ) (string , sql.CollationID , error ) {
506
529
var content string
507
530
var collation sql.CollationID
@@ -519,43 +542,17 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
519
542
} else if IsEnum (typ ) {
520
543
// Handle enum types in string context - return the string value, not the index
521
544
if enumType , ok := typ .(sql.EnumType ); ok {
522
- if enumVal , ok := val .(uint16 ); ok {
523
- if enumStr , exists := enumType .At (int (enumVal )); exists {
524
- content = enumStr
525
- } else {
526
- content = ""
527
- }
528
- } else {
529
- val , _ , err = LongText .Convert (ctx , val )
530
- if err != nil {
531
- return "" , sql .Collation_Unspecified , err
532
- }
533
- if val == nil {
534
- content = ""
535
- } else {
536
- content = val .(string )
537
- }
538
- }
545
+ content , err = convertEnumToString (ctx , val , enumType )
539
546
} else {
540
- val , _ , err = LongText .Convert (ctx , val )
541
- if err != nil {
542
- return "" , sql .Collation_Unspecified , err
543
- }
544
- if val == nil {
545
- content = ""
546
- } else {
547
- content = val .(string )
548
- }
547
+ content , err = convertToLongTextString (ctx , val )
549
548
}
550
- } else {
551
- val , _ , err = LongText .Convert (ctx , val )
552
549
if err != nil {
553
550
return "" , sql .Collation_Unspecified , err
554
551
}
555
- if val == nil {
556
- content = ""
557
- } else {
558
- content = val .( string )
552
+ } else {
553
+ content , err = convertToLongTextString ( ctx , val )
554
+ if err != nil {
555
+ return "" , sql . Collation_Unspecified , err
559
556
}
560
557
}
561
558
} else {
@@ -571,15 +568,10 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
571
568
}
572
569
}
573
570
}
574
- val , _ , err = LongText . Convert (ctx , val )
571
+ content , err = convertToLongTextString (ctx , val )
575
572
if err != nil {
576
573
return "" , sql .Collation_Unspecified , err
577
574
}
578
- if val == nil {
579
- content = ""
580
- } else {
581
- content = val .(string )
582
- }
583
575
}
584
576
return content , collation , nil
585
577
}
0 commit comments