From dc711bd7c69394788e6532ad9c89f5bdcb12b007 Mon Sep 17 00:00:00 2001 From: jasonchuan Date: Sun, 4 Feb 2024 14:12:39 +0800 Subject: [PATCH] let gorm support limit and offset binding parameters,change the BindVar of postgres driver (#239) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update postgres.go if limit and offset use the parameter in new gorm version,must remove config vars before bindvar to ,and then restore them. * optimise the BindVarTo code to ensure the code more efficient * fix"for bug " for bindvar * remove cases * remove for logic * just check the 0 index --- postgres.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/postgres.go b/postgres.go index 960e067..50bb307 100644 --- a/postgres.go +++ b/postgres.go @@ -115,7 +115,15 @@ func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) { writer.WriteByte('$') - writer.WriteString(strconv.Itoa(len(stmt.Vars))) + index := 0 + varLen := len(stmt.Vars) + if varLen > 0 { + switch stmt.Vars[0].(type) { + case pgx.QueryExecMode: + index++ + } + } + writer.WriteString(strconv.Itoa(varLen - index)) } func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {