-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: Improve Convert function #8
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
|
||
"github.com/juju/errors" | ||
mysql "github.com/pingcap/tidb/mysqldef" | ||
"github.com/pingcap/tidb/util/charset" | ||
) | ||
|
||
// InvConv returns a failed convertion error. | ||
|
@@ -71,13 +72,21 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // | |
} | ||
// TODO: consider target.Charset/Collate | ||
x = truncateStr(x, target.Flen) | ||
if target.Charset == charset.CharsetBin { | ||
bx := []byte(x) | ||
return bx, nil | ||
} | ||
return x, nil | ||
case mysql.TypeBlob: | ||
x, err := ToString(val) | ||
if err != nil { | ||
return InvConv(val, tp) | ||
} | ||
x = truncateStr(x, target.Flen) | ||
if target.Charset == charset.CharsetBin { | ||
bx := []byte(x) | ||
return bx, nil | ||
} | ||
return x, nil | ||
case mysql.TypeDuration: | ||
fsp := mysql.DefaultFsp | ||
|
@@ -127,6 +136,10 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { // | |
if err != nil { | ||
return InvConv(val, tp) | ||
} | ||
// TODO: We should first convert to uint64 then check unsigned flag. | ||
if mysql.HasUnsignedFlag(target.Flag) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about negative number? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Negative number will be converted to a big uint. mysql> select cast(-5 as unsigned); mysql> |
||
return uint64(x), nil | ||
} | ||
return x, nil | ||
case mysql.TypeNewDecimal: | ||
x, err := ToDecimal(val) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
return []byte(x), nil
?