@@ -502,6 +502,29 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
502502// conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
503503// original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
504504// 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+
505528func ConvertToCollatedString (ctx context.Context , val interface {}, typ sql.Type ) (string , sql.CollationID , error ) {
506529 var content string
507530 var collation sql.CollationID
@@ -519,43 +542,17 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
519542 } else if IsEnum (typ ) {
520543 // Handle enum types in string context - return the string value, not the index
521544 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 )
539546 } 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 )
549548 }
550- } else {
551- val , _ , err = LongText .Convert (ctx , val )
552549 if err != nil {
553550 return "" , sql .Collation_Unspecified , err
554551 }
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
559556 }
560557 }
561558 } else {
@@ -571,15 +568,10 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
571568 }
572569 }
573570 }
574- val , _ , err = LongText . Convert (ctx , val )
571+ content , err = convertToLongTextString (ctx , val )
575572 if err != nil {
576573 return "" , sql .Collation_Unspecified , err
577574 }
578- if val == nil {
579- content = ""
580- } else {
581- content = val .(string )
582- }
583575 }
584576 return content , collation , nil
585577}
0 commit comments