From afda9156ecd6fdcc3c05e555716c6a531b975f34 Mon Sep 17 00:00:00 2001 From: flike Date: Fri, 15 Jan 2016 14:16:41 +0800 Subject: [PATCH] bugfix[GetInt func] --- mysql/resultset.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/mysql/resultset.go b/mysql/resultset.go index a3dc1441..ecc131c0 100644 --- a/mysql/resultset.go +++ b/mysql/resultset.go @@ -334,23 +334,36 @@ func (r *Resultset) GetUintByName(row int, name string) (uint64, error) { } } -//!!only can get positive integer -func (r *Resultset) GetInt(row, column int) (int64, error) { - v, err := r.GetUint(row, column) - if err != nil { +func (r *Resultset) GetIntByName(row int, name string) (int64, error) { + if column, err := r.NameIndex(name); err != nil { return 0, err + } else { + return r.GetInt(row, column) } - - return int64(v), nil } -func (r *Resultset) GetIntByName(row int, name string) (int64, error) { - v, err := r.GetUintByName(row, name) +func (r *Resultset) GetInt(row, column int) (int64, error) { + d, err := r.GetValue(row, column) if err != nil { return 0, err } - return int64(v), nil + switch v := d.(type) { + case uint64: + return int64(v), nil + case int64: + return v, nil + case float64: + return int64(v), nil + case string: + return strconv.ParseInt(v, 10, 64) + case []byte: + return strconv.ParseInt(string(v), 10, 64) + case nil: + return 0, nil + default: + return 0, fmt.Errorf("data type is %T", v) + } } func (r *Resultset) GetFloat(row, column int) (float64, error) {