lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.02k stars 910 forks source link

database col type is int32 but result is int64, why? #893

Open hzmsrv opened 5 years ago

hzmsrv commented 5 years ago

we found that the column type is INT4 in database. but the data we read from the database is int8.

Is it a bug or any other explain for below code?


func binaryDecode(parameterStatus *parameterStatus, s []byte, typ oid.Oid) interface{} {
    switch typ {
    case oid.T_bytea:
        return s
    case oid.T_int8:
        return int64(binary.BigEndian.Uint64(s))
    case oid.T_int4:
        return int64(int32(binary.BigEndian.Uint32(s)))
    case oid.T_int2:
        return int64(int16(binary.BigEndian.Uint16(s)))
    case oid.T_uuid:
        b, err := decodeUUIDBinary(s)
        if err != nil {
            panic(err)
        }
        return b

    default:
        errorf("don't know how to decode binary parameter of type %d", uint32(typ))
    }
    panic("not reached")
}
cbandy commented 5 years ago

If you know the result is 32-bits (4 bytes), then scan into an int32. See sql.Rows.Scan for some discussion.

The database/sql package talks to any driver using only a few types. All integer values are int64.