-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returned values are always []byte for mariadb with mysql driver #1386
Comments
Please write a reproducible step to reproduce. |
As explained, this is a reproducible step, you can create any table, and try to query it using mysql driver with Mariadb into func getJSON(sqlString string) (string, error) {
stmt, err := db.Conn.Prepare(query)
if err != nil {
return res, err
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
return "", err
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return "", err
}
count := len(columns)
tableData := make([]map[string]interface{}, 0)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i := 0; i < count; i++ {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
entry := make(map[string]interface{})
for i, col := range columns {
var v interface{}
val := values[i]
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
entry[col] = v
}
tableData = append(tableData, entry)
}
jsonData, err := json.Marshal(tableData)
if err != nil {
return "", err
}
fmt.Println(string(jsonData))
return string(jsonData), nil
} // so i am expecting a result for entry like this
map[string]any{
"id":1,
"is_admin":true,
"email":"[email protected]"
}
// but i got
map[string]any{
"id":[]byte("1"),
"is_admin":[]byte("true"),
"email":[]byte("[email protected]")
} |
so, i have tested everything again with mysql , maria, sqlite and postgres, here is the current behavior: |
I can not reproduce. I can not compile your code because it don't have main() function. Do not force maintainer to write any code by only guess. "reproducible" means no guess is needed to reproduce. Anyway, driver is low level library. And driver doesn't know about output type. If you don't want to manually convert data, use higher level library. |
Using a higher level library to build an ORM in Go is not a very good idea for me at least. |
No. All driver don't know output type. database/sql doesn't pass such information to driver.
No! We do not guarantee anything. Do not expect mysql driver return data type you expect. Then you know how to get suitable type dynamically. |
Amazing stuff, Thanks |
I think this should be done by the driver, like how every other driver does. |
You must not read the discussion. |
I have read. But the fact is all other drivers did it properly except yours. I have the feeling you are determined not to make any change. That's fine. We can do the mapping ourselves the hard way, but this is something I don't understand why you don't want to make it to the best. |
All driver don't know output type is interface{} or not. |
Can you please take a look at the code I shared in this issue ticket? #1401 |
So what? I don't want to allow double allocation&convertion for everyone only for your use case. |
Thanks for confirming it. It's ok that I will do the mapping on my end. |
I have a problem related to |
Which Go type is suitable for decimal? |
It's a pity that no Go type is fully suitable, but it is still possible to support it using float64 or custom type. |
If you need support for customization or non builtin types, you should just ignore ScanType. |
We built an orm, which works well if the user decides the type to receive, but when the user receives using the |
ColumnType provides not only ScanType, but also DatabaseTypeName. So you can use custom mapping. |
Issue description
Scan rows into any should return the underline type if it used with the new fix using Prepared statements
Example code
Error log
Configuration
Go version: 1.18
Server version: MariaDB 10.9
Server OS: , Windows 10
The text was updated successfully, but these errors were encountered: