Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested scan (#6136) #6143

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions callbacks/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ func RowQuery(db *gorm.DB) {
return
}

if isRows, ok := db.Get("rows"); ok && isRows.(bool) {
db.Statement.Settings.Delete("rows")
if types, ok := db.Statement.Settings.Load("rows"); ok && types.(*gorm.QueryTypes).Pop() {
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
12 changes: 10 additions & 2 deletions finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,11 @@ func (db *DB) Count(count *int64) (tx *DB) {
}

func (db *DB) Row() *sql.Row {
tx := db.getInstance().Set("rows", false)
tx := db.getInstance()

value, _ := tx.Statement.Settings.LoadOrStore("rows", &QueryTypes{})
value.(*QueryTypes).Push(false)

tx = tx.callbacks.Row().Execute(tx)
row, ok := tx.Statement.Dest.(*sql.Row)
if !ok && tx.DryRun {
Expand All @@ -509,7 +513,11 @@ func (db *DB) Row() *sql.Row {
}

func (db *DB) Rows() (*sql.Rows, error) {
tx := db.getInstance().Set("rows", true)
tx := db.getInstance()

value, _ := tx.Statement.Settings.LoadOrStore("rows", &QueryTypes{})
value.(*QueryTypes).Push(true)

tx = tx.callbacks.Row().Execute(tx)
rows, ok := tx.Statement.Dest.(*sql.Rows)
if !ok && tx.DryRun && tx.Error == nil {
Expand Down
55 changes: 54 additions & 1 deletion statement.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorm

import (
"container/list"
"context"
"database/sql"
"database/sql/driver"
Expand Down Expand Up @@ -57,6 +58,50 @@ type join struct {
JoinType clause.JoinType
}

type QueryTypes struct {
mux sync.Mutex
list *list.List
}

func (q *QueryTypes) Push(isRows bool) {
q.mux.Lock()
defer q.mux.Unlock()
if q.list == nil {
q.list = list.New()
}
q.list.PushBack(isRows)
}

func (q *QueryTypes) Pop() bool {
q.mux.Lock()
defer q.mux.Unlock()

if q.list == nil {
return false
}
element := q.list.Back()
if element == nil {
return false
}
q.list.Remove(element)
return element.Value.(bool)
}

func (q *QueryTypes) Clone() interface{} {
q.mux.Lock()
defer q.mux.Unlock()

if q.list == nil {
return &QueryTypes{}
}

cloneList := list.New()
for e := q.list.Front(); e != nil; e = e.Next() {
cloneList.PushFront(e.Value)
}
return &QueryTypes{list: cloneList}
}

// StatementModifier statement modifier interface
type StatementModifier interface {
ModifyStatement(*Statement)
Expand Down Expand Up @@ -544,13 +589,21 @@ func (stmt *Statement) clone() *Statement {
}

stmt.Settings.Range(func(k, v interface{}) bool {
newStmt.Settings.Store(k, v)
if cloneable, ok := v.(Cloneable); ok {
newStmt.Settings.Store(k, cloneable.Clone())
} else {
newStmt.Settings.Store(k, v)
}
return true
})

return newStmt
}

type Cloneable interface {
Clone() interface{}
}

// SetColumn set column's value
//
// stmt.SetColumn("Name", "jinzhu") // Hooks Method
Expand Down
20 changes: 20 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ func TestNameMatcher(t *testing.T) {
}
}
}

func TestQueryTypes(t *testing.T) {
types := &QueryTypes{}
values := []bool{true, false, false, true}
for _, value := range values {
types.Push(value)
}

clone := types.Clone().(*QueryTypes)
for _, value := range values {
actual := clone.Pop()
if actual != value {
t.Errorf("failed to pop, got %v, expect %v", actual, value)
}
}

if clone.list.Len() != 0 || clone.Pop() {
t.Errorf("clone list should be empty")
}
}
11 changes: 11 additions & 0 deletions tests/scopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,15 @@ func TestScopes(t *testing.T) {
if err := DB.Scopes(userTable).Select("max(id)").Scan(&maxId).Error; err != nil {
t.Errorf("select max(id)")
}

var user User
if err := DB.Scopes(func(db *gorm.DB) *gorm.DB {
var maxID int64
if err := db.Raw("select max(id) from users").Scan(&maxID).Error; err != nil {
return db
}
return db.Raw("select * from users where id = ?", maxID)
}).Scan(&user).Error; err != nil {
t.Errorf("failed to find user, got err: %v", err)
}
}