bob icon indicating copy to clipboard operation
bob copied to clipboard

Issue with `bigint unsigned` Support for MySQL

Open korECM opened this issue 10 months ago • 3 comments

Hi,

I've encountered an issue when working with the bob library. It appears that the conversion of uint64 to int64 in the following code is causing problems when handling MySQL's unsigned bigint type (defined as bigint unsigned null):

// aarondl/opt

func ToDriverValue(val any) (driver.Value, error) {
	refVal := reflect.ValueOf(val)
	if refVal.Type().Implements(globaldata.DriverValuerIntf) {
		valuer := refVal.Interface().(driver.Valuer)
		return valuer.Value()
	}

	// If it is of time.Time type, return it as is.
	if refVal.Type() == globaldata.TimeType {
		return val, nil
	}

	// If it implements encoding.TextMarshaler, use that.
	if refVal.Type().Implements(globaldata.EncodingTextMarshalerIntf) {
		marshaler := refVal.Interface().(encoding.TextMarshaler)
		return marshaler.MarshalText()
	}

	// If it implements encoding.BinaryMarshaler, use that.
	if refVal.Type().Implements(globaldata.EncodingBinaryMarshalerIntf) {
		marshaler := refVal.Interface().(encoding.BinaryMarshaler)
		return marshaler.MarshalBinary()
	}

	switch refVal.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return refVal.Int(), nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// Problem appears to originate from converting uint64 to int64.
		return int64(refVal.Uint()), nil
	case reflect.Float32, reflect.Float64:
		return refVal.Float(), nil
	case reflect.Bool:
		return refVal.Bool(), nil
	case reflect.Slice:
		if refVal.Type().Elem().Kind() == reflect.Uint8 {
			return refVal.Bytes(), nil
		}
	case reflect.String:
		return refVal.String(), nil
	}

	return val, nil
}

Could you confirm if this behavior is indeed a bug? Additionally, is there a temporary workaround available to resolve the issue with unsigned bigint support?

Thanks for your help!

korECM avatar Feb 06 '25 00:02 korECM

I am aware that Go's driver does not support uint64, but the go-mysql-driver supports uint64. Is there any way in bob to handle this issue properly?

korECM avatar Feb 06 '25 00:02 korECM

This should be fixed in v0.37.0 with the switch from github.com/aarondl/opt/null.Val[T] to database/sql.Null[T]

stephenafamo avatar May 31 '25 14:05 stephenafamo

database/sql.Null[T] will throw an error if the uint64 range is larger than math.MaxInt64.

It appears the only way is to have a special NullUint64 type that sends its value as a string if it is greater than math.MaxInt64

stephenafamo avatar Jun 07 '25 02:06 stephenafamo