MCPcopy
hub / github.com/github/gh-ost / convertArg

Method convertArg

go/sql/types.go:60–127  ·  view source on GitHub ↗
(arg interface{})

Source from the content-addressed store, hash-verified

58}
59
60func (cl *Column) convertArg(arg interface{}) interface{} {
61 var arg2Bytes []byte
62 if s, ok := arg.(string); ok {
63 arg2Bytes = []byte(s)
64 } else if b, ok := arg.([]uint8); ok {
65 arg2Bytes = b
66 } else {
67 arg2Bytes = nil
68 }
69
70 if arg2Bytes != nil {
71 if cl.Charset != "" && cl.charsetConversion == nil {
72 arg = arg2Bytes
73 } else if cl.Charset == "" && (strings.Contains(cl.MySQLType, "binary") || strings.HasSuffix(cl.MySQLType, "blob")) {
74 // varbinary/binary/blob column: no charset means binary storage. Return []byte so
75 // the MySQL driver sends MYSQL_TYPE_BLOB (binary) rather than MYSQL_TYPE_VAR_STRING
76 // (text with the connection's charset/collation metadata, often utf8mb4), which would
77 // cause MySQL to validate the bytes and emit Warning 1300 for byte sequences that are
78 // invalid in that charset.
79 arg = arg2Bytes
80 } else {
81 if encoding, ok := charsetEncodingMap[cl.Charset]; ok {
82 decodedBytes, _ := encoding.NewDecoder().Bytes(arg2Bytes)
83 arg = string(decodedBytes)
84 }
85 }
86
87 if cl.Type == BinaryColumnType {
88 size := len(arg2Bytes)
89 if uint(size) < cl.BinaryOctetLength {
90 buf := bytes.NewBuffer(arg2Bytes)
91 for i := uint(0); i < (cl.BinaryOctetLength - uint(size)); i++ {
92 buf.Write([]byte{0})
93 }
94 arg = buf.Bytes()
95 }
96 }
97
98 return arg
99 }
100
101 if cl.IsUnsigned {
102 if i, ok := arg.(int8); ok {
103 return uint8(i)
104 }
105 if i, ok := arg.(int16); ok {
106 return uint16(i)
107 }
108 if i, ok := arg.(int32); ok {
109 if cl.Type == MediumIntColumnType {
110 // problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that.
111 // So to convert from negative to positive we'd need to convert the value manually
112 if i >= 0 {
113 return i
114 }
115 return uint32(maxMediumintUnsigned + i + 1)
116 }
117 return uint32(i)

Calls

no outgoing calls