Skip to content

Commit

Permalink
fix: scan array (#5624)
Browse files Browse the repository at this point in the history
Co-authored-by: Jinzhu <wosmvp@gmail.com>
  • Loading branch information
a631807682 and jinzhu committed Sep 22, 2022
1 parent 3a72ba1 commit 101a7c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
22 changes: 15 additions & 7 deletions scan.go
Expand Up @@ -243,15 +243,18 @@ func Scan(rows Rows, db *DB, mode ScanMode) {

switch reflectValue.Kind() {
case reflect.Slice, reflect.Array:
var elem reflect.Value
recyclableStruct := reflect.New(reflectValueType)
var (
elem reflect.Value
recyclableStruct = reflect.New(reflectValueType)
isArrayKind = reflectValue.Kind() == reflect.Array
)

if !update || reflectValue.Len() == 0 {
update = false
// if the slice cap is externally initialized, the externally initialized slice is directly used here
if reflectValue.Cap() == 0 {
db.Statement.ReflectValue.Set(reflect.MakeSlice(reflectValue.Type(), 0, 20))
} else {
} else if !isArrayKind {
reflectValue.SetLen(0)
db.Statement.ReflectValue.Set(reflectValue)
}
Expand Down Expand Up @@ -285,10 +288,15 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
db.scanIntoStruct(rows, elem, values, fields, joinFields)

if !update {
if isPtr {
reflectValue = reflect.Append(reflectValue, elem)
if !isPtr {
elem = elem.Elem()
}
if isArrayKind {
if reflectValue.Len() >= int(db.RowsAffected) {
reflectValue.Index(int(db.RowsAffected - 1)).Set(elem)
}
} else {
reflectValue = reflect.Append(reflectValue, elem.Elem())
reflectValue = reflect.Append(reflectValue, elem)
}
}
}
Expand All @@ -312,4 +320,4 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
if db.RowsAffected == 0 && db.Statement.RaiseErrorOnNotFound && db.Error == nil {
db.AddError(ErrRecordNotFound)
}
}
}
24 changes: 24 additions & 0 deletions tests/query_test.go
Expand Up @@ -216,6 +216,30 @@ func TestFind(t *testing.T) {
}
}

// test array
var models2 [3]User
if err := DB.Where("name in (?)", []string{"find"}).Find(&models2).Error; err != nil || len(models2) != 3 {
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models2))
} else {
for idx, user := range users {
t.Run("FindWithInClause#"+strconv.Itoa(idx+1), func(t *testing.T) {
CheckUser(t, models2[idx], user)
})
}
}

// test smaller array
var models3 [2]User
if err := DB.Where("name in (?)", []string{"find"}).Find(&models3).Error; err != nil || len(models3) != 2 {
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models3))
} else {
for idx, user := range users[:2] {
t.Run("FindWithInClause#"+strconv.Itoa(idx+1), func(t *testing.T) {
CheckUser(t, models3[idx], user)
})
}
}

var none []User
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))
Expand Down

0 comments on commit 101a7c7

Please sign in to comment.