Skip to content
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

Closed
oylshe1314 opened this issue Apr 5, 2019 · 7 comments
Closed

database/sql: question about convertAssignRows #31274

oylshe1314 opened this issue Apr 5, 2019 · 7 comments
Assignees
Milestone

Comments

@oylshe1314
Copy link

@oylshe1314 oylshe1314 commented Apr 5, 2019

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函数即可。
既然数据库的字段是空值,我不接收并没有什么影响。

@agnivade
Copy link
Contributor

@agnivade agnivade commented Apr 5, 2019

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.

@mvdan mvdan added the WaitingForInfo label Apr 5, 2019
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Apr 5, 2019

I added Google Translate's English version above it.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Apr 5, 2019

@bradfitz bradfitz changed the title 代码文件src/database/sql/sql.go发现有问题,可能是BUG。 database/sql: question about convertAssignRows Apr 5, 2019
@bradfitz bradfitz added this to the Go1.13 milestone Apr 5, 2019
@kardianos
Copy link
Contributor

@kardianos kardianos commented Apr 5, 2019

@oylshe1314 I think you're right.

I think the fix is easy enough and seams reasonable.

@oylshe1314
Copy link
Author

@oylshe1314 oylshe1314 commented Apr 20, 2019

I added Google Translate's English version above it.

Very thanks.

@kardianos
Copy link
Contributor

@kardianos kardianos commented Apr 26, 2019

@oylshe1314 I'm about to push a change that will improve the error message, but not change the behavior.

If you scan a **int64, then check the *int64 == nil, this will work today. The problem with scanning a *int64 is that there is no way to signal a NULL, other then setting the zero value.

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.

@gopherbot
Copy link

@gopherbot gopherbot commented Apr 26, 2019

Change https://golang.org/cl/174177 mentions this issue: database/sql: check if src is nil before converting to string

@gopherbot gopherbot closed this in 1805f42 Apr 26, 2019
@golang golang locked and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.