Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

解决Conversion接口ToDB() ([]byte, error)方法返回*Type值为nil时,数据类型为[]byte(nil)的… #1296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

peihexian
Copy link

解决字段类型为*Type,值为nil时,插入数据变为""的bug。解决update操作时,*Type类型字段的值为nil时,数据值变成[]byte(nil)的bug,正确的情况应该是interface(nil)。

@codecov-io
Copy link

codecov-io commented May 8, 2019

Codecov Report

Merging #1296 into master will decrease coverage by 0.08%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1296      +/-   ##
==========================================
- Coverage   57.41%   57.33%   -0.09%     
==========================================
  Files          44       44              
  Lines        7912     7823      -89     
==========================================
- Hits         4543     4485      -58     
+ Misses       2806     2781      -25     
+ Partials      563      557       -6
Impacted Files Coverage Δ
statement.go 68.05% <0%> (+0.19%) ⬆️
session_convert.go 22.19% <0%> (-0.1%) ⬇️
rows.go 52.38% <0%> (-3.76%) ⬇️
helpers.go 57.41% <0%> (-2.32%) ⬇️
tag.go 77.09% <0%> (-2.22%) ⬇️
engine.go 61.53% <0%> (-1.06%) ⬇️
session_update.go 56.44% <0%> (-0.18%) ⬇️
session_insert.go 56.84% <0%> (-0.12%) ⬇️
dialect_oracle.go 0% <0%> (ø) ⬆️
dialect_postgres.go 76.11% <0%> (+0.15%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73a7003...9e0cd1a. Read the comment docs.

Copy link
Author

@peihexian peihexian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because Conversion interface ToDB method return []byte type data,when field value is nil,ToDB() method will return []byte(nil) (is goland it's displayed []uint8(nil)),but db engine can't convert that type to interface(nil),then insert or update operation will be failed. now ,val=data fixed to if nil==data { val=nil } ,when data is nil,val type is interface(nil),db engine will translate it to NULL,then bug fixed.
if nil==data return nil,nil same as above。

@peihexian
Copy link
Author

为什么不给合并?是不是没有说清楚? 自定义类型例如 type MyTime time.Time 这种,如果在struct里面使用时是指针类型的话,例如 type Student struct {
Name string
Birthday *MyTime
} 这种情况,需要为MyTime去实现Conversion接口,向数据库写入是ToDB方面,该方法返回值类型是[]byte ,error,如果Birthday字段用户并没有输入数据值的话,也就是Birthday字段值为nil的话,ToDB方法只能返回nil,但是因为已经限定了数据类型是[]byte,所以数据库引擎在执行时给该字段赋值为[]byte(nil)这样的参数数据,这种[]byte(nil)数据库引擎是不认的,但是改为interface(nil)的话就可以正确识别为NULL,所以加了上面改动那些代码,不知道用中文说明白没有?

@lunny
Copy link
Member

lunny commented May 12, 2019

Could you add some tests?

mei-rune pushed a commit to mei-rune/xorm that referenced this pull request Apr 19, 2020
go-xorm#1296)

add test

解决Conversion接口ToDB() ([]byte, error)方法返回*Type值为nil时,数据类型为[]byte(nil)的bug,解决*Type值为nil时插入数据变为""的bug

Co-authored-by: peihexian <peihexian@qq.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1296
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants