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
database/sql: question about convertAssignRows #31274
Comments
Please fill the complete issue template. Would it also be to write it in English so that it is easier for us to understand what the exact issue is ? Thanks. |
I added Google Translate's English version above it. |
/cc @kardianos |
@oylshe1314 I think you're right. I think the fix is easy enough and seams reasonable. |
Very thanks. |
@oylshe1314 I'm about to push a change that will improve the error message, but not change the behavior. If you scan a I agree that this is not optimal. I hope to make things easier with a sql/v2 release. But right now it is just the limitations of the API we have to work within. |
Change https://golang.org/cl/174177 mentions this issue: |
File src/database/sql/sql.go, line 2955: err := convertAssignRows(dest[i], sv, rs)
When the variable sv is nil, because the type of data I receive is an int64 variable. So dest[4] is *int64,
When the convertAssignRows function is inside,
The code runs to line 432 of the file src/database/sql/convert.go as follows:
s := asString(src)
U64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
If err != nil {
Err = strconvErr(err)
Return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetUint(u64)
Return nil
Src is the sv parameter when calling the convertAssignRows function, when src is nil
s := asString(src)
s = “nil” when this line is executed
Execute again
U64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
After u64 = 0, err = "invalid syntax"
Because strconv.ParseUint returns an error, convertAssignRows also returns an error. Eventually, the read value fails and the program is abnormal.
Calling the convertAssignRows function sv parameter is empty because the data has a field that allows null values, this is no problem,
I really don't understand why the logic of the above code is like this? ? ?? ? ?
Err := convertAssignRows(dest[i], sv, rs) directly before executing the code
If nil == sv {
Continue
}
Determine if sv is empty. If it is empty, it will not be received. Or in line 289 of the file src/database/sql/convert.go case nil:
When src is nil, you can directly exit the convertAssignRows function.
Since the field of the database is null, I don't accept it and it doesn't matter.
文件src/database/sql/sql.go,代码2955行: err := convertAssignRows(dest[i], sv, rs)
当变量sv为nil时,因为我接收数据的类型是一个int64的变量。所以dest[4] 是 *int64,
当convertAssignRows函数内部,
代码运行到了文件src/database/sql/convert.go的第432行代码,如下:
s := asString(src)
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetUint(u64)
return nil
src既是是调用convertAssignRows函数时的sv参数,当src为nil时
s := asString(src)
执行完此行时s = “nil”
再执行
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
后 u64 = 0, err = "invalid syntax"
因为strconv.ParseUint反回了错误导致convertAssignRows也返回错误,最终,读取值失败,程序异常。
调用convertAssignRows函数sv参数为空是因为数据存在允许空值的字段,这并没有什么问题,
我十分不理解以上的代码的逻辑为何是这样的??????
err := convertAssignRows(dest[i], sv, rs)直接在执行代码之前
if nil == sv {
continue
}
判断sv是否为空,如果为空,不接收即可。或者在文件src/database/sql/convert.go的289行 case nil:
src为nil时直接退出convertAssignRows函数即可。
既然数据库的字段是空值,我不接收并没有什么影响。
The text was updated successfully, but these errors were encountered: