Fix tests & refactor for PR #3429

This commit is contained in:
Jinzhu 2020-09-09 10:31:48 +08:00
parent aceb3dad3b
commit 2242ac6c0e
3 changed files with 12 additions and 7 deletions

View File

@ -39,13 +39,14 @@ func (expr Expr) Build(builder Builder) {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if rv.Len() == 0 { if rv.Len() == 0 {
builder.AddVar(builder, nil) builder.AddVar(builder, nil)
} } else {
for i := 0; i < rv.Len(); i++ { for i := 0; i < rv.Len(); i++ {
if i > 0 { if i > 0 {
builder.WriteByte(',') builder.WriteByte(',')
} }
builder.AddVar(builder, rv.Index(i).Interface()) builder.AddVar(builder, rv.Index(i).Interface())
} }
}
default: default:
builder.AddVar(builder, expr.Vars[idx]) builder.AddVar(builder, expr.Vars[idx])
} }

View File

@ -6,6 +6,10 @@ require (
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/jinzhu/now v1.1.1 github.com/jinzhu/now v1.1.1
github.com/lib/pq v1.6.0 github.com/lib/pq v1.6.0
gorm.io/driver/mysql v1.0.1
gorm.io/driver/postgres v1.0.0
gorm.io/driver/sqlite v1.1.1
gorm.io/driver/sqlserver v1.0.3
gorm.io/gorm v1.9.19 gorm.io/gorm v1.9.19
) )

View File

@ -204,7 +204,7 @@ func TestFind(t *testing.T) {
}) })
var models []User var models []User
if err := DB.Where("name in ?", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 { if err := DB.Where("name in (?)", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 {
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models)) t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models))
} else { } else {
for idx, user := range users { for idx, user := range users {
@ -215,7 +215,7 @@ func TestFind(t *testing.T) {
} }
var none []User var none []User
if err := DB.Where("name in ?", []string{}).Find(&none).Error; err != nil || len(none) != 0 { if err := DB.Where("name in (?)", []string{}).Find(&none).Error; err != nil || len(none) != 0 {
t.Errorf("errors happened when query find with in clause and zero length parameter: %v, length: %v", err, len(none)) t.Errorf("errors happened when query find with in clause and zero length parameter: %v, length: %v", err, len(none))
} }
} }