Your Question
Is there a way to run a query in dry run mode, and NOT bind the vars?
I have a section of code where I use DryRun to get a copy of the query that will be run, and then make some modifications before passing it back in as a tx.Raw(). I pass the same vars as the initial dry run query through to the Raw, using Statement.Vars. This almost always works fine, however there is an edge case where a scope function tries to compare an @ symbol, and that will then it will not properly handle the Vars.
trivial example scope func:
func(q *gorm.DB) *gorm.DB {
return q.Where("'@' = ?", "@")
}
It seems to be expected behavior in func (expr NamedExpr) Build(builder Builder) { to start handling a named expression, and then consider ? for cases where it needs to call builder.AddVar. However, since the dryrun has already converted the vars by calling BindVarTo to $N, it can no longer locate them, and I eventually end up with errors to the effect of "expected 1 arguments, got 0".
What you want
One solution would be for the Session object should take a flag to skip this BindVarTo call converting the ? to $1. I'm relatively new to Gorm so open to any other options for this as well.
Comment From: jinzhu
Or you can replace the binded var back to ?, refer https://github.com/go-gorm/gorm/blob/d402765f694ade8fd3a0da1b7a2f9d2fa4453957/statement.go#L211 as example.