forked from mirror/gorm
Merge branch 'a631807682-i5091'
This commit is contained in:
commit
4d14ac39ff
6
scan.go
6
scan.go
|
@ -68,7 +68,11 @@ func (db *DB) scanIntoStruct(sch *schema.Schema, rows *sql.Rows, reflectValue re
|
|||
values[idx] = &sql.RawBytes{}
|
||||
} else if len(columns) == 1 {
|
||||
sch = nil
|
||||
values[idx] = reflectValue.Interface()
|
||||
if reflectValue.CanAddr() {
|
||||
values[idx] = reflectValue.Addr().Interface()
|
||||
} else {
|
||||
values[idx] = reflectValue.Interface()
|
||||
}
|
||||
} else {
|
||||
values[idx] = &sql.RawBytes{}
|
||||
}
|
||||
|
|
|
@ -1158,3 +1158,39 @@ func TestQueryWithTableAndConditionsAndAllFields(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue