Skip to content

Commit

Permalink
Fix Scan struct with primary key, close #3357
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Sep 2, 2020
1 parent 9a101c8 commit dbaa6b0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions callbacks.go
Expand Up @@ -79,6 +79,8 @@ func (p *processor) Execute(db *DB) {

if stmt.Model == nil {
stmt.Model = stmt.Dest
} else if stmt.Dest == nil {
stmt.Dest = stmt.Model
}

if stmt.Model != nil {
Expand Down
2 changes: 1 addition & 1 deletion callbacks/row.go
Expand Up @@ -11,7 +11,7 @@ func RowQuery(db *gorm.DB) {
}

if !db.DryRun {
if _, ok := db.Get("rows"); ok {
if isRows, ok := db.InstanceGet("rows"); ok && isRows.(bool) {
db.Statement.Dest, db.Error = db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
} else {
db.Statement.Dest = db.Statement.ConnPool.QueryRowContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
Expand Down
19 changes: 14 additions & 5 deletions finisher_api.go
Expand Up @@ -331,22 +331,28 @@ func (db *DB) Count(count *int64) (tx *DB) {
}

func (db *DB) Row() *sql.Row {
tx := db.getInstance()
tx := db.getInstance().InstanceSet("rows", false)
tx.callbacks.Row().Execute(tx)
return tx.Statement.Dest.(*sql.Row)
}

func (db *DB) Rows() (*sql.Rows, error) {
tx := db.Set("rows", true)
tx := db.getInstance().InstanceSet("rows", true)
tx.callbacks.Row().Execute(tx)
return tx.Statement.Dest.(*sql.Rows), tx.Error
}

// Scan scan value to a struct
func (db *DB) Scan(dest interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx)
if rows, err := tx.Rows(); err != nil {
tx.AddError(err)
} else {
defer rows.Close()
if rows.Next() {
tx.ScanRows(rows, dest)
}
}
return
}

Expand Down Expand Up @@ -379,7 +385,10 @@ func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error {
tx := db.getInstance()
tx.Error = tx.Statement.Parse(dest)
tx.Statement.Dest = dest
tx.Statement.ReflectValue = reflect.Indirect(reflect.ValueOf(dest))
tx.Statement.ReflectValue = reflect.ValueOf(dest)
for tx.Statement.ReflectValue.Kind() == reflect.Ptr {
tx.Statement.ReflectValue = tx.Statement.ReflectValue.Elem()
}
Scan(rows, tx, true)
return tx.Error
}
Expand Down
3 changes: 2 additions & 1 deletion logger/sql.go
Expand Up @@ -3,13 +3,14 @@ package logger
import (
"database/sql/driver"
"fmt"
"gorm.io/gorm/utils"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode"

"gorm.io/gorm/utils"
)

func isPrintable(s []byte) bool {
Expand Down
2 changes: 1 addition & 1 deletion migrator.go
Expand Up @@ -9,7 +9,7 @@ import (

// Migrator returns migrator
func (db *DB) Migrator() Migrator {
return db.Dialector.Migrator(db)
return db.Dialector.Migrator(db.Session(&Session{WithConditions: true}))
}

// AutoMigrate run auto migration for given models
Expand Down
18 changes: 15 additions & 3 deletions tests/scan_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)

Expand All @@ -16,14 +17,25 @@ func TestScan(t *testing.T) {
DB.Save(&user1).Save(&user2).Save(&user3)

type result struct {
ID uint
Name string
Age int
}

var res result
DB.Table("users").Select("name, age").Where("id = ?", user3.ID).Scan(&res)
if res.Name != user3.Name || res.Age != int(user3.Age) {
t.Errorf("Scan into struct should work")
DB.Table("users").Select("id, name, age").Where("id = ?", user3.ID).Scan(&res)
if res.ID != user3.ID || res.Name != user3.Name || res.Age != int(user3.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user3)
}

DB.Table("users").Select("id, name, age").Where("id = ?", user2.ID).Scan(&res)
if res.ID != user2.ID || res.Name != user2.Name || res.Age != int(user2.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user2)
}

DB.Model(&User{Model: gorm.Model{ID: user3.ID}}).Select("id, name, age").Scan(&res)
if res.ID != user3.ID || res.Name != user3.Name || res.Age != int(user3.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user3)
}

var doubleAgeRes = &result{}
Expand Down

0 comments on commit dbaa6b0

Please sign in to comment.