Merge branch 'a631807682-i5091'

This commit is contained in:
Jinzhu 2022-02-27 09:12:35 +08:00
commit 4d14ac39ff
2 changed files with 41 additions and 1 deletions

View File

@ -68,7 +68,11 @@ func (db *DB) scanIntoStruct(sch *schema.Schema, rows *sql.Rows, reflectValue re
values[idx] = &sql.RawBytes{} values[idx] = &sql.RawBytes{}
} else if len(columns) == 1 { } else if len(columns) == 1 {
sch = nil sch = nil
if reflectValue.CanAddr() {
values[idx] = reflectValue.Addr().Interface()
} else {
values[idx] = reflectValue.Interface() values[idx] = reflectValue.Interface()
}
} else { } else {
values[idx] = &sql.RawBytes{} values[idx] = &sql.RawBytes{}
} }

View File

@ -1158,3 +1158,39 @@ func TestQueryWithTableAndConditionsAndAllFields(t *testing.T) {
t.Errorf("invalid query SQL, got %v", result.Statement.SQL.String()) t.Errorf("invalid query SQL, got %v", result.Statement.SQL.String())
} }
} }
type DoubleInt64 struct {
data int64
}
func (t *DoubleInt64) Scan(val interface{}) error {
switch v := val.(type) {
case int64:
t.data = v * 2
return nil
default:
return fmt.Errorf("DoubleInt64 cant not scan with:%v", v)
}
}
// https://github.com/go-gorm/gorm/issues/5091
func TestQueryScannerWithSingleColumn(t *testing.T) {
user := User{Name: "scanner_raw_1", Age: 10}
DB.Create(&user)
var result1 DoubleInt64
if err := DB.Model(&User{}).Where("name LIKE ?", "scanner_raw_%").Limit(1).Pluck(
"age", &result1).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
AssertEqual(t, result1.data, 20)
var result2 DoubleInt64
if err := DB.Model(&User{}).Where("name LIKE ?", "scanner_raw_%").Limit(1).Select(
"age").Scan(&result2).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
AssertEqual(t, result2.data, 20)
}