forked from mirror/gorm
Test Pluck
This commit is contained in:
parent
e26abb84b3
commit
95a6539331
|
@ -273,6 +273,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
|
||||||
// db.Find(&users).Pluck("age", &ages)
|
// db.Find(&users).Pluck("age", &ages)
|
||||||
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
||||||
tx = db.getInstance()
|
tx = db.getInstance()
|
||||||
|
tx.Statement.AddClause(clause.Select{Columns: []clause.Column{{Name: column}}})
|
||||||
tx.Statement.Dest = dest
|
tx.Statement.Dest = dest
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
|
|
14
scan.go
14
scan.go
|
@ -58,7 +58,12 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
||||||
default:
|
default:
|
||||||
switch db.Statement.ReflectValue.Kind() {
|
switch db.Statement.ReflectValue.Kind() {
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
isPtr := db.Statement.ReflectValue.Type().Elem().Kind() == reflect.Ptr
|
reflectValueType := db.Statement.ReflectValue.Type().Elem()
|
||||||
|
isPtr := reflectValueType.Kind() == reflect.Ptr
|
||||||
|
if isPtr {
|
||||||
|
reflectValueType = reflectValueType.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
||||||
fields := make([]*schema.Field, len(columns))
|
fields := make([]*schema.Field, len(columns))
|
||||||
joinFields := make([][2]*schema.Field, len(columns))
|
joinFields := make([][2]*schema.Field, len(columns))
|
||||||
|
@ -81,7 +86,11 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
||||||
|
|
||||||
for initialized || rows.Next() {
|
for initialized || rows.Next() {
|
||||||
initialized = false
|
initialized = false
|
||||||
elem := reflect.New(db.Statement.Schema.ModelType).Elem()
|
elem := reflect.New(reflectValueType).Elem()
|
||||||
|
|
||||||
|
if reflectValueType.Kind() != reflect.Struct && len(fields) == 1 {
|
||||||
|
values[0] = elem.Addr().Interface()
|
||||||
|
} else {
|
||||||
for idx, field := range fields {
|
for idx, field := range fields {
|
||||||
if field != nil {
|
if field != nil {
|
||||||
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
|
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
|
||||||
|
@ -94,6 +103,7 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
||||||
values[idx] = joinFields[idx][1].ReflectValueOf(relValue).Addr().Interface()
|
values[idx] = joinFields[idx][1].ReflectValueOf(relValue).Addr().Interface()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
db.RowsAffected++
|
db.RowsAffected++
|
||||||
db.AddError(rows.Scan(values...))
|
db.AddError(rows.Scan(values...))
|
||||||
|
|
|
@ -80,3 +80,35 @@ func TestFind(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPluck(t *testing.T) {
|
||||||
|
users := []*User{
|
||||||
|
GetUser("pluck-user1", Config{}),
|
||||||
|
GetUser("pluck-user2", Config{}),
|
||||||
|
GetUser("pluck-user3", Config{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.Create(&users)
|
||||||
|
|
||||||
|
var names []string
|
||||||
|
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("name", &names).Error; err != nil {
|
||||||
|
t.Errorf("Raise error when pluck name, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var ids []int
|
||||||
|
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name").Pluck("id", &ids).Error; err != nil {
|
||||||
|
t.Errorf("Raise error when pluck id, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx, name := range names {
|
||||||
|
if name != users[idx].Name {
|
||||||
|
t.Errorf("Unexpected result on pluck name, got %+v", names)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx, id := range ids {
|
||||||
|
if int(id) != int(users[idx].ID) {
|
||||||
|
t.Errorf("Unexpected result on pluck id, got %+v", ids)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue