go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.45k stars 2.3k forks source link

Scanner should return one of int64,float64,bool,[]byte,string,time.Time,nil , but I got uint64 #1575

Open bronze1man opened 5 months ago

bronze1man commented 5 months ago

It maybe a bug of database/sql , if it is ,please tell me.

Issue description

https://pkg.go.dev/database/sql#Scanner says Scanner should return one of int64,float64,bool,[]byte,string,time.Time,nil , but I got uint64

Example code

the sql:

select * from INFORMATION_SCHEMA.TABLES

some of my scaner_t implement.

type scaner_t struct{
    src any
}
func (s *scaner_t) Scan(src any) error{
    s.src = src
    return nil
}
func (s *scaner_t) AsString() (outS string,err error){
    switch obj:=s.src.(type) {
    case int64:
        return strconv.FormatInt(obj,10),nil
    case float64:
        return strconv.FormatFloat(obj,'f',-1,64),nil
    case bool:
        return strconv.FormatBool(obj),nil
    case []byte:
        return string(obj),nil
    case string:
        return obj,nil
    case time.Time:
        b,err:=obj.MarshalText()
        if err!=nil{
            return "",err
        }
        return string(b),nil
    case nil:
        return "",nil
    default:
        return "",errors.New("unknow type "+fmt.Sprintf("%T",s.src))
    }
}

Error log

unknow type uint64

Configuration

Driver version (or git SHA): github.com/go-sql-driver/mysql v1.8.0

Go version: run go version in your console go version go1.19.2 darwin/amd64

Server version: E.g. MySQL 5.6, MariaDB 10.0.20

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.44    |
+-----------+

Server OS: E.g. Debian 8.1 (Jessie), Windows 10 mac os 14.2.1

methane commented 5 months ago

Yes, it violates database/sql convention. But this violation is necessary to support uint64. So we can not fix it until database/sql supports uint64 natively.

bronze1man commented 5 months ago

@methane Is there any document or examples about how to implement sql.Scanner to work with https://github.com/go-sql-driver/mysql? I do not like try and fail again and again.

methane commented 5 months ago

There is perfect document.

https://cs.opensource.google/go/go/+/master:src/database/sql/convert.go;l=219-480

https://github.com/go-sql-driver/mysql/blob/d7ddb8b9e324830b1ede89c5fea090c824497c51/packets.go#L769-L856

https://github.com/go-sql-driver/mysql/blob/d7ddb8b9e324830b1ede89c5fea090c824497c51/packets.go#L1230-L1403

It must be readable than my English.