Lazy call registered scopes

This commit is contained in:
Jinzhu 2021-02-25 18:49:01 +08:00
parent 940da051a7
commit 828e6b646b
2 changed files with 15 additions and 2 deletions

View File

@ -72,8 +72,10 @@ func (cs *callbacks) Raw() *processor {
}
func (p *processor) Execute(db *DB) {
curTime := time.Now()
stmt := db.Statement
var (
curTime = time.Now()
stmt = db.Statement
)
if stmt.Model == nil {
stmt.Model = stmt.Dest
@ -106,6 +108,12 @@ func (p *processor) Execute(db *DB) {
}
}
// call scopes
for _, scope := range stmt.scopes {
db = scope(db)
}
stmt.scopes = nil
for _, f := range p.fns {
f(db)
}

View File

@ -43,6 +43,7 @@ type Statement struct {
CurDestIndex int
attrs []interface{}
assigns []interface{}
scopes []func(*DB) *DB
}
type join struct {
@ -481,6 +482,10 @@ func (stmt *Statement) clone() *Statement {
copy(newStmt.Joins, stmt.Joins)
}
for _, scope := range stmt.scopes {
stmt.scopes = append(stmt.scopes, scope)
}
stmt.Settings.Range(func(k, v interface{}) bool {
newStmt.Settings.Store(k, v)
return true