table/tables: use types.ToInt64

This commit is contained in:
Ewan Chou
2015-12-26 21:57:34 +08:00
parent c3bdfb1fed
commit 2bb206fbc9
2 changed files with 13 additions and 10 deletions

View File

@ -393,16 +393,9 @@ func (t *Table) AddRecord(ctx context.Context, r []interface{}) (recordID int64,
var hasRecordID bool
for _, col := range t.Cols() {
if mysql.HasPriKeyFlag(col.Flag) && t.meta.PKIsHandle {
// The value must have been filled with int64 or uint64 value, or it will not pass null check.
switch x := r[col.Offset].(type) {
case int64:
recordID = x
case uint64:
recordID = int64(x)
case int:
recordID = int64(x)
default:
return 0, errors.Errorf("unexpected type %T, should not happen!", x)
recordID, err = types.ToInt64(r[col.Offset])
if err != nil {
return 0, errors.Trace(err)
}
hasRecordID = true
break

View File

@ -559,6 +559,16 @@ func ToUint64(value interface{}) (uint64, error) {
// ToInt64 converts an interface to an int64.
func ToInt64(value interface{}) (int64, error) {
switch x := value.(type) {
case int64:
return x, nil
case int:
return int64(x), nil
case uint64:
if int64(x) > 0 {
return int64(x), nil
}
}
return convertToInt(value, NewFieldType(mysql.TypeLonglong))
}