From 89ea62077d4f6a1b9de92fd26b7acd6e72eb1761 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 4 Jul 2020 08:33:10 +0800 Subject: [PATCH] DryRun for RowQuery, Exec, close #3106 --- callbacks/raw.go | 2 +- callbacks/row.go | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/callbacks/raw.go b/callbacks/raw.go index 4093a5ab..d594ab39 100644 --- a/callbacks/raw.go +++ b/callbacks/raw.go @@ -5,7 +5,7 @@ import ( ) func RawExec(db *gorm.DB) { - if db.Error == nil { + if db.Error == nil && !db.DryRun { result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err != nil { db.AddError(err) diff --git a/callbacks/row.go b/callbacks/row.go index b25503ff..7e70382e 100644 --- a/callbacks/row.go +++ b/callbacks/row.go @@ -10,10 +10,12 @@ func RowQuery(db *gorm.DB) { BuildQuerySQL(db) } - if _, ok := db.Get("rows"); ok { - 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...) + if !db.DryRun { + if _, ok := db.Get("rows"); ok { + 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...) + } } } }