github.com/dolthub/go-mysql-server@v0.18.0/sql/convert_value.go (about) 1 package sql 2 3 import ( 4 "fmt" 5 6 "github.com/dolthub/vitess/go/vt/proto/query" 7 8 "github.com/dolthub/go-mysql-server/sql/values" 9 ) 10 11 // ConvertToValue converts the interface to a sql value. 12 func ConvertToValue(v interface{}) (Value, error) { 13 switch v := v.(type) { 14 case nil: 15 return Value{ 16 Typ: query.Type_NULL_TYPE, 17 Val: nil, 18 }, nil 19 case int: 20 return Value{ 21 Typ: query.Type_INT64, 22 Val: values.WriteInt64(make([]byte, values.Int64Size), int64(v)), 23 }, nil 24 case int8: 25 return Value{ 26 Typ: query.Type_INT8, 27 Val: values.WriteInt8(make([]byte, values.Int8Size), v), 28 }, nil 29 case int16: 30 return Value{ 31 Typ: query.Type_INT16, 32 Val: values.WriteInt16(make([]byte, values.Int16Size), v), 33 }, nil 34 case int32: 35 return Value{ 36 Typ: query.Type_INT32, 37 Val: values.WriteInt32(make([]byte, values.Int32Size), v), 38 }, nil 39 case int64: 40 return Value{ 41 Typ: query.Type_INT64, 42 Val: values.WriteInt64(make([]byte, values.Int64Size), v), 43 }, nil 44 case uint: 45 return Value{ 46 Typ: query.Type_UINT64, 47 Val: values.WriteUint64(make([]byte, values.Uint64Size), uint64(v)), 48 }, nil 49 case uint8: 50 return Value{ 51 Typ: query.Type_UINT8, 52 Val: values.WriteUint8(make([]byte, values.Uint8Size), v), 53 }, nil 54 case uint16: 55 return Value{ 56 Typ: query.Type_UINT16, 57 Val: values.WriteUint16(make([]byte, values.Uint16Size), v), 58 }, nil 59 case uint32: 60 return Value{ 61 Typ: query.Type_UINT32, 62 Val: values.WriteUint32(make([]byte, values.Uint32Size), v), 63 }, nil 64 case uint64: 65 return Value{ 66 Typ: query.Type_UINT64, 67 Val: values.WriteUint64(make([]byte, values.Uint64Size), v), 68 }, nil 69 case float32: 70 return Value{ 71 Typ: query.Type_FLOAT32, 72 Val: values.WriteFloat32(make([]byte, values.Float32Size), v), 73 }, nil 74 case float64: 75 return Value{ 76 Typ: query.Type_FLOAT64, 77 Val: values.WriteFloat64(make([]byte, values.Float64Size), v), 78 }, nil 79 case string: 80 return Value{ 81 Typ: query.Type_VARCHAR, 82 Val: values.WriteString(make([]byte, len(v)), v, values.ByteOrderCollation), 83 }, nil 84 case []byte: 85 return Value{ 86 Typ: query.Type_BLOB, 87 Val: values.WriteBytes(make([]byte, len(v)), v, values.ByteOrderCollation), 88 }, nil 89 default: 90 return Value{}, fmt.Errorf("type %T not implemented", v) 91 } 92 } 93 94 func MustConvertToValue(v interface{}) Value { 95 ret, err := ConvertToValue(v) 96 if err != nil { 97 panic(err) 98 } 99 return ret 100 }