mysql icon indicating copy to clipboard operation
mysql copied to clipboard

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

Open bronze1man opened this issue 5 months ago • 3 comments

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

bronze1man avatar Mar 25 '24 03:03 bronze1man

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.

methane avatar Mar 25 '24 06:03 methane

@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.

bronze1man avatar Mar 26 '24 03:03 bronze1man

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.

methane avatar Mar 26 '24 03:03 methane