From ec9e61700fe9c2cf60d3fd13c7a9935e6ab8eadb Mon Sep 17 00:00:00 2001 From: Olof-Joachim Frahm Date: Tue, 1 Jun 2021 17:48:13 +0200 Subject: [PATCH] Add variant to set SQL on updates without parens. --- db/chain/constraint.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/db/chain/constraint.go b/db/chain/constraint.go index fb28b00..3c3936b 100644 --- a/db/chain/constraint.go +++ b/db/chain/constraint.go @@ -109,7 +109,7 @@ func (o *OnUpdate) Set(args ...interface{}) *OnUpdate { } // SetSQL Sets a field to a value that needs no escaping, it is assumed to be SQL valid (an -// expression or column) +// expression or column) and inserts parentheses around both keys and values func (o *OnUpdate) SetSQL(args ...string) *OnUpdate { if len(args)%2 != 0 { panic("arguments to `DoUpdate().SetSQL(...)` must be even in length") @@ -127,6 +127,25 @@ func (o *OnUpdate) SetSQL(args ...string) *OnUpdate { return o } +// SetSQLNoParens Sets a field to a value that needs no escaping, it is assumed to be SQL valid (an +// expression or column) and doesn't insert any parentheses around either keys or values +func (o *OnUpdate) SetSQLNoParens(args ...string) *OnUpdate { + if len(args)%2 != 0 { + panic("arguments to `DoUpdate().SetSQLNoParens(...)` must be even in length") + } + var key string + for index, arg := range args { + if index%2 == 0 { + key = arg + } else { + *o.operatorList = append(*o.operatorList, argList{ + text: key + " = " + arg, + }) + } + } + return o +} + // SetSQLRow Sets a field to a value that needs no escaping, it is assumed to be SQL valid (an // expression or column) it will append ROW to the values part because pg 12 updates func (o *OnUpdate) SetSQLRow(args ...string) *OnUpdate {